draw: Remove dead code.
[mesa.git] / progs / glsl / bump.c
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 */
6
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 <GL/glut.h>
14 #include "shaderutil.h"
15 #include "readtex.h"
16
17
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 = "../images/tile.rgb";
22
23 /* program/shader objects */
24 static GLuint fragShader;
25 static GLuint fragTexShader;
26 static GLuint vertShader;
27 static GLuint program;
28 static GLuint texProgram;
29
30
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
38 };
39
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
47 };
48
49 static GLint win = 0;
50
51 static GLfloat xRot = 20.0f, yRot = 0.0f, zRot = 0.0f;
52
53 static GLint tangentAttrib;
54
55 static GLuint Texture;
56
57 static GLboolean Anim = GL_FALSE;
58 static GLboolean Textured = GL_FALSE;
59
60
61 static void
62 CheckError(int line)
63 {
64 GLenum err = glGetError();
65 if (err) {
66 printf("GL Error %s (0x%x) at line %d\n",
67 gluErrorString(err), (int) err, line);
68 }
69 }
70
71 /*
72 * Draw a square, specifying normal and tangent vectors.
73 */
74 static void
75 Square(GLfloat size)
76 {
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();
85 }
86
87
88 static void
89 Cube(GLfloat size)
90 {
91 /* +X */
92 glPushMatrix();
93 glRotatef(90, 0, 1, 0);
94 glTranslatef(0, 0, size);
95 Square(size);
96 glPopMatrix();
97
98 /* -X */
99 glPushMatrix();
100 glRotatef(-90, 0, 1, 0);
101 glTranslatef(0, 0, size);
102 Square(size);
103 glPopMatrix();
104
105 /* +Y */
106 glPushMatrix();
107 glRotatef(90, 1, 0, 0);
108 glTranslatef(0, 0, size);
109 Square(size);
110 glPopMatrix();
111
112 /* -Y */
113 glPushMatrix();
114 glRotatef(-90, 1, 0, 0);
115 glTranslatef(0, 0, size);
116 Square(size);
117 glPopMatrix();
118
119
120 /* +Z */
121 glPushMatrix();
122 glTranslatef(0, 0, size);
123 Square(size);
124 glPopMatrix();
125
126 /* -Z */
127 glPushMatrix();
128 glRotatef(180, 0, 1, 0);
129 glTranslatef(0, 0, size);
130 Square(size);
131 glPopMatrix();
132
133 }
134
135
136 static void
137 Idle(void)
138 {
139 GLint t = glutGet(GLUT_ELAPSED_TIME);
140 yRot = t * 0.05;
141 glutPostRedisplay();
142 }
143
144
145 static void
146 Redisplay(void)
147 {
148 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
149
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);
154
155 if (Textured)
156 glUseProgram(texProgram);
157 else
158 glUseProgram(program);
159
160 Cube(1.5);
161
162 glPopMatrix();
163
164 CheckError(__LINE__);
165
166 glutSwapBuffers();
167 }
168
169
170 static void
171 Reshape(int width, int height)
172 {
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);
181 }
182
183
184 static void
185 CleanUp(void)
186 {
187 glDeleteShader(fragShader);
188 glDeleteShader(fragTexShader);
189 glDeleteShader(vertShader);
190 glDeleteProgram(program);
191 glDeleteProgram(texProgram);
192 glutDestroyWindow(win);
193 }
194
195
196 static void
197 Key(unsigned char key, int x, int y)
198 {
199 const GLfloat step = 2.0;
200 (void) x;
201 (void) y;
202
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;
221 }
222 glutPostRedisplay();
223 }
224
225
226 static void
227 SpecialKey(int key, int x, int y)
228 {
229 const GLfloat step = 2.0;
230
231 (void) x;
232 (void) y;
233
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;
247 }
248 glutPostRedisplay();
249 }
250
251
252 static void
253 Init(void)
254 {
255 if (!ShadersSupported())
256 exit(1);
257
258 vertShader = CompileShaderFile(GL_VERTEX_SHADER, VertProgFile);
259 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, FragProgFile);
260 program = LinkShaders(vertShader, fragShader);
261
262 glUseProgram(program);
263
264 assert(glIsProgram(program));
265 assert(glIsShader(fragShader));
266 assert(glIsShader(vertShader));
267
268 assert(glGetError() == 0);
269
270 CheckError(__LINE__);
271
272 SetUniformValues(program, Uniforms);
273 PrintUniforms(Uniforms);
274
275 CheckError(__LINE__);
276
277 tangentAttrib = glGetAttribLocation(program, "Tangent");
278 printf("Tangent Attrib: %d\n", tangentAttrib);
279
280 assert(tangentAttrib >= 0);
281
282 CheckError(__LINE__);
283
284
285 /*
286 * As above, but fragment shader also uses a texture map.
287 */
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);
295
296 /*
297 * Load tex image.
298 */
299 glGenTextures(1, &Texture);
300 glBindTexture(GL_TEXTURE_2D, Texture);
301 LoadRGBMipmaps(TextureFile, GL_RGB);
302
303
304 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
305
306 glEnable(GL_DEPTH_TEST);
307
308 glColor3f(1, 0, 0);
309 }
310
311
312 static void
313 ParseOptions(int argc, char *argv[])
314 {
315 int i;
316 for (i = 1; i < argc; i++) {
317 if (strcmp(argv[i], "-fs") == 0) {
318 FragProgFile = argv[++i];
319 }
320 else if (strcmp(argv[i], "-vs") == 0) {
321 VertProgFile = argv[++i];
322 }
323 else if (strcmp(argv[i], "-t") == 0) {
324 TextureFile = argv[++i];
325 }
326 }
327 }
328
329
330 int
331 main(int argc, char *argv[])
332 {
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;
346 }
347