1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
const std::string perPixLightingFragmentShader =
"#version 130 \n"
"uniform sampler2D normalMap;"
"uniform sampler2D heightMap;"
"uniform vec3 resolution;"
"uniform vec4 lightColor;"
"uniform sampler2D lightMap;"
"uniform vec3 lightPos;"
"uniform vec3 radius;"
"uniform float maxM;"
"uniform float maxP;"
"in mat4 projMat;"
"void main () { "
"float far=gl_DepthRange.far;"
"float near=gl_DepthRange.near;"
"vec2 position = vec2 (gl_FragCoord.x / resolution.x, gl_FragCoord.y / resolution.y);"
"vec4 bump = texture2D(normalMap, position);"
"vec4 specularInfos = texture2D(heightMap, position);"
"vec4 clipLightPos = projMat * vec4(0, 0, lightPos.z, 1);"
"vec3 sLightPos = vec3 (lightPos.x, resolution.y - lightPos.y, clipLightPos.z / clipLightPos.w);"
"vec3 pixPos = vec3 (gl_FragCoord.x, gl_FragCoord.y, bump.w);"
"vec4 lightMapColor = texture2D(lightMap, position);"
"vec3 viewPos = vec3(resolution.x * 0.5f, resolution.y * 0.5f, 1);"
"float z = (gl_FragCoord.w != 1.f) ? (inverse(projMat) * vec4(0, 0, 0, gl_FragCoord.w)).w : gl_FragCoord.z;"
"if (z >= bump.w) {"
"vec4 specularColor = vec4(0, 0, 0, 0);"
"vec3 vertexToLight = sLightPos - pixPos;"
"float attenuation = 1.0f;"
"vec3 a;"
"a.x = (radius.x != -1) ? 1.0f - (length(vertexToLight) / radius.x) : 1.0f;"
"a.y = (radius.y != -1) ? 1.0f - (length(vertexToLight) / radius.y) : 1.0f;"
"a.z = (radius.z != -1) ? 1.0f - (length(vertexToLight) / (radius.z / (far - near))) : 1.0f;"
"attenuation = (a.x + a.y + a.z) / 3;"
"vec3 pixToView = pixPos - viewPos;"
"float normalLength = sqrt(length(vertexToLight) * length(vertexToLight) - dot(bump.xyz, vertexToLight) * dot(bump.xyz, vertexToLight));"
"vec3 lightReflect = vertexToLight + 2 * (bump.xyz * normalLength - vertexToLight);"
"float m = specularInfos.r * maxM;"
"float p = specularInfos.g * maxP;"
"float specularFactor = dot(pixToView, lightReflect);"
"specularFactor = pow (specularFactor, p);"
"if (specularFactor > 0) {"
"specularColor = vec4(lightColor.rgb, 1) * m * specularFactor;"
"}"
"if (bump.x != 0 || bump.y != 0 || bump.z != 0 && vertexToLight.z > 0.f) {"
"vec3 dirToLight = normalize(vertexToLight.xyz);"
"float nDotl = dot (dirToLight, bump.xyz);"
"attenuation *= nDotl;"
"}"
"gl_FragColor = vec4(lightColor.rgb, 1) * max(0.0f, attenuation);"
"} else {"
"gl_FragColor = lightMapColor;"
"}" |
Partager