Преглед изворни кода

Fixed issues in textured shader when using bump mapping and spot lights.

sgrenier пре 12 година
родитељ
комит
215d3674c7

+ 7 - 6
gameplay/res/shaders/lighting.frag

@@ -73,11 +73,12 @@ vec3 getLitPixel()
         float attenuation = clamp(1.0 - dot(ldir, ldir), 0.0, 1.0);
         vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection[i]);
 
-        // TODO: 
-        // Let app normalize this! Need Node::getForwardVectorViewNorm
-        // This needs to be in TANGENT SPACE for bump mapping
-        // and should always pass from vertex shader via v_spotLightDirection[i]
-        vec3 spotLightDirection = normalize(u_spotLightDirection[i]);
+        // TODO: Let app normalize this! Need Node::getForwardVectorViewNorm
+        #if defined(BUMPED)
+            vec3 spotLightDirection = normalize(v_spotLightDirection[i]);
+        #else
+            vec3 spotLightDirection = normalize(u_spotLightDirection[i]);
+        #endif
 
         // "-lightDirection" is used because light direction points in opposite direction to spot direction.
         float spotCurrentAngleCos = dot(spotLightDirection, -vertexToSpotLightDirection);
@@ -87,6 +88,6 @@ vec3 getLitPixel()
         combinedColor += computeLighting(normalVector, vertexToSpotLightDirection, u_spotLightColor[i], attenuation);
     }
     #endif
-    
+
     return combinedColor;
 }

+ 3 - 2
gameplay/res/shaders/lighting.vert

@@ -27,6 +27,7 @@ void applyLight(vec4 position, mat3 tangentSpaceTransformMatrix)
     {
         // Compute the vertex to light direction, in tangent space
 	    v_vertexToSpotLightDirection[i] = tangentSpaceTransformMatrix * (u_spotLightPosition[i] - positionWorldViewSpace.xyz);
+        v_spotLightDirection[i] = tangentSpaceTransformMatrix * u_spotLightDirection[i];
     }
     #endif
     
@@ -41,7 +42,7 @@ void applyLight(vec4 position)
     #if defined(SPECULAR) || (POINT_LIGHT_COUNT > 0) || (SPOT_LIGHT_COUNT > 0)
 	vec4 positionWorldViewSpace = u_worldViewMatrix * position;
     #endif
-    
+
     #if (POINT_LIGHT_COUNT > 0)
     for (int i = 0; i < POINT_LIGHT_COUNT; ++i)
     {
@@ -49,7 +50,7 @@ void applyLight(vec4 position)
         v_vertexToPointLightDirection[i] = u_pointLightPosition[i] - positionWorldViewSpace.xyz;
     }
     #endif
