diff mbox

[Branch,~glmark2-dev/glmark2/trunk] Rev 106: Merge advanced lighting development branch into trunk.

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

Commit Message

Jesse Barker July 12, 2011, 6:01 p.m. UTC
Merge authors:
  Jesse Barker (jesse-barker)
------------------------------------------------------------
revno: 106 [merge]
committer: Jesse Barker <jesse.barker@linaro.org>
branch nick: trunk
timestamp: Tue 2011-07-12 10:58:48 -0700
message:
  Merge advanced lighting development branch into trunk.
added:
  data/shaders/light-phong.frag
  data/shaders/light-phong.vert
modified:
  data/shaders/light-advanced.frag
  src/main.cpp
  src/scene-shading.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-advanced.frag'
--- data/shaders/light-advanced.frag	2010-11-19 13:11:20 +0000
+++ data/shaders/light-advanced.frag	2011-07-12 17:53:10 +0000
@@ -2,35 +2,34 @@ 
 precision mediump float;
 #endif
 
-uniform vec3 LightSourceAmbient;
-uniform vec3 LightSourceDiffuse;
-uniform vec3 LightSourceSpecular;
-uniform vec3 MaterialAmbient;
-uniform vec3 MaterialDiffuse;
-uniform vec3 MaterialSpecular;
-uniform vec4 MaterialColor;
-
 varying vec3 Normal;
 varying vec3 Light;
 varying vec3 HalfVector;
 
 void main(void)
 {
+    const vec4 LightSourceAmbient = vec4(0.1, 0.1, 0.1, 1.0);
+    const vec4 LightSourceDiffuse = vec4(0.8, 0.8, 0.8, 1.0);
+    const vec4 LightSourceSpecular = vec4(0.8, 0.8, 0.8, 1.0);
+    const vec4 MaterialAmbient = vec4(1.0, 1.0, 1.0, 1.0);
+    const vec4 MaterialDiffuse = vec4(0.0, 0.0, 1.0, 1.0);
+    const vec4 MaterialSpecular = vec4(1.0, 1.0, 1.0, 1.0);
+    const float MaterialShininess = 100.0;
+
     vec3 N = normalize(Normal);
     vec3 L = normalize(Light);
     vec3 H = normalize(HalfVector);
 
     // Calculate the diffuse color according to Lambertian reflectance
-    vec3 diffuse = MaterialDiffuse * LightSourceDiffuse * max(dot(N, L), 0.0);
+    vec4 diffuse = MaterialDiffuse * LightSourceDiffuse * max(dot(N, L), 0.0);
 
     // Calculate the ambient color
-    vec3 ambient = MaterialAmbient * LightSourceAmbient;
+    vec4 ambient = MaterialAmbient * LightSourceAmbient;
 
     // Calculate the specular color according to the Blinn-Phong model
-    vec3 specular = MaterialSpecular * LightSourceSpecular *
-                    pow(max(dot(N,H), 0.0), 100.0);
+    vec4 specular = MaterialSpecular * LightSourceSpecular *
+                    pow(max(dot(N,H), 0.0), MaterialShininess);
 
     // Calculate the final color
-    gl_FragColor = vec4(ambient, 1.0) + vec4(specular, 1.0) +
-                   vec4(diffuse, 1.0) * MaterialColor;
+    gl_FragColor = ambient + specular + diffuse;
 }

=== added file 'data/shaders/light-phong.frag'
--- data/shaders/light-phong.frag	1970-01-01 00:00:00 +0000
+++ data/shaders/light-phong.frag	2011-07-12 17:53:10 +0000
@@ -0,0 +1,30 @@ 
+#ifdef GL_ES
+precision mediump float;
+#endif
+
+uniform vec4 LightSourcePosition;
+
+varying vec3 vertex_normal;
+varying vec4 vertex_position;
+
+void main(void)
+{
+    const vec4 lightAmbient = vec4(0.1, 0.1, 0.1, 1.0);
+    const vec4 lightDiffuse = vec4(0.8, 0.8, 0.8, 1.0);
+    const vec4 lightSpecular = vec4(0.8, 0.8, 0.8, 1.0);
+    const vec4 matAmbient = vec4(1.0, 1.0, 1.0, 1.0);
+    const vec4 matDiffuse = vec4(0.0, 0.0, 1.0, 1.0);
+    const vec4 matSpecular = vec4(1.0, 1.0, 1.0, 1.0);
+    const float matShininess = 100.0;
+    vec3 eye_direction = normalize(-vertex_position.xyz);
+    vec3 light_direction = normalize(LightSourcePosition.xyz/LightSourcePosition.w -
+                                     vertex_position.xyz/vertex_position.w);
+    vec3 normalized_normal = normalize(vertex_normal);
+    vec3 reflection = reflect(-light_direction, normalized_normal);
+    float specularTerm = pow(max(0.0, dot(reflection, eye_direction)), matShininess);
+    float diffuseTerm = max(0.0, dot(normalized_normal, light_direction));
+    vec4 specular = (lightSpecular * matSpecular);
+    vec4 ambient = (lightAmbient * matAmbient);
+    vec4 diffuse = (lightDiffuse * matDiffuse);
+    gl_FragColor = (specular * specularTerm) + ambient + (diffuse * diffuseTerm);
+}

