From fd00461b4e73fc6d8dcb8f16e3c83f2a3024bf31 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 5 Nov 2014 16:50:34 -0800 Subject: [PATCH] Perspective correction fix (lets us use lower tesselation on light volumes). --- .../resources/shaders/deferred_light_limited.vert | 3 ++- interface/resources/shaders/point_light.frag | 11 ++++++----- interface/resources/shaders/spot_light.frag | 11 ++++++----- interface/src/renderer/DeferredLightingEffect.cpp | 4 ++-- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/interface/resources/shaders/deferred_light_limited.vert b/interface/resources/shaders/deferred_light_limited.vert index 849d14bda4..f360f0307d 100644 --- a/interface/resources/shaders/deferred_light_limited.vert +++ b/interface/resources/shaders/deferred_light_limited.vert @@ -14,5 +14,6 @@ void main(void) { gl_Position = ftransform(); vec4 projected = gl_Position / gl_Position.w; - gl_TexCoord[0] = vec4(dot(projected, gl_ObjectPlaneS[3]), dot(projected, gl_ObjectPlaneT[3]), 0.0, 1.0); + gl_TexCoord[0] = vec4(dot(projected, gl_ObjectPlaneS[3]) * gl_Position.w, + dot(projected, gl_ObjectPlaneT[3]) * gl_Position.w, 0.0, gl_Position.w); } diff --git a/interface/resources/shaders/point_light.frag b/interface/resources/shaders/point_light.frag index b9ba638885..1d1b4f4073 100644 --- a/interface/resources/shaders/point_light.frag +++ b/interface/resources/shaders/point_light.frag @@ -40,16 +40,17 @@ uniform float radius; void main(void) { // get the depth and exit early if it doesn't pass the test - float depth = texture2D(depthMap, gl_TexCoord[0].st).r; + vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; + float depth = texture2D(depthMap, texCoord).r; if (depth < gl_FragCoord.z) { discard; } // compute the view space position using the depth float z = near / (depth * depthScale - 1.0); - vec4 position = vec4((depthTexCoordOffset + gl_TexCoord[0].st * depthTexCoordScale) * z, z, 1.0); + vec4 position = vec4((depthTexCoordOffset + texCoord * depthTexCoordScale) * z, z, 1.0); // get the normal from the map - vec4 normal = texture2D(normalMap, gl_TexCoord[0].st); + vec4 normal = texture2D(normalMap, texCoord); vec4 normalizedNormal = normalize(normal * 2.0 - vec4(1.0, 1.0, 1.0, 2.0)); // compute the base color based on OpenGL lighting model @@ -58,7 +59,7 @@ void main(void) { lightVector = lightVector / lightDistance; float diffuse = dot(normalizedNormal, lightVector); float facingLight = step(0.0, diffuse); - vec4 baseColor = texture2D(diffuseMap, gl_TexCoord[0].st) * (gl_FrontLightProduct[1].ambient + + vec4 baseColor = texture2D(diffuseMap, texCoord) * (gl_FrontLightProduct[1].ambient + gl_FrontLightProduct[1].diffuse * (diffuse * facingLight)); // compute attenuation based on distance, etc. @@ -69,6 +70,6 @@ void main(void) { // add base to specular, modulate by attenuation float specular = facingLight * max(0.0, dot(normalize(lightVector - normalize(vec4(position.xyz, 0.0))), normalizedNormal)); - vec4 specularColor = texture2D(specularMap, gl_TexCoord[0].st); + vec4 specularColor = texture2D(specularMap, texCoord); gl_FragColor = vec4((baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb) * attenuation, 0.0); } diff --git a/interface/resources/shaders/spot_light.frag b/interface/resources/shaders/spot_light.frag index a496abd7b2..590ed8b6ac 100644 --- a/interface/resources/shaders/spot_light.frag +++ b/interface/resources/shaders/spot_light.frag @@ -40,16 +40,17 @@ uniform float radius; void main(void) { // get the depth and exit early if it doesn't pass the test - float depth = texture2D(depthMap, gl_TexCoord[0].st).r; + vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; + float depth = texture2D(depthMap, texCoord).r; if (depth < gl_FragCoord.z) { discard; } // compute the view space position using the depth float z = near / (depth * depthScale - 1.0); - vec4 position = vec4((depthTexCoordOffset + gl_TexCoord[0].st * depthTexCoordScale) * z, z, 1.0); + vec4 position = vec4((depthTexCoordOffset + texCoord * depthTexCoordScale) * z, z, 1.0); // get the normal from the map - vec4 normal = texture2D(normalMap, gl_TexCoord[0].st); + vec4 normal = texture2D(normalMap, texCoord); vec4 normalizedNormal = normalize(normal * 2.0 - vec4(1.0, 1.0, 1.0, 2.0)); // compute the base color based on OpenGL lighting model @@ -58,7 +59,7 @@ void main(void) { lightVector = lightVector / lightDistance; float diffuse = dot(normalizedNormal, lightVector); float facingLight = step(0.0, diffuse); - vec4 baseColor = texture2D(diffuseMap, gl_TexCoord[0].st) * (gl_FrontLightProduct[1].ambient + + vec4 baseColor = texture2D(diffuseMap, texCoord) * (gl_FrontLightProduct[1].ambient + gl_FrontLightProduct[1].diffuse * (diffuse * facingLight)); // compute attenuation based on spot angle, distance, etc. @@ -71,6 +72,6 @@ void main(void) { // add base to specular, modulate by attenuation float specular = facingLight * max(0.0, dot(normalize(lightVector - normalize(vec4(position.xyz, 0.0))), normalizedNormal)); - vec4 specularColor = texture2D(specularMap, gl_TexCoord[0].st); + vec4 specularColor = texture2D(specularMap, texCoord); gl_FragColor = vec4((baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb) * attenuation, 0.0); } diff --git a/interface/src/renderer/DeferredLightingEffect.cpp b/interface/src/renderer/DeferredLightingEffect.cpp index 6e96d106cc..fa07bf419d 100644 --- a/interface/src/renderer/DeferredLightingEffect.cpp +++ b/interface/src/renderer/DeferredLightingEffect.cpp @@ -269,7 +269,7 @@ void DeferredLightingEffect::render() { } else { glTranslatef(light.position.x, light.position.y, light.position.z); - Application::getInstance()->getGeometryCache()->renderSphere(expandedRadius, 64, 64); + Application::getInstance()->getGeometryCache()->renderSphere(expandedRadius, 32, 32); } glPopMatrix(); @@ -323,7 +323,7 @@ void DeferredLightingEffect::render() { glRotatef(glm::degrees(glm::angle(spotRotation)), axis.x, axis.y, axis.z); glTranslatef(0.0f, 0.0f, -light.radius * (1.0f + SCALE_EXPANSION * 0.5f)); Application::getInstance()->getGeometryCache()->renderCone(expandedRadius * glm::tan(light.cutoff), - expandedRadius, 64, 32); + expandedRadius, 32, 1); } glPopMatrix();