Parcourir la source

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

Next setaylor
Sean Paul Taylor il y a 13 ans
Parent
commit
5e7513ee0a
52 fichiers modifiés avec 1171 ajouts et 2945 suppressions
  1. 7 0
      CHANGES.md
  2. 2 2
      gameplay-template/res/box.material
  3. 16 92
      gameplay-template/res/colored.fsh
  4. 4 164
      gameplay-template/res/colored.vsh
  5. 17 2
      gameplay-template/src/TemplateGame.cpp
  6. 7 2
      gameplay-template/src/TemplateGame.h
  7. 14 14
      gameplay/gameplay.vcxproj
  8. 63 60
      gameplay/gameplay.vcxproj.filters
  9. 0 124
      gameplay/res/shaders/bumped-specular.fsh
  10. 0 248
      gameplay/res/shaders/bumped-specular.vsh
  11. 26 92
      gameplay/res/shaders/bumped.fsh
  12. 42 208
      gameplay/res/shaders/bumped.vsh
  13. 0 132
      gameplay/res/shaders/colored-specular.fsh
  14. 0 203
      gameplay/res/shaders/colored-specular.vsh
  15. 36 0
      gameplay/res/shaders/colored-unlit.fsh
  16. 29 0
      gameplay/res/shaders/colored-unlit.vsh
  17. 34 92
      gameplay/res/shaders/colored.fsh
  18. 23 155
      gameplay/res/shaders/colored.vsh
  19. 0 124
      gameplay/res/shaders/diffuse-specular.fsh
  20. 0 194
      gameplay/res/shaders/diffuse-specular.vsh
  21. 0 111
      gameplay/res/shaders/diffuse.fsh
  22. 0 191
      gameplay/res/shaders/diffuse.vsh
  23. 102 0
      gameplay/res/shaders/lib/attributes-skinning.vsh
  24. 27 0
      gameplay/res/shaders/lib/attributes.vsh
  25. 43 0
      gameplay/res/shaders/lib/lighting-directional.fsh
  26. 33 0
      gameplay/res/shaders/lib/lighting-directional.vsh
  27. 49 0
      gameplay/res/shaders/lib/lighting-point.fsh
  28. 62 0
      gameplay/res/shaders/lib/lighting-point.vsh
  29. 74 0
      gameplay/res/shaders/lib/lighting-spot.fsh
  30. 68 0
      gameplay/res/shaders/lib/lighting-spot.vsh
  31. 54 0
      gameplay/res/shaders/lib/lighting.fsh
  32. 11 0
      gameplay/res/shaders/lib/lighting.vsh
  33. 0 175
      gameplay/res/shaders/parallax-specular.fsh
  34. 0 118
      gameplay/res/shaders/parallax-specular.vsh
  35. 0 167
      gameplay/res/shaders/parallax.fsh
  36. 0 118
      gameplay/res/shaders/parallax.vsh
  37. 0 18
      gameplay/res/shaders/solid.fsh
  38. 0 10
      gameplay/res/shaders/solid.vsh
  39. 30 0
      gameplay/res/shaders/textured-unlit.fsh
  40. 40 0
      gameplay/res/shaders/textured-unlit.vsh
  41. 34 9
      gameplay/res/shaders/textured.fsh
  42. 42 59
      gameplay/res/shaders/textured.vsh
  43. 4 4
      gameplay/src/Control.cpp
  44. 4 4
      gameplay/src/Control.h
  45. 139 10
      gameplay/src/Effect.cpp
  46. 8 5
      gameplay/src/FileSystem.cpp
  47. 4 4
      gameplay/src/FileSystem.h
  48. 1 13
      gameplay/src/Material.cpp
  49. 0 1
      gameplay/src/MaterialParameter.cpp
  50. 16 15
      gameplay/src/RenderState.cpp
  51. 3 4
      gameplay/src/RenderState.h
  52. 3 1
      gameplay/src/Texture.h

+ 7 - 0
CHANGES.md

@@ -1,3 +1,10 @@
+## v1.4.0
+
+- Pre-built versions gameplay-encoder added to bin folder with TTF, DAE and FBX support built-in.
+- Improved modular shader library with support for #include in shaders.
+- Fixes to FrameBuffer, RenderTarget and DepthStencilTarget.
+- TODO
+
 ## v1.3.0
 
 - Portrait mode games on mobile platforms.

+ 2 - 2
gameplay-template/res/box.material

@@ -2,7 +2,7 @@ material box
 {
     technique
     {
-        pass
+        pass 0
         {
             // shaders
             vertexShader = res/colored.vsh
@@ -12,7 +12,7 @@ material box
             u_worldViewProjectionMatrix = WORLD_VIEW_PROJECTION_MATRIX
             u_inverseTransposeWorldViewMatrix = INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX
             u_ambientColor = 0.2, 0.2, 0.2
-            u_diffuseColor = 1.0, 1.0, 1.0, 1.0
+            u_baseColor = 1.0, 1.0, 1.0, 1.0
 
             // render state
             renderState

+ 16 - 92
gameplay-template/res/colored.fsh

@@ -3,109 +3,33 @@ precision highp float;
 #endif
 
 // Uniforms
+uniform vec3 u_lightDirection;       	        // Light direction
 uniform vec3 u_lightColor;                      // Light color
 uniform vec3 u_ambientColor;                    // Ambient color
-uniform vec4 u_diffuseColor;                    // Diffuse color
-#if defined(GLOBAL_ALPHA)
-uniform float u_globalAlpha;                    // Global alpha value
-#endif
+uniform vec4 u_baseColor;                    	// Base color
 
 // Inputs
-varying vec3 v_normalVector;                    // NormalVector in view space.
-
-// Global variables
-vec4 _baseColor;                                // Base color
-vec3 _ambientColor;                             // Ambient Color
-vec3 _diffuseColor;                             // Diffuse Color
-
-void lighting(vec3 normalVector, vec3 lightDirection, float attenuation)
-{
-    // Ambient
-    _ambientColor = _baseColor.rgb * u_ambientColor;
-
-    // Diffuse
-    float ddot = dot(normalVector, lightDirection);
-    float diffuseIntensity = attenuation * ddot;
-    diffuseIntensity = max(0.0, diffuseIntensity);
-    _diffuseColor = u_lightColor * _baseColor.rgb * diffuseIntensity;
-}
-
-#if defined(POINT_LIGHT)
+varying vec3 v_normalVector;                    // Normal vector in view space.
 
-varying vec4 v_vertexToPointLightDirection;     // Light direction w.r.t current vertex.
-
-void applyLight()
-{
-    // Normalize the vectors.
-    vec3 normalVector = normalize(v_normalVector);
-    
-    vec3 vertexToPointLightDirection = normalize(v_vertexToPointLightDirection.xyz);
-    
-    // Fetch point light attenuation.
-    float pointLightAttenuation = v_vertexToPointLightDirection.w;
-    lighting(normalVector, vertexToPointLightDirection, pointLightAttenuation);
-}
-
-#elif defined(SPOT_LIGHT)
-
-uniform vec3 u_spotLightDirection;              // Direction of the 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]
-varying vec3 v_vertexToSpotLightDirection;      // Light direction w.r.t current vertex.
-varying float v_spotLightAttenuation;           // Attenuation of spot light.
-
-float lerpstep( float lower, float upper, float s)
-{
-    return clamp( ( s - lower ) / ( upper - lower ), 0.0, 1.0 );
-}
-
-void applyLight()
+void main()
 {
-    // Normalize the vectors.
-    vec3 normalVector = normalize(v_normalVector);
-    vec3 spotLightDirection = u_spotLightDirection; 
-    vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
-
-    // "-lightDirection" is used because light direction points in opposite direction to
-    // to spot direction.
-    // Calculate spot light effect.
-    float spotCurrentAngleCos = max(0.0, 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);
-    spotLightAttenuation *= lerpstep(u_spotLightOuterAngleCos, u_spotLightInnerAngleCos, spotCurrentAngleCos);
-
-    lighting(normalVector, vertexToSpotLightDirection, spotLightAttenuation);
-}
+	// Base color
+    vec4 baseColor = u_baseColor;
 
-#else
-
-uniform vec3 u_lightDirection;       	        // Light direction
-
-void applyLight()
-{
     // Normalize the vectors.
-    vec3 normalVector = normalize(v_normalVector);
     vec3 lightDirection = normalize(u_lightDirection);
+    vec3 normalVector = normalize(v_normalVector);
 
-    lighting(normalVector, -lightDirection, 1.0);
-}
-#endif
-
-void main()
-{
-    // Fetch diffuse color from texture.
-    _baseColor = u_diffuseColor;
+    // Ambient
+    vec3 ambientColor = baseColor.rgb * u_ambientColor;
 
-    // Apply light
-    applyLight();
+    // Diffuse
+	float attenuation = 1.0;
+    float ddot = dot(normalVector, lightDirection);
+    float intensity =  max(0.0, attenuation * ddot);
+    vec3 diffuseColor = u_lightColor * baseColor.rgb * intensity;
 
     // Light the pixel
-    gl_FragColor.a = _baseColor.a;
-    gl_FragColor.rgb = _ambientColor + _diffuseColor;
-
-#if defined(GLOBAL_ALPHA)
-    gl_FragColor.a *= u_globalAlpha;
-#endif
+    gl_FragColor.a = baseColor.a;
+    gl_FragColor.rgb = ambientColor + diffuseColor;
 }

+ 4 - 164
gameplay-template/res/colored.vsh

@@ -7,177 +7,17 @@ attribute vec4 a_position;                          // Vertex Position (x, y, z,
 attribute vec3 a_normal;                            // Vertex Normal (x, y, z)
 
 // Outputs
-varying vec3 v_normalVector;                        // NormalVector in view space.
-
-#if defined(SKINNING)
-
-attribute vec4 a_blendWeights;
-attribute vec4 a_blendIndices;
-
-// 32 4x3 matrices as an array of floats
-uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
-
-// Common vectors.
-vec4 _skinnedPosition;
-vec3 _skinnedNormal;
-
-void skinPosition(float blendWeight, int matrixIndex)
-{
-    vec4 tmp;
-
-    tmp.x = dot(a_position, u_matrixPalette[matrixIndex]);
-    tmp.y = dot(a_position, u_matrixPalette[matrixIndex + 1]);
-    tmp.z = dot(a_position, u_matrixPalette[matrixIndex + 2]);
-    tmp.w = a_position.w;
-
-    _skinnedPosition += blendWeight * tmp;
-}
-
-vec4 getPosition()
-{
-    _skinnedPosition = vec4(0.0);
-
-    // Transform position to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    return _skinnedPosition;    
-}
-
-void skinNormal(float blendWeight, int matrixIndex)
-{
-    vec3 tmp;
-
-    tmp.x = dot(a_normal, u_matrixPalette[matrixIndex].xyz);
-    tmp.y = dot(a_normal, u_matrixPalette[matrixIndex + 1].xyz);
-    tmp.z = dot(a_normal, u_matrixPalette[matrixIndex + 2].xyz);
-
-    _skinnedNormal += blendWeight * tmp;
-}
-
-vec3 getNormal()
-{
-    _skinnedNormal = vec3(0.0);
-
-    // Transform normal to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    return _skinnedNormal;
-}
-
-#else
-
-vec4 getPosition()
-{
-    return a_position;    
-}
-
-vec3 getNormal()
-{
-    return a_normal;
-}
-
-#endif
-
-#if defined(POINT_LIGHT)
-
-uniform mat4 u_worldViewMatrix;                     // Matrix to tranform a position to view space.
-uniform vec3 u_pointLightPosition;                  // Position
-uniform float u_pointLightRangeInverse;             // Inverse of light range.
-varying vec4 v_vertexToPointLightDirection;         // Light direction w.r.t current vertex.
-
-void applyLight(vec4 position)
-{
-    vec4 positionWorldViewSpace = u_worldViewMatrix * position;
-    
-    // Compute the light direction.
-    vec3 lightDirection = u_pointLightPosition - positionWorldViewSpace.xyz;
-    
-    vec4 vertexToPointLightDirection;
-    vertexToPointLightDirection.xyz = lightDirection;
-    
-    // Attenuation.
-    vertexToPointLightDirection.w = 1.0 - dot(lightDirection * u_pointLightRangeInverse, lightDirection * u_pointLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToPointLightDirection =  vertexToPointLightDirection;
-}
-
-#elif defined(SPOT_LIGHT)
-
-uniform mat4 u_worldViewMatrix;                     // Matrix to tranform a position to view space.
-uniform vec3 u_spotLightPosition;                   // Position
-uniform float u_spotLightRangeInverse;              // Inverse of light range.
-varying vec3 v_vertexToSpotLightDirection;          // Light direction w.r.t current vertex.
-varying float v_spotLightAttenuation;               // Attenuation of spot light.
-
-void applyLight(vec4 position)
-{
-    vec4 positionWorldViewSpace = u_worldViewMatrix * position;
-
-    // Compute the light direction.
-    vec3 lightDirection = u_spotLightPosition - positionWorldViewSpace.xyz;
-
-    // Attenuation
-    v_spotLightAttenuation = 1.0 - dot(lightDirection * u_spotLightRangeInverse, lightDirection * u_spotLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToSpotLightDirection = lightDirection;
-}
-
-#else
-
-void applyLight(vec4 position)
-{
-}
-
-#endif
+varying vec3 v_normalVector;                        // Normal vector in view space.
 
 void main()
 {
-    vec4 position = getPosition();
-    vec3 normal = getNormal();
+    vec4 position = a_position;
+    vec3 normal = a_normal;
         
     // Transform position to clip space.
     gl_Position = u_worldViewProjectionMatrix * position;
 
     // Transform normal to view space.
-    mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz,
-                                                u_inverseTransposeWorldViewMatrix[1].xyz,
-                                                u_inverseTransposeWorldViewMatrix[2].xyz);
+    mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz, u_inverseTransposeWorldViewMatrix[1].xyz, u_inverseTransposeWorldViewMatrix[2].xyz);
     v_normalVector = inverseTransposeWorldViewMatrix * normal;
-
-    // Apply light.
-    applyLight(position);
 }

+ 17 - 2
gameplay-template/src/TemplateGame.cpp

@@ -26,6 +26,8 @@ void TemplateGame::initialize()
     Node* boxNode = _scene->findNode("box");
     Model* boxModel = boxNode->getModel();
     Material* boxMaterial = boxModel->setMaterial("res/box.material");
+	boxMaterial->getParameter("u_ambientColor")->setValue(_scene->getAmbientColor());
+	boxMaterial->getParameter("u_ambientColor")->setValue(_scene->getAmbientColor());
     boxMaterial->getParameter("u_lightColor")->setValue(light->getColor());
     boxMaterial->getParameter("u_lightDirection")->setValue(lightNode->getForwardVectorView());
 }
@@ -35,13 +37,13 @@ void TemplateGame::finalize()
     SAFE_RELEASE(_scene);
 }
 
-void TemplateGame::update(long elapsedTime)
+void TemplateGame::update(float elapsedTime)
 {
     // Rotate model
     _scene->findNode("box")->rotateY(MATH_DEG_TO_RAD((float)elapsedTime / 1000.0f * 180.0f));
 }
 
-void TemplateGame::render(long elapsedTime)
+void TemplateGame::render(float elapsedTime)
 {
     // Clear the color and depth buffers
     clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
@@ -61,6 +63,19 @@ bool TemplateGame::drawScene(Node* node)
     return true;
 }
 
+void TemplateGame::keyEvent(Keyboard::KeyEvent evt, int key)
+{
+    if (evt == Keyboard::KEY_PRESS)
+    {
+        switch (key)
+        {
+        case Keyboard::KEY_ESCAPE:
+            exit();
+            break;
+        }
+    }
+}
+
 void TemplateGame::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
 {
     switch (evt)

+ 7 - 2
gameplay-template/src/TemplateGame.h

@@ -17,6 +17,11 @@ public:
      */
     TemplateGame();
 
+    /**
+     * @see Game::keyEvent
+     */
+	void keyEvent(Keyboard::KeyEvent evt, int key);
+	
     /**
      * @see Game::touchEvent
      */
@@ -37,12 +42,12 @@ protected:
     /**
      * @see Game::update
      */
-    void update(long elapsedTime);
+    void update(float elapsedTime);
 
     /**
      * @see Game::render
      */
-    void render(long elapsedTime);
+    void render(float elapsedTime);
 
 private:
 

+ 14 - 14
gameplay/gameplay.vcxproj

@@ -204,24 +204,24 @@
     <None Include="res\logo_powered_black.png" />
     <None Include="res\logo_powered_white.png" />
     <None Include="res\logo_white.png" />
-    <None Include="res\shaders\bumped-specular.fsh" />
-    <None Include="res\shaders\bumped-specular.vsh" />
     <None Include="res\shaders\bumped.fsh" />
     <None Include="res\shaders\bumped.vsh" />
-    <None Include="res\shaders\colored-specular.fsh" />
-    <None Include="res\shaders\colored-specular.vsh" />
+    <None Include="res\shaders\colored-unlit.fsh" />
+    <None Include="res\shaders\colored-unlit.vsh" />
     <None Include="res\shaders\colored.fsh" />
     <None Include="res\shaders\colored.vsh" />
-    <None Include="res\shaders\diffuse-specular.fsh" />
-    <None Include="res\shaders\diffuse-specular.vsh" />
-    <None Include="res\shaders\diffuse.fsh" />
-    <None Include="res\shaders\diffuse.vsh" />
-    <None Include="res\shaders\parallax-specular.fsh" />
-    <None Include="res\shaders\parallax-specular.vsh" />
-    <None Include="res\shaders\parallax.fsh" />
-    <None Include="res\shaders\parallax.vsh" />
-    <None Include="res\shaders\solid.fsh" />
-    <None Include="res\shaders\solid.vsh" />
+    <None Include="res\shaders\lib-attributes-skinning.vsh" />
+    <None Include="res\shaders\lib-attributes.vsh" />
+    <None Include="res\shaders\lib-lighting-directional.fsh" />
+    <None Include="res\shaders\lib-lighting-directional.vsh" />
+    <None Include="res\shaders\lib-lighting-point.fsh" />
+    <None Include="res\shaders\lib-lighting-point.vsh" />
+    <None Include="res\shaders\lib-lighting-spot.fsh" />
+    <None Include="res\shaders\lib-lighting-spot.vsh" />
+    <None Include="res\shaders\lib-lighting.fsh" />
+    <None Include="res\shaders\lib-lighting.vsh" />
+    <None Include="res\shaders\textured-unlit.fsh" />
+    <None Include="res\shaders\textured-unlit.vsh" />
     <None Include="res\shaders\textured.fsh" />
     <None Include="res\shaders\textured.vsh" />
     <None Include="src\BoundingBox.inl" />

+ 63 - 60
gameplay/gameplay.vcxproj.filters

@@ -10,6 +10,9 @@
     <Filter Include="res\shaders">
       <UniqueIdentifier>{be0b36f1-49ed-4a06-9f1f-57c654a554fe}</UniqueIdentifier>
     </Filter>
+    <Filter Include="res\shaders\lib">
+      <UniqueIdentifier>{ab587dd0-fd08-4b5e-a38d-7b3a706ab53b}</UniqueIdentifier>
+    </Filter>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="src\Animation.cpp">
@@ -559,66 +562,6 @@
     </ClInclude>
   </ItemGroup>
   <ItemGroup>
-    <None Include="res\shaders\bumped-specular.vsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\colored.fsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\colored.vsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\colored-specular.fsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\colored-specular.vsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\diffuse.fsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\diffuse.vsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\diffuse-specular.fsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\diffuse-specular.vsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\parallax.fsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\parallax.vsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\parallax-specular.fsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\parallax-specular.vsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\solid.fsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\solid.vsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\textured.fsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\textured.vsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\bumped.fsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\bumped.vsh">
-      <Filter>res\shaders</Filter>
-    </None>
-    <None Include="res\shaders\bumped-specular.fsh">
-      <Filter>res\shaders</Filter>
-    </None>
     <None Include="src\gameplay-main-macosx.mm">
       <Filter>src</Filter>
     </None>
@@ -664,6 +607,66 @@
     <None Include="src\Joystick.inl">
       <Filter>src</Filter>
     </None>
+    <None Include="res\shaders\bumped.fsh">
+      <Filter>res\shaders</Filter>
+    </None>
+    <None Include="res\shaders\bumped.vsh">
+      <Filter>res\shaders</Filter>
+    </None>
+    <None Include="res\shaders\colored.fsh">
+      <Filter>res\shaders</Filter>
+    </None>
+    <None Include="res\shaders\colored.vsh">
+      <Filter>res\shaders</Filter>
+    </None>
+    <None Include="res\shaders\colored-unlit.fsh">
+      <Filter>res\shaders</Filter>
+    </None>
+    <None Include="res\shaders\colored-unlit.vsh">
+      <Filter>res\shaders</Filter>
+    </None>
+    <None Include="res\shaders\textured.fsh">
+      <Filter>res\shaders</Filter>
+    </None>
+    <None Include="res\shaders\textured.vsh">
+      <Filter>res\shaders</Filter>
+    </None>
+    <None Include="res\shaders\textured-unlit.fsh">
+      <Filter>res\shaders</Filter>
+    </None>
+    <None Include="res\shaders\textured-unlit.vsh">
+      <Filter>res\shaders</Filter>
+    </None>
+    <None Include="res\shaders\lib-attributes-skinning.vsh">
+      <Filter>res\shaders\lib</Filter>
+    </None>
+    <None Include="res\shaders\lib-lighting.vsh">
+      <Filter>res\shaders\lib</Filter>
+    </None>
+    <None Include="res\shaders\lib-attributes.vsh">
+      <Filter>res\shaders\lib</Filter>
+    </None>
+    <None Include="res\shaders\lib-lighting-directional.fsh">
+      <Filter>res\shaders\lib</Filter>
+    </None>
+    <None Include="res\shaders\lib-lighting-directional.vsh">
+      <Filter>res\shaders\lib</Filter>
+    </None>
+    <None Include="res\shaders\lib-lighting-point.fsh">
+      <Filter>res\shaders\lib</Filter>
+    </None>
+    <None Include="res\shaders\lib-lighting-point.vsh">
+      <Filter>res\shaders\lib</Filter>
+    </None>
+    <None Include="res\shaders\lib-lighting-spot.fsh">
+      <Filter>res\shaders\lib</Filter>
+    </None>
+    <None Include="res\shaders\lib-lighting-spot.vsh">
+      <Filter>res\shaders\lib</Filter>
+    </None>
+    <None Include="res\shaders\lib-lighting.fsh">
+      <Filter>res\shaders\lib</Filter>
+    </None>
   </ItemGroup>
   <ItemGroup>
     <None Include="src\PhysicsFixedConstraint.inl">

+ 0 - 124
gameplay/res/shaders/bumped-specular.fsh

@@ -1,124 +0,0 @@
-#ifdef OPENGL_ES
-precision highp float;
-#endif
-
-// Uniforms
-uniform vec3 u_lightColor;                  // Light color.
-uniform vec3 u_ambientColor;                // Ambient color.
-uniform float u_specularExponent;           // Specular exponent or shininess property.
-uniform sampler2D u_diffuseTexture;         // Diffuse texture.
-uniform sampler2D u_normalMapTexture;       // Normal map texture.
-#if defined(GLOBAL_ALPHA)
-uniform float u_globalAlpha;                    // Global alpha value
-#endif
-
-// Inputs
-varying vec2 v_texCoord;                    // Texture Coordinate.
-varying vec3 v_cameraDirection;             // Direction the camera is looking at in tangent space.
-
-// Global variables
-vec4 _baseColor;                            // Base color
-vec3 _ambientColor;                         // Ambient Color
-vec3 _diffuseColor;                         // Diffuse Color
-vec3 _specularColor;                        // Specular color
-
-void lighting(vec3 normalVector, vec3 cameraDirection, vec3 lightDirection, float attenuation)
-{
-    // Ambient
-    _ambientColor = _baseColor.rgb * u_ambientColor;
-
-    // Diffuse
-    float diffuseIntensity = attenuation * max(0.0, dot(normalVector, lightDirection));
-    diffuseIntensity = max(0.0, diffuseIntensity);
-    _diffuseColor = u_lightColor * _baseColor.rgb * diffuseIntensity;
-
-    // Specular
-    vec3 halfVector = normalize(cameraDirection + lightDirection);
-    float specularIntensity = attenuation * max(0.0, pow(dot(normalVector, halfVector), u_specularExponent));
-    specularIntensity = max(0.0, specularIntensity);
-    _specularColor = u_lightColor * _baseColor.rgb * specularIntensity;
-}
-
-#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.
-
-void applyLight()
-{
-    // Normalize the vectors.
-    // Fetch normals from the normal map.
-    vec3 normalVector = normalize(texture2D(u_normalMapTexture, v_texCoord).rgb * 2.0 - 1.0);
-    vec3 cameraDirection = normalize(v_cameraDirection);
-    vec3 vertexToPointLightDirection = normalize(v_vertexToPointLightDirection);
-    
-    float pointLightAttenuation = clamp(v_pointLightAttenuation, 0.0, 1.0);
-    
-    lighting(normalVector, cameraDirection, vertexToPointLightDirection, pointLightAttenuation);
-}
-
-#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]
-varying vec3 v_spotLightDirection;            // Direction of spot light in tangent space.
-varying vec3 v_vertexToSpotLightDirection;    // Direction of the spot light w.r.t current vertex in tangent space.
-varying float v_spotLightAttenuation;         // Attenuation of spot light.
-
-float lerpstep( float lower, float upper, float s)
-{
-    return clamp( ( s - lower ) / ( upper - lower ), 0.0, 1.0 );
-}
-
-void applyLight()
-{
-    // Fetch normals from the normal map.
-    vec3 normalVector = normalize(texture2D(u_normalMapTexture, v_texCoord).rgb * 2.0 - 1.0);
-    vec3 cameraDirection = normalize(v_cameraDirection);
-    vec3 spotLightDirection = normalize(v_spotLightDirection);
-    vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
-    
-    // "-lightDirection" because light direction points in opposite direction to
-    // to spot direction.
-    // Calculate spot light effect.
-    float spotCurrentAngleCos = max(0.0, 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);
-    spotLightAttenuation *= lerpstep(u_spotLightOuterAngleCos, u_spotLightInnerAngleCos, spotCurrentAngleCos);
-
-    lighting(normalVector, cameraDirection, vertexToSpotLightDirection, spotLightAttenuation);
-}
-
-#else
-
-varying vec3 v_lightDirection;                 // Direction of light in tangent space.
-
-void applyLight()
-{
-    // Fetch normals from the normal map
-    vec3 normalVector = normalize(texture2D(u_normalMapTexture, v_texCoord).rgb * 2.0 - 1.0);
-    vec3 cameraDirection = normalize(v_cameraDirection);
-    vec3 lightDirection = normalize(v_lightDirection);
-
-    lighting(normalVector, cameraDirection, -lightDirection, 1.0);
-}
-#endif
-
-void main()
-{
-    // Fetch diffuse color from texture.
-    _baseColor = texture2D(u_diffuseTexture, v_texCoord);
-
-    // Apply light
-    applyLight();
-
-    // Light the pixel
-    gl_FragColor.a = _baseColor.a;
-    gl_FragColor.rgb = _ambientColor + _diffuseColor + _specularColor;
-
-#if defined(GLOBAL_ALPHA)
-    gl_FragColor.a *= u_globalAlpha;
-#endif
-}

+ 0 - 248
gameplay/res/shaders/bumped-specular.vsh

@@ -1,248 +0,0 @@
-// Uniforms
-uniform mat4 u_worldViewMatrix;                     // Matrix to tranform a position to view space.
-uniform mat4 u_worldViewProjectionMatrix;           // Matrix to transform a position to clip space.
-uniform mat4 u_inverseTransposeWorldViewMatrix;     // Matrix to transform a normal to view space.
-uniform vec3 u_cameraPosition;                      // Position of the camera.
-
-// 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 (actually Bi-tangent) (x, y, z)
-
-// Outputs
-varying vec2 v_texCoord;                            // Texture Coordinate (u,v)
-varying vec3 v_cameraDirection;                     // Direction the camera is looking at in tangent space.
-
-#if defined(SKINNING)
-
-attribute vec4 a_blendWeights;
-attribute vec4 a_blendIndices;
-
-// 32 4x3 matrices as an array of floats
-uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
-
-// Common vectors.
-vec4 _skinnedPosition;
-vec3 _skinnedNormal;
-
-void skinPosition(float blendWeight, int matrixIndex)
-{
-    vec4 tmp;
-
-    tmp.x = dot(a_position, u_matrixPalette[matrixIndex]);
-    tmp.y = dot(a_position, u_matrixPalette[matrixIndex + 1]);
-    tmp.z = dot(a_position, u_matrixPalette[matrixIndex + 2]);
-    tmp.w = a_position.w;
-
-    _skinnedPosition += blendWeight * tmp;
-}
-
-vec4 getPosition()
-{
-    _skinnedPosition = vec4(0.0);
-
-    // Transform position to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    return _skinnedPosition;    
-}
-
-void skinTangentSpaceVector(vec3 vector, float blendWeight, int matrixIndex)
-{
-    vec3 tmp;
-
-    tmp.x = dot(vector, u_matrixPalette[matrixIndex].xyz);
-    tmp.y = dot(vector, u_matrixPalette[matrixIndex + 1].xyz);
-    tmp.z = dot(vector, u_matrixPalette[matrixIndex + 2].xyz);
-
-    _skinnedNormal += blendWeight * tmp;
-}
-
-vec3 getTangentSpaceVector(vec3 vector)
-{
-    _skinnedNormal = vec3(0.0);
-
-    // Transform normal to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinTangentSpaceVector(vector, blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinTangentSpaceVector(vector, blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinTangentSpaceVector(vector, blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinTangentSpaceVector(vector, blendWeight, matrixIndex);
-
-    return _skinnedNormal;
-}
-
-vec3 getNormal()
-{
-    return getTangentSpaceVector(a_normal);
-}
-
-vec3 getTangent()
-{
-    return getTangentSpaceVector(a_tangent);
-}
-
-vec3 getBinormal()
-{
-    return getTangentSpaceVector(a_binormal);
-}
-
-#else
-
-vec4 getPosition()
-{
-    return a_position;    
-}
-
-vec3 getNormal()
-{
-    return a_normal;
-}
-
-vec3 getTangent()
-{
-    return a_tangent;
-}
-
-vec3 getBinormal()
-{
-    return a_binormal;
-}
-
-#endif
-
-#if defined(POINT_LIGHT)
-
-uniform vec3 u_pointLightPosition;                  // Position
-uniform float u_pointLightRangeInverse;             // Inverse of light range 
-varying vec3 v_vertexToPointLightDirection;         // Direction of point light w.r.t current vertex in tangent space.
-varying float v_pointLightAttenuation;              // Attenuation of point light.
-
-void applyLight(mat3 tangentSpaceTransformMatrix)
-{
-    vec4 positionWorldViewSpace = u_worldViewMatrix * a_position;
-    
-    // Compute camera direction and transform it to tangent space.
-    v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
-    
-    // Compute the light direction with light position and the vertex position.
-    vec3 lightDirection = u_pointLightPosition - positionWorldViewSpace.xyz;
-    
-    // Transform current light direction to tangent space.
-    vec3 vertexToPointLightDirection = tangentSpaceTransformMatrix * lightDirection;
-
-    // Attenuation
-    v_pointLightAttenuation = 1.0 - dot(lightDirection * u_pointLightRangeInverse, lightDirection * u_pointLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToPointLightDirection =  vertexToPointLightDirection;
-}
-
-#elif defined(SPOT_LIGHT)
-
-uniform vec3 u_spotLightPosition;                   // Position
-uniform float u_spotLightRangeInverse;              // Inverse of light range.
-uniform vec3 u_spotLightDirection;                  // Direction
-varying vec3 v_spotLightDirection;                  // Direction of spot light in tangent space.
-varying vec3 v_vertexToSpotLightDirection;          // Direction of the spot light w.r.t current vertex in tangent space.
-varying float v_spotLightAttenuation;               // Attenuation of spot light.
-
-void applyLight(mat3 tangentSpaceTransformMatrix)
-{
-    vec4 positionWorldViewSpace = u_worldViewMatrix * a_position;
-    
-    // Compute camera direction and transform it to tangent space.
-    v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
-
-    // 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;
-    
-    // Transform current light direction to tangent space.
-    lightDirection = tangentSpaceTransformMatrix * lightDirection;
-    
-    // Attenuation
-    v_spotLightAttenuation = 1.0 - dot(lightDirection * u_spotLightRangeInverse, lightDirection * u_spotLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToSpotLightDirection = lightDirection;
-}
-
-#else
-
-uniform vec3 u_lightDirection;                      // Direction
-varying vec3 v_lightDirection;                      // Direction of light in tangent space.
-
-void applyLight(mat3 tangentSpaceTransformMatrix)
-{
-    vec4 positionWorldViewSpace = u_worldViewMatrix * a_position;
-    
-    // Compute camera direction and transform it to tangent space.
-    v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
-    
-    // Transform light direction to tangent space.
-    v_lightDirection = tangentSpaceTransformMatrix * u_lightDirection;
-}
-
-#endif
-
-void main()
-{
-    vec4 position = getPosition();
-    vec3 normal = getNormal();
-    vec3 tangent = getTangent();
-    vec3 binormal = getBinormal();
-    
-    // Transform position to clip space.
-    gl_Position = u_worldViewProjectionMatrix * position;
-
-    // Transform the normal, tangent and binormals to  view space.
-    mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz,
-                                                u_inverseTransposeWorldViewMatrix[1].xyz,
-                                                u_inverseTransposeWorldViewMatrix[2].xyz);
-    vec3 tangentVector  = normalize(inverseTransposeWorldViewMatrix * tangent);
-    vec3 normalVector = normalize(inverseTransposeWorldViewMatrix * normal);
-    vec3 binormalVector = normalize(inverseTransposeWorldViewMatrix * binormal);
-
-    // Create a transform to convert a vector to tangent space.
-    mat3 tangentSpaceTransformMatrix = mat3(tangentVector.x, binormalVector.x, normalVector.x,
-                                            tangentVector.y, binormalVector.y, normalVector.y,
-                                            tangentVector.z, binormalVector.z, normalVector.z);
-    // Apply light.
-    applyLight(tangentSpaceTransformMatrix);
-
-    // Pass on the texture coordinates to Fragment shader.
-    v_texCoord = a_texCoord;
-}

+ 26 - 92
gameplay/res/shaders/bumped.fsh

@@ -3,96 +3,29 @@ precision highp float;
 #endif
 
 // Uniforms
-uniform vec3 u_lightColor;                   // Light color.
-uniform vec3 u_ambientColor;                 // Ambient color.
-uniform sampler2D u_diffuseTexture;          // Diffuse texture.
-uniform sampler2D u_normalMapTexture;        // Normal map texture.
-#if defined(GLOBAL_ALPHA)
-uniform float u_globalAlpha;                    // Global alpha value
+uniform vec3 u_lightColor;                  // Light color.
+uniform vec3 u_ambientColor;                // Ambient color.
+uniform sampler2D u_textureDiffuse;        	// Diffuse texture.
+uniform sampler2D u_textureNormal;       	// Normal map texture.
+
+#if defined(MODULATE_COLOR)
+uniform vec4 u_modulateColor;               // Modulation color
+#endif
+#if defined(MODULATE_ALPHA)
+uniform float u_modulateAlpha;              // Modulation alpha
 #endif
 
 // Inputs
-varying vec2 v_texCoord;                     // Texture Coordinate.
-
-// Common colors
-vec4 _baseColor;                             // Base color
-vec3 _ambientColor;                          // Ambient Color
-vec3 _diffuseColor;                          // Diffuse Color
-
-void lighting(vec3 normalVector, vec3 lightDirection, float attenuation)
-{
-    // Ambient
-    _ambientColor = _baseColor.rgb * u_ambientColor;
-
-    // Diffuse
-    float diffuseIntensity = attenuation * max(0.0, dot(normalVector, lightDirection));
-    diffuseIntensity = max(0.0, diffuseIntensity);
-    _diffuseColor = u_lightColor * _baseColor.rgb * diffuseIntensity;
-}
+varying vec2 v_texCoord;                    // Texture Coordinate.
 
+// Lighting
+#include "lib/lighting.fsh"
 #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.
-
-void applyLight()
-{
-    // Normalize the vectors.
-    // Fetch normals from the normal map.
-    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);
-    
-    // Fetch point light attenuation.
-    lighting(normalVector, vertexToPointLightDirection, pointLightAttenuation);
-}
-
+#include "lib/lighting-point.fsh"
 #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]
-varying vec3 v_spotLightDirection;            // Direction of spot light in tangent space.
-varying vec3 v_vertexToSpotLightDirection;    // Direction of the spot light w.r.t current vertex in tangent space.
-varying float v_spotLightAttenuation;         // Attenuation of spot light.
-
-float lerpstep( float lower, float upper, float s)
-{
-    return clamp( ( s - lower ) / ( upper - lower ), 0.0, 1.0 );
-}
-
-void applyLight()
-{
-    // Fetch normals from the normal map.
-    vec3 normalVector = normalize(texture2D(u_normalMapTexture, v_texCoord).xyz * 2.0 - 1.0);
-    vec3 spotLightDirection =normalize(v_spotLightDirection);
-    vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
-    
-    // "-lightDirection" because light direction points in opposite direction to
-    // to spot direction.
-    // Calculate spot light effect.
-    float spotCurrentAngleCos = max(0.0, 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);
-    spotLightAttenuation *= lerpstep(u_spotLightOuterAngleCos, u_spotLightInnerAngleCos, spotCurrentAngleCos);
-
-    lighting(normalVector, vertexToSpotLightDirection, spotLightAttenuation);
-}
-
+#include "lib/lighting-spot.fsh"
 #else
-
-varying vec3 v_lightDirection;                  // Direction of light in tangent space.
-
-void applyLight()
-{
-    // Normalize vectors.
-    // Fetch normals from the normal map
-    vec3 normalVector = normalize(texture2D(u_normalMapTexture, v_texCoord).xyz * 2.0 - 1.0);
-    vec3 lightDirection = normalize(v_lightDirection);
-
-    lighting(normalVector, -lightDirection, 1.0);
-}
+#include "lib/lighting-directional.fsh"
 #endif
 
 void main()
@@ -100,14 +33,15 @@ void main()
     // Fetch diffuse color from texture.
     _baseColor = texture2D(u_diffuseTexture, v_texCoord);
 
-    // Apply light
-    applyLight();
-
     // Light the pixel
     gl_FragColor.a = _baseColor.a;
-    gl_FragColor.rgb = _ambientColor + _diffuseColor;
-
-#if defined(GLOBAL_ALPHA)
-    gl_FragColor.a *= u_globalAlpha;
-#endif
-}
+    gl_FragColor.rgb = getLitPixel();
+
+	// Global color modulation
+	#if defined(MODULATE_COLOR)
+	gl_FragColor *= u_modulateColor;
+	#endif
+	#if defined(MODULATE_ALPHA)
+    gl_FragColor.a *= u_modulateAlpha;
+    #endif
+}

+ 42 - 208
gameplay/res/shaders/bumped.vsh

@@ -1,214 +1,44 @@
 // 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.
+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(TEXTURE_REPEAT)
+uniform vec2 u_textureRepeat;
+#endif
+#if defined(TEXTURE_OFFSET)
+uniform vec2 u_textureOffset;
+#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 (actually Bi-tangent) (x, y, z)
+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 vec2 v_texCoord;                        // Texture Coordinate (u,v)
-
-#if defined(SKINNING)
-
-attribute vec4 a_blendWeights;
-attribute vec4 a_blendIndices;
-
-// 32 4x3 matrices as an array of floats
-uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
-
-// Common vectors.
-vec4 _skinnedPosition;
-vec3 _skinnedNormal;
-
-void skinPosition(float blendWeight, int matrixIndex)
-{
-    vec4 tmp;
-
-    tmp.x = dot(a_position, u_matrixPalette[matrixIndex]);
-    tmp.y = dot(a_position, u_matrixPalette[matrixIndex + 1]);
-    tmp.z = dot(a_position, u_matrixPalette[matrixIndex + 2]);
-    tmp.w = a_position.w;
-
-    _skinnedPosition += blendWeight * tmp;
-}
-
-vec4 getPosition()
-{
-    _skinnedPosition = vec4(0.0);
-
-    // Transform position to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    return _skinnedPosition;    
-}
-
-void skinTangentSpaceVector(vec3 vector, float blendWeight, int matrixIndex)
-{
-    vec3 tmp;
-
-    tmp.x = dot(vector, u_matrixPalette[matrixIndex].xyz);
-    tmp.y = dot(vector, u_matrixPalette[matrixIndex + 1].xyz);
-    tmp.z = dot(vector, u_matrixPalette[matrixIndex + 2].xyz);
-
-    _skinnedNormal += blendWeight * tmp;
-}
-
-vec3 getTangentSpaceVector(vec3 vector)
-{
-    _skinnedNormal = vec3(0.0);
-
-    // Transform normal to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinTangentSpaceVector(vector, blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinTangentSpaceVector(vector, blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinTangentSpaceVector(vector, blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinTangentSpaceVector(vector, blendWeight, matrixIndex);
-
-    return _skinnedNormal;
-}
-
-vec3 getNormal()
-{
-    return getTangentSpaceVector(a_normal);
-}
-
-vec3 getTangent()
-{
-    return getTangentSpaceVector(a_tangent);
-}
-
-vec3 getBinormal()
-{
-    return getTangentSpaceVector(a_binormal);
-}
-
-#else
-
-vec4 getPosition()
-{
-    return a_position;    
-}
-
-vec3 getNormal()
-{
-    return a_normal;
-}
-
-vec3 getTangent()
-{
-    return a_tangent;
-}
-
-vec3 getBinormal()
-{
-    return a_binormal;
-}
-
-#endif
+varying vec2 v_texCoord;                            // Output Texture Coordinate     (u,v)
 
+// Lighting
+#include "lib/lighting.vsh"
 #if defined(POINT_LIGHT)
-
-uniform mat4 u_worldViewMatrix;                 // Matrix to tranform a position to view space.
-uniform vec3 u_pointLightPosition;              // Position
-uniform float u_pointLightRangeInverse;         // Inverse of light range.
-varying vec3 v_vertexToPointLightDirection;     // Direction of point light w.r.t current vertex in tangent space.
-varying float v_pointLightAttenuation;          // Attenuation of point light.
-
-void applyLight(mat3 tangentSpaceTransformMatrix)
-{
-    // World space position.
-    vec4 positionWorldViewSpace = u_worldViewMatrix * a_position;
-    
-    // Compute the light direction with light position and the vertex position.
-    vec3 lightDirection = u_pointLightPosition - positionWorldViewSpace.xyz;
-    
-    // Transform current light direction to tangent space.
-    vec3 vertexToSpotLightDirection = tangentSpaceTransformMatrix * lightDirection;
-
-    // Attenuation
-    v_pointLightAttenuation = 1.0 - dot(lightDirection * u_pointLightRangeInverse, lightDirection * u_pointLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToPointLightDirection =  vertexToSpotLightDirection;
-}
-
+#include "lib/lighting-point.vsh"
 #elif defined(SPOT_LIGHT)
-
-uniform mat4 u_worldViewMatrix;                 // Matrix to tranform a position to view space.
-uniform vec3 u_spotLightPosition;               // Position
-uniform float u_spotLightRangeInverse;          // Inverse of light range.
-uniform vec3 u_spotLightDirection;              // Direction
-varying vec3 v_spotLightDirection;              // Direction of spot light in tangent space.
-varying vec3 v_vertexToSpotLightDirection;      // Direction of the spot light w.r.t current vertex in tangent space.
-varying float v_spotLightAttenuation;           // Attenuation of spot light.
-
-void applyLight(mat3 tangentSpaceTransformMatrix)
-{
-    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;
-    
-    // Transform current light direction to tangent space.
-    lightDirection = tangentSpaceTransformMatrix * lightDirection;
-    
-    // Attenuation
-    v_spotLightAttenuation = 1.0 - dot(lightDirection * u_spotLightRangeInverse, lightDirection * u_spotLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToSpotLightDirection = lightDirection;
-}
-
+#include "lib/lighting-spot.vsh"
 #else
+#include "lib/lighting-directional.vsh"
+#endif
 
-uniform vec3 u_lightDirection;                  // Direction
-varying vec3 v_lightDirection;                  // Direction of light in tangent space.
-
-void applyLight(mat3 tangentSpaceTransformMatrix)
-{
-    // Transform light direction to tangent space.
-    v_lightDirection = tangentSpaceTransformMatrix * u_lightDirection;
-}
-
+// Attribute Accessors (getPosition(), getNormal(), etc.)
+#define BUMPED
+#if defined(SKINNING)
+#include "lib/attributes-skinning.vsh"
+#else
+#include "lib/attributes.vsh" 
 #endif
 
 void main()
 {
+    // Get the position, normal, tangents and binormals.
     vec4 position = getPosition();
     vec3 normal = getNormal();
     vec3 tangent = getTangent();
@@ -217,20 +47,24 @@ void main()
     // Transform position to clip space.
     gl_Position = u_worldViewProjectionMatrix * position;
 
-    // Transform the normal, tangent and binormals to  view space.
-    mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix);
-    vec3 tangentVector  = normalize(inverseTransposeWorldViewMatrix * tangent);
+    // Transform the normal, tangent and binormals to view space.
+    mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz, u_inverseTransposeWorldViewMatrix[1].xyz, u_inverseTransposeWorldViewMatrix[2].xyz);
     vec3 normalVector = normalize(inverseTransposeWorldViewMatrix * normal);
-    vec3 binormalVector = normalize(inverseTransposeWorldViewMatrix * binormal);
-
+    
     // Create a transform to convert a vector to tangent space.
-    mat3 tangentSpaceTransformMatrix = mat3(tangentVector.x, binormalVector.x, normalVector.x,
-                                            tangentVector.y, binormalVector.y, normalVector.y,
-                                            tangentVector.z, binormalVector.z, normalVector.z);
-
+    vec3 tangentVector  = normalize(inverseTransposeWorldViewMatrix * tangent);
+    vec3 binormalVector = normalize(inverseTransposeWorldViewMatrix * binormal);
+    mat3 tangentSpaceTransformMatrix = mat3(tangentVector.x, binormalVector.x, normalVector.x, tangentVector.y, binormalVector.y, normalVector.y, tangentVector.z, binormalVector.z, normalVector.z);
+    
     // Apply light.
     applyLight(tangentSpaceTransformMatrix);
-
-    // Pass on the texture coordinates to Fragment shader.
+    
+    // Texture transformation.
     v_texCoord = a_texCoord;
+    #if defined(TEXTURE_REPEAT)
+    v_texCoord *= u_textureRepeat;
+    #endif
+    #if defined(TEXTURE_OFFSET)
+    v_texCoord += u_textureOffset;
+    #endif
 }

+ 0 - 132
gameplay/res/shaders/colored-specular.fsh

@@ -1,132 +0,0 @@
-#ifdef OPENGL_ES
-precision highp float;
-#endif
-
-// Uniforms
-uniform vec3 u_lightColor;                      // Light color
-uniform vec3 u_ambientColor;                    // Ambient color
-uniform float u_specularExponent;               // Specular exponent or shininess property
-#if !defined(VERTEX_COLOR)
-uniform vec4 u_diffuseColor;                    // Diffuse color
-#endif
-#if defined(GLOBAL_ALPHA)
-uniform float u_globalAlpha;                    // Global alpha value
-#endif
-
-// Inputs
-varying vec3 v_normalVector;                    // NormalVector in view space
-varying vec3 v_cameraDirection;                 // Camera direction
-#if defined(VERTEX_COLOR)
-varying vec4 v_color;							// Vertex color
-#endif
-
-// Global variables
-vec4 _baseColor;                                // Base color
-vec3 _ambientColor;                             // Ambient Color
-vec3 _diffuseColor;                             // Diffuse Color
-vec3 _specularColor;                            // Specular color
-
-void lighting(vec3 normalVector, vec3 cameraDirection, vec3 lightDirection, float attenuation)
-{
-    // Ambient
-    _ambientColor = _baseColor.rgb * u_ambientColor;
-
-    // Diffuse
-    float ddot = abs(dot(normalVector, lightDirection));
-    float diffuseIntensity = attenuation * ddot;
-    diffuseIntensity = max(0.0, diffuseIntensity);
-    _diffuseColor = u_lightColor * _baseColor.rgb * diffuseIntensity;
-
-    // Specular
-    vec3 halfVector = normalize(cameraDirection + lightDirection);
-    float specularIntensity = attenuation * max(0.0, pow(dot(normalVector, halfVector), u_specularExponent));
-    specularIntensity = max(0.0, specularIntensity);
-    _specularColor = u_lightColor * _baseColor.rgb * specularIntensity;
-}
-
-#if defined(POINT_LIGHT)
-
-varying vec4 v_vertexToPointLightDirection;      // Light direction w.r.t current vertex.
-
-void applyLight()
-{
-    // Normalize the vectors.
-    vec3 normalVector = normalize(v_normalVector);
-    vec3 cameraDirection = normalize(v_cameraDirection);
-
-    vec3 vertexToPointLightDirection = normalize(v_vertexToPointLightDirection.xyz);
-
-    // Fetch point light attenuation.
-    float pointLightAttenuation = v_vertexToPointLightDirection.w;
-    lighting(normalVector, cameraDirection, vertexToPointLightDirection, pointLightAttenuation);
-}
-
-#elif defined(SPOT_LIGHT)
-
-uniform vec3 u_spotLightDirection;              // Direction of the 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]
-varying vec3 v_vertexToSpotLightDirection;      // Light direction w.r.t current vertex.
-varying float v_spotLightAttenuation;           // Attenuation of spot light.
-
-float lerpstep( float lower, float upper, float s)
-{
-    return clamp( ( s - lower ) / ( upper - lower ), 0.0, 1.0 );
-}
-
-void applyLight()
-{
-    // Normalize the vectors.
-    vec3 normalVector = normalize(v_normalVector);
-    vec3 cameraDirection = normalize(v_cameraDirection);
-    vec3 spotLightDirection = normalize(u_spotLightDirection); 
-    vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
-
-    // "-lightDirection" is used because light direction points in opposite direction to
-    // to spot direction.
-    // Calculate spot light effect.
-    float spotCurrentAngleCos = max(0.0, 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);
-    spotLightAttenuation *= lerpstep(u_spotLightOuterAngleCos, u_spotLightInnerAngleCos, spotCurrentAngleCos);
-
-    lighting(normalVector, cameraDirection, vertexToSpotLightDirection, spotLightAttenuation);
-}
-
-#else
-
-uniform vec3 u_lightDirection;                  // Light direction
-
-void applyLight()
-{
-    // Normalize the vectors.
-    vec3 normalVector = normalize(v_normalVector);
-    vec3 cameraDirection = normalize(v_cameraDirection);
-    vec3 lightDirection = normalize(u_lightDirection);
-
-    lighting(normalVector, cameraDirection, -lightDirection, 1.0);
-}
-#endif
-
-void main()
-{
-    // Set base diffuse color
-#if defined(VERTEX_COLOR)
-	_baseColor = v_color;
-#else
-	_baseColor = u_diffuseColor;
-#endif
-
-    // Apply light
-    applyLight();
-
-    // Light the pixel
-    gl_FragColor.a = _baseColor.a;
-    gl_FragColor.rgb = _ambientColor + _diffuseColor + _specularColor;
-
-#if defined(GLOBAL_ALPHA)
-    gl_FragColor.a *= u_globalAlpha;
-#endif
-}

+ 0 - 203
gameplay/res/shaders/colored-specular.vsh

@@ -1,203 +0,0 @@
-// 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.
-uniform mat4 u_worldMatrix;                         // Matrix to tranform a position to world space.
-uniform vec3 u_cameraPosition;                      // Position of the camera.
-
-// Inputs
-attribute vec4 a_position;                          // Vertex Position (x, y, z, w)
-attribute vec3 a_normal;                            // Vertex Normal (x, y, z)
-#if defined(VERTEX_COLOR)
-attribute vec4 a_color;
-#endif
-
-// Outputs
-varying vec3 v_normalVector;                        // NormalVector in view space
-varying vec3 v_cameraDirection;                     // Camera direction
-#if defined(VERTEX_COLOR)
-varying vec4 v_color;								// Vertex color
-#endif
-
-#if defined(SKINNING)
-
-attribute vec4 a_blendWeights;
-attribute vec4 a_blendIndices;
-
-// 32 4x3 matrices as an array of floats
-uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
-
-// Common vectors.
-vec4 _skinnedPosition;
-vec3 _skinnedNormal;
-
-void skinPosition(float blendWeight, int matrixIndex)
-{
-    vec4 tmp;
-
-    tmp.x = dot(a_position, u_matrixPalette[matrixIndex]);
-    tmp.y = dot(a_position, u_matrixPalette[matrixIndex + 1]);
-    tmp.z = dot(a_position, u_matrixPalette[matrixIndex + 2]);
-    tmp.w = a_position.w;
-
-    _skinnedPosition += blendWeight * tmp;
-}
-
-vec4 getPosition()
-{
-    _skinnedPosition = vec4(0.0);
-
-    // Transform position to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    return _skinnedPosition;    
-}
-
-void skinNormal(float blendWeight, int matrixIndex)
-{
-    vec3 tmp;
-
-    tmp.x = dot(a_normal, u_matrixPalette[matrixIndex].xyz);
-    tmp.y = dot(a_normal, u_matrixPalette[matrixIndex + 1].xyz);
-    tmp.z = dot(a_normal, u_matrixPalette[matrixIndex + 2].xyz);
-
-    _skinnedNormal += blendWeight * tmp;
-}
-
-vec3 getNormal()
-{
-    _skinnedNormal = vec3(0.0);
-
-    // Transform normal to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    return _skinnedNormal;
-}
-
-#else
-
-vec4 getPosition()
-{
-    return a_position;    
-}
-
-vec3 getNormal()
-{
-    return a_normal;
-}
-
-#endif
-
-
-#if defined(POINT_LIGHT)
-
-uniform mat4 u_worldViewMatrix;                     // Matrix to tranform a position to view space.
-uniform vec3 u_pointLightPosition;                  // Position
-uniform float u_pointLightRangeInverse;             // Inverse of light range. 
-varying vec4 v_vertexToPointLightDirection;         // Light direction w.r.t current vertex.
-
-void applyLight(vec4 position)
-{
-    // World space position.
-    vec4 positionWorldViewSpace = u_worldViewMatrix * position;
-    
-    // Compute the light direction with light position and the vertex position.
-    vec3 lightDirection = u_pointLightPosition - positionWorldViewSpace.xyz;
-    
-    vec4 vertexToPointLightDirection;
-    vertexToPointLightDirection.xyz = lightDirection;
-    
-    // Attenuation
-    vertexToPointLightDirection.w = 1.0 - dot(lightDirection * u_pointLightRangeInverse, lightDirection * u_pointLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToPointLightDirection =  vertexToPointLightDirection;
-}
-
-#elif defined(SPOT_LIGHT)
-
-uniform mat4 u_worldViewMatrix;                     // Matrix to tranform a position to view space.
-uniform vec3 u_spotLightPosition;                   // Position
-uniform float u_spotLightRangeInverse;              // Inverse of light range.
-varying vec3 v_vertexToSpotLightDirection;          // Light direction w.r.t current vertex.
-varying float v_spotLightAttenuation;               // Attenuation of spot light.
-
-void applyLight(vec4 position)
-{
-    // World 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);
-
-    // Compute the light direction with light position and the vertex position.
-    v_vertexToSpotLightDirection = lightDirection;
-}
-
-#else
-
-void applyLight(vec4 position)
-{
-}
-
-#endif
-
-void main()
-{
-    vec4 position = getPosition();
-    vec3 normal = getNormal();
-
-    // Transform position to clip space.
-    gl_Position = u_worldViewProjectionMatrix * position;
-
-    // Transform normal to view space.
-    mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz,
-                                                u_inverseTransposeWorldViewMatrix[1].xyz,
-                                                u_inverseTransposeWorldViewMatrix[2].xyz);
-    v_normalVector = inverseTransposeWorldViewMatrix * normal;
-
-    // Compute the camera direction.
-    vec4 positionWorldSpace = u_worldMatrix * position;
-    v_cameraDirection = u_cameraPosition - positionWorldSpace.xyz;
-
-#if defined(VERTEX_COLOR)
-	v_color = a_color;
-#endif
-
-    // Apply light.
-    applyLight(position);
-}

+ 36 - 0
gameplay/res/shaders/colored-unlit.fsh

@@ -0,0 +1,36 @@
+#ifdef OPENGL_ES
+precision highp float;
+#endif
+
+// Inputs
+#if defined(VERTEX_COLOR)
+varying vec3 v_color;						// Input Vertex color ( r g b )
+#endif
+
+uniform vec4 u_baseColor;               	// Base color
+
+// Uniforms
+#if defined(MODULATE_COLOR)
+uniform vec4 u_modulateColor;               // Modulation color
+#endif
+#if defined(MODULTE_ALPHA)
+uniform float u_modulateAlpha;              // Modulation alpha
+#endif
+
+void main()
+{
+    // Set base diffuse color
+    #if defined(VERTEX_COLOR)
+	gl_FragColor.rgb = v_color;
+	#else
+	gl_FragColor = u_baseColor;
+    #endif
+	
+	// Global color modulation
+	#if defined(MODULATE_COLOR)
+	gl_FragColor *= u_modulateColor;
+	#endif
+	#if defined(MODULTE_ALPHA)
+    gl_FragColor.a *= u_modulateAlpha;
+    #endif
+}

+ 29 - 0
gameplay/res/shaders/colored-unlit.vsh

