Jelajahi Sumber

Merge pull request #502 from blackberry-gaming/next-setaylor

Next setaylor
Sean Paul Taylor 13 tahun lalu
induk
melakukan
9280042fe1

+ 5 - 6
gameplay/res/shaders/bumped.frag

@@ -1,5 +1,6 @@
 #define LIGHTING
 #define BUMPED
+
 #ifdef OPENGL_ES
 precision highp float;
 #endif
@@ -7,9 +8,6 @@ precision highp float;
 // Inputs
 varying vec3 v_normalVector;					// Normal vector in view space
 varying vec2 v_texCoord;						// Texture Coordinate
-#if defined(SPECULAR)
-varying vec3 v_cameraDirection;                 // Camera direction
-#endif
 #if defined(POINT_LIGHT)
 varying vec3 v_vertexToPointLightDirection;		// Light direction w.r.t current vertex in tangent space.
 varying float v_pointLightAttenuation;			// Attenuation of point light.
@@ -25,8 +23,8 @@ varying vec3 v_cameraDirection;                 // Camera direction
 #endif
 
 // Uniforms
-uniform sampler2D u_textureDiffuse;        		// Diffuse map texture
-uniform sampler2D u_textureNormal;       		// Normal map texture
+uniform sampler2D u_diffuseTexture;        		// Diffuse map texture
+uniform sampler2D u_normalmapTexture;       	// Normalmap texture
 uniform vec3 u_lightDirection;					// Light direction
 uniform vec3 u_lightColor;                      // Light color
 uniform vec3 u_ambientColor;                    // Ambient color
@@ -50,10 +48,11 @@ uniform float u_spotLightOuterAngleCos;			// The soft outer part [0.0 - 1.0]
 #include "lib/lighting-directional.frag"
 #endif
 
