diff mbox

[Branch,~glmark2-dev/glmark2/trunk] Rev 33: Use a shader to draw the Texture Scene.

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

Commit Message

Alexandros Frantzis July 21, 2011, 12:36 p.m. UTC
------------------------------------------------------------
revno: 33
committer: Alexandros Frantzis <alf82@freemail.gr>
timestamp: Fri 2010-07-09 16:36:15 +0300
message:
  Use a shader to draw the Texture Scene.
added:
  data/shaders/light-basic-tex.frag
modified:
  scene.h
  scenetexture.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

=== added file 'data/shaders/light-basic-tex.frag'
--- data/shaders/light-basic-tex.frag	1970-01-01 00:00:00 +0000
+++ data/shaders/light-basic-tex.frag	2010-07-09 13:36:15 +0000
@@ -0,0 +1,11 @@ 
+uniform sampler2D MaterialTexture0;
+
+varying vec4 Color;
+varying vec2 TextureCoord;
+
+void main(void)
+{
+    vec4 texel = texture2D(MaterialTexture0, TextureCoord);
+    gl_FragColor = texel * Color;
+}
+

=== modified file 'scene.h'
--- scene.h	2010-07-09 12:11:52 +0000
+++ scene.h	2010-07-09 13:36:15 +0000
@@ -76,6 +76,8 @@ 
     ~SceneTexture();
     
 protected:
+    Shader mShader;
+
     Mesh mCubeMesh;
     GLuint mTexture[3];
     Vector3f mRotation;

=== modified file 'scenetexture.cpp'
--- scenetexture.cpp	2010-07-09 12:16:42 +0000
+++ scenetexture.cpp	2010-07-09 13:36:15 +0000
@@ -1,4 +1,5 @@ 
 #include "scene.h"
+#include "matrix.h"
 
 SceneTexture::~SceneTexture()
 {
@@ -19,6 +20,9 @@ 
     model.calculate_normals();
     model.convert_to_mesh(&mCubeMesh);
     mCubeMesh.build_vbo();
+
+    mShader.load(GLMARK_DATA_PATH"data/shaders/light-basic.vert",
+                 GLMARK_DATA_PATH"data/shaders/light-basic-tex.frag");
     
     mRotationSpeed = Vector3f(36.0f, 36.0f, 36.0f);
     
@@ -46,6 +50,8 @@ 
 
 void SceneTexture::unload()
 {
+    mShader.remove();
+    mShader.unload();
 }
 
 void SceneTexture::start()
@@ -53,15 +59,18 @@ 
     GLfloat lightAmbient[] = {0.0f, 0.0f, 0.0f, 1.0f};
     GLfloat lightDiffuse[] = {0.8f, 0.8f, 0.8f, 1.0f};
     GLfloat lightPosition[] = {20.0f, 20.0f, 10.0f, 1.0f};
-    
-    glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
-    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
-    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
-    glEnable(GL_LIGHT0);
-    glEnable(GL_LIGHTING);
-    
-    glEnable(GL_TEXTURE_2D);
-    
+    GLfloat materialColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
+    
+    mShader.use();
+
+    // Load lighting and material uniforms
+    glUniform4fv(mShader.mLocations.LightSourcePosition, 1, lightPosition);
+
+    glUniform3fv(mShader.mLocations.LightSourceAmbient, 1, lightAmbient);
+    glUniform3fv(mShader.mLocations.LightSourceDiffuse, 1, lightDiffuse);
+
+    glUniform3fv(mShader.mLocations.MaterialColor, 1, materialColor);
+
     mCurrentFrame = 0;
     mRunning = true;
     mStartTime = SDL_GetTicks() / 1000.0;
@@ -107,26 +116,27 @@ 
 
 void SceneTexture::draw()
 {
-    glLoadIdentity();
-    glColor3f(1.0f, 1.0f, 1.0f);
-    glTranslatef(0.0f, 0.0f, -4.0f);
-    
-    glRotatef(mRotation.x, 1.0f, 0.0f, 0.0f);
-    glRotatef(mRotation.y, 0.0f, 1.0f, 0.0f);
-    glRotatef(mRotation.z, 0.0f, 0.0f, 1.0f);
-    
-    switch(mCurrentPart)
-    {
-    case 0:
-        glBindTexture(GL_TEXTURE_2D, mTexture[0]);
-        mCubeMesh.render_vbo();
-        break;
-    case 1:
-        glBindTexture(GL_TEXTURE_2D, mTexture[1]);
-        mCubeMesh.render_vbo();
-    case 2:
-        glBindTexture(GL_TEXTURE_2D, mTexture[2]);
-        mCubeMesh.render_vbo();
-        break;
-    }    
+    // 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.x / 360.0, 1.0f, 0.0f, 0.0f);
+    model_view.rotate(2 * M_PI * mRotation.y / 360.0, 0.0f, 1.0f, 0.0f);
+    model_view.rotate(2 * M_PI * mRotation.z / 360.0, 0.0f, 0.0f, 1.0f);
+    model_view_proj *= model_view;
+
+    glUniformMatrix4fv(mShader.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.mLocations.NormalMatrix, 1,
+                       GL_FALSE, model_view.m);
+
+    glActiveTexture(GL_TEXTURE0);
+    glBindTexture(GL_TEXTURE_2D, mTexture[mCurrentPart]);
+
+    mCubeMesh.render_vbo_attrib();
 }