@@ -0,0 +1,29 @@
+// Uniforms
+uniform mat4 u_worldViewProjectionMatrix;           // Matrix to transform a position to clip space.
+
+// Inputs
+attribute vec4 a_position;                          // Vertex Position          (x, y, z, w)
+#if defined(VERTEX_COLOR)
+attribute vec3 a_color;                             // Vertex Color             (r, g, b)
+varying vec3 v_color;								// Output Vertex color      (r, g, b)
+#endif
+
+#if defined(SKINNING)
+#include "lib/attributes-skinning.vsh"
+#else
+#include "lib/attributes.vsh" 
+#endif
+
+void main()
+{
+    // Get the vertex position
+    vec4 position = getPosition();
+    
+    // Transform position to clip space.a
+    gl_Position = u_worldViewProjectionMatrix *  position;
+    
+     // Pass on vertex color to fragment shader
+    #if defined(VERTEX_COLOR)
+	v_color = a_color;
+    #endif
+}

+ 34 - 92
gameplay/res/shaders/colored.fsh

@@ -3,109 +3,51 @@ precision highp float;
 #endif
 
 // Uniforms
-uniform vec3 u_lightColor;                      // Light color
-uniform vec3 u_ambientColor;                    // Ambient color
-uniform vec4 u_diffuseColor;                    // Diffuse color
-#if defined(GLOBAL_ALPHA)
-uniform float u_globalAlpha;                    // Global alpha value
-#endif
-
-// Inputs
-varying vec3 v_normalVector;                    // NormalVector in view space.
+uniform vec3 u_lightColor;                  // Light color
+uniform vec3 u_ambientColor;                // Ambient color
 
-// Global variables
-vec4 _baseColor;                                // Base color
-vec3 _ambientColor;                             // Ambient Color
-vec3 _diffuseColor;                             // Diffuse Color
+uniform vec4 u_baseColor;               	// Modulation color
 
-void lighting(vec3 normalVector, vec3 lightDirection, float attenuation)
-{
-    // Ambient
-    _ambientColor = _baseColor.rgb * u_ambientColor;
+#if defined(MODULATE_COLOR)
+uniform vec4 u_modulateColor;               // Modulation color
+#endif
+#if defined(MODULATE_ALPHA)
+uniform float u_tintAlpha;              	// Tint alpha
+#endif
 
-    // Diffuse
-    float ddot = dot(normalVector, lightDirection);
-    float diffuseIntensity = attenuation * ddot;
-    diffuseIntensity = max(0.0, diffuseIntensity);
-    _diffuseColor = u_lightColor * _baseColor.rgb * diffuseIntensity;
-}
+// Inputs
+varying vec3 v_normalVector;                // Normal vector in view space
+#if defined(VERTEX_COLOR)
+varying vec3 v_color;						// Vertex color
+#endif
 
+// Lighting
+#include "lib/lighting.fsh"
 #if defined(POINT_LIGHT)
-
-varying vec4 v_vertexToPointLightDirection;     // Light direction w.r.t current vertex.
-
-void applyLight()
-{
-    // Normalize the vectors.
-    vec3 normalVector = normalize(v_normalVector);
-    
-    vec3 vertexToPointLightDirection = normalize(v_vertexToPointLightDirection.xyz);
-    
-    // Fetch point light attenuation.
-    float pointLightAttenuation = v_vertexToPointLightDirection.w;
-    lighting(normalVector, vertexToPointLightDirection, pointLightAttenuation);
-}
-
+#include "lib/lighting-point.fsh"
 #elif defined(SPOT_LIGHT)
-
-uniform vec3 u_spotLightDirection;              // Direction of the 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]
-varying vec3 v_vertexToSpotLightDirection;      // Light direction w.r.t current vertex.
-varying float v_spotLightAttenuation;           // Attenuation of spot light.
-
-float lerpstep( float lower, float upper, float s)
-{
-    return clamp( ( s - lower ) / ( upper - lower ), 0.0, 1.0 );
-}
-
-void applyLight()
-{
-    // Normalize the vectors.
-    vec3 normalVector = normalize(v_normalVector);
-    vec3 spotLightDirection = u_spotLightDirection; 
-    vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
-
-    // "-lightDirection" is used because light direction points in opposite direction to
-    // to spot direction.
-    // Calculate spot light effect.
-    float spotCurrentAngleCos = max(0.0, 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);
-    spotLightAttenuation *= lerpstep(u_spotLightOuterAngleCos, u_spotLightInnerAngleCos, spotCurrentAngleCos);
-
-    lighting(normalVector, vertexToSpotLightDirection, spotLightAttenuation);
-}
-
+#include "lib/lighting-spot.fsh"
 #else
-
-uniform vec3 u_lightDirection;       	        // Light direction
-
-void applyLight()
-{
-    // Normalize the vectors.
-    vec3 normalVector = normalize(v_normalVector);
-    vec3 lightDirection = normalize(u_lightDirection);
-
-    lighting(normalVector, -lightDirection, 1.0);
-}
+#include "lib/lighting-directional.fsh"
 #endif
 
 void main()
 {
-    // Fetch diffuse color from texture.
-    _baseColor = u_diffuseColor;
-
-    // Apply light
-    applyLight();
+    // Set base diffuse color
+    #if defined(VERTEX_COLOR)
+	_baseColor.rgb = v_color;
+	#else
+	_baseColor = u_baseColor;
+	#endif
 
     // Light the pixel
     gl_FragColor.a = _baseColor.a;
-    gl_FragColor.rgb = _ambientColor + _diffuseColor;
-
-#if defined(GLOBAL_ALPHA)
-    gl_FragColor.a *= u_globalAlpha;
-#endif
-}
+    gl_FragColor.rgb = getLitPixel();
+    
+	#if defined(MODULATE_COLOR)
+    gl_FragColor.a *= u_modulateColor;
+    #endif
+	#if defined(MODULATE_ALPHA)
+    gl_FragColor.a *= u_modulateAlpha;
+    #endif
+}

+ 23 - 155
gameplay/res/shaders/colored.vsh

@@ -5,179 +5,47 @@ uniform mat4 u_inverseTransposeWorldViewMatrix;     // Matrix to transform a nor
 // Inputs
 attribute vec4 a_position;                          // Vertex Position (x, y, z, w)
 attribute vec3 a_normal;                            // Vertex Normal (x, y, z)
+#if defined(VERTEX_COLOR)
+attribute vec3 a_color;                             // Vertex Color (r, g, b)
+varying vec3 v_color;								// Output Vertex color 
+#endif
 
 // Outputs
-varying vec3 v_normalVector;                        // NormalVector in view space.
-
-#if defined(SKINNING)
-
-attribute vec4 a_blendWeights;
-attribute vec4 a_blendIndices;
-
-// 32 4x3 matrices as an array of floats
-uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
-
-// Common vectors.
-vec4 _skinnedPosition;
-vec3 _skinnedNormal;
-
-void skinPosition(float blendWeight, int matrixIndex)
-{
-    vec4 tmp;
-
-    tmp.x = dot(a_position, u_matrixPalette[matrixIndex]);
-    tmp.y = dot(a_position, u_matrixPalette[matrixIndex + 1]);
-    tmp.z = dot(a_position, u_matrixPalette[matrixIndex + 2]);
-    tmp.w = a_position.w;
-
-    _skinnedPosition += blendWeight * tmp;
-}
-
-vec4 getPosition()
-{
-    _skinnedPosition = vec4(0.0);
-
-    // Transform position to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    return _skinnedPosition;    
-}
-
-void skinNormal(float blendWeight, int matrixIndex)
-{
-    vec3 tmp;
-
-    tmp.x = dot(a_normal, u_matrixPalette[matrixIndex].xyz);
-    tmp.y = dot(a_normal, u_matrixPalette[matrixIndex + 1].xyz);
-    tmp.z = dot(a_normal, u_matrixPalette[matrixIndex + 2].xyz);
-
-    _skinnedNormal += blendWeight * tmp;
-}
-
-vec3 getNormal()
-{
-    _skinnedNormal = vec3(0.0);
-
-    // Transform normal to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    return _skinnedNormal;
-}
-
-#else
-
-vec4 getPosition()
-{
-    return a_position;    
-}
-
-vec3 getNormal()
-{
-    return a_normal;
-}
-
-#endif
+varying vec3 v_normalVector;                        // Normal vector in view space.
 
+#include "lib/lighting.vsh"
 #if defined(POINT_LIGHT)
-
-uniform mat4 u_worldViewMatrix;                     // Matrix to tranform a position to view space.
-uniform vec3 u_pointLightPosition;                  // Position
-uniform float u_pointLightRangeInverse;             // Inverse of light range.
-varying vec4 v_vertexToPointLightDirection;         // Light direction w.r.t current vertex.
-
-void applyLight(vec4 position)
-{
-    vec4 positionWorldViewSpace = u_worldViewMatrix * position;
-    
-    // Compute the light direction.
-    vec3 lightDirection = u_pointLightPosition - positionWorldViewSpace.xyz;
-    
-    vec4 vertexToPointLightDirection;
-    vertexToPointLightDirection.xyz = lightDirection;
-    
-    // Attenuation.
-    vertexToPointLightDirection.w = 1.0 - dot(lightDirection * u_pointLightRangeInverse, lightDirection * u_pointLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToPointLightDirection =  vertexToPointLightDirection;
-}
-
+#include "lib/lighting-point.vsh"
 #elif defined(SPOT_LIGHT)
-
-uniform mat4 u_worldViewMatrix;                     // Matrix to tranform a position to view space.
-uniform vec3 u_spotLightPosition;                   // Position
-uniform float u_spotLightRangeInverse;              // Inverse of light range.
-varying vec3 v_vertexToSpotLightDirection;          // Light direction w.r.t current vertex.
-varying float v_spotLightAttenuation;               // Attenuation of spot light.
-
-void applyLight(vec4 position)
-{
-    vec4 positionWorldViewSpace = u_worldViewMatrix * position;
-
-    // Compute the light direction.
-    vec3 lightDirection = u_spotLightPosition - positionWorldViewSpace.xyz;
-
-    // Attenuation
-    v_spotLightAttenuation = 1.0 - dot(lightDirection * u_spotLightRangeInverse, lightDirection * u_spotLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToSpotLightDirection = lightDirection;
-}
-
+#include "lib/lighting-spot.vsh"
 #else
+#include "lib/lighting-directional.vsh"
+#endif
 
-void applyLight(vec4 position)
-{
-}
-
+#if defined(SKINNING)
+#include "lib/attributes-skinning.vsh"
+#else
+#include "lib/attributes.vsh" 
 #endif
 
 void main()
 {
+    // Get the position and normal
     vec4 position = getPosition();
     vec3 normal = getNormal();
-        
+
     // Transform position to clip space.
     gl_Position = u_worldViewProjectionMatrix * position;
 
     // Transform normal to view space.
-    mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz,
-                                                u_inverseTransposeWorldViewMatrix[1].xyz,
-                                                u_inverseTransposeWorldViewMatrix[2].xyz);
+    mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz, u_inverseTransposeWorldViewMatrix[1].xyz, u_inverseTransposeWorldViewMatrix[2].xyz);
     v_normalVector = inverseTransposeWorldViewMatrix * normal;
 
     // Apply light.
     applyLight(position);
-}
+    
+    // Pass the vertex color to fragment shader
+    #if defined(VERTEX_COLOR)
+	v_color = a_color;
+    #endif
+}

+ 0 - 124
gameplay/res/shaders/diffuse-specular.fsh

@@ -1,124 +0,0 @@
-#ifdef OPENGL_ES
-precision highp float;
-#endif
-
-// Uniforms
-uniform vec3 u_lightColor;                      // Light color
-uniform vec3 u_ambientColor;                    // Ambient color
-uniform float u_specularExponent;               // Specular exponent or shininess property.
-uniform sampler2D u_diffuseTexture;             // Diffuse texture.
-#if defined(GLOBAL_ALPHA)
-uniform float u_globalAlpha;                    // Global alpha value
-#endif
-
-// Inputs
-varying vec3 v_normalVector;                    // NormalVector in view space.
-varying vec2 v_texCoord;                        // Texture coordinate (u, v).
-varying vec3 v_cameraDirection;                 // Camera direction
-
-// Global variables
-vec4 _baseColor;                                // Base color
-vec3 _ambientColor;                             // Ambient Color
-vec3 _diffuseColor;                             // Diffuse Color
-vec3 _specularColor;                            // Specular color
-
-void lighting(vec3 normalVector, vec3 cameraDirection, vec3 lightDirection, float attenuation)
-{
-    // Ambient
-    _ambientColor = _baseColor.rgb * u_ambientColor;
-
-    // Diffuse
-    float ddot = dot(normalVector, lightDirection);
-    float diffuseIntensity = attenuation * ddot;
-    diffuseIntensity = max(0.0, diffuseIntensity);
-    _diffuseColor = u_lightColor * _baseColor.rgb * diffuseIntensity;
-
-    // Specular
-    vec3 halfVector = normalize(lightDirection + cameraDirection);
-    float specularIntensity = attenuation * max(0.0, pow(dot(normalVector, halfVector), u_specularExponent));
-    specularIntensity = max(0.0, specularIntensity);
-    _specularColor = u_lightColor * _baseColor.rgb * specularIntensity;
-}
-
-#if defined(POINT_LIGHT)
-
-varying vec4 v_vertexToPointLightDirection;      // Light direction w.r.t current vertex.
-
-void applyLight()
-{
-    // Normalize the vectors.
-    vec3 normalVector = normalize(v_normalVector);
-    vec3 cameraDirection = normalize(v_cameraDirection);
-    
-    vec3 vertexToPointLightDirection = normalize(v_vertexToPointLightDirection.xyz);
-    
-    // Fetch point light attenuation.
-    float pointLightAttenuation = v_vertexToPointLightDirection.w;
-    lighting(normalVector, cameraDirection, vertexToPointLightDirection, pointLightAttenuation);
-}
-
-#elif defined(SPOT_LIGHT)
-
-uniform vec3 u_spotLightDirection;              // Direction of the 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]
-varying vec3 v_vertexToSpotLightDirection;      // Light direction w.r.t current vertex.
-varying float v_spotLightAttenuation;           // Attenuation of spot light.
-
-float lerpstep( float lower, float upper, float s)
-{
-    return clamp( ( s - lower ) / ( upper - lower ), 0.0, 1.0 );
-}
-
-void applyLight()
-{
-    // Normalize the vectors.
-    vec3 normalVector = normalize(v_normalVector);
-    vec3 cameraDirection = normalize(v_cameraDirection);
-    vec3 spotLightDirection = normalize(u_spotLightDirection); 
-    vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
-
-    // "-lightDirection" is used because light direction points in opposite direction to
-    // to spot direction.
-    // Calculate spot light effect.
-    float spotCurrentAngleCos = max(0.0, 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);
-    spotLightAttenuation *= lerpstep(u_spotLightOuterAngleCos, u_spotLightInnerAngleCos, spotCurrentAngleCos);
-
-    lighting(normalVector, cameraDirection, vertexToSpotLightDirection, spotLightAttenuation);
-}
-
-#else
-
-uniform vec3 u_lightDirection;                  // Light direction
-
-void applyLight()
-{
-    // Normalize the vectors.
-    vec3 normalVector = normalize(v_normalVector);
-    vec3 cameraDirection = normalize(v_cameraDirection);
-    vec3 lightDirection = normalize(u_lightDirection);
-
-    lighting(normalVector, cameraDirection, -lightDirection, 1.0);
-}
-#endif
-
-void main()
-{
-    // Fetch diffuse color from texture.
-    _baseColor = texture2D(u_diffuseTexture, v_texCoord);
-
-    // Apply light
-    applyLight();
-
-    // Light the pixel
-    gl_FragColor.a = _baseColor.a;
-    gl_FragColor.rgb = _ambientColor + _diffuseColor + _specularColor;
-
-#if defined(GLOBAL_ALPHA)
-    gl_FragColor.a *= u_globalAlpha;
-#endif
-}

+ 0 - 194
gameplay/res/shaders/diffuse-specular.vsh

@@ -1,194 +0,0 @@
-// 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.
-uniform mat4 u_worldViewMatrix;                     // Matrix to tranform a position to view space.
-uniform vec3 u_cameraPosition;                      // Position of the camera in view space.
-
-// 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)
-
-// Outputs
-varying vec3 v_normalVector;                        // NormalVector in view space.
-varying vec2 v_texCoord;                            // Texture coordinate (u, v).
-varying vec3 v_cameraDirection;                     // Camera direction
-
-#if defined(SKINNING)
-
-attribute vec4 a_blendWeights;
-attribute vec4 a_blendIndices;
-
-// 32 4x3 matrices as an array of floats
-uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
-
-// Common vectors.
-vec4 _skinnedPosition;
-vec3 _skinnedNormal;
-
-void skinPosition(float blendWeight, int matrixIndex)
-{
-    vec4 tmp;
-
-    tmp.x = dot(a_position, u_matrixPalette[matrixIndex]);
-    tmp.y = dot(a_position, u_matrixPalette[matrixIndex + 1]);
-    tmp.z = dot(a_position, u_matrixPalette[matrixIndex + 2]);
-    tmp.w = a_position.w;
-
-    _skinnedPosition += blendWeight * tmp;
-}
-
-vec4 getPosition()
-{
-    _skinnedPosition = vec4(0.0);
-
-    // Transform position to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    return _skinnedPosition;    
-}
-
-void skinNormal(float blendWeight, int matrixIndex)
-{
-    vec3 tmp;
-
-    tmp.x = dot(a_normal, u_matrixPalette[matrixIndex].xyz);
-    tmp.y = dot(a_normal, u_matrixPalette[matrixIndex + 1].xyz);
-    tmp.z = dot(a_normal, u_matrixPalette[matrixIndex + 2].xyz);
-
-    _skinnedNormal += blendWeight * tmp;
-}
-
-vec3 getNormal()
-{
-    _skinnedNormal = vec3(0.0);
-
-    // Transform normal to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    return _skinnedNormal;
-}
-
-#else
-
-vec4 getPosition()
-{
-    return a_position;    
-}
-
-vec3 getNormal()
-{
-    return a_normal;
-}
-
-#endif
-
-#if defined(POINT_LIGHT)
-
-uniform vec3 u_pointLightPosition;                      // Position
-uniform float u_pointLightRangeInverse;                 // Inverse of light range.
-varying vec4 v_vertexToPointLightDirection;             // Light direction w.r.t current vertex.
-
-void applyLight(vec4 position)
-{
-    vec4 positionWorldViewSpace = u_worldViewMatrix * position;
-    
-    // Compute the light direction with light position and the vertex position.
-    vec3 lightDirection = u_pointLightPosition - positionWorldViewSpace.xyz;
-
-    vec4 vertexToPointLightDirection;
-    vertexToPointLightDirection.xyz = lightDirection;
-
-    // Attenuation
-    vertexToPointLightDirection.w = 1.0 - dot(lightDirection * u_pointLightRangeInverse, lightDirection * u_pointLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToPointLightDirection =  vertexToPointLightDirection;
-}
-
-#elif defined(SPOT_LIGHT)
-
-uniform vec3 u_spotLightPosition;                       // Position
-uniform float u_spotLightRangeInverse;                  // Inverse of light range.
-varying vec3 v_vertexToSpotLightDirection;              // Light direction w.r.t current vertex.
-varying float v_spotLightAttenuation;                   // Attenuation of spot light.
-
-void applyLight(vec4 position)
-{
-    // World 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);
-
-    // Compute the light direction with light position and the vertex position.
-    v_vertexToSpotLightDirection = lightDirection;
-}
-
-#else
-
-void applyLight(vec4 position)
-{
-}
-
-#endif
-
-void main()
-{
-    vec4 position = getPosition();
-    vec3 normal = getNormal();
-
-    // Transform position to clip space.
-    gl_Position = u_worldViewProjectionMatrix * position;
-
-    // Transform normal to view space.
-    mat3 normalMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz,
-                             u_inverseTransposeWorldViewMatrix[1].xyz,
-                             u_inverseTransposeWorldViewMatrix[2].xyz);
-    v_normalVector = normalMatrix * normal;
-
-    // Compute the camera direction.
-    vec4 positionWorldSpace = u_worldViewMatrix * position;
-    v_cameraDirection = u_cameraPosition - positionWorldSpace.xyz;
-
-    // Apply light.
-    applyLight(position);
-
-	// Pass on the texture coordinates to Fragment shader.
-    v_texCoord = a_texCoord;
-}

