Mesa (master): progs/glsl: added texture mapping to bump demo

Brian Paul brianp at kemper.freedesktop.org
Mon Dec 7 08:04:39 PST 2009


Module: Mesa
Branch: master
Commit: 8796dbf5196bcc75d39372570fa6347cd018354d
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=8796dbf5196bcc75d39372570fa6347cd018354d

Author: Brian Paul <brianp at vmware.com>
Date:   Thu Dec  3 09:49:27 2009 -0700

progs/glsl: added texture mapping to bump demo

Press 't' to toggle texture map.

---

 progs/glsl/CH11-bumpmaptex.frag |   47 +++++++++++++++++++++++++++++++++
 progs/glsl/bump.c               |   55 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 100 insertions(+), 2 deletions(-)

diff --git a/progs/glsl/CH11-bumpmaptex.frag b/progs/glsl/CH11-bumpmaptex.frag
new file mode 100644
index 0000000..b1f93b7
--- /dev/null
+++ b/progs/glsl/CH11-bumpmaptex.frag
@@ -0,0 +1,47 @@
+//
+// Fragment shader for procedural bumps
+//
+// Authors: John Kessenich, Randi Rost
+//
+// Copyright (c) 2002-2006 3Dlabs Inc. Ltd. 
+//
+// See 3Dlabs-License.txt for license information
+//
+// Texture mapping/modulation added by Brian Paul
+//
+
+varying vec3 LightDir;
+varying vec3 EyeDir;
+
+uniform float BumpDensity;     // = 16.0
+uniform float BumpSize;        // = 0.15
+uniform float SpecularFactor;  // = 0.5
+
+sampler2D Tex;
+
+void main()
+{
+    vec3 ambient = vec3(0.25);
+    vec3 litColor;
+    vec2 c = BumpDensity * gl_TexCoord[0].st;
+    vec2 p = fract(c) - vec2(0.5);
+
+    float d, f;
+    d = p.x * p.x + p.y * p.y;
+    f = inversesqrt(d + 1.0);
+
+    if (d >= BumpSize)
+        { p = vec2(0.0); f = 1.0; }
+
+    vec3 SurfaceColor = texture2D(Tex, gl_TexCoord[0].st).xyz;
+
+    vec3 normDelta = vec3(p.x, p.y, 1.0) * f;
+    litColor = SurfaceColor * (ambient + max(dot(normDelta, LightDir), 0.0));
+    vec3 reflectDir = reflect(LightDir, normDelta);
+    
+    float spec = max(dot(EyeDir, reflectDir), 0.0);
+    spec *= SpecularFactor;
+    litColor = min(litColor + spec, vec3(1.0));
+
+    gl_FragColor = vec4(litColor, 1.0);
+}
diff --git a/progs/glsl/bump.c b/progs/glsl/bump.c
index 50a0900..e31afab 100644
--- a/progs/glsl/bump.c
+++ b/progs/glsl/bump.c
@@ -12,15 +12,20 @@
 #include <GL/glew.h>
 #include <GL/glut.h>
 #include "shaderutil.h"
+#include "readtex.h"
 
 
 static char *FragProgFile = "CH11-bumpmap.frag";
+static char *FragTexProgFile = "CH11-bumpmaptex.frag";
 static char *VertProgFile = "CH11-bumpmap.vert";
+static char *TextureFile = "../images/tile.rgb";
 
 /* program/shader objects */
 static GLuint fragShader;
+static GLuint fragTexShader;
 static GLuint vertShader;
 static GLuint program;
+static GLuint texProgram;
 
 
 static struct uniform_info Uniforms[] = {
@@ -32,13 +37,26 @@ static struct uniform_info Uniforms[] = {
    END_OF_UNIFORMS
 };
 
+static struct uniform_info TexUniforms[] = {
+   { "LightPosition",  1, GL_FLOAT_VEC3, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 },
+   { "Tex",            1, GL_INT,   { 0, 0, 0, 0 }, -1 },
+   { "BumpDensity",    1, GL_FLOAT, { 10.0, 0, 0, 0 }, -1 },
+   { "BumpSize",       1, GL_FLOAT, { 0.125, 0, 0, 0 }, -1 },
+   { "SpecularFactor", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 },
+   END_OF_UNIFORMS
+};
+
 static GLint win = 0;
 
 static GLfloat xRot = 20.0f, yRot = 0.0f, zRot = 0.0f;
 
 static GLint tangentAttrib;
+static GLint tangentAttribTex;
+
+static GLuint Texture;
 
 static GLboolean Anim = GL_FALSE;
+static GLboolean Textured = GL_FALSE;
 
 
 static void
@@ -135,6 +153,11 @@ Redisplay(void)
    glRotatef(yRot, 0.0f, 1.0f, 0.0f);
    glRotatef(zRot, 0.0f, 0.0f, 1.0f);
 
+   if (Textured)
+      glUseProgram(texProgram);
+   else
+      glUseProgram(program);
+
    Cube(1.5);
 
    glPopMatrix();
@@ -163,8 +186,10 @@ static void
 CleanUp(void)
 {
    glDeleteShader(fragShader);
+   glDeleteShader(fragTexShader);
    glDeleteShader(vertShader);
    glDeleteProgram(program);
+   glDeleteProgram(texProgram);
    glutDestroyWindow(win);
 }
 
@@ -181,6 +206,9 @@ Key(unsigned char key, int x, int y)
       Anim = !Anim;
       glutIdleFunc(Anim ? Idle : NULL);
       break;
+   case 't':
+      Textured = !Textured;
+      break;
    case 'z':
       zRot += step;
       break;
@@ -254,6 +282,26 @@ Init(void)
 
    CheckError(__LINE__);
 
+
+   /*
+    * As above, but fragment shader also uses a texture map.
+    */
+   fragTexShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragTexProgFile);
+   texProgram = LinkShaders(vertShader, fragTexShader);
+   glUseProgram(texProgram);
+   assert(glIsProgram(texProgram));
+   assert(glIsShader(fragTexShader));
+   SetUniformValues(texProgram, TexUniforms);
+   PrintUniforms(TexUniforms);
+
+   /*
+    * Load tex image.
+    */
+   glGenTextures(1, &Texture);
+   glBindTexture(GL_TEXTURE_2D, Texture);
+   LoadRGBMipmaps(TextureFile, GL_RGB);
+
+
    glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
 
    glEnable(GL_DEPTH_TEST);
@@ -268,10 +316,13 @@ ParseOptions(int argc, char *argv[])
    int i;
    for (i = 1; i < argc; i++) {
       if (strcmp(argv[i], "-fs") == 0) {
-         FragProgFile = argv[i+1];
+         FragProgFile = argv[++i];
       }
       else if (strcmp(argv[i], "-vs") == 0) {
-         VertProgFile = argv[i+1];
+         VertProgFile = argv[++i];
+      }
+      else if (strcmp(argv[i], "-t") == 0) {
+         TextureFile = argv[++i];
       }
    }
 }



More information about the mesa-commit mailing list