浏览代码

This commit adds a test for lighting system of gameplay.

In order to make things function properly a number of small changes were made to the textured/colored/textured-bumped shaders.
Dimitry Tomilovskiy 13 年之前
父节点
当前提交
c6bfc711d7

+ 2 - 2
gameplay/res/shaders/colored.vert

@@ -14,7 +14,7 @@ varying vec3 v_color;										// Output Vertex Color
 
 // Uniforms
 uniform mat4 u_worldViewProjectionMatrix;					// Matrix to transform a position to clip space.
-uniform mat4 u_inverseTransposeWorldMatrix;				    // Matrix to transform a normal to view space.
+uniform mat4 u_inverseTransposeWorldViewMatrix;				// Matrix to transform a normal to view space
 #if defined(SKINNING)
 uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];		// Array of 4x3 matrices
 #endif
@@ -68,7 +68,7 @@ void main()
     gl_Position = u_worldViewProjectionMatrix * position;
 
     // Transform normal to view space.
-    mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldMatrix[0].xyz, u_inverseTransposeWorldMatrix[1].xyz, u_inverseTransposeWorldMatrix[2].xyz);
+    mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz, u_inverseTransposeWorldViewMatrix[1].xyz, u_inverseTransposeWorldViewMatrix[2].xyz);
     v_normalVector = inverseTransposeWorldViewMatrix * normal;
 
     // Apply light.

+ 6 - 5
gameplay/res/shaders/lib/lighting-directional.vert

@@ -8,10 +8,11 @@ void applyLight(mat3 tangentSpaceTransformMatrix)
     #if defined(SPECULAR)
 
     // Compute the camera direction for specular lighting
-    vec4 positionWorldSpace = u_worldViewMatrix * a_position;
-    v_cameraDirection = u_cameraPosition - positionWorldSpace.xyz;
-    
+	vec4 positionWorldViewSpace = u_worldViewMatrix * a_position;
+    v_cameraDirection = u_cameraPosition - positionWorldViewSpace.xyz;
+
     #endif
+
 }
 
 #else
@@ -22,8 +23,8 @@ void applyLight(vec4 position)
     #if defined(SPECULAR)
 
     // Compute the camera direction for specular lighting
-    vec4 positionWorldSpace = u_worldViewMatrix * position;
-    v_cameraDirection = u_cameraPosition - positionWorldSpace.xyz;
+	vec4 positionWorldViewSpace = u_worldViewMatrix * position;
+    v_cameraDirection = u_cameraPosition - positionWorldViewSpace.xyz;
 
     #endif
 }

+ 9 - 9
gameplay/res/shaders/lib/lighting-point.vert

@@ -28,22 +28,22 @@ void applyLight(mat3 tangentSpaceTransformMatrix)
 
 void applyLight(vec4 position)
 {
-    // World space position.
-    vec4 positionWorldViewSpace = u_worldViewMatrix * position;
+    // World view space position.
+	vec4 positionWorldViewSpace = u_worldViewMatrix * position;
     
-    // Compute the light direction.
-    vec3 lightDirection = u_pointLightPosition - positionWorldViewSpace.xyz;
+    // Compute the light direction with light position and the vertex position.
+	v_vertexToPointLightDirection = u_pointLightPosition - positionWorldViewSpace.xyz;
    
     // Attenuation
-    v_pointLightAttenuation = 1.0 - dot(lightDirection * u_pointLightRangeInverse, lightDirection * u_pointLightRangeInverse);
+    v_pointLightAttenuation = 1.0 - dot(v_vertexToPointLightDirection * u_pointLightRangeInverse, v_vertexToPointLightDirection * u_pointLightRangeInverse);
 
     // Output light direction.
-    v_vertexToPointLightDirection =  lightDirection;
-   
-    #if defined (SPECULAR)
+    //v_vertexToPointLightDirection = lightDirection;
    
-    vec3 cameraDirection = normalize(v_cameraDirection);
+    #if defined (SPECULAR)  
 
+	v_cameraDirection = u_cameraPosition - positionWorldViewSpace.xyz;
+	
     #endif
 }
 

+ 3 - 3
gameplay/res/shaders/lib/lighting-spot.frag

@@ -13,7 +13,7 @@ vec3 getLitPixel()
     vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
     
     // "-lightDirection" because light direction points in opposite direction to to spot direction.
