Revert "wglgears: show stereo-option in usage"
[mesa-demos.git] / src / glsl / bump.c
blob59f62cdb963623807ff3f7d3d30a78d583516c62
1 /**
2 * Procedural Bump Mapping demo. Uses the example shaders from
3 * chapter 11 of the OpenGL Shading Language "orange" book.
4 * 16 Jan 2007
5 */
7 #include <assert.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <GL/glew.h>
13 #include "glut_wrap.h"
14 #include "shaderutil.h"
15 #include "readtex.h"
18 static char *FragProgFile = "CH11-bumpmap.frag";
19 static char *FragTexProgFile = "CH11-bumpmaptex.frag";
20 static char *VertProgFile = "CH11-bumpmap.vert";
21 static char *TextureFile = DEMOS_DATA_DIR "tile.rgb";
23 /* program/shader objects */
24 static GLuint fragShader;
25 static GLuint fragTexShader;
26 static GLuint vertShader;
27 static GLuint program;
28 static GLuint texProgram;
31 static struct uniform_info Uniforms[] = {
32 { "LightPosition", 1, GL_FLOAT_VEC3, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 },
33 { "SurfaceColor", 1, GL_FLOAT_VEC3, { 0.8, 0.8, 0.2, 0 }, -1 },
34 { "BumpDensity", 1, GL_FLOAT, { 10.0, 0, 0, 0 }, -1 },
35 { "BumpSize", 1, GL_FLOAT, { 0.125, 0, 0, 0 }, -1 },
36 { "SpecularFactor", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 },
37 END_OF_UNIFORMS
40 static struct uniform_info TexUniforms[] = {
41 { "LightPosition", 1, GL_FLOAT_VEC3, { 0.57737, 0.57735, 0.57735, 0.0 }, -1 },
42 { "Tex", 1, GL_INT, { 0, 0, 0, 0 }, -1 },
43 { "BumpDensity", 1, GL_FLOAT, { 10.0, 0, 0, 0 }, -1 },
44 { "BumpSize", 1, GL_FLOAT, { 0.125, 0, 0, 0 }, -1 },
45 { "SpecularFactor", 1, GL_FLOAT, { 0.5, 0, 0, 0 }, -1 },
46 END_OF_UNIFORMS
49 static GLint win = 0;
51 static GLfloat xRot = 20.0f, yRot = 0.0f, zRot = 0.0f;
53 static GLint tangentAttrib;
55 static GLuint Texture;
57 static GLboolean Anim = GL_FALSE;
58 static GLboolean Textured = GL_FALSE;
61 static void
62 CheckError(int line)
64 GLenum err = glGetError();
65 if (err) {
66 printf("GL Error %s (0x%x) at line %d\n",
67 gluErrorString(err), (int) err, line);
72 * Draw a square, specifying normal and tangent vectors.
74 static void
75 Square(GLfloat size)
77 glNormal3f(0, 0, 1);
78 glVertexAttrib3f(tangentAttrib, 1, 0, 0);
79 glBegin(GL_POLYGON);
80 glTexCoord2f(0, 0); glVertex2f(-size, -size);
81 glTexCoord2f(1, 0); glVertex2f( size, -size);
82 glTexCoord2f(1, 1); glVertex2f( size, size);
83 glTexCoord2f(0, 1); glVertex2f(-size, size);
84 glEnd();
88 static void
89 Cube(GLfloat size)
91 /* +X */
92 glPushMatrix();
93 glRotatef(90, 0, 1, 0);
94 glTranslatef(0, 0, size);
95 Square(size);
96 glPopMatrix();
98 /* -X */
99 glPushMatrix();
100 glRotatef(-90, 0, 1, 0);
101 glTranslatef(0, 0, size);
102 Square(size);
103 glPopMatrix();
105 /* +Y */
106 glPushMatrix();
107 glRotatef(90, 1, 0, 0);
108 glTranslatef(0, 0, size);
109 Square(size);
110 glPopMatrix();
112 /* -Y */
113 glPushMatrix();
114 glRotatef(-90, 1, 0, 0);
115 glTranslatef(0, 0, size);
116 Square(size);
117 glPopMatrix();
120 /* +Z */
121 glPushMatrix();
122 glTranslatef(0, 0, size);
123 Square(size);
124 glPopMatrix();
126 /* -Z */
127 glPushMatrix();
128 glRotatef(180, 0, 1, 0);
129 glTranslatef(0, 0, size);
130 Square(size);
131 glPopMatrix();
136 static void
137 Idle(void)
139 GLint t = glutGet(GLUT_ELAPSED_TIME);
140 yRot = t * 0.05;
141 glutPostRedisplay();
145 static void
146 Redisplay(void)
148 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
150 glPushMatrix();
151 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
152 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
153 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
155 if (Textured)
156 glUseProgram(texProgram);
157 else
158 glUseProgram(program);
160 Cube(1.5);
162 glPopMatrix();
164 CheckError(__LINE__);
166 glutSwapBuffers();
170 static void
171 Reshape(int width, int height)
173 float ar = (float) width / (float) height;
174 glViewport(0, 0, width, height);
175 glMatrixMode(GL_PROJECTION);
176 glLoadIdentity();
177 glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0);
178 glMatrixMode(GL_MODELVIEW);
179 glLoadIdentity();
180 glTranslatef(0.0f, 0.0f, -15.0f);
184 static void
185 CleanUp(void)
187 glDeleteShader(fragShader);
188 glDeleteShader(fragTexShader);
189 glDeleteShader(vertShader);
190 glDeleteProgram(program);
191 glDeleteProgram(texProgram);
192 glutDestroyWindow(win);
196 static void
197 Key(unsigned char key, int x, int y)
199 const GLfloat step = 2.0;
200 (void) x;
201 (void) y;
203 switch(key) {
204 case 'a':
205 Anim = !Anim;
206 glutIdleFunc(Anim ? Idle : NULL);
207 break;
208 case 't':
209 Textured = !Textured;
210 break;
211 case 'z':
212 zRot += step;
213 break;
214 case 'Z':
215 zRot -= step;
216 break;
217 case 27:
218 CleanUp();
219 exit(0);
220 break;
222 glutPostRedisplay();
226 static void
227 SpecialKey(int key, int x, int y)
229 const GLfloat step = 2.0;
231 (void) x;
232 (void) y;
234 switch(key) {
235 case GLUT_KEY_UP:
236 xRot += step;
237 break;
238 case GLUT_KEY_DOWN:
239 xRot -= step;
240 break;
241 case GLUT_KEY_LEFT:
242 yRot -= step;
243 break;
244 case GLUT_KEY_RIGHT:
245 yRot += step;
246 break;
248 glutPostRedisplay();
252 static void
253 Init(void)
255 if (!ShadersSupported())
256 exit(1);
258 vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
259 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
260 program = LinkShaders(vertShader, fragShader);
262 glUseProgram(program);
264 assert(glIsProgram(program));
265 assert(glIsShader(fragShader));
266 assert(glIsShader(vertShader));
268 assert(glGetError() == 0);
270 CheckError(__LINE__);
272 SetUniformValues(program, Uniforms);
273 PrintUniforms(Uniforms);
275 CheckError(__LINE__);
277 tangentAttrib = glGetAttribLocation(program, "Tangent");
278 printf("Tangent Attrib: %d\n", tangentAttrib);
280 assert(tangentAttrib >= 0);
282 CheckError(__LINE__);
286 * As above, but fragment shader also uses a texture map.
288 fragTexShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragTexProgFile);
289 texProgram = LinkShaders(vertShader, fragTexShader);
290 glUseProgram(texProgram);
291 assert(glIsProgram(texProgram));
292 assert(glIsShader(fragTexShader));
293 SetUniformValues(texProgram, TexUniforms);
294 PrintUniforms(TexUniforms);
297 * Load tex image.
299 glGenTextures(1, &Texture);
300 glBindTexture(GL_TEXTURE_2D, Texture);
301 LoadRGBMipmaps(TextureFile, GL_RGB);
304 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
306 glEnable(GL_DEPTH_TEST);
308 glColor3f(1, 0, 0);
312 static void
313 ParseOptions(int argc, char *argv[])
315 int i;
316 for (i = 1; i < argc; i++) {
317 if (strcmp(argv[i], "-fs") == 0) {
318 FragProgFile = argv[++i];
320 else if (strcmp(argv[i], "-vs") == 0) {
321 VertProgFile = argv[++i];
323 else if (strcmp(argv[i], "-t") == 0) {
324 TextureFile = argv[++i];
331 main(int argc, char *argv[])
333 glutInit(&argc, argv);
334 glutInitWindowSize(400, 400);
335 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
336 win = glutCreateWindow(argv[0]);
337 glewInit();
338 glutReshapeFunc(Reshape);
339 glutKeyboardFunc(Key);
340 glutSpecialFunc(SpecialKey);
341 glutDisplayFunc(Redisplay);
342 ParseOptions(argc, argv);
343 Init();
344 glutMainLoop();
345 return 0;