+ 0 - 111
gameplay/res/shaders/diffuse.fsh

@@ -1,111 +0,0 @@
-#ifdef OPENGL_ES
-precision highp float;
-#endif
-
-// Uniforms
-uniform vec3 u_lightColor;                      // Light color
-uniform vec3 u_ambientColor;                    // Ambient color
-uniform sampler2D u_diffuseTexture;             // Diffuse texture.
-#if defined(GLOBAL_ALPHA)
-uniform float u_globalAlpha;                    // Global alpha value
-#endif
-
-// Inputs
-varying vec3 v_normalVector;                    // NormalVector in view space.
-varying vec2 v_texCoord;                        // Texture coordinate (u, v).
-
-// Global variables
-vec4 _baseColor;                                // Base color
-vec3 _ambientColor;                             // Ambient Color
-vec3 _diffuseColor;                             // Diffuse Color
-
-void lighting(vec3 normalVector, vec3 lightDirection, float attenuation)
-{
-    // Ambient
-    _ambientColor = _baseColor.rgb * u_ambientColor;
-
-    // Diffuse
-	float ddot = dot(normalVector, lightDirection);
-    float diffuseIntensity = attenuation * ddot;
-    diffuseIntensity = max(0.0, diffuseIntensity);
-    _diffuseColor = u_lightColor * _baseColor.rgb * diffuseIntensity;
-}
-
-#if defined(POINT_LIGHT)
-
-varying vec4 v_vertexToPointLightDirection;     // Light direction w.r.t current vertex.
-
-void applyLight()
-{
-    // Normalize the vectors.
-    vec3 normalVector = normalize(v_normalVector);
-    
-    vec3 vertexToPointLightDirection = normalize(v_vertexToPointLightDirection.xyz);
-    
-    // Fetch point light attenuation.
-    float pointLightAttenuation = v_vertexToPointLightDirection.w;
-    lighting(normalVector, vertexToPointLightDirection, pointLightAttenuation);
-}
-
-#elif defined(SPOT_LIGHT)
-
-uniform vec3 u_spotLightDirection;              // Direction of the 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]
-varying vec3 v_vertexToSpotLightDirection;      // Light direction w.r.t current vertex.
-varying float v_spotLightAttenuation;           // Attenuation of spot light.
-
-float lerpstep( float lower, float upper, float s)
-{
-    return clamp( ( s - lower ) / ( upper - lower ), 0.0, 1.0 );
-}
-
-void applyLight()
-{
-    // Normalize the vectors.
-    vec3 normalVector = normalize(v_normalVector);
-    vec3 spotLightDirection =normalize(u_spotLightDirection); 
-    vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
-
-    // "-lightDirection" is used because light direction points in opposite direction to
-    // to spot direction.
-    // Calculate spot light effect.
-    float spotCurrentAngleCos = max(0.0, 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);
-    spotLightAttenuation *= lerpstep(u_spotLightOuterAngleCos, u_spotLightInnerAngleCos, spotCurrentAngleCos);
-
-    lighting(normalVector, vertexToSpotLightDirection, spotLightAttenuation);
-}
-
-#else
-
-uniform vec3 u_lightDirection;                  // Light direction
-
-void applyLight()
-{
-    vec3 normalVector = normalize(v_normalVector);
-    vec3 lightDirection = normalize(u_lightDirection);
-
-    lighting(normalVector, -lightDirection, 1.0);
-}
-#endif
-
-void main()
-{
-    // Fetch diffuse color from texture.
-    _baseColor = texture2D(u_diffuseTexture, v_texCoord);
-
-    // Apply light
-    applyLight();
-
-    // Light the pixel
-    gl_FragColor.a = _baseColor.a;
-    gl_FragColor.rgb = _ambientColor + _diffuseColor;
-
-#if defined(GLOBAL_ALPHA)
-    gl_FragColor.a *= u_globalAlpha;
-#endif
-}

+ 0 - 191
gameplay/res/shaders/diffuse.vsh

@@ -1,191 +0,0 @@
-// 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.
-
-// 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)
-
-// Outputs
-varying vec3 v_normalVector;                        // NormalVector in view space.
-varying vec2 v_texCoord;                            // Texture coordinate (u, v).
-varying vec3 v_cameraDirection;                     // Camera direction
-
-#if defined(SKINNING)
-
-attribute vec4 a_blendWeights;
-attribute vec4 a_blendIndices;
-
-// 32 4x3 matrices as an array of floats
-uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
-
-// Common vectors.
-vec4 _skinnedPosition;
-vec3 _skinnedNormal;
-
-void skinPosition(float blendWeight, int matrixIndex)
-{
-    vec4 tmp;
-
-    tmp.x = dot(a_position, u_matrixPalette[matrixIndex]);
-    tmp.y = dot(a_position, u_matrixPalette[matrixIndex + 1]);
-    tmp.z = dot(a_position, u_matrixPalette[matrixIndex + 2]);
-    tmp.w = a_position.w;
-
-    _skinnedPosition += blendWeight * tmp;
-}
-
-vec4 getPosition()
-{
-    _skinnedPosition = vec4(0.0);
-
-    // Transform position to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    return _skinnedPosition;    
-}
-
-void skinNormal(float blendWeight, int matrixIndex)
-{
-    vec3 tmp;
-
-    tmp.x = dot(a_normal, u_matrixPalette[matrixIndex].xyz);
-    tmp.y = dot(a_normal, u_matrixPalette[matrixIndex + 1].xyz);
-    tmp.z = dot(a_normal, u_matrixPalette[matrixIndex + 2].xyz);
-
-    _skinnedNormal += blendWeight * tmp;
-}
-
-vec3 getNormal()
-{
-    _skinnedNormal = vec3(0.0);
-
-    // Transform normal to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinNormal(blendWeight, matrixIndex);
-
-    return _skinnedNormal;
-}
-
-#else
-
-vec4 getPosition()
-{
-    return a_position;    
-}
-
-vec3 getNormal()
-{
-    return a_normal;
-}
-
-#endif
-
-#if defined(POINT_LIGHT)
-
-uniform mat4 u_worldViewMatrix;                     // Matrix to tranform a position to view space.
-uniform vec3 u_pointLightPosition;                  // Position
-uniform float u_pointLightRange;                    // Inverse of light range.
-varying vec4 v_vertexToPointLightDirection;         // Light direction w.r.t current vertex.
-
-void applyLight(vec4 position)
-{
-    // World space position.
-    vec4 positionWorldViewSpace = u_worldViewMatrix * position;
-    
-    // Compute the light direction.
-    vec3 lightDirection = u_pointLightPosition - positionWorldViewSpace.xyz;
-    
-    vec4 vertexToPointLightDirection;
-    vertexToPointLightDirection.xyz = lightDirection;
-    
-    // Attenuation
-    vertexToPointLightDirection.w = 1.0 - dot(lightDirection * u_pointLightRangeInverse, lightDirection * u_pointLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToPointLightDirection =  vertexToPointLightDirection;
-}
-
-#elif defined(SPOT_LIGHT)
-
-uniform mat4 u_worldViewMatrix;                     // Matrix to tranform a position to view space.
-uniform vec3 u_spotLightPosition;                   // Position
-uniform float u_spotLightRangeInverse;              // Inverse of light range.
-varying vec3 v_vertexToSpotLightDirection;          // Light direction w.r.t current vertex.
-varying float v_spotLightAttenuation;               // Attenuation of spot light.
-
-void applyLight(vec4 position)
-{
-    // World 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;
-}
-
-#else
-
-void applyLight(vec4 position)
-{
-}
-
-#endif
-
-void main()
-{
-    vec4 position = getPosition();
-    vec3 normal = getNormal();
-    
-    // Transform position to clip space.
-    gl_Position = u_worldViewProjectionMatrix * position;
-
-    // Transform normal to view space.
-    mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz,
-                                                u_inverseTransposeWorldViewMatrix[1].xyz,
-                                                u_inverseTransposeWorldViewMatrix[2].xyz);
-    v_normalVector = inverseTransposeWorldViewMatrix * normal;
-
-    // Apply light.
-    applyLight(position);
-
-    // Pass on the texture coordinates to Fragment shader.
-    v_texCoord = a_texCoord;
-}

+ 102 - 0
gameplay/res/shaders/lib/attributes-skinning.vsh

@@ -0,0 +1,102 @@
+attribute vec4 a_blendWeights;
+attribute vec4 a_blendIndices;
+
+// 32 4x3 matrices as an array of floats
+uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
+
+// Global 
+vec4 _skinnedPosition;
+#if defined(LIGHTING)
+vec3 _skinnedNormal;
+#endif
+
+void skinPosition(float blendWeight, int matrixIndex)
+{
+    vec4 tmp;
+    tmp.x = dot(a_position, u_matrixPalette[matrixIndex]);
+    tmp.y = dot(a_position, u_matrixPalette[matrixIndex + 1]);
+    tmp.z = dot(a_position, u_matrixPalette[matrixIndex + 2]);
+    tmp.w = a_position.w;
+
+    _skinnedPosition += blendWeight * tmp;
+}
+
+vec4 getPosition()
+{
+    _skinnedPosition = vec4(0.0);
+
+    // Transform position to view space using matrix palette with four matrices used to transform a vertex.
+    float blendWeight = a_blendWeights[0];
+    int matrixIndex = int (a_blendIndices[0]) * 3;
+    skinPosition(blendWeight, matrixIndex);
+
+    blendWeight = a_blendWeights[1];
+    matrixIndex = int(a_blendIndices[1]) * 3;
+    skinPosition(blendWeight, matrixIndex);
+
+    blendWeight = a_blendWeights[2];
+    matrixIndex = int(a_blendIndices[2]) * 3;
+    skinPosition(blendWeight, matrixIndex);
+
+    blendWeight = a_blendWeights[3];
+    matrixIndex = int(a_blendIndices[3]) * 3;
+    skinPosition(blendWeight, matrixIndex);
+
+    return _skinnedPosition;    
+}
+
+void skinTangentSpaceVector(vec3 vector, float blendWeight, int matrixIndex)
+{
+    vec3 tmp;
+    tmp.x = dot(vector, u_matrixPalette[matrixIndex].xyz);
+    tmp.y = dot(vector, u_matrixPalette[matrixIndex + 1].xyz);
+    tmp.z = dot(vector, u_matrixPalette[matrixIndex + 2].xyz);
+	
+    _skinnedNormal += blendWeight * tmp;
+}
+
+vec3 getTangentSpaceVector(vec3 vector)
+{
+    _skinnedNormal = vec3(0.0);
+
+    // Transform normal to view space using matrix palette with four matrices used to transform a vertex.
+    float blendWeight = a_blendWeights[0];
+    int matrixIndex = int (a_blendIndices[0]) * 3;
+    skinTangentSpaceVector(vector, blendWeight, matrixIndex);
+
+    blendWeight = a_blendWeights[1];
+    matrixIndex = int(a_blendIndices[1]) * 3;
+    skinTangentSpaceVector(vector, blendWeight, matrixIndex);
+
+    blendWeight = a_blendWeights[2];
+    matrixIndex = int(a_blendIndices[2]) * 3;
+    skinTangentSpaceVector(vector, blendWeight, matrixIndex);
+
+    blendWeight = a_blendWeights[3];
+    matrixIndex = int(a_blendIndices[3]) * 3;
+    skinTangentSpaceVector(vector, blendWeight, matrixIndex);
+
+    return _skinnedNormal;
+}
+
+#if defined(LIGHTING)
+
+vec3 getNormal()
+{
+    return getTangentSpaceVector(a_normal);
+}
+
+#if defined(BUMPED)
+
+vec3 getTangent()
+{
+    return getTangentSpaceVector(a_tangent);
+}
+
+vec3 getBinormal()
+{
+    return getTangentSpaceVector(a_binormal);
+}
+
+#endif
+#endif

+ 27 - 0
gameplay/res/shaders/lib/attributes.vsh

@@ -0,0 +1,27 @@
+vec4 getPosition()
+{
+    return a_position;    
+}
+
+#if defined(LIGHTING)
+
+vec3 getNormal()
+{
+    return a_normal;
+}
+
+#if defined(BUMPED)
+
+vec3 getTangent()
+{
+    return a_tangent;
+}
+
+vec3 getBinormal()
+{
+    return a_binormal;
+}
+
+#endif
+
+#endif

+ 43 - 0
gameplay/res/shaders/lib/lighting-directional.fsh

@@ -0,0 +1,43 @@
+varying vec3 v_lightDirection;                 // Direction of light in tangent space.
+
+#if defined(BUMPED)
+
+vec3 getLitPixel()
+{
+    // Fetch normals from the normal map
+    vec3 normalVector = normalize(texture2D(u_textureNormal, v_texCoord).rgb * 2.0 - 1.0);
+    vec3 lightDirection = normalize(v_lightDirection);
+    
+    #if defined(SPECULAR)
+    
+    vec3 cameraDirection = normalize(v_cameraDirection);
+    return computeLighting(normalVector, -lightDirection, 1.0, cameraDirection);
+    
+    #else
+    
+    return computeLighting(normalVector, -lightDirection, 1.0);
+    
+    #endif
+}
+
+#else
+
+vec3 getLitPixel()
+{
+    // Normalize the vectors.
+    vec3 normalVector = normalize(v_normalVector);
+    vec3 lightDirection = normalize(u_lightDirection);
+
+    #if defined(SPECULAR)
+    
+    vec3 cameraDirection = normalize(v_cameraDirection);
+    return computeLighting(normalVector, -lightDirection, 1.0, cameraDirection);
+    
+    #else
+    
+    return computeLighting(normalVector, -lightDirection, 1.0);
+    
+    #endif
+}
+
+#endif

+ 33 - 0
gameplay/res/shaders/lib/lighting-directional.vsh

@@ -0,0 +1,33 @@
+#if defined(BUMPED)
+
+uniform vec3 u_lightDirection;                  // Direction of light
+varying vec3 v_lightDirection;                  // Direction of light in tangent space.
+
+void applyLight(mat3 tangentSpaceTransformMatrix)
+{
+    // Transform light direction to tangent space
+    v_lightDirection = tangentSpaceTransformMatrix * u_lightDirection;
+    
+    // Compute the camera direction for specular lighting
+    #if defined(SPECULAR)
+    
+    vec4 positionWorldSpace = u_worldViewMatrix * position;
+    v_cameraDirection = u_cameraPosition - positionWorldSpace.xyz;
+    
+    #endif
+}
+
+#else
+
+void applyLight(vec4 position)
+{
+    // Compute the camera direction for specular lighting
+    #if defined(SPECULAR)
+    
+    vec4 positionWorldSpace = u_worldViewMatrix * position;
+    v_cameraDirection = u_cameraPosition - positionWorldSpace.xyz;
+
+    #endif
+}
+
+#endif

+ 49 - 0
gameplay/res/shaders/lib/lighting-point.fsh

@@ -0,0 +1,49 @@
+varying vec3 v_vertexToPointLightDirection;   // Light direction w.r.t current vertex in tangent space.
+varying float v_pointLightAttenuation;        // Attenuation of point light.
+
+#if defined(BUMPED)
+
+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 vertexToPointLightDirection = normalize(v_vertexToPointLightDirection);
+    
+    float pointLightAttenuation = clamp(v_pointLightAttenuation, 0.0, 1.0);
+    
+    #if defined(SPECULAR)
+    
+    vec3 cameraDirection = normalize(v_cameraDirection);
+    return computeLighting(normalVector, vertexToPointLightDirection, pointLightAttenuation, cameraDirection);
+    
+    #else
+    
+    return computeLighting(normalVector, vertexToPointLightDirection, pointLightAttenuation);
+    
+    #endif
+}
+
+#else
+
+vec3 getLitPixel()
+{
+    // Normalize the vectors.
+    vec3 normalVector = normalize(v_normalVector);    
+    vec3 vertexToPointLightDirection = normalize(v_vertexToPointLightDirection.xyz);
+    
+    // Fetch point light attenuation.
+    float pointLightAttenuation = v_vertexToPointLightDirection.w;
+    
+    #if defined (SPECULAR)
+    
+    vec3 cameraDirection = normalize(v_cameraDirection);    
+    return computeLighting(normalVector, vertexToPointLightDirection, pointLightAttenuation, cameraDirection);
+    
+    #else
+    
+    return computeLighting(normalVector, vertexToPointLightDirection, pointLightAttenuation);
+    
+    #endif
+}
+
+#endif

+ 62 - 0
gameplay/res/shaders/lib/lighting-point.vsh

@@ -0,0 +1,62 @@
+#if defined(BUMPED)
+
+uniform vec3 u_pointLightPosition;                  // Position
+uniform float u_pointLightRangeInverse;             // Inverse of light range 
+varying vec3 v_vertexToPointLightDirection;         // Direction of point light w.r.t current vertex in tangent space.
+varying float v_pointLightAttenuation;              // Attenuation of point light.
+
+void applyLight(mat3 tangentSpaceTransformMatrix)
+{
+    vec4 positionWorldViewSpace = u_worldViewMatrix * a_position;
+    
+    // Compute the light direction with light position and the vertex position.
+    vec3 lightDirection = u_pointLightPosition - positionWorldViewSpace.xyz;
+    
+    // Transform current light direction to tangent space.
+    vec3 vertexToPointLightDirection = tangentSpaceTransformMatrix * lightDirection;
+
+    // Attenuation
+    v_pointLightAttenuation = 1.0 - dot(lightDirection * u_pointLightRangeInverse, lightDirection * u_pointLightRangeInverse);
+
+    // Output light direction.
+    v_vertexToPointLightDirection =  vertexToPointLightDirection;
+    
+    #if defined (SPECULAR)
+ 
+    // Compute camera direction and transform it to tangent space.
+    v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
+    
+    #endif
+}
+
+#else
+
+uniform vec3 u_pointLightPosition;                  // Position
+uniform float u_pointLightRange;                    // Inverse of light range.
+varying vec4 v_vertexToPointLightDirection;         // Light direction w.r.t current vertex.
+
+void applyLight(vec4 position)
+{
+    // World space position.
+    vec4 positionWorldViewSpace = u_worldViewMatrix * position;
+    
+    // Compute the light direction.
+    vec3 lightDirection = u_pointLightPosition - positionWorldViewSpace.xyz;
+   
+    vec4 vertexToPointLightDirection;
+    vertexToPointLightDirection.xyz = lightDirection;
+   
+    // Attenuation
+    vertexToPointLightDirection.w = 1.0 - dot(lightDirection * u_pointLightRangeInverse, lightDirection * u_pointLightRangeInverse);
+
+    // Output light direction.
+    v_vertexToPointLightDirection =  vertexToPointLightDirection;
+   
+    #if defined (SPECULAR)
+   
+    vec3 cameraDirection = normalize(v_cameraDirection);
+
+    #endif
+}
+
+#endif

+ 74 - 0
gameplay/res/shaders/lib/lighting-spot.fsh

@@ -0,0 +1,74 @@
+uniform float u_spotLightInnerAngleCos;       // The bright spot [0.0 - 1.0]
+uniform float u_spotLightOuterAngleCos;       // The soft outer part [0.0 - 1.0]
+
+varying vec3 v_spotLightDirection;            // Direction of spot light in tangent space.
+varying vec3 v_vertexToSpotLightDirection;    // Direction of the spot light w.r.t current vertex in tangent space.
+varying float v_spotLightAttenuation;         // Attenuation of spot light.
+
+float lerpstep( float lower, float upper, float s)
+{
+    return clamp( ( s - lower ) / ( upper - lower ), 0.0, 1.0 );
+}
+
+#if defined(BUMPED)
+
+vec3 getLitPixel()
+{
+    // Fetch normals from the normal map.
+    vec3 normalVector = normalize(texture2D(u_textureNormal, v_texCoord).rgb * 2.0 - 1.0);
+    vec3 spotLightDirection = normalize(v_spotLightDirection);
+    vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
+    
+    // "-lightDirection" because light direction points in opposite direction to
+    // to spot direction.
+    // Calculate spot light effect.
+    float spotCurrentAngleCos = max(0.0, 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);
+    spotLightAttenuation *= lerpstep(u_spotLightOuterAngleCos, u_spotLightInnerAngleCos, spotCurrentAngleCos);
+
+    #if defined(SPECULAR)
+    
+    vec3 cameraDirection = normalize(v_cameraDirection);
+    return computeLighting(normalVector, vertexToSpotLightDirection, spotLightAttenuation, cameraDirection);
+    
+    #else
+    
+    return computeLighting(normalVector, vertexToSpotLightDirection, spotLightAttenuation);
+    
+    #endif
+}
+
+#else
+
+vec3 getLitPixel()
+{
+    // Normalize the vectors.
+    vec3 normalVector = normalize(v_normalVector);
+    vec3 spotLightDirection = normalize(u_spotLightDirection); 
+    vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
+
+    // "-lightDirection" is used because light direction points in opposite direction to to spot direction.
+    // Calculate spot light effect.
+    float spotCurrentAngleCos = max(0.0, 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);
+    spotLightAttenuation *= lerpstep(u_spotLightOuterAngleCos, u_spotLightInnerAngleCos, spotCurrentAngleCos);
+
+    #if defined(SPECULAR)
+    
+    vec3 cameraDirection = normalize(v_cameraDirection);
+    return computeLighting(normalVector, vertexToSpotLightDirection, spotLightAttenuation, cameraDirection);
+    
+    #else
+    
+    return computeLighting(normalVector, vertexToSpotLightDirection, spotLightAttenuation);
+    
+    #endif
+}
+
+#endif

+ 68 - 0
gameplay/res/shaders/lib/lighting-spot.vsh

@@ -0,0 +1,68 @@
+#if defined(BUMPED)
+
+uniform vec3 u_spotLightPosition;                   // Position
+uniform float u_spotLightRangeInverse;              // Inverse of light range.
+uniform vec3 u_spotLightDirection;                  // Direction
+
+varying vec3 v_spotLightDirection;                  // Direction of spot light in tangent space.
+varying vec3 v_vertexToSpotLightDirection;          // Direction of the spot light w.r.t current vertex in tangent space.
+varying float v_spotLightAttenuation;               // Attenuation of spot light.
+
+void applyLight(mat3 tangentSpaceTransformMatrix)
+{
+    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;
+    
+    // Transform current light direction to tangent space.
+    lightDirection = tangentSpaceTransformMatrix * lightDirection;
+    
+    // Attenuation
+    v_spotLightAttenuation = 1.0 - dot(lightDirection * u_spotLightRangeInverse, lightDirection * u_spotLightRangeInverse);
+
+    // Output light direction.
+    v_vertexToSpotLightDirection = lightDirection;
+    
+    #if defined(SPECULAR)
+    
+    // Compute camera direction and transform it to tangent space.
+    v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
+    
+    #endif
+}
+
+#else
+
+uniform vec3 u_spotLightPosition;                   // Position
+uniform float u_spotLightRangeInverse;              // Inverse of light range.
+
+varying vec3 v_vertexToSpotLightDirection;          // Light direction w.r.t current vertex.
+varying float v_spotLightAttenuation;               // Attenuation of spot light.
+
+void applyLight(vec4 position)
+{
+    // World 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)
+    
+    // Compute camera direction and transform it to tangent space.
+    v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
+    
+    #endif
+}
+
+#endif

+ 54 - 0
gameplay/res/shaders/lib/lighting.fsh

@@ -0,0 +1,54 @@
+uniform vec3 u_lightDirection;
+
+// Variables
+vec4 _baseColor;                                // Base color
+vec3 _ambientColor;                             // Ambient Color
+vec3 _diffuseColor;                             // Diffuse Color
+
+#if defined(SPECULAR)
+
+vec3 _specularColor;    						// Specular color
+
+uniform float u_specularExponent;				// Specular exponent
+
+varying vec3 v_cameraDirection;                 // Camera direction
+
+
+vec3 computeLighting(vec3 normalVector, vec3 lightDirection, float attenuation, vec3 cameraDirection)
+{
+    // Ambient
+    _ambientColor = _baseColor.rgb * u_ambientColor;
+
+    // Diffuse
+    float ddot = dot(normalVector, lightDirection);
+    float diffuseIntensity = attenuation * ddot;
+    diffuseIntensity = max(0.0, diffuseIntensity);
+    _diffuseColor = u_lightColor * _baseColor.rgb * diffuseIntensity;
+
+    // Specular
+    vec3 halfVector = normalize(lightDirection + cameraDirection);
+    float specularIntensity = attenuation * max(0.0, pow(dot(normalVector, halfVector), u_specularExponent));
+    specularIntensity = max(0.0, specularIntensity);
+    _specularColor = u_lightColor * _baseColor.rgb * specularIntensity;
+	
+	return _ambientColor + _diffuseColor + _specularColor;
+}
+
+#else
+
+vec3 computeLighting(vec3 normalVector, vec3 lightDirection, float attenuation)
+{
+    // Ambient
+    _ambientColor = _baseColor.rgb * u_ambientColor;
+
+    // Diffuse
+	float ddot = dot(normalVector, lightDirection);
+    float diffuseIntensity = attenuation * ddot;
+    diffuseIntensity = max(0.0, diffuseIntensity);
+    _diffuseColor = u_lightColor * _baseColor.rgb * diffuseIntensity;
+	
+	return _ambientColor + _diffuseColor;
+}
+
+#endif
+

+ 11 - 0
gameplay/res/shaders/lib/lighting.vsh

@@ -0,0 +1,11 @@
+#define LIGHTING
+
+uniform vec3 u_lightDirection;					// Direction of the light.
+
+#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.
+varying vec3 v_cameraDirection;                 // Direction the camera is looking at in tangent space.
+
+#endif

+ 0 - 175
gameplay/res/shaders/parallax-specular.fsh

@@ -1,175 +0,0 @@
-#ifdef OPENGL_ES
-precision highp float;
-#endif
-
-// Uniforms
-uniform vec3 u_lightColor;                      // Light color.
-uniform vec3 u_ambientColor;                    // Ambient color.
-uniform float u_specularExponent;               // Specular exponent or shininess property.
-uniform sampler2D u_diffuseTexture;             // Diffuse texture.
-uniform sampler2D u_bumpMapTexture;             // Height map texture
-uniform sampler2D u_normalMapTexture;           // Normal map texture.
-uniform float u_parallaxHeight;                 // Parallax height
-#if defined(GLOBAL_ALPHA)
-uniform float u_globalAlpha;                    // Global alpha value
-#endif
-
-// Inputs
-varying vec2 v_texCoord;                        // Texture Coordinate.
-varying vec3 v_cameraDirection;                 // Direction the camera is looking at in tangent space.
-
-// Global variables
-vec4 _baseColor;                                // Base color
-vec3 _ambientColor;                             // Ambient Color
-vec3 _diffuseColor;                             // Diffuse Color
-vec3 _specularColor;                            // Specular color
-
-void lighting(vec3 normalVector, vec3 cameraDirection, vec3 lightDirection, float attenuation)
-{
-    // Ambient
-    _ambientColor = _baseColor.rgb * u_ambientColor;
-
-    // Diffuse
-    float diffuseIntensity = attenuation * max(0.0, dot(normalVector, lightDirection));
-    diffuseIntensity = max(0.0, diffuseIntensity);
-    _diffuseColor = u_lightColor * _baseColor.rgb * diffuseIntensity;
-
-    // Specular
-    vec3 halfVector = normalize(cameraDirection + lightDirection);
-    float specularIntensity = attenuation * max(0.0, pow(dot(normalVector, halfVector), u_specularExponent));
-    specularIntensity = max(0.0, specularIntensity);
-    _specularColor = u_lightColor * _baseColor.rgb * specularIntensity;
-}
-
-#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.
-
-void applyLight()
-{
-    // Normalize the vectors.
-    vec3 cameraDirection = normalize(v_cameraDirection);
-    vec3 vertexToPointLightDirection = normalize(v_vertexToPointLightDirection);
-    
-    float pointLightAttenuation = clamp(v_pointLightAttenuation, 0.0, 1.0);
-
-    // Get the height in the bumpmap texture.
-    float height = texture2D(u_bumpMapTexture, v_texCoord).r;
-
-    // Compute offset within the range (-parallax height to +parallax height).
-    float offset = u_parallaxHeight * (2.0 * height - 1.0);
-    vec2 parallaxTexCoord = v_texCoord + (cameraDirection.xy * offset);
-
-    // Compute offset again with the new texture coordinates to get better precision.
-    // Find out the mean height.
-    height += texture2D(u_bumpMapTexture, parallaxTexCoord).r;
-    offset = u_parallaxHeight * (height - 1.0);
-    parallaxTexCoord = v_texCoord + ( cameraDirection.xy * offset);
-
-    // Fetch normals from the normal map with the modified texture coordinates.
-    vec3 normalVector = normalize(texture2D(u_normalMapTexture, parallaxTexCoord).xyz * 2.0 - 1.0);
-
-    // Fetch diffuse color from texture.
-    _baseColor = texture2D(u_diffuseTexture, parallaxTexCoord);
-    
-    lighting(normalVector, cameraDirection, vertexToPointLightDirection, pointLightAttenuation);
-}
-
-#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]
-varying vec3 v_spotLightDirection;              // Direction of spot light in tangent space.
-varying vec3 v_vertexToSpotLightDirection;      // Direction of the spot light w.r.t current vertex in tangent space.
-varying float v_spotLightAttenuation;           // Attenuation of spot light.
-
-float lerpstep( float lower, float upper, float s)
-{
-    return clamp( ( s - lower ) / ( upper - lower ), 0.0, 1.0 );
-}
-
-void applyLight()
-{
-    vec3 cameraDirection = normalize(v_cameraDirection);
-    vec3 spotLightDirection = normalize(v_spotLightDirection);
-    vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
-
-    // "-lightDirection" because light direction points in opposite direction to
-    // to spot direction.
-    // Calculate spot light effect.
-    float spotCurrentAngleCos = max(0.0, 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);
-    spotLightAttenuation *= lerpstep(u_spotLightOuterAngleCos, u_spotLightInnerAngleCos, spotCurrentAngleCos);
-
-    // Get the height in the bumpmap texture.
-    float height = texture2D(u_bumpMapTexture, v_texCoord).r;
-
-    // Compute offset within the range (-parallax height to +parallax height).
-    float offset = u_parallaxHeight * (2.0 * height - 1.0);
-    vec2 parallaxTexCoord = v_texCoord + (cameraDirection.xy * offset);
-
-    // Compute offset again with the new texture coordinates to get better precision.
-    // Find out the mean height.
-    height += texture2D(u_bumpMapTexture, parallaxTexCoord).r;
-    offset = u_parallaxHeight * (height - 1.0);
-    parallaxTexCoord = v_texCoord + ( cameraDirection.xy * offset);
-
-    // Fetch normals from the normal map with the modified texture coordinates.
-    vec3 normalVector = normalize(texture2D(u_normalMapTexture, parallaxTexCoord).xyz * 2.0 - 1.0);
-
-    // Fetch diffuse color from texture.
-    _baseColor = texture2D(u_diffuseTexture, parallaxTexCoord);
-
-    lighting(normalVector, cameraDirection, vertexToSpotLightDirection, spotLightAttenuation);
-}
-
-#else
-
-varying vec3 v_lightDirection;                  // Direction of light in tangent space.
-
-void applyLight()
-{
-    // Normalize vectors.
-    vec3 cameraDirection = normalize(v_cameraDirection);
-    vec3 lightDirection = normalize(v_lightDirection);
-
-    // Get the height in the bumpmap texture.
-    float height = texture2D(u_bumpMapTexture, v_texCoord).r;
-
-    // Compute offset within the range (-parallax height to +parallax height).
-    float offset = u_parallaxHeight * (2.0 * height - 1.0);
-    vec2 parallaxTexCoord = v_texCoord + (cameraDirection.xy * offset);
-
-    // Compute offset again with the new texture coordinates to get better precision.
-    // Find out the mean height.
-    height += texture2D(u_bumpMapTexture, parallaxTexCoord).r;
-    offset = u_parallaxHeight * (height - 1.0);
-    parallaxTexCoord = v_texCoord + ( cameraDirection.xy * offset);
-
-    // Fetch normals from the normal map with the modified texture coordinates.
-    vec3 normalVector = normalize(texture2D(u_normalMapTexture, parallaxTexCoord).xyz * 2.0 - 1.0);
-
-    // Fetch diffuse color from texture.
-    _baseColor = texture2D(u_diffuseTexture, parallaxTexCoord);
-
-    lighting(normalVector, cameraDirection, -lightDirection, 1.0);
-}
-#endif
-
-void main()
-{
-    // Apply light
-    applyLight();
-
-    // Light the pixel
-    gl_FragColor.a = _baseColor.a;
-    gl_FragColor.rgb = _ambientColor + _diffuseColor + _specularColor;
-
-#if defined(GLOBAL_ALPHA)
-    gl_FragColor.a *= u_globalAlpha;
-#endif
-}

+ 0 - 118
gameplay/res/shaders/parallax-specular.vsh

@@ -1,118 +0,0 @@
-// Uniforms
-uniform mat4 u_worldViewMatrix;                     // Matrix to transform a position to view space.
-uniform mat4 u_worldViewProjectionMatrix;           // Matrix to transform a position to clip space.
-uniform mat4 u_inverseTransposeWorldViewMatrix;     // Matrix to transform a normal to view space.
-uniform vec3 u_cameraPosition;                      // Position of the camera.
-
-
-// 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 (actually Bi-tangent) (x, y, z)
-
-// Outputs
-varying vec2 v_texCoord;                            // Texture Coordinate (u,v)
-varying vec3 v_cameraDirection;                     // Direction the camera is looking at in tangent space.
-
-#if defined(POINT_LIGHT)
-
-uniform vec3 u_pointLightPosition;                  // Position
-uniform float u_pointLightRangeInverse;             // Inverse of light range 
-varying vec3 v_vertexToPointLightDirection;         // Direction of point light w.r.t current vertex in tangent space.
-varying float v_pointLightAttenuation;              // Attenuation of point light.
-
-void applyLight(mat3 tangentSpaceTransformMatrix)
-{
-    vec4 positionWorldViewSpace = u_worldViewMatrix * a_position;
-    
-    // Compute camera direction and transform it to tangent space.
-    v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
-    
-    // Compute the light direction with light position and the vertex position.
-    vec3 lightDirection = u_pointLightPosition - positionWorldViewSpace.xyz;
-    
-    // Transform current light direction to tangent space.
-    vec3 vertexToPointLightDirection = tangentSpaceTransformMatrix * lightDirection;
-
-    // Attenuation
-    v_pointLightAttenuation = 1.0 - dot(lightDirection * u_pointLightRangeInverse, lightDirection * u_pointLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToPointLightDirection =  vertexToPointLightDirection;
-}
-
-#elif defined(SPOT_LIGHT)
-
-uniform vec3 u_spotLightPosition;                   // Position
-uniform float u_spotLightRangeInverse;              // Inverse of light range.
-uniform vec3 u_spotLightDirection;                  // Direction
-varying vec3 v_spotLightDirection;                  // Direction of spot light in tangent space.
-varying vec3 v_vertexToSpotLightDirection;          // Direction of the spot light w.r.t current vertex in tangent space.
-varying float v_spotLightAttenuation;               // Attenuation of spot light.
-
-void applyLight(mat3 tangentSpaceTransformMatrix)
-{
-    vec4 positionWorldViewSpace = u_worldViewMatrix * a_position;
-    
-    // Compute camera direction and transform it to tangent space.
-    v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
-
-    // 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;
-    
-    // Transform current light direction to tangent space.
-    lightDirection = tangentSpaceTransformMatrix * lightDirection;
-    
-    // Attenuation
-    v_spotLightAttenuation = 1.0 - dot(lightDirection * u_spotLightRangeInverse, lightDirection * u_spotLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToSpotLightDirection = lightDirection;
-}
-
-#else
-
-uniform vec3 u_lightDirection;                      // Direction
-varying vec3 v_lightDirection;                      // Direction of light in tangent space.
-
-void applyLight(mat3 tangentSpaceTransformMatrix)
-{
-    vec4 positionWorldViewSpace = u_worldViewMatrix * a_position;
-    
-    // Compute camera direction and transform it to tangent space.
-    v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
-    
-    // Transform light direction to tangent space.
-    v_lightDirection = tangentSpaceTransformMatrix * u_lightDirection;
-}
-
-#endif
-
-void main()
-{
-    // Transform position to clip space.
-    gl_Position = u_worldViewProjectionMatrix * a_position;
-
-    // Transform the normal, tangent and binormals to  view space.
-    mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz,
-                                                u_inverseTransposeWorldViewMatrix[1].xyz,
-                                                u_inverseTransposeWorldViewMatrix[2].xyz);
-    vec3 tangentVector  = normalize(inverseTransposeWorldViewMatrix * a_tangent);
-    vec3 normalVector = normalize(inverseTransposeWorldViewMatrix * a_normal);
-    vec3 binormalVector = normalize(inverseTransposeWorldViewMatrix * a_binormal);
-
-    // Create a transform to convert a vector to tangent space.
-    mat3 tangentSpaceTransformMatrix = mat3(tangentVector.x, binormalVector.x, normalVector.x,
-                                            tangentVector.y, binormalVector.y, normalVector.y,
-                                            tangentVector.z, binormalVector.z, normalVector.z);
-    // Apply light.
-    applyLight(tangentSpaceTransformMatrix);
-
-    // Pass on the texture coordinates to Fragment shader.
-    v_texCoord = a_texCoord;
-}

+ 0 - 167
gameplay/res/shaders/parallax.fsh

@@ -1,167 +0,0 @@
-#ifdef OPENGL_ES
-precision highp float;
-#endif
-
-// Uniforms
-uniform vec3 u_lightColor;                      // Light color.
-uniform vec3 u_ambientColor;                    // Ambient color.
-uniform sampler2D u_diffuseTexture;             // Diffuse texture.
-uniform sampler2D u_bumpMapTexture;             // Height map texture
-uniform sampler2D u_normalMapTexture;           // Normal map texture.
-uniform float u_parallaxHeight;                 // Parallax height
-#if defined(GLOBAL_ALPHA)
-uniform float u_globalAlpha;                    // Global alpha value
-#endif
-
-// Inputs
-varying vec2 v_texCoord;                        // Texture Coordinate.
-varying vec3 v_cameraDirection;                 // Direction the camera is looking at in tangent space.
-
-// Global variables
-vec4 _baseColor;                                // Base color
-vec3 _ambientColor;                             // Ambient Color
-vec3 _diffuseColor;                             // Diffuse Color
-
-void lighting(vec3 normalVector, vec3 lightDirection, float attenuation)
-{
-    // Ambient
-    _ambientColor = _baseColor.rgb * u_ambientColor;
-
-    // Diffuse
-    float diffuseIntensity = attenuation * max(0.0, dot(normalVector, lightDirection));
-    diffuseIntensity = max(0.0, diffuseIntensity);
-    _diffuseColor = u_lightColor * _baseColor.rgb * diffuseIntensity;
-}
-
-#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.
-
-void applyLight()
-{
-    // Normalize the vectors.
-    vec3 cameraDirection = normalize(v_cameraDirection);
-    vec3 vertexToPointLightDirection = normalize(v_vertexToPointLightDirection);
-    
-    float pointLightAttenuation = clamp(v_pointLightAttenuation, 0.0, 1.0);
-
-    // Get the height in the bumpmap texture.
-    float height = texture2D(u_bumpMapTexture, v_texCoord).r;
-
-    // Compute offset within the range (-parallax height to +parallax height).
-    float offset = u_parallaxHeight * (2.0 * height - 1.0);
-    vec2 parallaxTexCoord = v_texCoord + (cameraDirection.xy * offset);
-
-    // Compute offset again with the new texture coordinates to get better precision.
-    // Find out the mean height.
-    height += texture2D(u_bumpMapTexture, parallaxTexCoord).r;
-    offset = u_parallaxHeight * (height - 1.0);
-    parallaxTexCoord = v_texCoord + ( cameraDirection.xy * offset);
-
-    // Fetch normals from the normal map with the modified texture coordinates.
-    vec3 normalVector = normalize(texture2D(u_normalMapTexture, parallaxTexCoord).xyz * 2.0 - 1.0);
-
-    // Fetch diffuse color from texture.
-    _baseColor = texture2D(u_diffuseTexture, parallaxTexCoord);
-    
-    lighting(normalVector, vertexToPointLightDirection, pointLightAttenuation);
-}
-
-#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]
-varying vec3 v_spotLightDirection;              // Direction of spot light in tangent space.
-varying vec3 v_vertexToSpotLightDirection;      // Direction of the spot light w.r.t current vertex in tangent space.
-varying float v_spotLightAttenuation;           // Attenuation of spot light.
-
-float lerpstep( float lower, float upper, float s)
-{
-    return clamp( ( s - lower ) / ( upper - lower ), 0.0, 1.0 );
-}
-
-void applyLight()
-{
-    vec3 cameraDirection = normalize(v_cameraDirection);
-    vec3 spotLightDirection = normalize(v_spotLightDirection);
-    vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
-
-    // "-lightDirection" because light direction points in opposite direction to
-    // to spot direction.
-    // Calculate spot light effect.
-    float spotCurrentAngleCos = max(0.0, 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);
-    spotLightAttenuation *= lerpstep(u_spotLightOuterAngleCos, u_spotLightInnerAngleCos, spotCurrentAngleCos);
-
-    // Get the height in the bumpmap texture.
-    float height = texture2D(u_bumpMapTexture, v_texCoord).r;
-
-    // Compute offset within the range (-parallax height to +parallax height).
-    float offset = u_parallaxHeight * (2.0 * height - 1.0);
-    vec2 parallaxTexCoord = v_texCoord + (cameraDirection.xy * offset);
-
-    // Compute offset again with the new texture coordinates to get better precision.
-    // Find out the mean height.
-    height += texture2D(u_bumpMapTexture, parallaxTexCoord).r;
-    offset = u_parallaxHeight * (height - 1.0);
-    parallaxTexCoord = v_texCoord + ( cameraDirection.xy * offset);
-
-    // Fetch normals from the normal map with the modified texture coordinates.
-    vec3 normalVector = normalize(texture2D(u_normalMapTexture, parallaxTexCoord).xyz * 2.0 - 1.0);
-
-    // Fetch diffuse color from texture.
-    _baseColor = texture2D(u_diffuseTexture, parallaxTexCoord);
-
-    lighting(normalVector, vertexToSpotLightDirection, spotLightAttenuation);
-}
-
-#else
-
-varying vec3 v_lightDirection;                      // Direction of light in tangent space.
-
-void applyLight()
-{
-    // Normalize vectors.
-    vec3 cameraDirection = normalize(v_cameraDirection);
-    vec3 lightDirection = normalize(v_lightDirection);
-
-    // Get the height in the bumpmap texture.
-    float height = texture2D(u_bumpMapTexture, v_texCoord).r;
-
-    // Compute offset within the range (-parallax height to +parallax height).
-    float offset = u_parallaxHeight * (2.0 * height - 1.0);
-    vec2 parallaxTexCoord = v_texCoord + (cameraDirection.xy * offset);
-
-    // Compute offset again with the new texture coordinates to get better precision.
-    // Find out the mean height.
-    height += texture2D(u_bumpMapTexture, parallaxTexCoord).r;
-    offset = u_parallaxHeight * (height - 1.0);
-    parallaxTexCoord = v_texCoord + ( cameraDirection.xy * offset);
-
-    // Fetch normals from the normal map with the modified texture coordinates.
-    vec3 normalVector = normalize(texture2D(u_normalMapTexture, parallaxTexCoord).xyz * 2.0 - 1.0);
-
-    // Fetch diffuse color from texture.
-    _baseColor = texture2D(u_diffuseTexture, parallaxTexCoord);
-
-    lighting(normalVector, -lightDirection, 1.0);
-}
-#endif
-
-void main()
-{
-    // Apply light
-    applyLight();
-
-    // Light the pixel
-    gl_FragColor.a = _baseColor.a;
-    gl_FragColor.rgb = _ambientColor + _diffuseColor;
-
-#if defined(GLOBAL_ALPHA)
-    gl_FragColor.a *= u_globalAlpha;
-#endif
-}

+ 0 - 118
gameplay/res/shaders/parallax.vsh

@@ -1,118 +0,0 @@
-// Uniforms
-uniform mat4 u_worldViewMatrix;                     // Matrix to transform a position to view space.
-uniform mat4 u_worldViewProjectionMatrix;           // Matrix to transform a position to clip space.
-uniform mat4 u_inverseTransposeWorldViewMatrix;     // Matrix to transform a normal to view space.
-uniform vec3 u_cameraPosition;                      // Position of the camera.
-
-
-// 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 (actually Bi-tangent) (x, y, z)
-
-// Outputs
-varying vec2 v_texCoord;                            // Texture Coordinate (u,v)
-varying vec3 v_cameraDirection;                     // Direction the camera is looking at in tangent space.
-
-#if defined(POINT_LIGHT)
-
-uniform vec3 u_pointLightPosition;                  // Position
-uniform float u_pointLightRangeInverse;             // Inverse of light range 
-varying vec3 v_vertexToPointLightDirection;         // Direction of point light w.r.t current vertex in tangent space.
-varying float v_pointLightAttenuation;              // Attenuation of point light.
-
-void applyLight(mat3 tangentSpaceTransformMatrix)
-{
-    vec4 positionWorldViewSpace = u_worldViewMatrix * a_position;
-    
-    // Compute camera direction and transform it to tangent space.
-    v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
-    
-    // Compute the light direction with light position and the vertex position.
-    vec3 lightDirection = u_pointLightPosition - positionWorldViewSpace.xyz;
-    
-    // Transform current light direction to tangent space.
-    vec3 vertexToPointLightDirection = tangentSpaceTransformMatrix * lightDirection;
-
-    // Attenuation
-    v_pointLightAttenuation = 1.0 - dot(lightDirection * u_pointLightRangeInverse, lightDirection * u_pointLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToPointLightDirection =  vertexToPointLightDirection;
-}
-
-#elif defined(SPOT_LIGHT)
-
-uniform vec3 u_spotLightPosition;                   // Position
-uniform float u_spotLightRangeInverse;              // Inverse of light range.
-uniform vec3 u_spotLightDirection;                  // Direction
-varying vec3 v_spotLightDirection;                  // Direction of spot light in tangent space.
-varying vec3 v_vertexToSpotLightDirection;          // Direction of the spot light w.r.t current vertex in tangent space.
-varying float v_spotLightAttenuation;               // Attenuation of spot light.
-
-void applyLight(mat3 tangentSpaceTransformMatrix)
-{
-    vec4 positionWorldViewSpace = u_worldViewMatrix * a_position;
-    
-    // Compute camera direction and transform it to tangent space.
-    v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
-
-    // 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;
-    
-    // Transform current light direction to tangent space.
-    lightDirection = tangentSpaceTransformMatrix * lightDirection;
-    
-    // Attenuation
-    v_spotLightAttenuation = 1.0 - dot(lightDirection * u_spotLightRangeInverse, lightDirection * u_spotLightRangeInverse);
-
-    // Output light direction.
-    v_vertexToSpotLightDirection = lightDirection;
-}
-
-#else
-
-uniform vec3 u_lightDirection;                      // Direction
-varying vec3 v_lightDirection;                      // Direction of light in tangent space.
-
-void applyLight(mat3 tangentSpaceTransformMatrix)
-{
-    vec4 positionWorldViewSpace = u_worldViewMatrix * a_position;
-    
-    // Compute camera direction and transform it to tangent space.
-    v_cameraDirection = tangentSpaceTransformMatrix * (u_cameraPosition - positionWorldViewSpace.xyz);
-    
-    // Transform light direction to tangent space.
-    v_lightDirection = tangentSpaceTransformMatrix * u_lightDirection;
-}
-
-#endif
-
-void main()
-{
-    // Transform position to clip space.
-    gl_Position = u_worldViewProjectionMatrix * a_position;
-
-    // Transform the normal, tangent and binormals to  view space.
-    mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz,
-                                                u_inverseTransposeWorldViewMatrix[1].xyz,
-                                                u_inverseTransposeWorldViewMatrix[2].xyz);
-    vec3 tangentVector  = normalize(inverseTransposeWorldViewMatrix * a_tangent);
-    vec3 normalVector = inverseTransposeWorldViewMatrix * a_normal;
-    vec3 binormalVector = inverseTransposeWorldViewMatrix * a_binormal;
-
-    // Create a transform to convert a vector to tangent space.
-    mat3 tangentSpaceTransformMatrix = mat3(tangentVector.x, binormalVector.x, normalVector.x,
-                                            tangentVector.y, binormalVector.y, normalVector.y,
-                                            tangentVector.z, binormalVector.z, normalVector.z);
-    // Apply light.
-    applyLight(tangentSpaceTransformMatrix);
-
-    // Pass on the texture coordinates to Fragment shader.
-    v_texCoord = a_texCoord;
-}

+ 0 - 18
gameplay/res/shaders/solid.fsh

@@ -1,18 +0,0 @@
-#ifdef OPENGL_ES
-precision highp float;
-#endif
-
-// Uniforms
-uniform vec4 u_diffuseColor;        // Diffuse color
-#if defined(GLOBAL_ALPHA)
-uniform float u_globalAlpha;        // Global alpha value
-#endif
-
-void main()
-{
-    gl_FragColor = u_diffuseColor;
-
-#if defined(GLOBAL_ALPHA)
-    gl_FragColor.a *= u_globalAlpha;
-#endif
-}

+ 0 - 10
gameplay/res/shaders/solid.vsh

@@ -1,10 +0,0 @@
-// Uniforms
-uniform mat4 u_worldViewProjectionMatrix;        // Matrix to transform a position to clip space.
-
-// Inputs
-attribute vec4 a_position;                        // Vertex Position (x, y, z, w)
-
-void main()
-{
-    gl_Position = u_worldViewProjectionMatrix *  a_position;
-}

+ 30 - 0
gameplay/res/shaders/textured-unlit.fsh

@@ -0,0 +1,30 @@
+#ifdef OPENGL_ES
+precision highp float;
+#endif
+
+// Uniforms
+uniform sampler2D u_textureDiffuse;     	// Diffuse texture
+
+#if defined(MODULATE_COLOR)
+uniform vec4 u_modulateColor;               // Modulation color
+#endif
+#if defined(MODULATE_ALPHA)
+uniform float u_modulateAlpha;              // Modulation alpha
+#endif
+
+// Inputs
+varying vec2 v_texCoord;                	// Texture coordinate(u, v)
+
+void main()
+{
+    // Sample the texture for the color
+    gl_FragColor = texture2D(u_textureDiffuse, v_texCoord);
+
+	// Global color modulation
+	#if defined(MODULATE_COLOR)
+	gl_FragColor *= u_modulateColor;
+	#endif
+	#if defined(MODULATE_ALPHA)
+    gl_FragColor.a *= u_modulateAlpha;
+    #endif
+}

+ 40 - 0
gameplay/res/shaders/textured-unlit.vsh

@@ -0,0 +1,40 @@
+// Uniforms
+uniform mat4 u_worldViewProjectionMatrix;       // Matrix to transform a position to clip space
+#if defined(TEXTURE_REPEAT)
+uniform vec2 u_textureRepeat;
+#endif
+#if defined(TEXTURE_OFFSET)
+uniform vec2 u_textureOffset;
+#endif
+
+// Inputs
+attribute vec4 a_position;                      // Vertex Position              (x, y, z, w)
+attribute vec2 a_texCoord;                      // Vertex Texture Coordinate    (u, v)
+
+// Outputs
+varying vec2 v_texCoord;                        // Output Texture Coordinate    (u, v)
+
+// Attribute Accessors (getPosition(), getNormal(), etc.)
+#if defined(SKINNING)
+#include "lib/attributes-skinning.vsh"
+#else
+#include "lib/attributes.vsh" 
+#endif
+
+void main()
+{
+    // Get the vertex position
+    vec4 position = getPosition();
+
+    // Transform position to clip space.
+    gl_Position = u_worldViewProjectionMatrix * position;
+
+    // Texture transformation.
+    v_texCoord = a_texCoord;
+    #if defined(TEXTURE_REPEAT)
+    v_texCoord *= u_textureRepeat;
+    #endif
+    #if defined(TEXTURE_OFFSET)
+    v_texCoord += u_textureOffset;
+    #endif
+}

+ 34 - 9
gameplay/res/shaders/textured.fsh

@@ -3,20 +3,45 @@ precision highp float;
 #endif
 
 // Uniforms
-uniform sampler2D u_diffuseTexture;     // Diffuse texture
-uniform vec4 u_diffuseColor;            // Diffuse color/tint
-#if defined(GLOBAL_ALPHA)
-uniform float u_globalAlpha;            // Global alpha value
+uniform sampler2D u_textureDiffuse;             // Diffuse texture.
+uniform vec3 u_lightColor;                      // Light color
+uniform vec3 u_ambientColor;                    // Ambient color
+
+#if defined(MODULATE_COLOR)
+uniform vec4 u_modulateColor;               	// Modulation color
+#endif
+#if defined(MODULATE_ALPHA)
+uniform float u_modulateAlpha;              	// Modulation alpha
 #endif
 
 // Inputs
-varying vec2 v_texCoord;                // Texture coordinate (u, v).
+varying vec3 v_normalVector;                    // NormalVector in view space.
+varying vec2 v_texCoord;                        // Texture coordinate (u, v).
+
+// Lighting
+#include "lib/lighting.fsh"                     // Functions for lighting
+#if defined(POINT_LIGHT)
+#include "lib/lighting-point.fsh"
+#elif defined(SPOT_LIGHT)
+#include "lib/lighting-spot.fsh"
+#else
+#include "lib/lighting-directional.fsh"
+#endif
 
 void main()
 {
-    gl_FragColor = texture2D(u_diffuseTexture, v_texCoord) * u_diffuseColor;
+    // Sample the texture for base color
+    _baseColor = texture2D(u_textureDiffuse, v_texCoord);
 
-#if defined(GLOBAL_ALPHA)
-    gl_FragColor.a *= u_globalAlpha;
-#endif
+    // Light the pixel
+    gl_FragColor.a = _baseColor.a;
+    gl_FragColor.rgb = getLitPixel();
+	
+	// Global color modulation
+	#if defined(MODULATE_COLOR)
+	gl_FragColor *= u_modulateColor;
+	#endif
+	#if defined(MODULATE_ALPHA)
+    gl_FragColor.a *= u_modulateAlpha;
+    #endif
 }

+ 42 - 59
gameplay/res/shaders/textured.vsh

@@ -1,78 +1,61 @@
 // Uniforms
-uniform mat4 u_worldViewProjectionMatrix;       // Matrix to transform a position to clip space.
+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(TEXTURE_REPEAT)
+uniform vec2 u_textureRepeat;
+#endif
+#if defined(TEXTURE_OFFSET)
+uniform vec2 u_textureOffset;
+#endif
 
 // Inputs
-attribute vec4 a_position;                      // Vertex Position (x, y, z, w).
-attribute vec2 a_texCoord;                      // Vertex Texture Coordinate (u, v).
+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)
 
 // Outputs
-varying vec2 v_texCoord;                        // Texture coordinate (u, v).
+varying vec3 v_normalVector;                        // Output normal vector in view space   (x, y, z)
+varying vec2 v_texCoord;                            // Output texture coordinate            (u, v)
+
+// Lighting
+#include "lib/lighting.vsh"
+#if defined(POINT_LIGHT)
+#include "lib/lighting-point.vsh"
+#elif defined(SPOT_LIGHT)
+#include "lib/lighting-spot.vsh"
+#else
+#include "lib/lighting-directional.vsh"
+#endif
 
+// Attribute Accessors (getPosition(), getNormal(), etc.)
 #if defined(SKINNING)