-    float spotCurrentAngleCos = max(0.0, dot(spotLightDirection, -vertexToSpotLightDirection));
+    float spotCurrentAngleCos = dot(spotLightDirection, -vertexToSpotLightDirection);
     
     // Intensity of spot depends on the spot light attenuation and the part of the cone vertexToSpotLightDirection points to (inner or outer).
     float spotLightAttenuation = clamp(v_spotLightAttenuation, 0.0, 1.0);
@@ -40,8 +40,8 @@ vec3 getLitPixel()
     vec3 spotLightDirection = normalize(u_spotLightDirection); 
     vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
 
-    // "-lightDirection" is used because light direction points in opposite direction to to spot direction.
-    float spotCurrentAngleCos = max(0.0, dot(spotLightDirection, -vertexToSpotLightDirection));
+    // "-lightDirection" is used because light direction points in opposite direction to spot direction.
+    float spotCurrentAngleCos = dot(spotLightDirection, -vertexToSpotLightDirection);
     
     // Intensity of spot depends on the spot light attenuation and the 
     // part of the cone vertexToSpotLightDirection points to (inner or outer).

+ 15 - 17
gameplay/res/shaders/lib/lighting-spot.vert

@@ -2,13 +2,14 @@
 
 void applyLight(mat3 tangentSpaceTransformMatrix)
 {
-    vec4 positionWorldViewSpace = u_worldViewMatrix * a_position;
+    //vec4 positionWorldSpace = u_worldMatrix * a_position;
+	vec4 positionWorldViewSpace = u_worldViewMatrix * a_position;
 
     // Transform spot light direction to tangent space.
     v_spotLightDirection = tangentSpaceTransformMatrix * u_spotLightDirection;
 
     // Compute the light direction with light position and the vertex position.
-    vec3 lightDirection = u_spotLightPosition - positionWorldViewSpace.xyz;
+	vec3 lightDirection = u_spotLightPosition - positionWorldViewSpace.xyz;
     
     // Transform current light direction to tangent space.
     lightDirection = tangentSpaceTransformMatrix * lightDirection;
@@ -22,7 +23,7 @@ void applyLight(mat3 tangentSpaceTransformMatrix)
     #if defined(SPECULAR)
     
     // Compute camera direction and transform it to tangent space.
-    v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
+	v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
     
     #endif
 }
@@ -31,24 +32,21 @@ void applyLight(mat3 tangentSpaceTransformMatrix)
 
 void applyLight(vec4 position)
 {
-    // World space position.
-    vec4 positionWorldViewSpace = u_worldViewMatrix * position;
+    // World view space position.
+	vec4 positionWorldViewSpace = u_worldViewMatrix * position;
 
     // Compute the light direction with light position and the vertex position.
-    vec3 lightDirection = u_spotLightPosition - positionWorldViewSpace.xyz;
-
-    // Attenuation
-    v_spotLightAttenuation = 1.0 - dot(lightDirection * u_spotLightRangeInverse, lightDirection * u_spotLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToSpotLightDirection = lightDirection;
-    
-    #if defined(SPECULAR)
+	v_vertexToSpotLightDirection = u_spotLightPosition - positionWorldViewSpace.xyz;
     
+    // Attenuation
+    v_spotLightAttenuation = 1.0 - dot(v_vertexToSpotLightDirection * u_spotLightRangeInverse, v_vertexToSpotLightDirection * u_spotLightRangeInverse);
+  
     // Compute camera direction and transform it to tangent space.
-    v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
-    
-    #endif
+	#if defined(SPECULAR)
+	
+	v_cameraDirection = u_cameraPosition - positionWorldViewSpace.xyz;
+	
+	#endif
 }
 
 #endif

+ 8 - 3
gameplay/res/shaders/textured-bumped.vert

@@ -14,12 +14,17 @@ attribute vec4 a_blendIndices;								// Vertex blend index int u_matrixPalette
 
 // Uniforms
 uniform mat4 u_worldViewProjectionMatrix;					// Matrix to transform a position to clip space
-uniform mat4 u_inverseTransposeWorldMatrix;				    // Matrix to transform a normal to view space
+uniform mat4 u_inverseTransposeWorldViewMatrix;				// Matrix to transform a normal to view space
+
+#if defined(SPECULAR) || defined(SPOT_LIGHT) || defined(POINT_LIGHT)
+uniform mat4 u_worldViewMatrix;								// Matrix to tranform a position to view space
+uniform mat4 u_worldMatrix;								    // Matrix to tranform a position to world space
+#endif
+
 #if defined(SKINNING)
 uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];		// Array of 4x3 matrices
 #endif
 #if defined(SPECULAR)
