diff --git a/.vs/shader3/v14/.suo b/.vs/shader3/v14/.suo index e77638f..56039f5 100644 Binary files a/.vs/shader3/v14/.suo and b/.vs/shader3/v14/.suo differ diff --git a/main.cpp b/main.cpp index ebdfdc7..05832f9 100644 --- a/main.cpp +++ b/main.cpp @@ -96,8 +96,8 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _ float ambientLightColor[] = {0.4f,0.4f,0.4f,1.0f}; 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,0.0f}; + float diffuseMaterial[] = { 0.6f,0.6f,0.6f,1.0f }; + float lightPos[] = {3.0f,3.0f,0.0f,1.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 }; diff --git a/res/shader/pointLight.fs b/res/shader/dirLight.fs similarity index 100% rename from res/shader/pointLight.fs rename to res/shader/dirLight.fs diff --git a/res/shader/pointLight.vs b/res/shader/dirLight.vs similarity index 100% rename from res/shader/pointLight.vs rename to res/shader/dirLight.vs diff --git a/res/shader/test.fs b/res/shader/test.fs index c90c75b..598f0f5 100644 --- a/res/shader/test.fs +++ b/res/shader/test.fs @@ -17,20 +17,35 @@ void main() vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial; //diffuse - vec3 L = normalize(U_LightPos.xyz); + vec3 L = vec3(0.0); + float distance = 0.0; + float attenuation = 1.0; + + //light attribute + float constantFactor=0.5; + float linearFactor=0.3; + float expFactor=0.1; + + if(U_LightPos.w==0.0){ + //这是方向光 + L = normalize(U_LightPos.xyz); + } else { + //这是点光源 + //model point -> light pos + L = normalize(U_LightPos.xyz - V_WorldPos); + distance = length(U_LightPos.xyz - V_WorldPos); + attenuation = 1.0 / (constantFactor + linearFactor * distance + expFactor * distance * distance); + } 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); + + float diffuseIntensity = max(0.0, dot(L,N)); + vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * diffuseIntensity * attenuation; //specular blin vec3 viewDir = normalize(U_EyePos - V_WorldPos); vec3 M = normalize(L + viewDir); - vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(M,N)), 64.0); + vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(M,N)), 64.0) * attenuation; gl_FragColor = ambientColor + diffuseColor + specularColor;