-    
+
     #if (SPOT_LIGHT_COUNT > 0)
     for (int i = 0; i < SPOT_LIGHT_COUNT; ++i)
     {

+ 9 - 2
gameplay/res/shaders/textured.frag

@@ -33,8 +33,10 @@ uniform sampler2D u_normalmapTexture;
 
 #if (DIRECTIONAL_LIGHT_COUNT > 0)
 uniform vec3 u_directionalLightColor[DIRECTIONAL_LIGHT_COUNT];
+#if !defined(BUMPED)
 uniform vec3 u_directionalLightDirection[DIRECTIONAL_LIGHT_COUNT];
 #endif
+#endif
 
 #if (POINT_LIGHT_COUNT > 0)
 uniform vec3 u_pointLightColor[POINT_LIGHT_COUNT];
@@ -44,10 +46,12 @@ uniform float u_pointLightRangeInverse[POINT_LIGHT_COUNT];
 
 #if (SPOT_LIGHT_COUNT > 0)
 uniform vec3 u_spotLightColor[SPOT_LIGHT_COUNT];
-uniform vec3 u_spotLightDirection[SPOT_LIGHT_COUNT];
 uniform float u_spotLightRangeInverse[SPOT_LIGHT_COUNT];
 uniform float u_spotLightInnerAngleCos[SPOT_LIGHT_COUNT];
 uniform float u_spotLightOuterAngleCos[SPOT_LIGHT_COUNT];
+#if !defined(BUMPED)
+uniform vec3 u_spotLightDirection[SPOT_LIGHT_COUNT];
+#endif
 #endif
 
 #if defined(SPECULAR)
@@ -82,7 +86,7 @@ varying vec2 v_texCoord1;
 varying vec3 v_normalVector;
 #endif
 
-#if (defined(BUMPED) && (DIRECTIONAL_LIGHT_COUNT > 0))
+#if defined(BUMPED) && (DIRECTIONAL_LIGHT_COUNT > 0)
 varying vec3 v_directionalLightDirection[DIRECTIONAL_LIGHT_COUNT];
 #endif
 
@@ -92,6 +96,9 @@ varying vec3 v_vertexToPointLightDirection[POINT_LIGHT_COUNT];
 
 #if (SPOT_LIGHT_COUNT > 0)
 varying vec3 v_vertexToSpotLightDirection[SPOT_LIGHT_COUNT];
+#if defined(BUMPED)
+varying vec3 v_spotLightDirection[SPOT_LIGHT_COUNT];
+#endif
 #endif
 
 #if defined(SPECULAR)

+ 6 - 2
gameplay/res/shaders/textured.vert

@@ -50,7 +50,7 @@ uniform mat4 u_inverseTransposeWorldViewMatrix;
 uniform mat4 u_worldViewMatrix;
 #endif
 
-#if (DIRECTIONAL_LIGHT_COUNT > 0)
+#if defined(BUMPED) && (DIRECTIONAL_LIGHT_COUNT > 0)
 uniform vec3 u_directionalLightDirection[DIRECTIONAL_LIGHT_COUNT];
 #endif
 
@@ -60,8 +60,10 @@ uniform vec3 u_pointLightPosition[POINT_LIGHT_COUNT];
 
 #if (SPOT_LIGHT_COUNT > 0) 
 uniform vec3 u_spotLightPosition[SPOT_LIGHT_COUNT];
+#if defined(BUMPED)
 uniform vec3 u_spotLightDirection[SPOT_LIGHT_COUNT];
 #endif
+#endif
 
 #if defined(SPECULAR)
 uniform vec3 u_cameraPosition;
@@ -101,6 +103,9 @@ varying vec3 v_vertexToPointLightDirection[POINT_LIGHT_COUNT];
 
 #if (SPOT_LIGHT_COUNT > 0)
 varying vec3 v_vertexToSpotLightDirection[SPOT_LIGHT_COUNT];
+#if defined(BUMPED)
+varying vec3 v_spotLightDirection[SPOT_LIGHT_COUNT];
+#endif
 #endif
 
 #if defined(SPECULAR)
@@ -117,7 +122,6 @@ varying vec3 v_cameraDirection;
 #include "skinning-none.vert" 
 #endif
 
-
 void main()
 {
     vec4 position = getPosition();

+ 1 - 1
samples/browser/src/LightSample.cpp

@@ -380,7 +380,7 @@ void LightSample::initializeDirectionalTechnique(const char* technique)
 {
 	_lighting->getTechnique(technique)->getParameter("u_ambientColor")->setValue(Vector3(0.0f, 0.0f, 0.0f));
     _lighting->getTechnique(technique)->getParameter("u_directionalLightColor[0]")->setValue(Vector3(_redSlider->getValue(), _greenSlider->getValue(), _blueSlider->getValue()));
-    _lighting->getTechnique(technique)->getParameter("u_directionalLightDirection[0]")->bindValue(_directionalLightNode, &Node::getForwardVectorWorld); 
+    _lighting->getTechnique(technique)->getParameter("u_directionalLightDirection[0]")->bindValue(_directionalLightNode, &Node::getForwardVectorView); 
 }	
 
 void LightSample::initializeSpotTechnique(const char* technique)