-uniform mat4 u_worldViewMatrix;								// Matrix to tranform a position to view space
 uniform vec3 u_cameraPosition;                 				// Position of the camera in view space
 #endif
 #if defined(TEXTURE_REPEAT)
@@ -80,7 +85,7 @@ void main()
     gl_Position = u_worldViewProjectionMatrix * position;
 
     // Transform the normal, tangent and binormals to view space.
-    mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldMatrix[0].xyz, u_inverseTransposeWorldMatrix[1].xyz, u_inverseTransposeWorldMatrix[2].xyz);
+	mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz, u_inverseTransposeWorldViewMatrix[1].xyz, u_inverseTransposeWorldViewMatrix[2].xyz);
     vec3 normalVector = normalize(inverseTransposeWorldViewMatrix * normal);
     
     // Create a transform to convert a vector to tangent space.

+ 3 - 0
gameplay/res/shaders/textured.frag

@@ -42,11 +42,14 @@ uniform float u_modulateAlpha;              	// Modulation alpha
 #elif defined(SPOT_LIGHT)
 uniform float u_spotLightInnerAngleCos;			// The bright spot [0.0 - 1.0]
 uniform float u_spotLightOuterAngleCos;			// The soft outer part [0.0 - 1.0]
+uniform vec3 u_spotLightDirection;              // Direction of a spot light source
 #include "lib/lighting-spot.frag"
 #else
 #include "lib/lighting-directional.frag"
 #endif
 
+uniform vec3 u_cameraPosition;
+
 // Fragment Program
 void main()
 {

+ 11 - 4
gameplay/res/shaders/textured.vert

@@ -11,12 +11,18 @@ attribute vec4 a_blendIndices;								// Vertex blend index int u_matrixPalette
 
 // Uniforms
 uniform mat4 u_worldViewProjectionMatrix;					// Matrix to transform a position to clip space
-uniform mat4 u_inverseTransposeWorldMatrix;				    // Matrix to transform a normal to view space
+uniform mat4 u_inverseTransposeWorldViewMatrix;				// Matrix to transform a normal to view space
+
+//uniform mat4 u_worldViewMatrix;	
+
+#if defined(SPECULAR) || defined(SPOT_LIGHT) || defined(POINT_LIGHT)
+uniform mat4 u_worldViewMatrix;								// Matrix to tranform a position to view space
+#endif
+
 #if defined(SKINNING)
 uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];		// Array of 4x3 matrices
 #endif
 #if defined(SPECULAR)
-uniform mat4 u_worldViewMatrix;								// Matrix to tranform a position to view space
 uniform vec3 u_cameraPosition;                 				// Position of the camera in view space
 #endif
 #if defined(TEXTURE_REPEAT)
@@ -31,6 +37,7 @@ uniform float u_pointLightRangeInverse;						// Inverse of light range
 #elif defined(SPOT_LIGHT)
 uniform vec3 u_spotLightPosition;							// Position of light
 uniform float u_spotLightRangeInverse;						// Inverse of light range
+uniform vec3 u_spotLightDirection;                          // Direction of a spot light source
 #else
 #endif
 
@@ -70,12 +77,12 @@ void main()
     gl_Position = u_worldViewProjectionMatrix * position;
 
     // Transform normal to view space.
-    mat3 normalMatrix = mat3(u_inverseTransposeWorldMatrix[0].xyz, u_inverseTransposeWorldMatrix[1].xyz, u_inverseTransposeWorldMatrix[2].xyz);
+	mat3 normalMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz, u_inverseTransposeWorldViewMatrix[1].xyz, u_inverseTransposeWorldViewMatrix[2].xyz);
     v_normalVector = normalMatrix * normal;
 
     // Apply light.
     applyLight(position);
-    
+
     // Texture transformation
     v_texCoord = a_texCoord;
     #if defined(TEXTURE_REPEAT)