Browse Source

exp fog

master
blobt 4 years ago
parent
commit
41396820fb
  1. BIN
      .vs/shader3/v14/.suo
  2. 3
      main.cpp
  3. 10
      res/shader/forg.fs

BIN
.vs/shader3/v14/.suo

3
main.cpp

@ -99,6 +99,7 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
gpuProgram.DetectUniform("U_FogStart"); gpuProgram.DetectUniform("U_FogStart");
gpuProgram.DetectUniform("U_FogEnd"); gpuProgram.DetectUniform("U_FogEnd");
gpuProgram.DetectUniform("U_FogColor"); gpuProgram.DetectUniform("U_FogColor");
gpuProgram.DetectUniform("U_FogDensity");
@ -129,6 +130,7 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
float fogStart = 2.0f; float fogStart = 2.0f;
float fogEnd = 23.0f; float fogEnd = 23.0f;
float fogColor[] = { 0.6f, 0.6f, 0.6f, 1.0f }; float fogColor[] = { 0.6f, 0.6f, 0.6f, 1.0f };
float fogDensity = 0.11;
glm::mat4 cubeModelMatrix = glm::translate<float>(-2.2f, 0.0f, -4.0f) * glm::rotate(-30.0f, 1.0f, 1.0f, 1.0f); glm::mat4 cubeModelMatrix = glm::translate<float>(-2.2f, 0.0f, -4.0f) * glm::rotate(-30.0f, 1.0f, 1.0f, 1.0f);
glm::mat4 cubeNormalMatrix = glm::inverseTranspose(cubeModelMatrix); glm::mat4 cubeNormalMatrix = glm::inverseTranspose(cubeModelMatrix);
@ -182,6 +184,7 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
glUniform1f(gpuProgram.GetLocation("U_FogStart"), fogStart); glUniform1f(gpuProgram.GetLocation("U_FogStart"), fogStart);
glUniform1f(gpuProgram.GetLocation("U_FogEnd"), fogEnd); glUniform1f(gpuProgram.GetLocation("U_FogEnd"), fogEnd);
glUniform4fv(gpuProgram.GetLocation("U_FogColor"), 1, fogColor); glUniform4fv(gpuProgram.GetLocation("U_FogColor"), 1, fogColor);
glUniform1f(gpuProgram.GetLocation("U_FogDensity"), fogDensity);
glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(cubeModelMatrix)); glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(cubeModelMatrix));
glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(cubeNormalMatrix)); glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(cubeNormalMatrix));

10
res/shader/forg.fs

@ -13,6 +13,7 @@ uniform vec4 U_SpecularMaterial;
uniform float U_FogStart; uniform float U_FogStart;
uniform float U_FogEnd; uniform float U_FogEnd;
uniform vec4 U_FogColor; uniform vec4 U_FogColor;
uniform float U_FogDensity;
varying vec3 V_Normal; varying vec3 V_Normal;
varying vec3 V_WorldPos; varying vec3 V_WorldPos;
@ -25,6 +26,13 @@ float calculateLinearFog(float distance){
return fogAlpha; return fogAlpha;
} }
float calculateExpFog(float distance)
{
float fogAlpha=exp(-(distance*U_FogDensity));
fogAlpha=clamp(fogAlpha,0.0,1.0);
return fogAlpha;
}
void main() void main()
{ {
@ -77,6 +85,6 @@ 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);
float fogAlpha = calculateLinearFog(abs(V_EyeSpacePos.z/V_EyeSpacePos.w));
float fogAlpha = calculateExpFog(abs(V_EyeSpacePos.z/V_EyeSpacePos.w));
gl_FragColor = mix(U_FogColor,ambientColor + diffuseColor + specularColor, fogAlpha); gl_FragColor = mix(U_FogColor,ambientColor + diffuseColor + specularColor, fogAlpha);
} }
Loading…
Cancel
Save