diff mbox

[Branch,~glmark2-dev/glmark2/trunk] Rev 22: Change basic shader to use user-defined vertex attributes exclusively.

Message ID 20110721123631.17019.41284.launchpad@loganberry.canonical.com
State Accepted
Headers show

Commit Message

Alexandros Frantzis July 21, 2011, 12:36 p.m. UTC
------------------------------------------------------------
revno: 22
committer: Alexandros Frantzis <alf82@freemail.gr>
timestamp: Thu 2010-07-08 18:17:11 +0300
message:
  Change basic shader to use user-defined vertex attributes exclusively.
  Fix Shading Scene so that it works with the new the shader.
modified:
  data/shaders/light-basic.frag
  data/shaders/light-basic.vert
  sceneshading.cpp


--
lp:glmark2
https://code.launchpad.net/~glmark2-dev/glmark2/trunk

You are subscribed to branch lp:glmark2.
To unsubscribe from this branch go to https://code.launchpad.net/~glmark2-dev/glmark2/trunk/+edit-subscription
diff mbox

Patch

=== modified file 'data/shaders/light-basic.frag'
--- data/shaders/light-basic.frag	2010-07-07 10:32:18 +0000
+++ data/shaders/light-basic.frag	2010-07-08 15:17:11 +0000
@@ -18,7 +18,7 @@ 
 varying float Diffuse;
 
 void main(void)
-{	
-//Multiply the light Diffuse intensity by the color of the cube 
-	gl_FragColor = Diffuse * vec4(0,0,1,1);
+{
+    // Multiply the light Diffuse intensity by the color of the 
+    gl_FragColor = Diffuse * vec4(0.0, 0.0, 1.0, 1.0);
 }

=== modified file 'data/shaders/light-basic.vert'
--- data/shaders/light-basic.vert	2010-07-08 09:08:36 +0000
+++ data/shaders/light-basic.vert	2010-07-08 15:17:11 +0000
@@ -58,15 +58,25 @@ 
 // lesson, but it even cleared up some stuff for me without even
 // thinking about it. GO MATHS :)
 
+attribute vec3 position;
+attribute vec3 normal;
+attribute vec2 texture;
+
+uniform mat4 ModelViewProjectionMatrix;
+uniform mat4 NormalMatrix;
+uniform vec4 LightSourcePosition;
+uniform vec3 LightSourceDiffuse;
+uniform vec3 MaterialDiffuse;
+
 varying float Diffuse;
 
 void main(void)
-{			
-	vec3 Normal = normalize(gl_NormalMatrix * gl_Normal);
-
-	vec3 Light = normalize(gl_LightSource[0].position.xyz);
-
-	Diffuse = max(dot(Normal, Light),0.0);
-
-	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
+{
+    vec3 N = normalize(vec3(NormalMatrix * vec4(normal, 1.0)));
+
+    vec3 L = normalize(LightSourcePosition.xyz);
+
+    Diffuse = max(dot(N, L), 0.0);
+
+    gl_Position = ModelViewProjectionMatrix * vec4(position, 1.0);
 }

=== modified file 'sceneshading.cpp'
--- sceneshading.cpp	2010-07-08 13:34:28 +0000
+++ sceneshading.cpp	2010-07-08 15:17:11 +0000
@@ -1,4 +1,5 @@ 
 #include "scene.h"
+#include "matrix.h"
 
 SceneShading::~SceneShading()
 {
@@ -79,8 +80,18 @@ 
         glDisable(GL_TEXTURE_2D);
         mShader[1].use();
         break;
-    };
-    
+    }
+
+    if (mCurrentPart == 0) {
+        glUniform4fv(mShader[mCurrentPart].mLocations.LightSourcePosition, 1,
+                lightPosition);
+        glUniform3fv(mShader[mCurrentPart].mLocations.LightSourceDiffuse, 1,
+                lightDiffuse);
+        glUniform3fv(mShader[mCurrentPart].mLocations.MaterialDiffuse, 1,
+                mat_diffuse);
+    }
+
+
     mCurrentFrame = 0;
     mRunning = true;
     mStartTime = SDL_GetTicks() / 1000.0;
@@ -128,5 +139,27 @@ 
 
     glColor3f(0.0f, 1.0f, 1.0f);
 
-    mMesh.render_vbo();
+    if (mCurrentPart == 0) {
+        // Load the ModelViewProjectionMatrix uniform in the shader
+        Matrix4f model_view(1.0f, 1.0f, 1.0f);
+        Matrix4f model_view_proj(mScreen.mProjection);
+
+        model_view.translate(0.0f, 0.0f, -5.0f);
+        model_view.rotate(2 * M_PI * mRotation / 360.0, 0.0f, 1.0f, 0.0f);
+        model_view_proj *= model_view;
+
+        glUniformMatrix4fv(mShader[mCurrentPart].mLocations.ModelViewProjectionMatrix, 1,
+                GL_FALSE, model_view_proj.m);
+
+        // Load the NormalMatrix uniform in the shader
+        // The NormalMatrix is the inverse transpose of the model view matrix.
+        model_view.invert().transpose();
+        glUniformMatrix4fv(mShader[mCurrentPart].mLocations.NormalMatrix, 1,
+                GL_FALSE, model_view.m);
+
+        mMesh.render_vbo_attrib();
+    }
+    else {
+        mMesh.render_vbo();
+    }
 }