-
-attribute vec4 a_blendWeights;
-attribute vec4 a_blendIndices;
-
-// 32 4x3 matrices as an array of floats
-uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
-
-// Common vectors.
-vec4 _skinnedPosition;
-
-void skinPosition(float blendWeight, int matrixIndex)
-{
-    vec4 tmp;
-
-    tmp.x = dot(a_position, u_matrixPalette[matrixIndex]);
-    tmp.y = dot(a_position, u_matrixPalette[matrixIndex + 1]);
-    tmp.z = dot(a_position, u_matrixPalette[matrixIndex + 2]);
-    tmp.w = a_position.w;
-
-    _skinnedPosition += blendWeight * tmp;
-}
-
-vec4 getPosition()
-{
-    _skinnedPosition = vec4(0.0);
-
-    // Transform position to view space using 
-    // matrix palette with four matrices used to transform a vertex.
-
-    float blendWeight = a_blendWeights[0];
-    int matrixIndex = int (a_blendIndices[0]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[1];
-    matrixIndex = int(a_blendIndices[1]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[2];
-    matrixIndex = int(a_blendIndices[2]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    blendWeight = a_blendWeights[3];
-    matrixIndex = int(a_blendIndices[3]) * 3;
-    skinPosition(blendWeight, matrixIndex);
-
-    return _skinnedPosition;    
-}
-
+#include "lib/attributes-skinning.vsh"
 #else
-
-vec4 getPosition()
-{
-    return a_position;    
-}
-
+#include "lib/attributes.vsh" 
 #endif
 
 void main()
 {
+    // Get the position and normal
     vec4 position = getPosition();
+    vec3 normal = getNormal();
 
     // Transform position to clip space.
     gl_Position = u_worldViewProjectionMatrix * position;
 
-    // Pass on texture coordinate to fragment shader.
+    // Transform normal to view space.
+    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)
+    v_texCoord *= u_textureRepeat;
+    #endif
+    #if defined(TEXTURE_OFFSET)
+    v_texCoord += u_textureOffset;
+    #endif
 }

+ 4 - 4
gameplay/src/Control.cpp

@@ -6,8 +6,7 @@ namespace gameplay
 {
 
 Control::Control()
-    : _id(""), _state(Control::NORMAL), _bounds(Rectangle::empty()), _clipBounds(Rectangle::empty()), _viewportClipBounds(Rectangle::empty()),
-    _dirty(true), _consumeInputEvents(true), _listeners(NULL), _styleOverridden(false), _skin(NULL), _clearBounds(Rectangle::empty())
+    : _id(""), _state(Control::NORMAL), _dirty(true), _consumeInputEvents(true), _listeners(NULL), _style(NULL), _styleOverridden(false), _skin(NULL)
 {
 }
 
@@ -109,12 +108,12 @@ void Control::initialize(Theme::Style* style, Properties* properties)
         else if (spaceName == "MARGIN")
         {
             setMargin(innerSpace->getFloat("top"), innerSpace->getFloat("bottom"),
-                innerSpace->getFloat("left"), innerSpace->getFloat("right"));
+                      innerSpace->getFloat("left"), innerSpace->getFloat("right"));
         }
         else if (spaceName == "PADDING")
         {
             setPadding(innerSpace->getFloat("top"), innerSpace->getFloat("bottom"),
-                innerSpace->getFloat("left"), innerSpace->getFloat("right"));
+                       innerSpace->getFloat("left"), innerSpace->getFloat("right"));
         }
 
         innerSpace = properties->getNextNamespace();
@@ -1086,6 +1085,7 @@ void Control::setAnimationPropertyValue(int propertyId, AnimationValue* value, f
         break;
     case ANIMATE_OPACITY:
         _dirty = true;
+        break;
     default:
         break;
     }

+ 4 - 4
gameplay/src/Control.h

@@ -930,14 +930,14 @@ protected:
     bool _autoHeight;
     
     /**
-     * The Control's Theme::Style.
+     * Listeners map of EventType's to a list of Listeners.
      */
-    Theme::Style* _style;
+    std::map<Listener::EventType, std::list<Listener*>*>* _listeners;
     
     /**
-     * Listeners map of EventType's to a list of Listeners.
+     * The Control's Theme::Style.
      */
-    std::map<Listener::EventType, std::list<Listener*>*>* _listeners;
+    Theme::Style* _style;
 
     /**
      * The current opacity of the control.

+ 139 - 10
gameplay/src/Effect.cpp

@@ -2,7 +2,7 @@
 #include "Effect.h"
 #include "FileSystem.h"
 
-#define OPENGL_ES_DEFINE  "#define OPENGL_ES"
+#define OPENGL_ES_DEFINE  "#define OPENGL_ES\n"
 
 namespace gameplay
 {
@@ -102,6 +102,111 @@ Effect* Effect::createFromSource(const char* vshSource, const char* fshSource, c
     return createFromSource(NULL, vshSource, NULL, fshSource, defines);
 }
 
+
+void replaceDefines(const char* defines, std::string& out)
+{
+    if (defines && strlen(defines) != 0)
+    {
+        out = defines;
+        unsigned int pos;
+        out.insert(0, "#define ");
+        while ((pos = out.find(';')) != std::string::npos)
+        {
+            out.replace(pos, 1, "\n#define ");
+        }
+        out += "\n";
+    }
+#ifdef OPENGL_ES
+    out.insert(0, OPENGL_ES_DEFINE);
+#endif
+}
+
+void replaceIncludes(const char* filepath, const char* source, std::string& out)
+{
+    // Replace the #include "xxxx.xxx" with the sourced file contents of "filepath/xxxx.xxx"
+    std::string str = source;
+    size_t lastPos = 0;
+    size_t headPos = 0;
+    size_t tailPos = 0;
+    size_t fileLen = str.length();
+    tailPos = fileLen;
+    while (headPos < fileLen)
+    {
+        lastPos = headPos;
+        if (headPos == 0)
+        {
+            // find the first "#include"
+            headPos = str.find("#include");
+        }
+        else
+        {
+            // find the next "#include"
+            headPos = str.find("#include", headPos + 1);
+        }
+
+        // If "#include" is found
+        if (headPos != std::string::npos)
+        {
+            // append from our last position for the legth (head - last position) 
+            out.append(str.substr(lastPos,  headPos - lastPos));
+
+            // find the start quote "
+            size_t startQuote = str.find("\"", headPos) + 1;
+            if (startQuote == std::string::npos)
+            {
+                // We have started an "#include" but missing the leading quote "
+                GP_ERROR("Compile failed for shader '%s' missing leading \".", filepath);
+                return;
+            }
+            // find the end quote "
+            size_t endQuote = str.find("\"", startQuote);
+            if (endQuote == std::string::npos)
+            {
+                // We have a start quote but missing the trailing quote "
+                GP_ERROR("Compile failed for shader '%s' missing trailing \".", filepath);
+                return;
+            }
+
+            // jump the head position past the end quote
+            headPos = endQuote + 1;
+            
+            // File path to include and 'stitch' in the value in the quotes to the file path and source it.
+            std::string filepathStr = filepath;
+            std::string directoryPath = filepathStr.substr(0, filepathStr.rfind('/') + 1);
+            size_t len = endQuote - (startQuote);
+            std::string includeStr = str.substr(startQuote, len);
+            directoryPath.append(includeStr);
+            const char* includedSource = FileSystem::readAll(directoryPath.c_str());
+            if (includedSource == NULL)
+            {
+                GP_ERROR("Compile failed for shader '%s' invalid filepath.", filepathStr.c_str());
+                SAFE_DELETE_ARRAY(includedSource);
+                return;
+            }
+            else
+            {
+                // Valid file so lets attempt to see if we need to append anything to it too (recurse...)
+                replaceIncludes(directoryPath.c_str(), includedSource, out);
+            }
+        }
+        else
+        {
+            // Append the remaining
+            out.append(str.c_str(), lastPos, tailPos);
+        }
+    }
+}
+
+void writeShaderToErrorFile(const char* filePath, const char* source)
+{
+    std::string path = filePath;
+    path += ".err";
+    FILE* file = FileSystem::openFile(path.c_str(), "wb");
+    int err = ferror(file);
+    fwrite(source, 1, strlen(source), file);
+    fclose(file);
+}
+
 Effect* Effect::createFromSource(const char* vshPath, const char* vshSource, const char* fshPath, const char* fshSource, const char* defines)
 {
     GP_ASSERT(vshSource);
@@ -116,16 +221,23 @@ Effect* Effect::createFromSource(const char* vshPath, const char* vshSource, con
     GLint length;
     GLint success;
 
-    // Compile vertex shader.
-    std::string definesStr = (defines == NULL) ? "" : defines;
-#ifdef OPENGL_ES
-    if (defines && strlen(defines) != 0)
-        definesStr += "\n";
-    definesStr+= OPENGL_ES_DEFINE;
-#endif
+    // Replace all comma seperated definitions with #define prefix and \n suffix
+    std::string definesStr = "";
+    replaceDefines(defines, definesStr);
+    
     shaderSource[0] = definesStr.c_str();
     shaderSource[1] = "\n";
-    shaderSource[2] = vshSource;
+    std::string vshSourceStr = "";
+    if (vshPath)
+    {
+        // Replace the #include "xxxxx.xxx" with the sources that come from file paths
+        replaceIncludes(vshPath, vshSource, vshSourceStr);
+        if (vshSource && strlen(vshSource) != 0)
+            vshSourceStr += "\n";
+            
+        //writeShaderToErrorFile(vshPath, vshSourceStr.c_str());   // Debugging
+    }
+    shaderSource[2] = vshPath ? vshSourceStr.c_str() :  vshSource;
     GL_ASSERT( vertexShader = glCreateShader(GL_VERTEX_SHADER) );
     GL_ASSERT( glShaderSource(vertexShader, SHADER_SOURCE_LENGTH, shaderSource, NULL) );
     GL_ASSERT( glCompileShader(vertexShader) );
@@ -139,6 +251,9 @@ Effect* Effect::createFromSource(const char* vshPath, const char* vshSource, con
             GL_ASSERT( glGetShaderInfoLog(vertexShader, length, NULL, infoLog) );
             infoLog[length-1] = '\0';
         }
+
+        // Write out the expanded shader file.
+        writeShaderToErrorFile(vshPath, shaderSource[2]);
         GP_ERROR("Compile failed for vertex shader '%s' with error '%s'.", vshPath == NULL ? "NULL" : vshPath, infoLog == NULL ? "" : infoLog);
         SAFE_DELETE_ARRAY(infoLog);
 
@@ -149,7 +264,17 @@ Effect* Effect::createFromSource(const char* vshPath, const char* vshSource, con
     }
 
     // Compile the fragment shader.
-    shaderSource[2] = fshSource;
+    std::string fshSourceStr;
+    if (fshPath)
+    {
+        // Replace the #include "xxxxx.xxx" with the sources that come from file paths
+        replaceIncludes(fshPath, fshSource, fshSourceStr);
+        if (fshSource && strlen(fshSource) != 0)
+            fshSourceStr += "\n";
+
+        //writeShaderToErrorFile(fshPath, fshSourceStr.c_str()); // Debugging
+    }
+    shaderSource[2] = fshPath ? fshSourceStr.c_str() : fshSource;
     GL_ASSERT( fragmentShader = glCreateShader(GL_FRAGMENT_SHADER) );
     GL_ASSERT( glShaderSource(fragmentShader, SHADER_SOURCE_LENGTH, shaderSource, NULL) );
     GL_ASSERT( glCompileShader(fragmentShader) );
@@ -163,6 +288,10 @@ Effect* Effect::createFromSource(const char* vshPath, const char* vshSource, con
             GL_ASSERT( glGetShaderInfoLog(fragmentShader, length, NULL, infoLog) );
             infoLog[length-1] = '\0';
         }
+        
+        // Write out the expanded shader file.
+        writeShaderToErrorFile(fshPath, shaderSource[2]);
+
         GP_ERROR("Compile failed for fragment shader (%s): %s", fshPath == NULL ? "NULL" : fshPath, infoLog == NULL ? "" : infoLog);
         SAFE_DELETE_ARRAY(infoLog);
 

+ 8 - 5
gameplay/src/FileSystem.cpp

@@ -200,14 +200,14 @@ bool FileSystem::listFiles(const char* dirPath, std::vector<std::string>& files)
 #endif
 }
 
-bool FileSystem::fileExists(const char* path)
+bool FileSystem::fileExists(const char* filePath)
 {
-    GP_ASSERT(path);
+    GP_ASSERT(filePath);
 
     std::string fullPath(__resourcePath);
-    fullPath += resolvePath(path);
+    fullPath += resolvePath(filePath);
 
-    createFileFromAsset(path);
+    createFileFromAsset(filePath);
 
     gp_stat_struct s;
 // Win32 doesn't support an asset or bundle definitions.
@@ -216,7 +216,7 @@ bool FileSystem::fileExists(const char* path)
     {
         fullPath = __resourcePath;
         fullPath += "../../gameplay/";
-        fullPath += path;
+        fullPath += filePath;
         
         return stat(fullPath.c_str(), &s) == 0;
     }
@@ -229,6 +229,7 @@ bool FileSystem::fileExists(const char* path)
 FILE* FileSystem::openFile(const char* path, const char* mode)
 {
     GP_ASSERT(path);
+    GP_ASSERT(mode);
 
     std::string fullPath(__resourcePath);
     fullPath += resolvePath(path);
@@ -254,6 +255,8 @@ FILE* FileSystem::openFile(const char* path, const char* mode)
 
 char* FileSystem::readAll(const char* filePath, int* fileSize)
 {
+    GP_ASSERT(filePath);
+
     // Open file for reading.
     FILE* file = openFile(filePath, "rb");
     if (file == NULL)

+ 4 - 4
gameplay/src/FileSystem.h

@@ -95,10 +95,10 @@ public:
     /**
      * Checks if the file at the given path exists.
      * 
-     * @param path The path to the file.
+     * @param filePath The path to the file.
      * @return <code>true</code> if the file exists; <code>false</code> otherwise.
      */
-    static bool fileExists(const char* path);
+    static bool fileExists(const char* filePath);
 
     /**
      * Opens the specified file.
@@ -106,12 +106,12 @@ public:
      * The file at the specified location is opened, relative to the currently set
      * resource path.
      *
-     * @param path The path to the file to be opened, relative to the currently set resource path.
+     * @param filePath The path to the file to be opened, relative to the currently set resource path.
      * @param mode The mode used to open the file, passed directly to fopen.
      * 
      * @see setResourcePath(const char*)
      */
-    static FILE* openFile(const char* path, const char* mode);
+    static FILE* openFile(const char* filePath, const char* mode);
 
     /**
      * Reads the entire contents of the specified file and returns its contents.

+ 1 - 13
gameplay/src/Material.cpp

@@ -242,21 +242,9 @@ bool Material::loadPass(Technique* technique, Properties* passProperties)
     const char* fragmentShaderPath = passProperties->getString("fragmentShader");
     GP_ASSERT(fragmentShaderPath);
     const char* defines = passProperties->getString("defines");
-    std::string define;
-    if (defines != NULL)
-    {
-        define = defines;
-        define.insert(0, "#define ");
-        unsigned int pos;
-        while ((pos = define.find(';')) != std::string::npos)
-        {
-            define.replace(pos, 1, "\n#define ");
-        }
-        define += "\n";
-    }
 
     // Create the pass.
-    Pass* pass = Pass::create(passProperties->getId(), technique, vertexShaderPath, fragmentShaderPath, define.c_str());
+    Pass* pass = Pass::create(passProperties->getId(), technique, vertexShaderPath, fragmentShaderPath, defines);
     if (!pass)
     {
         GP_ERROR("Failed to create pass for technique.");

+ 0 - 1
gameplay/src/MaterialParameter.cpp

@@ -227,7 +227,6 @@ Texture::Sampler* MaterialParameter::setValue(const char* texturePath, bool gene
         }
         return sampler;
     }
-
     return NULL;
 }
 

+ 16 - 15
gameplay/src/RenderState.cpp

@@ -293,7 +293,7 @@ void RenderState::bind(Pass* pass)
     // Apply parameter bindings and renderer state for the entire hierarchy, top-down.
     rs = NULL;
     Effect* effect = pass->getEffect();
-    while (rs = getTopmost(rs))
+    while ((rs = getTopmost(rs)))
     {
         for (unsigned int i = 0, count = rs->_parameters.size(); i < count; ++i)
         {
@@ -360,8 +360,9 @@ void RenderState::cloneInto(RenderState* renderState, NodeCloneContext& context)
 }
 
 RenderState::StateBlock::StateBlock()
-    : _blendEnabled(false), _cullFaceEnabled(false), _depthTestEnabled(false), _depthWriteEnabled(false),
-      _srcBlend(RenderState::BLEND_ONE), _dstBlend(RenderState::BLEND_ONE), _bits(0L)
+    : _cullFaceEnabled(false), _depthTestEnabled(false), _depthWriteEnabled(false),
+      _blendEnabled(false), _blendSrc(RenderState::BLEND_ONE), _blendDst(RenderState::BLEND_ONE),
+      _bits(0L)
 {
 }
 
@@ -404,11 +405,11 @@ void RenderState::StateBlock::bindNoRestore()
             GL_ASSERT( glDisable(GL_BLEND) );
         _defaultState->_blendEnabled = _blendEnabled;
     }
-    if ((_bits & RS_BLEND_FUNC) && (_srcBlend != _defaultState->_srcBlend || _dstBlend != _defaultState->_dstBlend))
+    if ((_bits & RS_BLEND_FUNC) && (_blendSrc != _defaultState->_blendSrc || _blendDst != _defaultState->_blendDst))
     {
-        GL_ASSERT( glBlendFunc((GLenum)_srcBlend, (GLenum)_dstBlend) );
-        _defaultState->_srcBlend = _srcBlend;
-        _defaultState->_dstBlend = _dstBlend;
+        GL_ASSERT( glBlendFunc((GLenum)_blendSrc, (GLenum)_blendDst) );
+        _defaultState->_blendSrc = _blendSrc;
+        _defaultState->_blendDst = _blendDst;
     }
     if ((_bits & RS_CULL_FACE) && (_cullFaceEnabled != _defaultState->_cullFaceEnabled))
     {
@@ -456,8 +457,8 @@ void RenderState::StateBlock::restore(long stateOverrideBits)
     {
         GL_ASSERT( glBlendFunc(GL_ONE, GL_ONE) );
         _defaultState->_bits &= ~RS_BLEND_FUNC;
-        _defaultState->_srcBlend = RenderState::BLEND_ONE;
-        _defaultState->_dstBlend = RenderState::BLEND_ONE;
+        _defaultState->_blendSrc = RenderState::BLEND_ONE;
+        _defaultState->_blendDst = RenderState::BLEND_ONE;
     }
     if (!(stateOverrideBits & RS_CULL_FACE) && (_defaultState->_bits & RS_CULL_FACE))
     {
@@ -550,11 +551,11 @@ void RenderState::StateBlock::setState(const char* name, const char* value)
     {
         setBlend(parseBoolean(value));
     }
-    else if (strcmp(name, "srcBlend") == 0)
+    else if (strcmp(name, "blendSrc") == 0 || strcmp(name, "srcBlend") == 0 )   // Leaving srcBlend for backward compat.
     {
         setBlendSrc(parseBlend(value));
     }
-    else if (strcmp(name, "dstBlend") == 0)
+    else if (strcmp(name, "blendDst") == 0 || strcmp(name, "dstBlend") == 0)    // // Leaving dstBlend for backward compat.
     {
         setBlendDst(parseBlend(value));
     }
@@ -591,8 +592,8 @@ void RenderState::StateBlock::setBlend(bool enabled)
 
 void RenderState::StateBlock::setBlendSrc(Blend blend)
 {
-    _srcBlend = blend;
-    if (_srcBlend == BLEND_ONE && _dstBlend == BLEND_ONE)
+    _blendSrc = blend;
+    if (_blendSrc == BLEND_ONE && _blendDst == BLEND_ONE)
     {
         _bits &= ~RS_BLEND_FUNC;
     }
@@ -604,8 +605,8 @@ void RenderState::StateBlock::setBlendSrc(Blend blend)
 
 void RenderState::StateBlock::setBlendDst(Blend blend)
 {
-    _dstBlend = blend;
-    if (_srcBlend == BLEND_ONE && _dstBlend == BLEND_ONE)
+    _blendDst = blend;
+    if (_blendSrc == BLEND_ONE && _blendDst == BLEND_ONE)
     {
         _bits &= ~RS_BLEND_FUNC;
     }

+ 3 - 4
gameplay/src/RenderState.h

@@ -213,13 +213,12 @@ public:
         static void enableDepthWrite();
 
         // States
-        bool _blendEnabled;
         bool _cullFaceEnabled;
         bool _depthTestEnabled;
         bool _depthWriteEnabled;
-        Blend _srcBlend;
-        Blend _dstBlend;
-        // State bits
+        bool _blendEnabled;
+        Blend _blendSrc;
+        Blend _blendDst;
         long _bits;
 
         static StateBlock* _defaultState;

+ 3 - 1
gameplay/src/Texture.h

@@ -99,7 +99,9 @@ public:
         void setFilterMode(Filter minificationFilter, Filter magnificationFilter);
 
         /**
-         * Returns the texture for this sampler.
+         * Gets the texture for this sampler.
+         *
+         * @return The texture for this sampler.
          */
         Texture* getTexture() const;