Browse Source

修复光照1

master
blobt 4 years ago
parent
commit
93e73759d3
  1. 10
      main.cpp
  2. 29
      res/shader/test.fs

10
main.cpp

@ -87,7 +87,7 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
//init 3d model //init 3d model
ObjModel model; ObjModel model;
model.Init("res/model/Quad.obj");
model.Init("res/model/Sphere.obj");
float identity[] = { float identity[] = {
1.0f,0,0,0, 1.0f,0,0,0,
@ -99,15 +99,15 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
float ambientMaterial[] = { 0.2f,0.2f,0.2f,1.0f }; float ambientMaterial[] = { 0.2f,0.2f,0.2f,1.0f };
float diffuseLightColor[] = { 1.0f,1.0f,1.0f,1.0f }; float diffuseLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
float diffuseMaterial[] = { 0.6f, 0.6f, 0.6f, 1.0f }; float diffuseMaterial[] = { 0.6f, 0.6f, 0.6f, 1.0f };
float lightPos[] = {0.0f,1.5f,-3.0f,1.0f};
float lightPos[] = {0.0f,3.0f,-2.5f,1.0f};
float spotLightDirection[] = {0.0f, -1.0f, 0.0f, 110.0f};//把最后一位改成......
float spotLightCutoff = 15.0f;
float spotLightDirection[] = {0.0f, -1.0f, 0.0f, 628.0f};//把最后一位改成......
float spotLightCutoff = 5.0f;
float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f }; float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
float specularMaterial[] = { 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 }; float eyePos[] = { 0.0f,0.0f,0.0f };
glm::mat4 modelMatrix = glm::translate<float>(0.0f, -0.5f, -3.0f) * glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f) * glm::scale(2.0f, 2.0f, 2.0f);
glm::mat4 modelMatrix = glm::translate<float>(0.0f, 0.0f, -3.0f) * glm::rotate(-30.0f, 1.0f, 1.0f, 1.0f);
glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f); glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
glm::mat4 normalMatrix = glm::inverseTranspose(modelMatrix); glm::mat4 normalMatrix = glm::inverseTranspose(modelMatrix);

29
res/shader/test.fs

@ -26,11 +26,8 @@ void main()
vec3 L = vec3(0.0); vec3 L = vec3(0.0);
float attenuation = 1.0; float attenuation = 1.0;
float diffuseIntensity = 0.0; float diffuseIntensity = 0.0;
if(U_LightPos.w == 0 && U_Cutoff == 0){
//
L = normalize(U_LightPos.xyz - vec3(0.0));
} else {
//
if(U_LightPos.w != 0){//
L = normalize(U_LightPos.xyz - V_WorldPos); L = normalize(U_LightPos.xyz - V_WorldPos);
float distance = length(U_LightPos.xyz - V_WorldPos); float distance = length(U_LightPos.xyz - V_WorldPos);
float constantFactor=0.5; float constantFactor=0.5;
@ -38,15 +35,21 @@ void main()
float expFactor=0.1; float expFactor=0.1;
attenuation = 1.0 / (constantFactor + linearFactor * distance + expFactor * distance * distance); attenuation = 1.0 / (constantFactor + linearFactor * distance + expFactor * distance * distance);
//
float radianCutoff = U_Cutoff * 3.14 / 180;
float cosThta = cos(radianCutoff);
vec3 spotLightDirection = normalize(U_LightDirection.xyz);
float currentThta = max(0.0, dot(-L, spotLightDirection));
if( U_Cutoff > 0){//
//
float radianCutoff = U_Cutoff * 3.14 / 180;
float cosThta = cos(radianCutoff);
vec3 spotLightDirection = normalize(U_LightDirection.xyz);
float currentThta = max(0.0, dot(-L, spotLightDirection));
if(currentThta > cosThta){
diffuseIntensity = pow(currentThta, U_LightDirection.w);
if(currentThta > cosThta){
diffuseIntensity = pow(currentThta, U_LightDirection.w);
}
} }
} else {
//
L = normalize(U_LightPos.xyz - vec3(0.0));
} }
vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * max(0.0, dot(L,N)) * attenuation * diffuseIntensity; vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * max(0.0, dot(L,N)) * attenuation * diffuseIntensity;
@ -56,5 +59,5 @@ void main()
vec3 VD = normalize(U_EyePos - V_WorldPos); vec3 VD = normalize(U_EyePos - V_WorldPos);
vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(RD,VD)) ,128); vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(RD,VD)) ,128);
gl_FragColor = ambientColor + diffuseColor + specularColor;
gl_FragColor = ambientColor + diffuseColor;// + specularColor;
} }
Loading…
Cancel
Save