diff --git a/.vs/shader3/v14/.suo b/.vs/shader3/v14/.suo index d952caf..e77638f 100644 Binary files a/.vs/shader3/v14/.suo and b/.vs/shader3/v14/.suo differ diff --git a/main.cpp b/main.cpp index ae165d8..ebdfdc7 100644 --- a/main.cpp +++ b/main.cpp @@ -97,7 +97,7 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ float ambientMaterial[] = { 0.2f,0.2f,0.2f,1.0f }; float diffuseLightColor[] = { 1.0f,1.0f,1.0f,1.0f }; float diffuseMaterial[] = { 0.8f,0.8f,0.8f,1.0f }; - float lightPos[] = {1.0f,1.0f,0.0f}; + float lightPos[] = {1.0f,1.0f,0.0f,0.0f}; float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f }; float specularMaterial[] = { 1.0f,1.0f,1.0f,1.0f }; float eyePos[] = { 0.0f,0.0f,0.0f }; @@ -137,7 +137,7 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ glUniform4fv(gpuProgram.GetLocation("U_AmbientMaterial"), 1, ambientMaterial); glUniform4fv(gpuProgram.GetLocation("U_DiffuseLightColor"), 1, diffuseLightColor); glUniform4fv(gpuProgram.GetLocation("U_DiffuseMaterial"), 1, diffuseMaterial); - glUniform3fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos); + glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos); glUniform4fv(gpuProgram.GetLocation("U_SpecularLightColor"), 1, specularLightColor); glUniform4fv(gpuProgram.GetLocation("U_SpecularMaterial"), 1, specularMaterial); glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos); diff --git a/res/shader/pointLight.fs b/res/shader/pointLight.fs new file mode 100644 index 0000000..51e6eca --- /dev/null +++ b/res/shader/pointLight.fs @@ -0,0 +1,39 @@ +uniform vec4 U_AmbientLightColor; +uniform vec4 U_AmbientMaterial; +uniform vec4 U_LightPos; +uniform vec4 U_DiffuseLightColor; +uniform vec4 U_DiffuseMaterial; +uniform vec3 U_EyePos; +uniform vec4 U_SpecularLightColor; +uniform vec4 U_SpecularMaterial; + + +varying vec3 V_Normal; +varying vec3 V_WorldPos; + +void main() +{ + //ambient + vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial; + + //diffuse + vec3 L = vec3(0.0); + if(U_LightPos.w==0.0){ + //这是方向光 + L = normalize(U_LightPos.xyz); + } else { + //这是点光源 + //model point -> light pos + } + vec3 N = normalize(V_Normal); + float diffuseIntensity = max(0.0, dot(L,N)); + vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * diffuseIntensity; + + //specular + vec3 reflectDir = normalize(reflect(-L,N));//-L是入射光向量,通过reflect函数求到反射光方向 + vec3 viewDir = normalize(U_EyePos - V_WorldPos); + vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(viewDir,reflectDir)), 64.0); + + + gl_FragColor = ambientColor + diffuseColor + specularColor; +} \ No newline at end of file diff --git a/res/shader/pointLight.vs b/res/shader/pointLight.vs new file mode 100644 index 0000000..356d514 --- /dev/null +++ b/res/shader/pointLight.vs @@ -0,0 +1,19 @@ +attribute vec3 pos; +attribute vec2 texcoord; +attribute vec3 normal; + +uniform mat4 M; +uniform mat4 P; +uniform mat4 V; +uniform mat4 NM;//Normal Matrix 使用来修正法线的 + +varying vec3 V_Normal; +varying vec3 V_WorldPos; +void main() +{ + V_Normal = mat3(NM)*normal; + vec4 worldPos = M * vec4(pos, 1.0); + V_WorldPos = worldPos.xyz; + + gl_Position=P*V*M*vec4(pos,1.0); +} \ No newline at end of file diff --git a/res/shader/specular.fs b/res/shader/specular.fs new file mode 100644 index 0000000..3d510e2 --- /dev/null +++ b/res/shader/specular.fs @@ -0,0 +1,32 @@ +uniform vec4 U_AmbientLightColor; +uniform vec4 U_AmbientMaterial; +uniform vec3 U_LightPos; +uniform vec4 U_DiffuseLightColor; +uniform vec4 U_DiffuseMaterial; +uniform vec3 U_EyePos; +uniform vec4 U_SpecularLightColor; +uniform vec4 U_SpecularMaterial; + + +varying vec3 V_Normal; +varying vec3 V_WorldPos; + +void main() +{ + //ambient + vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial; + + //diffuse + vec3 N = normalize(V_Normal); + vec3 L = normalize(U_LightPos); + float diffuseIntensity = max(0.0, dot(L,N)); + vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * diffuseIntensity; + + //specular + vec3 reflectDir = normalize(reflect(-L,N));//-L是入射光向量,通过reflect函数求到反射光方向 + vec3 viewDir = normalize(U_EyePos - V_WorldPos); + vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(viewDir,reflectDir)), 64.0); + + + gl_FragColor = ambientColor + diffuseColor + specularColor; +} \ No newline at end of file diff --git a/res/shader/specular.vs b/res/shader/specular.vs new file mode 100644 index 0000000..356d514 --- /dev/null +++ b/res/shader/specular.vs @@ -0,0 +1,19 @@ +attribute vec3 pos; +attribute vec2 texcoord; +attribute vec3 normal; + +uniform mat4 M; +uniform mat4 P; +uniform mat4 V; +uniform mat4 NM;//Normal Matrix 使用来修正法线的 + +varying vec3 V_Normal; +varying vec3 V_WorldPos; +void main() +{ + V_Normal = mat3(NM)*normal; + vec4 worldPos = M * vec4(pos, 1.0); + V_WorldPos = worldPos.xyz; + + gl_Position=P*V*M*vec4(pos,1.0); +} \ No newline at end of file diff --git a/res/shader/test.fs b/res/shader/test.fs index 3d510e2..51e6eca 100644 --- a/res/shader/test.fs +++ b/res/shader/test.fs @@ -1,6 +1,6 @@ uniform vec4 U_AmbientLightColor; uniform vec4 U_AmbientMaterial; -uniform vec3 U_LightPos; +uniform vec4 U_LightPos; uniform vec4 U_DiffuseLightColor; uniform vec4 U_DiffuseMaterial; uniform vec3 U_EyePos; @@ -17,8 +17,15 @@ void main() vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial; //diffuse + vec3 L = vec3(0.0); + if(U_LightPos.w==0.0){ + //这是方向光 + L = normalize(U_LightPos.xyz); + } else { + //这是点光源 + //model point -> light pos + } vec3 N = normalize(V_Normal); - vec3 L = normalize(U_LightPos); float diffuseIntensity = max(0.0, dot(L,N)); vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * diffuseIntensity; diff --git a/res/shader/test.vs b/res/shader/test.vs index 5cfa86b..356d514 100644 --- a/res/shader/test.vs +++ b/res/shader/test.vs @@ -12,7 +12,8 @@ varying vec3 V_WorldPos; void main() { V_Normal = mat3(NM)*normal; - V_WorldPos = M * vec4(pos, 1.0); + vec4 worldPos = M * vec4(pos, 1.0); + V_WorldPos = worldPos.xyz; gl_Position=P*V*M*vec4(pos,1.0); } \ No newline at end of file