=== added file 'data/shaders/light-phong.vert'
--- data/shaders/light-phong.vert	1970-01-01 00:00:00 +0000
+++ data/shaders/light-phong.vert	2011-07-12 17:53:10 +0000
@@ -0,0 +1,23 @@ 
+attribute vec3 position;
+attribute vec3 normal;
+
+uniform mat4 ModelViewProjectionMatrix;
+uniform mat4 NormalMatrix;
+uniform mat4 ModelViewMatrix;
+
+varying vec3 vertex_normal;
+varying vec4 vertex_position;
+
+void main(void)
+{			
+    vec4 current_position = vec4(position, 1.0);
+
+    // Transform the normal to eye coordinates
+    vertex_normal = normalize(vec3(NormalMatrix * vec4(normal, 1.0)));
+
+    // Transform the current position to eye coordinates
+    vertex_position = ModelViewMatrix * current_position;
+
+    // Transform the current position to clip coordinates
+    gl_Position = ModelViewProjectionMatrix * current_position;
+}

=== modified file 'src/main.cpp'
--- src/main.cpp	2011-07-08 13:46:32 +0000
+++ src/main.cpp	2011-07-12 17:53:10 +0000
@@ -46,6 +46,7 @@ 
     "texture:texture-filter=linear",
     "texture:texture-filter=mipmap",
     "shading:shading=gouraud",
+    "shading:shading=blinn-phong-inf",
     "shading:shading=phong",
     "conditionals:vertex-steps=0:fragment-steps=0",
     "conditionals:vertex-steps=0:fragment-steps=5",

=== modified file 'src/scene-shading.cpp'
--- src/scene-shading.cpp	2011-07-05 13:45:41 +0000
+++ src/scene-shading.cpp	2011-07-12 17:53:10 +0000
@@ -33,7 +33,7 @@ 
     Scene(pCanvas, "shading")
 {
     mOptions["shading"] = Scene::Option("shading", "gouraud",
-                                        "[gouraud, phong]");
+                                        "[gouraud, blinn-phong-inf, phong]");
 }
 
 SceneShading::~SceneShading()
@@ -74,15 +74,8 @@ 
 {
     Scene::setup();
 
-    static const LibMatrix::vec3 lightAmbient(0.1f, 0.1f, 0.1f);
-    static const LibMatrix::vec3 lightDiffuse(0.8f, 0.8f, 0.8f);
-    static const LibMatrix::vec3 lightSpecular(0.8f, 0.8f, 0.8f);
     static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f);
-
-    static const LibMatrix::vec3 materialAmbient(1.0f, 1.0f, 1.0f);
-    static const LibMatrix::vec3 materialDiffuse(1.0f, 1.0f, 1.0f);
-    static const LibMatrix::vec3 materialSpecular(1.0f, 1.0f, 1.0f);
-    static const LibMatrix::vec4 materialColor(0.0f, 0.0f, 1.0f, 1.0f);
+    static const LibMatrix::vec4 materialDiffuse(0.0f, 0.0f, 1.0f, 1.0f);
 
     std::string vtx_shader_filename;
     std::string frg_shader_filename;
@@ -92,10 +85,14 @@ 
         vtx_shader_filename = GLMARK_DATA_PATH"/shaders/light-basic.vert";
         frg_shader_filename = GLMARK_DATA_PATH"/shaders/light-basic.frag";
     }
-    else if (shading == "phong") {
+    else if (shading == "blinn-phong-inf") {
         vtx_shader_filename = GLMARK_DATA_PATH"/shaders/light-advanced.vert";
         frg_shader_filename = GLMARK_DATA_PATH"/shaders/light-advanced.frag";
     }
+    else if (shading == "phong") {
+        vtx_shader_filename = GLMARK_DATA_PATH"/shaders/light-phong.vert";
+        frg_shader_filename = GLMARK_DATA_PATH"/shaders/light-phong.frag";
+    }
 
     if (!Scene::load_shaders_from_files(mProgram, vtx_shader_filename,
                                         frg_shader_filename))
@@ -111,17 +108,8 @@ 
     mMesh.set_attrib_locations(attrib_locations);
 
     // Load lighting and material uniforms
-    mProgram.loadUniformVector(lightAmbient, "LightSourceAmbient");
     mProgram.loadUniformVector(lightPosition, "LightSourcePosition");
-    mProgram.loadUniformVector(lightDiffuse, "LightSourceDiffuse");
-    mProgram.loadUniformVector(lightSpecular, "LightSourceSpecular");
-
-    mProgram.loadUniformVector(materialAmbient, "MaterialAmbient");
-    mProgram.loadUniformVector(materialDiffuse, "MaterialDiffuse");
-    mProgram.loadUniformVector(materialSpecular, "MaterialSpecular");
-    mProgram.loadUniformVector(materialColor, "MaterialColor");
-
-    // Calculate and load the half vector
+    mProgram.loadUniformVector(materialDiffuse, "MaterialColor");
     LibMatrix::vec3 halfVector(lightPosition[0], lightPosition[1], lightPosition[2]);
     halfVector.normalize();
     halfVector += LibMatrix::vec3(0.0, 0.0, 1.0);
@@ -179,6 +167,9 @@ 
     normal_matrix.inverse().transpose();
     mProgram.loadUniformMatrix(normal_matrix, "NormalMatrix");
 
+    // Load the modelview matrix itself
+    mProgram.loadUniformMatrix(model_view.getCurrent(), "ModelViewMatrix");
+
     mMesh.render_vbo();
 }