Просмотр исходного кода

Updated the template game for changes to Game interface for long->float elapsed time on update and render methods.

seanpaultaylor 13 лет назад
Родитель
Сommit
cdb1553fbb

+ 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: