Merge branch 'gallium-0.1' into gallium-tex-surfaces
[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/glut.h>
13 #include <GL/glu.h>
14 #include <GL/glext.h>
15 #include "extfuncs.h"
16
17
18 static char *FragProgFile = "CH11-bumpmap.frag.txt";
19 static char *VertProgFile = "CH11-bumpmap.vert.txt";
20
21 /* program/shader objects */
22 static GLuint fragShader;
23 static GLuint vertShader;
24 static GLuint program;
25
26
27 struct uniform_info {
28 const char *name;
29 GLuint size;
30 GLint location;
31 GLfloat value[4];
32 };
33
34 static struct uniform_info Uniforms[] = {
35 { "LightPosition", 3, -1, { 0.57737, 0.57735, 0.57735, 0.0 } },
36 { "SurfaceColor", 3, -1, { 0.8, 0.8, 0.2, 0 } },
37 { "BumpDensity", 1, -1, { 10.0, 0, 0, 0 } },
38 { "BumpSize", 1, -1, { 0.125, 0, 0, 0 } },
39 { "SpecularFactor", 1, -1, { 0.5, 0, 0, 0 } },
40 { NULL, 0, 0, { 0, 0, 0, 0 } }
41 };
42
43 static GLint win = 0;
44
45 static GLfloat xRot = 20.0f, yRot = 0.0f, zRot = 0.0f;
46
47 static GLuint tangentAttrib;
48
49 static GLboolean Anim = GL_FALSE;
50
51
52 static void
53 CheckError(int line)
54 {
55 GLenum err = glGetError();
56 if (err) {
57 printf("GL Error %s (0x%x) at line %d\n",
58 gluErrorString(err), (int) err, line);
59 }
60 }
61
62 /*
63 * Draw a square, specifying normal and tangent vectors.
64 */
65 static void
66 Square(GLfloat size)
67 {
68 glNormal3f(0, 0, 1);
69 glVertexAttrib3f_func(tangentAttrib, 1, 0, 0);
70 glBegin(GL_POLYGON);
71 glTexCoord2f(0, 0); glVertex2f(-size, -size);
72 glTexCoord2f(1, 0); glVertex2f( size, -size);
73 glTexCoord2f(1, 1); glVertex2f( size, size);
74 glTexCoord2f(0, 1); glVertex2f(-size, size);
75 glEnd();
76 }
77
78
79 static void
80 Cube(GLfloat size)
81 {
82 /* +X */
83 glPushMatrix();
84 glRotatef(90, 0, 1, 0);
85 glTranslatef(0, 0, size);
86 Square(size);
87 glPopMatrix();
88
89 /* -X */
90 glPushMatrix();
91 glRotatef(-90, 0, 1, 0);
92 glTranslatef(0, 0, size);
93 Square(size);
94 glPopMatrix();
95
96 /* +Y */
97 glPushMatrix();
98 glRotatef(90, 1, 0, 0);
99 glTranslatef(0, 0, size);
100 Square(size);
101 glPopMatrix();
102
103 /* -Y */
104 glPushMatrix();
105 glRotatef(-90, 1, 0, 0);
106 glTranslatef(0, 0, size);
107 Square(size);
108 glPopMatrix();
109
110
111 /* +Z */
112 glPushMatrix();
113 glTranslatef(0, 0, size);
114 Square(size);
115 glPopMatrix();
116
117 /* -Z */
118 glPushMatrix();
119 glRotatef(180, 0, 1, 0);
120 glTranslatef(0, 0, size);
121 Square(size);
122 glPopMatrix();
123
124 }
125
126
127 static void
128 Idle(void)
129 {
130 GLint t = glutGet(GLUT_ELAPSED_TIME);
131 yRot = t * 0.05;
132 glutPostRedisplay();
133 }
134
135
136 static void
137 Redisplay(void)
138 {
139 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
140
141 glPushMatrix();
142 glRotatef(xRot, 1.0f, 0.0f, 0.0f);
143 glRotatef(yRot, 0.0f, 1.0f, 0.0f);
144 glRotatef(zRot, 0.0f, 0.0f, 1.0f);
145
146 Cube(1.5);
147
148 glPopMatrix();
149
150 glFinish();
151 glFlush();
152
153 CheckError(__LINE__);
154
155 glutSwapBuffers();
156 }
157
158
159 static void
160 Reshape(int width, int height)
161 {
162 glViewport(0, 0, width, height);
163 glMatrixMode(GL_PROJECTION);
164 glLoadIdentity();
165 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
166 glMatrixMode(GL_MODELVIEW);
167 glLoadIdentity();
168 glTranslatef(0.0f, 0.0f, -15.0f);
169 }
170
171
172 static void
173 CleanUp(void)
174 {
175 glDeleteShader_func(fragShader);
176 glDeleteShader_func(vertShader);
177 glDeleteProgram_func(program);
178 glutDestroyWindow(win);
179 }
180
181
182 static void
183 Key(unsigned char key, int x, int y)
184 {
185 const GLfloat step = 2.0;
186 (void) x;
187 (void) y;
188
189 switch(key) {
190 case 'a':
191 Anim = !Anim;
192 glutIdleFunc(Anim ? Idle : NULL);
193 break;
194 case 'z':
195 zRot += step;
196 break;
197 case 'Z':
198 zRot -= step;
199 break;
200 case 27:
201 CleanUp();
202 exit(0);
203 break;
204 }
205 glutPostRedisplay();
206 }
207
208
209 static void
210 SpecialKey(int key, int x, int y)
211 {
212 const GLfloat step = 2.0;
213
214 (void) x;
215 (void) y;
216
217 switch(key) {
218 case GLUT_KEY_UP:
219 xRot += step;
220 break;
221 case GLUT_KEY_DOWN:
222 xRot -= step;
223 break;
224 case GLUT_KEY_LEFT:
225 yRot -= step;
226 break;
227 case GLUT_KEY_RIGHT:
228 yRot += step;
229 break;
230 }
231 glutPostRedisplay();
232 }
233
234
235
236 static void
237 LoadAndCompileShader(GLuint shader, const char *text)
238 {
239 GLint stat;
240
241 glShaderSource_func(shader, 1, (const GLchar **) &text, NULL);
242
243 glCompileShader_func(shader);
244
245 glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat);
246 if (!stat) {
247 GLchar log[1000];
248 GLsizei len;
249 glGetShaderInfoLog_func(shader, 1000, &len, log);
250 fprintf(stderr, "brick: problem compiling shader: %s\n", log);
251 exit(1);
252 }
253 else {
254 printf("Shader compiled OK\n");
255 }
256 }
257
258
259 /**
260 * Read a shader from a file.
261 */
262 static void
263 ReadShader(GLuint shader, const char *filename)
264 {
265 const int max = 100*1000;
266 int n;
267 char *buffer = (char*) malloc(max);
268 FILE *f = fopen(filename, "r");
269 if (!f) {
270 fprintf(stderr, "brick: Unable to open shader file %s\n", filename);
271 exit(1);
272 }
273
274 n = fread(buffer, 1, max, f);
275 printf("brick: read %d bytes from shader file %s\n", n, filename);
276 if (n > 0) {
277 buffer[n] = 0;
278 LoadAndCompileShader(shader, buffer);
279 }
280
281 fclose(f);
282 free(buffer);
283 }
284
285
286 static void
287 CheckLink(GLuint prog)
288 {
289 GLint stat;
290 glGetProgramiv_func(prog, GL_LINK_STATUS, &stat);
291 if (!stat) {
292 GLchar log[1000];
293 GLsizei len;
294 glGetProgramInfoLog_func(prog, 1000, &len, log);
295 fprintf(stderr, "Linker error:\n%s\n", log);
296 }
297 else {
298 fprintf(stderr, "Link success!\n");
299 }
300 }
301
302
303 static void
304 Init(void)
305 {
306 const char *version;
307 GLint i;
308
309 version = (const char *) glGetString(GL_VERSION);
310 if (version[0] != '2' || version[1] != '.') {
311 printf("Warning: this program expects OpenGL 2.0\n");
312 /*exit(1);*/
313 }
314 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
315
316 GetExtensionFuncs();
317
318 vertShader = glCreateShader_func(GL_VERTEX_SHADER);
319 ReadShader(vertShader, VertProgFile);
320
321 fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
322 ReadShader(fragShader, FragProgFile);
323
324 program = glCreateProgram_func();
325 glAttachShader_func(program, fragShader);
326 glAttachShader_func(program, vertShader);
327 glLinkProgram_func(program);
328 CheckLink(program);
329 glUseProgram_func(program);
330
331 assert(glIsProgram_func(program));
332 assert(glIsShader_func(fragShader));
333 assert(glIsShader_func(vertShader));
334
335 assert(glGetError() == 0);
336
337 CheckError(__LINE__);
338
339 for (i = 0; Uniforms[i].name; i++) {
340 Uniforms[i].location
341 = glGetUniformLocation_func(program, Uniforms[i].name);
342 printf("Uniform %s location: %d\n", Uniforms[i].name,
343 Uniforms[i].location);
344 switch (Uniforms[i].size) {
345 case 1:
346 glUniform1fv_func(Uniforms[i].location, 1, Uniforms[i].value);
347 break;
348 case 2:
349 glUniform2fv_func(Uniforms[i].location, 1, Uniforms[i].value);
350 break;
351 case 3:
352 glUniform3fv_func(Uniforms[i].location, 1, Uniforms[i].value);
353 break;
354 case 4:
355 glUniform4fv_func(Uniforms[i].location, 1, Uniforms[i].value);
356 break;
357 default:
358 abort();
359 }
360 }
361
362 CheckError(__LINE__);
363
364 tangentAttrib = glGetAttribLocation_func(program, "Tangent");
365 printf("Tangent Attrib: %d\n", tangentAttrib);
366
367 assert(tangentAttrib >= 0);
368
369 CheckError(__LINE__);
370
371 glClearColor(0.4f, 0.4f, 0.8f, 0.0f);
372
373 glEnable(GL_DEPTH_TEST);
374
375 glColor3f(1, 0, 0);
376 }
377
378
379 static void
380 ParseOptions(int argc, char *argv[])
381 {
382 int i;
383 for (i = 1; i < argc; i++) {
384 if (strcmp(argv[i], "-fs") == 0) {
385 FragProgFile = argv[i+1];
386 }
387 else if (strcmp(argv[i], "-vs") == 0) {
388 VertProgFile = argv[i+1];
389 }
390 }
391 }
392
393
394 int
395 main(int argc, char *argv[])
396 {
397 glutInit(&argc, argv);
398 glutInitWindowPosition( 0, 0);
399 glutInitWindowSize(400, 400);
400 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
401 win = glutCreateWindow(argv[0]);
402 glutReshapeFunc(Reshape);
403 glutKeyboardFunc(Key);
404 glutSpecialFunc(SpecialKey);
405 glutDisplayFunc(Redisplay);
406 ParseOptions(argc, argv);
407 Init();
408 glutMainLoop();
409 return 0;
410 }
411