+// Fragment program
 void main()
 {
     // Fetch diffuse color from texture.
-    _baseColor = texture2D(u_textureDiffuse, v_texCoord);
+    _baseColor = texture2D(u_diffuseTexture, v_texCoord);
 
     // Light the pixel
     gl_FragColor.a = _baseColor.a;

+ 23 - 15
gameplay/res/shaders/bumped.vert

@@ -1,16 +1,27 @@
 #define LIGHTING
 #define BUMPED
 
+// Inputs
+attribute vec4 a_position;									// Vertex Position							(x, y, z, w)
+attribute vec3 a_normal;									// Vertex Normal							(x, y, z)
+attribute vec2 a_texCoord;									// Vertex Texture Coordinate				(u, v)
+attribute vec3 a_tangent;									// Vertex Tangent							(x, y, z)
+attribute vec3 a_binormal;									// Vertex Binormal/Bitangent				(x, y, z)
+#if defined(SKINNING)
+attribute vec4 a_blendWeights;								// Vertex blend weight, up to 4				(0, 1, 2, 3) 
+attribute vec4 a_blendIndices;								// Vertex blend index int u_matrixPalette	(0, 1, 2, 3)
+#endif
+
 // Uniforms
 uniform mat4 u_worldViewProjectionMatrix;					// Matrix to transform a position to clip 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 as an array of floats
+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
-#endi
+#endif
 #if defined(TEXTURE_REPEAT)
 uniform vec2 u_textureRepeat;
 #endif
@@ -18,26 +29,22 @@ uniform vec2 u_textureRepeat;
 uniform vec2 u_textureOffset;
 #endif
 #if defined(POINT_LIGHT)
-uniform vec3 u_pointLightPosition;							// Position
+uniform vec3 u_pointLightPosition;							// Position of light
 uniform float u_pointLightRangeInverse;						// Inverse of light range
 #elif defined(SPOT_LIGHT)
-uniform vec3 u_spotLightPosition;							// Position
+uniform vec3 u_spotLightPosition;							// Position of light
 uniform float u_spotLightRangeInverse;						// Inverse of light range
-uniform vec3 u_spotLightDirection;							// Direction
+uniform vec3 u_spotLightDirection;							// Direction of light
 #else
-uniform vec3 u_lightDirection;								// Direction
+uniform vec3 u_lightDirection;								// Direction of light
 #endif
 
-// Inputs
-attribute vec4 a_position;									// Vertex Position              (x, y, z, w)
-attribute vec3 a_normal;									// Vertex Normal                (x, y, z)
-attribute vec2 a_texCoord;									// Vertex Texture Coordinate    (u, v)
-attribute vec3 a_tangent;									// Vertex Tangent               (x, y, z)
-attribute vec3 a_binormal;									// Vertex Binormal/Bitangent    (x, y, z)
-
 // Outputs
 varying vec3 v_normalVector;								// Normal vector in view space
-varying vec2 v_texCoord;									// Output Texture Coordinate     (u,v)
+varying vec2 v_texCoord;									// Texture Coordinate
+#if defined(SPECULAR)
+varying vec3 v_cameraDirection;								// Direction the camera is looking at in tangent space
+#endif
 // Lighting
 #if defined(POINT_LIGHT)
 varying vec3 v_vertexToPointLightDirection;					// Direction of point light w.r.t current vertex in tangent space
@@ -49,7 +56,7 @@ varying float v_spotLightAttenuation;						// Attenuation of spot light
 varying vec3 v_spotLightDirection;							// Direction of spot light in tangent space
 #include "lib/lighting-spot.vert"
 #else
-uniform vec3 u_lightDirection;								// Direction of light
+uniform vec3 v_lightDirection;								// Direction of light
 #include "lib/lighting-directional.vert"
 #endif
 
@@ -60,6 +67,7 @@ uniform vec3 u_lightDirection;								// Direction of light
 #include "lib/attributes.vert" 
 #endif
 
+// Vertex program
 void main()
 {
     // Get the position, normal, tangents and binormals.

+ 2 - 2
gameplay/res/shaders/colored-unlit.frag

@@ -7,7 +7,7 @@ precision highp float;
 varying vec3 v_color;						// Input Vertex color ( r g b )
 #endif
 
-uniform vec4 u_baseColor;               	// Base color
+uniform vec4 u_diffuseColor;               	// Diffuse color
 
 // Uniforms
 #if defined(MODULATE_COLOR)
@@ -24,7 +24,7 @@ void main()
     #if defined(VERTEX_COLOR)
 	gl_FragColor.rgb = v_color;
 	#else
-	gl_FragColor = u_baseColor;
+	gl_FragColor = u_diffuseColor;
     #endif
 	
 	// Global color modulation

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

@@ -23,7 +23,7 @@ varying vec3 v_cameraDirection;                 // Camera direction
 #endif
 
 // Uniforms
-uniform vec4 u_baseColor;               		// Base color
+uniform vec4 u_diffuseColor;               		// Diffuse color
 uniform vec3 u_lightDirection;					// Light direction
 uniform vec3 u_lightColor;                      // Light color
 uniform vec3 u_ambientColor;                    // Ambient color
@@ -54,7 +54,7 @@ void main()
     #if defined(VERTEX_COLOR)
 	_baseColor.rgb = v_color;
 	#else
-	_baseColor = u_baseColor;
+	_baseColor = u_diffuseColor;
 	#endif
 
     // Light the pixel

+ 1 - 1
gameplay/res/shaders/lib/lighting-directional.frag

@@ -3,7 +3,7 @@
 vec3 getLitPixel()
 {
     // Fetch normals from the normal map
-    vec3 normalVector = normalize(texture2D(u_textureNormal, v_texCoord).rgb * 2.0 - 1.0);
+    vec3 normalVector = normalize(texture2D(u_normalmapTexture, v_texCoord).rgb * 2.0 - 1.0);
     vec3 lightDirection = normalize(v_lightDirection);
     
     #if defined(SPECULAR)

+ 1 - 1
gameplay/res/shaders/lib/lighting-point.frag

@@ -3,7 +3,7 @@
 vec3 getLitPixel()
 {
     // Fetch normals from the normal map and normalize the vectors
-    vec3 normalVector = normalize(texture2D(u_textureNormal, v_texCoord).rgb * 2.0 - 1.0);
+    vec3 normalVector = normalize(texture2D(u_normalmapTexture, v_texCoord).rgb * 2.0 - 1.0);
     vec3 vertexToPointLightDirection = normalize(v_vertexToPointLightDirection);
     
     float pointLightAttenuation = clamp(v_pointLightAttenuation, 0.0, 1.0);

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

@@ -8,7 +8,7 @@ float lerpstep( float lower, float upper, float s)
 vec3 getLitPixel()
 {
     // Fetch normals from the normal map.
-    vec3 normalVector = normalize(texture2D(u_textureNormal, v_texCoord).rgb * 2.0 - 1.0);
+    vec3 normalVector = normalize(texture2D(u_normalmapTexture, v_texCoord).rgb * 2.0 - 1.0);
     vec3 spotLightDirection = normalize(v_spotLightDirection);
     vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
     

+ 4 - 4
gameplay/res/shaders/textured-unlit.frag

@@ -3,9 +3,9 @@ precision highp float;
 #endif
 
 // Uniforms
-uniform sampler2D u_textureDiffuse;     	// Diffuse texture
+uniform sampler2D u_diffuseTexture;     	// Diffuse texture
 #if defined(TEXTURE_LIGHT)
-uniform sampler2D u_textureLight;     		// Lightmap texture
+uniform sampler2D u_lightmapTexture;     	// Lightmap texture
 #endif
 #if defined(MODULATE_COLOR)
 uniform vec4 u_modulateColor;               // Modulation color
@@ -21,9 +21,9 @@ varying vec2 v_texCoord;                	// Texture coordinate(u, v)
 void main()
 {
     // Sample the texture for the color
-    gl_FragColor = texture2D(u_textureDiffuse, v_texCoord);
+    gl_FragColor = texture2D(u_diffuseTexture, v_texCoord);
 	#if defined(TEXTURE_LIGHT)
-	vec4 lightColor = texture2D(u_textureLight, v_texCoord);
+	vec4 lightColor = texture2D(u_lightTexture, v_texCoord);
 	gl_FragColor.a *= lightColor.a;
 	#endif
 	// Global color modulation

+ 5 - 4
gameplay/res/shaders/textured.frag

@@ -22,10 +22,11 @@ varying vec3 v_cameraDirection;                 // Camera direction
 #endif
 
 // Uniforms
-uniform sampler2D u_textureDiffuse;             // Diffuse map texture
-uniform vec3 u_lightDirection;					// Light direction
-uniform vec3 u_lightColor;                      // Light color
+uniform sampler2D u_diffuseTexture;             // Diffuse map texture
 uniform vec3 u_ambientColor;                    // Ambient color
+uniform vec3 u_lightColor;                      // Light color
+uniform vec3 u_lightDirection;					// Light direction
+
 #if defined(SPECULAR)
 uniform float u_specularExponent;				// Specular exponent
 #endif
@@ -50,7 +51,7 @@ uniform float u_spotLightOuterAngleCos;			// The soft outer part [0.0 - 1.0]
 void main()
 {
     // Sample the diffuse texture for base color
-    _baseColor = texture2D(u_textureDiffuse, v_texCoord);
+    _baseColor = texture2D(u_diffuseTexture, v_texCoord);
 
     // Light the pixel
     gl_FragColor.a = _baseColor.a;

+ 1 - 3
gameplay/src/Form.cpp

@@ -290,7 +290,6 @@ void Form::setAutoHeight(bool autoHeight)
 void Form::setQuad(const Vector3& p1, const Vector3& p2, const Vector3& p3, const Vector3& p4)
 {
     Mesh* mesh = Mesh::createQuad(p1, p2, p3, p4);
-
     initializeQuad(mesh);
     SAFE_RELEASE(mesh);
 }
@@ -536,7 +535,7 @@ void Form::initializeQuad(Mesh* mesh)
     _quad = Model::create(mesh);
 
     // Create the material.
-    Material* material = _quad->setMaterial("res/shaders/textured.vsh", "res/shaders/textured.fsh");
+    Material* material = _quad->setMaterial("res/shaders/textured-unlit.vert", "res/shaders/textured-unlit.frag");
     GP_ASSERT(material);
 
     // Set the common render state block for the material.
@@ -555,7 +554,6 @@ void Form::initializeQuad(Mesh* mesh)
     GP_ASSERT(sampler);
     sampler->setWrapMode(Texture::CLAMP, Texture::CLAMP);
     material->getParameter("u_diffuseTexture")->setValue(sampler);
-    material->getParameter("u_diffuseColor")->setValue(Vector4::one());
 
     SAFE_RELEASE(sampler);
 }

+ 23 - 23
gameplay/src/SpriteBatch.cpp

@@ -9,7 +9,7 @@
 #define SPRITE_BATCH_GROW_FACTOR 2.0f
 
 // Macro for adding a sprite to the batch
-#define ADD_SPRITE_VERTEX(vtx, vx, vy, vz, vu, vv, vr, vg, vb, va) \
+#define SPRITE_ADD_VERTEX(vtx, vx, vy, vz, vu, vv, vr, vg, vb, va) \
     vtx.x = vx; vtx.y = vy; vtx.z = vz; \
     vtx.u = vu; vtx.v = vv; \
     vtx.r = vr; vtx.g = vg; vtx.b = vb; vtx.a = va
@@ -45,7 +45,6 @@
 namespace gameplay
 {
 
-// Shared sprite effects
 static Effect* __spriteEffect = NULL;
 
 SpriteBatch::SpriteBatch()
@@ -69,7 +68,9 @@ SpriteBatch::~SpriteBatch()
             __spriteEffect = NULL;
         }
         else
+        {
             __spriteEffect->release();
+        }
     }
 }
 
@@ -97,7 +98,6 @@ SpriteBatch* SpriteBatch::create(Texture* texture, Effect* effect, unsigned int
                 GP_ERROR("Unable to load sprite effect.");
                 return NULL;
             }
-
             effect = __spriteEffect;
         }
         else
@@ -242,10 +242,10 @@ void SpriteBatch::draw(float x, float y, float z, float width, float height, flo
 
     // Write sprite vertex data.
     static SpriteVertex v[4];
-    ADD_SPRITE_VERTEX(v[0], downLeft.x, downLeft.y, z, u1, v1, color.x, color.y, color.z, color.w);
-    ADD_SPRITE_VERTEX(v[1], upLeft.x, upLeft.y, z, u1, v2, color.x, color.y, color.z, color.w);
-    ADD_SPRITE_VERTEX(v[2], downRight.x, downRight.y, z, u2, v1, color.x, color.y, color.z, color.w);
-    ADD_SPRITE_VERTEX(v[3], upRight.x, upRight.y, z, u2, v2, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(v[0], downLeft.x, downLeft.y, z, u1, v1, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(v[1], upLeft.x, upLeft.y, z, u1, v2, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(v[2], downRight.x, downRight.y, z, u2, v1, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(v[3], upRight.x, upRight.y, z, u2, v2, color.x, color.y, color.z, color.w);
     
     static unsigned short indices[4] = { 0, 1, 2, 3 };
 
@@ -308,10 +308,10 @@ void SpriteBatch::draw(const Vector3& position, const Vector3& right, const Vect
 
     // Add the sprite vertex data to the batch.
     static SpriteVertex v[4];
-    ADD_SPRITE_VERTEX(v[0], p0.x, p0.y, p0.z, u1, v1, color.x, color.y, color.z, color.w);
-    ADD_SPRITE_VERTEX(v[1], p1.x, p1.y, p1.z, u2, v1, color.x, color.y, color.z, color.w);
-    ADD_SPRITE_VERTEX(v[2], p2.x, p2.y, p2.z, u1, v2, color.x, color.y, color.z, color.w);
-    ADD_SPRITE_VERTEX(v[3], p3.x, p3.y, p3.z, u2, v2, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(v[0], p0.x, p0.y, p0.z, u1, v1, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(v[1], p1.x, p1.y, p1.z, u2, v1, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(v[2], p2.x, p2.y, p2.z, u1, v2, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(v[3], p3.x, p3.y, p3.z, u2, v2, color.x, color.y, color.z, color.w);
     
     static const unsigned short indices[4] = { 0, 1, 2, 3 };
     _batch->add(v, 4, const_cast<unsigned short*>(indices), 4);
@@ -335,10 +335,10 @@ void SpriteBatch::addSprite(float x, float y, float width, float height, float u
 
     const float x2 = x + width;
     const float y2 = y + height;
-    ADD_SPRITE_VERTEX(vertices[0], x, y, 0, u1, v1, color.x, color.y, color.z, color.w);
-    ADD_SPRITE_VERTEX(vertices[1], x, y2, 0, u1, v2, color.x, color.y, color.z, color.w);
-    ADD_SPRITE_VERTEX(vertices[2], x2, y, 0, u2, v1, color.x, color.y, color.z, color.w);
-    ADD_SPRITE_VERTEX(vertices[3], x2, y2, 0, u2, v2, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(vertices[0], x, y, 0, u1, v1, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(vertices[1], x, y2, 0, u1, v2, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(vertices[2], x2, y, 0, u2, v1, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(vertices[3], x2, y2, 0, u2, v2, color.x, color.y, color.z, color.w);
 }
 
 void SpriteBatch::addSprite(float x, float y, float width, float height, float u1, float v1, float u2, float v2, const Vector4& color, const Rectangle& clip, SpriteBatch::SpriteVertex* vertices)
@@ -350,10 +350,10 @@ void SpriteBatch::addSprite(float x, float y, float width, float height, float u
     {
         const float x2 = x + width;
         const float y2 = y + height;
-        ADD_SPRITE_VERTEX(vertices[0], x, y, 0, u1, v1, color.x, color.y, color.z, color.w);
-        ADD_SPRITE_VERTEX(vertices[1], x, y2, 0, u1, v2, color.x, color.y, color.z, color.w);
-        ADD_SPRITE_VERTEX(vertices[2], x2, y, 0, u2, v1, color.x, color.y, color.z, color.w);
-        ADD_SPRITE_VERTEX(vertices[3], x2, y2, 0, u2, v2, color.x, color.y, color.z, color.w);
+        SPRITE_ADD_VERTEX(vertices[0], x, y, 0, u1, v1, color.x, color.y, color.z, color.w);
+        SPRITE_ADD_VERTEX(vertices[1], x, y2, 0, u1, v2, color.x, color.y, color.z, color.w);
+        SPRITE_ADD_VERTEX(vertices[2], x2, y, 0, u2, v1, color.x, color.y, color.z, color.w);
+        SPRITE_ADD_VERTEX(vertices[3], x2, y2, 0, u2, v2, color.x, color.y, color.z, color.w);
     }
 }
 
@@ -378,10 +378,10 @@ void SpriteBatch::draw(float x, float y, float z, float width, float height, flo
     const float x2 = x + width;
     const float y2 = y + height;
     static SpriteVertex v[4];
-    ADD_SPRITE_VERTEX(v[0], x, y, z, u1, v1, color.x, color.y, color.z, color.w);
-    ADD_SPRITE_VERTEX(v[1], x, y2, z, u1, v2, color.x, color.y, color.z, color.w);
-    ADD_SPRITE_VERTEX(v[2], x2, y, z, u2, v1, color.x, color.y, color.z, color.w);
-    ADD_SPRITE_VERTEX(v[3], x2, y2, z, u2, v2, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(v[0], x, y, z, u1, v1, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(v[1], x, y2, z, u1, v2, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(v[2], x2, y, z, u2, v1, color.x, color.y, color.z, color.w);
+    SPRITE_ADD_VERTEX(v[3], x2, y2, z, u2, v2, color.x, color.y, color.z, color.w);
 
     static unsigned short indices[4] = { 0, 1, 2, 3 };