diff mbox

[Branch,~glmark2-dev/glmark2/trunk] Rev 227: Merge of lp:~glmark2-dev/glmark2/texgen

Message ID 20120628162015.27727.57099.launchpad@ackee.canonical.com
State Accepted
Headers show

Commit Message

Jesse Barker June 28, 2012, 4:20 p.m. UTC
Merge authors:
  Jesse Barker (jesse-barker)
------------------------------------------------------------
revno: 227 [merge]
committer: Jesse Barker <jesse.barker@linaro.org>
branch nick: trunk
timestamp: Thu 2012-06-28 09:16:43 -0700
message:
  Merge of lp:~glmark2-dev/glmark2/texgen
  
  SceneTexture: Add an option to compute the texture coordinates in the shader
  rather than supplying them as a vertex attribute.  This also includes a new
  shader to support this.
added:
  data/shaders/light-basic-texgen.vert
modified:
  src/scene-texture.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-texgen.vert'
--- data/shaders/light-basic-texgen.vert	1970-01-01 00:00:00 +0000
+++ data/shaders/light-basic-texgen.vert	2012-06-26 22:41:07 +0000
@@ -0,0 +1,30 @@ 
+attribute vec3 position;
+attribute vec3 normal;
+
+uniform mat4 ModelViewProjectionMatrix;
+uniform mat4 NormalMatrix;
+uniform vec3 CenterPoint;
+
+varying vec4 Color;
+varying vec2 TextureCoord;
+
+void main(void)
+{
+    // Transform the normal to eye coordinates
+    vec3 N = normalize(vec3(NormalMatrix * vec4(normal, 1.0)));
+
+    // The LightSourcePosition is actually its direction for directional light
+    vec3 L = normalize(LightSourcePosition.xyz);
+
+    // Multiply the diffuse value by the vertex color (which is fixed in this case)
+    // to get the actual color that we will use to draw this vertex with
+    float diffuse = max(dot(N, L), 0.0);
+    Color = vec4(diffuse * MaterialDiffuse.rgb, MaterialDiffuse.a);
+
+    // Compute the texture coordinate and as a varying
+    vec3 vnorm = normalize(position - CenterPoint);
+    TextureCoord = asin(vnorm.xy) / PI + 0.5;
+
+    // Transform the position to clip coordinates
+    gl_Position = ModelViewProjectionMatrix * vec4(position, 1.0);
+}

=== modified file 'src/scene-texture.cpp'
--- src/scene-texture.cpp	2012-05-22 12:26:43 +0000
+++ src/scene-texture.cpp	2012-06-26 22:41:07 +0000
@@ -78,6 +78,8 @@ 
     optionDesc += "]";
     options_["texture"] = Scene::Option("texture", "crate-base",
                                         optionDesc);
+    options_["texgen"] = Scene::Option("texgen", "false",
+                                       "Whether to generate texcoords in the shader");
 }
 
 SceneTexture::~SceneTexture()
@@ -106,6 +108,7 @@ 
     Scene::setup();
 
     static const std::string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/light-basic.vert");
+    static const std::string vtx_shader_texgen_filename(GLMARK_DATA_PATH"/shaders/light-basic-texgen.vert");
     static const std::string frg_shader_filename(GLMARK_DATA_PATH"/shaders/light-basic-tex.frag");
     static const std::string frg_shader_bilinear_filename(GLMARK_DATA_PATH"/shaders/light-basic-tex-bilinear.frag");
     static const LibMatrix::vec4 lightPosition(20.0f, 20.0f, 10.0f, 1.0f);
@@ -138,7 +141,15 @@ 
                   min_filter, mag_filter, 0);
 
     // Load shaders
-    ShaderSource vtx_source(vtx_shader_filename);
+    bool doTexGen(options_["texgen"].value == "true");
+    ShaderSource vtx_source;
+    if (doTexGen) {
+        vtx_source.append_file(vtx_shader_texgen_filename);
+        vtx_source.add_const("PI", static_cast<float>(M_PI));
+    }
+    else {
+        vtx_source.append_file(vtx_shader_filename);
+    }
     ShaderSource frg_source;
     if (filter == "linear-shader") {
         frg_source.append_file(frg_shader_bilinear_filename);
@@ -196,7 +207,14 @@ 
     if (model.needTexcoords())
         model.calculate_texcoords();
     model.calculate_normals();
-    model.convert_to_mesh(mesh_);
+    // Tell the converter which attributes we care about
+    std::vector<std::pair<Model::AttribType, int> > attribs;
+    attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypePosition, 3));
+    attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypeNormal, 3));
+    if (!doTexGen) {
+        attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypeTexcoord, 2));
+    }
+    model.convert_to_mesh(mesh_, attribs);
     mesh_.build_vbo();
 
     // Calculate a projection matrix that is a good fit for the model
@@ -219,7 +237,12 @@ 
     std::vector<GLint> attrib_locations;
     attrib_locations.push_back(program_["position"].location());
     attrib_locations.push_back(program_["normal"].location());
-    attrib_locations.push_back(program_["texcoord"].location());
+    if (doTexGen) {
+        program_["CenterPoint"] = centerVec_;
+    }
+    else {
+        attrib_locations.push_back(program_["texcoord"].location());
+    }
     mesh_.set_attrib_locations(attrib_locations);
 
     currentFrame_ = 0;