Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / progs / glsl / texdemo1.c
1 /**
2 * Test texturing with GL shading language.
3 *
4 * Copyright (C) 2007 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24
25
26 #include <assert.h>
27 #include <math.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "GL/glut.h"
32 #include "readtex.h"
33 #include "extfuncs.h"
34
35 static const char *Demo = "texdemo1";
36
37 static const char *ReflectVertFile = "reflect.vert.txt";
38 static const char *CubeFragFile = "cubemap.frag.txt";
39
40 static const char *SimpleVertFile = "simple.vert.txt";
41 static const char *SimpleTexFragFile = "shadowtex.frag.txt";
42
43 static const char *GroundImage = "../images/tile.rgb";
44
45 static GLuint Program1, Program2;
46
47 static GLfloat TexXrot = 0, TexYrot = 0;
48 static GLfloat Xrot = 20.0, Yrot = 20.0, Zrot = 0.0;
49 static GLfloat EyeDist = 10;
50 static GLboolean Anim = GL_TRUE;
51 static int win = 0;
52
53
54 struct uniform_info {
55 const char *name;
56 GLuint size;
57 GLint location;
58 GLenum type; /**< GL_FLOAT or GL_INT */
59 GLfloat value[4];
60 };
61
62 static struct uniform_info ReflectUniforms[] = {
63 { "cubeTex", 1, -1, GL_INT, { 0, 0, 0, 0 } },
64 { "lightPos", 3, -1, GL_FLOAT, { 10, 10, 20, 0 } },
65 { NULL, 0, 0, 0, { 0, 0, 0, 0 } }
66 };
67
68 static struct uniform_info SimpleUniforms[] = {
69 { "tex2d", 1, -1, GL_INT, { 1, 0, 0, 0 } },
70 { "lightPos", 3, -1, GL_FLOAT, { 10, 10, 20, 0 } },
71 { NULL, 0, 0, 0, { 0, 0, 0, 0 } }
72 };
73
74
75 static void
76 CheckError(int line)
77 {
78 GLenum err = glGetError();
79 if (err) {
80 printf("GL Error %s (0x%x) at line %d\n",
81 gluErrorString(err), (int) err, line);
82 }
83 }
84
85
86 static void
87 DrawGround(GLfloat size)
88 {
89 glPushMatrix();
90 glRotatef(90, 1, 0, 0);
91 glNormal3f(0, 0, 1);
92 glBegin(GL_POLYGON);
93 glTexCoord2f(-2, -2); glVertex2f(-size, -size);
94 glTexCoord2f( 2, -2); glVertex2f( size, -size);
95 glTexCoord2f( 2, 2); glVertex2f( size, size);
96 glTexCoord2f(-2, 2); glVertex2f(-size, size);
97 glEnd();
98 glPopMatrix();
99 }
100
101
102 static void
103 draw(void)
104 {
105 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
106
107 glEnable(GL_TEXTURE_2D);
108
109 glPushMatrix(); /* modelview matrix */
110 glTranslatef(0.0, 0.0, -EyeDist);
111 glRotatef(Xrot, 1, 0, 0);
112 glRotatef(Yrot, 0, 1, 0);
113 glRotatef(Zrot, 0, 0, 1);
114
115 /* sphere w/ reflection map */
116 glPushMatrix();
117 glTranslatef(0, 1, 0);
118 glUseProgram_func(Program1);
119
120 /* setup texture matrix */
121 glActiveTexture(GL_TEXTURE0);
122 glMatrixMode(GL_TEXTURE);
123 glLoadIdentity();
124 glRotatef(-TexYrot, 0, 1, 0);
125 glRotatef(-TexXrot, 1, 0, 0);
126
127 glEnable(GL_TEXTURE_GEN_S);
128 glEnable(GL_TEXTURE_GEN_T);
129 glEnable(GL_TEXTURE_GEN_R);
130 glutSolidSphere(2.0, 20, 20);
131
132 glLoadIdentity(); /* texture matrix */
133 glMatrixMode(GL_MODELVIEW);
134 glPopMatrix();
135
136 /* ground */
137 glUseProgram_func(Program2);
138 glTranslatef(0, -1.0, 0);
139 DrawGround(5);
140
141 glPopMatrix();
142
143 glutSwapBuffers();
144 }
145
146
147 static void
148 idle(void)
149 {
150 GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
151 TexYrot = t;
152 glutPostRedisplay();
153 }
154
155
156 static void
157 key(unsigned char k, int x, int y)
158 {
159 (void) x;
160 (void) y;
161 switch (k) {
162 case ' ':
163 case 'a':
164 Anim = !Anim;
165 if (Anim)
166 glutIdleFunc(idle);
167 else
168 glutIdleFunc(NULL);
169 break;
170 case 'z':
171 EyeDist -= 0.5;
172 if (EyeDist < 6.0)
173 EyeDist = 6.0;
174 break;
175 case 'Z':
176 EyeDist += 0.5;
177 if (EyeDist > 90.0)
178 EyeDist = 90;
179 break;
180 case 27:
181 glutDestroyWindow(win);
182 exit(0);
183 }
184 glutPostRedisplay();
185 }
186
187
188 static void
189 specialkey(int key, int x, int y)
190 {
191 GLfloat step = 2.0;
192 (void) x;
193 (void) y;
194 switch (key) {
195 case GLUT_KEY_UP:
196 Xrot += step;
197 break;
198 case GLUT_KEY_DOWN:
199 Xrot -= step;
200 break;
201 case GLUT_KEY_LEFT:
202 Yrot -= step;
203 break;
204 case GLUT_KEY_RIGHT:
205 Yrot += step;
206 break;
207 }
208 glutPostRedisplay();
209 }
210
211
212 /* new window size or exposure */
213 static void
214 Reshape(int width, int height)
215 {
216 GLfloat ar = (float) width / (float) height;
217 glViewport(0, 0, (GLint)width, (GLint)height);
218 glMatrixMode(GL_PROJECTION);
219 glLoadIdentity();
220 glFrustum(-2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0);
221 glMatrixMode(GL_MODELVIEW);
222 glLoadIdentity();
223 }
224
225
226 static void
227 InitCheckers(void)
228 {
229 #define CUBE_TEX_SIZE 64
230 GLubyte image[CUBE_TEX_SIZE][CUBE_TEX_SIZE][3];
231 static const GLubyte colors[6][3] = {
232 { 255, 0, 0 }, /* face 0 - red */
233 { 0, 255, 255 }, /* face 1 - cyan */
234 { 0, 255, 0 }, /* face 2 - green */
235 { 255, 0, 255 }, /* face 3 - purple */
236 { 0, 0, 255 }, /* face 4 - blue */
237 { 255, 255, 0 } /* face 5 - yellow */
238 };
239 static const GLenum targets[6] = {
240 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
241 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
242 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
243 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
244 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
245 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
246 };
247
248 GLint i, j, f;
249
250 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
251
252 /* make colored checkerboard cube faces */
253 for (f = 0; f < 6; f++) {
254 for (i = 0; i < CUBE_TEX_SIZE; i++) {
255 for (j = 0; j < CUBE_TEX_SIZE; j++) {
256 if ((i/4 + j/4) & 1) {
257 image[i][j][0] = colors[f][0];
258 image[i][j][1] = colors[f][1];
259 image[i][j][2] = colors[f][2];
260 }
261 else {
262 image[i][j][0] = 255;
263 image[i][j][1] = 255;
264 image[i][j][2] = 255;
265 }
266 }
267 }
268
269 glTexImage2D(targets[f], 0, GL_RGB, CUBE_TEX_SIZE, CUBE_TEX_SIZE, 0,
270 GL_RGB, GL_UNSIGNED_BYTE, image);
271 }
272 }
273
274
275 static void
276 LoadFace(GLenum target, const char *filename,
277 GLboolean flipTB, GLboolean flipLR)
278 {
279 GLint w, h;
280 GLenum format;
281 GLubyte *img = LoadRGBImage(filename, &w, &h, &format);
282 if (!img) {
283 printf("Error: couldn't load texture image %s\n", filename);
284 exit(1);
285 }
286 assert(format == GL_RGB);
287
288 /* <sigh> the way the texture cube mapping works, we have to flip
289 * images to make things look right.
290 */
291 if (flipTB) {
292 const int stride = 3 * w;
293 GLubyte temp[3*1024];
294 int i;
295 for (i = 0; i < h / 2; i++) {
296 memcpy(temp, img + i * stride, stride);
297 memcpy(img + i * stride, img + (h - i - 1) * stride, stride);
298 memcpy(img + (h - i - 1) * stride, temp, stride);
299 }
300 }
301 if (flipLR) {
302 const int stride = 3 * w;
303 GLubyte temp[3];
304 GLubyte *row;
305 int i, j;
306 for (i = 0; i < h; i++) {
307 row = img + i * stride;
308 for (j = 0; j < w / 2; j++) {
309 int k = w - j - 1;
310 temp[0] = row[j*3+0];
311 temp[1] = row[j*3+1];
312 temp[2] = row[j*3+2];
313 row[j*3+0] = row[k*3+0];
314 row[j*3+1] = row[k*3+1];
315 row[j*3+2] = row[k*3+2];
316 row[k*3+0] = temp[0];
317 row[k*3+1] = temp[1];
318 row[k*3+2] = temp[2];
319 }
320 }
321 }
322
323 gluBuild2DMipmaps(target, GL_RGB, w, h, format, GL_UNSIGNED_BYTE, img);
324 free(img);
325 }
326
327
328 static void
329 LoadEnvmaps(void)
330 {
331 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, "right.rgb", GL_TRUE, GL_FALSE);
332 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, "left.rgb", GL_TRUE, GL_FALSE);
333 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, "top.rgb", GL_FALSE, GL_TRUE);
334 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, "bottom.rgb", GL_FALSE, GL_TRUE);
335 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, "front.rgb", GL_TRUE, GL_FALSE);
336 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, "back.rgb", GL_TRUE, GL_FALSE);
337 }
338
339
340 static void
341 InitTextures(GLboolean useImageFiles)
342 {
343 GLenum filter;
344
345 /*
346 * Env map
347 */
348 glActiveTexture(GL_TEXTURE0);
349 glBindTexture(GL_TEXTURE_CUBE_MAP, 1);
350 if (useImageFiles) {
351 LoadEnvmaps();
352 filter = GL_LINEAR;
353 }
354 else {
355 InitCheckers();
356 filter = GL_NEAREST;
357 }
358 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, filter);
359 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, filter);
360 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
361 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
362
363 /*
364 * Ground texture
365 */
366 {
367 GLint imgWidth, imgHeight;
368 GLenum imgFormat;
369 GLubyte *image = NULL;
370
371 image = LoadRGBImage(GroundImage, &imgWidth, &imgHeight, &imgFormat);
372 if (!image) {
373 printf("Couldn't read %s\n", GroundImage);
374 exit(0);
375 }
376
377 glActiveTexture(GL_TEXTURE1);
378 glBindTexture(GL_TEXTURE_2D, 2);
379 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imgWidth, imgHeight,
380 imgFormat, GL_UNSIGNED_BYTE, image);
381 free(image);
382
383 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
384 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
385 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
386 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
387 }
388 }
389
390
391 static void
392 LoadAndCompileShader(GLuint shader, const char *text)
393 {
394 GLint stat;
395
396 glShaderSource_func(shader, 1, (const GLchar **) &text, NULL);
397
398 glCompileShader_func(shader);
399
400 glGetShaderiv_func(shader, GL_COMPILE_STATUS, &stat);
401 if (!stat) {
402 GLchar log[1000];
403 GLsizei len;
404 glGetShaderInfoLog_func(shader, 1000, &len, log);
405 fprintf(stderr, "%s: problem compiling shader: %s\n", Demo, log);
406 exit(1);
407 }
408 else {
409 printf("Shader compiled OK\n");
410 }
411 }
412
413
414 /**
415 * Read a shader from a file.
416 */
417 static void
418 ReadShader(GLuint shader, const char *filename)
419 {
420 const int max = 100*1000;
421 int n;
422 char *buffer = (char*) malloc(max);
423 FILE *f = fopen(filename, "r");
424 if (!f) {
425 fprintf(stderr, "%s: Unable to open shader file %s\n", Demo, filename);
426 exit(1);
427 }
428
429 n = fread(buffer, 1, max, f);
430 printf("%s: read %d bytes from shader file %s\n", Demo, n, filename);
431 if (n > 0) {
432 buffer[n] = 0;
433 LoadAndCompileShader(shader, buffer);
434 }
435
436 fclose(f);
437 free(buffer);
438 }
439
440
441 static void
442 CheckLink(GLuint prog)
443 {
444 GLint stat;
445 glGetProgramiv_func(prog, GL_LINK_STATUS, &stat);
446 if (!stat) {
447 GLchar log[1000];
448 GLsizei len;
449 glGetProgramInfoLog_func(prog, 1000, &len, log);
450 fprintf(stderr, "Linker error:\n%s\n", log);
451 }
452 else {
453 fprintf(stderr, "Link success!\n");
454 }
455 }
456
457
458 static GLuint
459 CreateProgram(const char *vertProgFile, const char *fragProgFile,
460 struct uniform_info *uniforms)
461 {
462 GLuint fragShader = 0, vertShader = 0, program = 0;
463 GLint i;
464
465 program = glCreateProgram_func();
466 if (vertProgFile) {
467 vertShader = glCreateShader_func(GL_VERTEX_SHADER);
468 ReadShader(vertShader, vertProgFile);
469 glAttachShader_func(program, vertShader);
470 }
471
472 if (fragProgFile) {
473 fragShader = glCreateShader_func(GL_FRAGMENT_SHADER);
474 ReadShader(fragShader, fragProgFile);
475 glAttachShader_func(program, fragShader);
476 }
477
478 glLinkProgram_func(program);
479 CheckLink(program);
480
481 glUseProgram_func(program);
482
483 assert(glIsProgram_func(program));
484 assert(glIsShader_func(fragShader));
485 assert(glIsShader_func(vertShader));
486
487 CheckError(__LINE__);
488 for (i = 0; uniforms[i].name; i++) {
489 uniforms[i].location
490 = glGetUniformLocation_func(program, uniforms[i].name);
491 printf("Uniform %s location: %d\n", uniforms[i].name,
492 uniforms[i].location);
493
494 switch (uniforms[i].size) {
495 case 1:
496 if (uniforms[i].type == GL_INT)
497 glUniform1i_func(uniforms[i].location,
498 (GLint) uniforms[i].value[0]);
499 else
500 glUniform1fv_func(uniforms[i].location, 1, uniforms[i].value);
501 break;
502 case 2:
503 glUniform2fv_func(uniforms[i].location, 1, uniforms[i].value);
504 break;
505 case 3:
506 glUniform3fv_func(uniforms[i].location, 1, uniforms[i].value);
507 break;
508 case 4:
509 glUniform4fv_func(uniforms[i].location, 1, uniforms[i].value);
510 break;
511 default:
512 abort();
513 }
514 }
515
516 CheckError(__LINE__);
517
518 return program;
519 }
520
521
522 static void
523 InitPrograms(void)
524 {
525 Program1 = CreateProgram(ReflectVertFile, CubeFragFile, ReflectUniforms);
526 Program2 = CreateProgram(SimpleVertFile, SimpleTexFragFile, SimpleUniforms);
527 }
528
529
530 static void
531 Init(GLboolean useImageFiles)
532 {
533 const char *version = (const char *) glGetString(GL_VERSION);
534
535 if (version[0] != '2' || version[1] != '.') {
536 printf("Warning: this program expects OpenGL 2.0\n");
537 /*exit(1);*/
538 }
539 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
540
541 GetExtensionFuncs();
542
543 InitTextures(useImageFiles);
544 InitPrograms();
545
546 glEnable(GL_DEPTH_TEST);
547
548 glClearColor(.6, .6, .9, 0);
549 glColor3f(1.0, 1.0, 1.0);
550 }
551
552
553 int
554 main(int argc, char *argv[])
555 {
556 glutInit(&argc, argv);
557 glutInitWindowSize(500, 400);
558 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
559 win = glutCreateWindow(Demo);
560 glutReshapeFunc(Reshape);
561 glutKeyboardFunc(key);
562 glutSpecialFunc(specialkey);
563 glutDisplayFunc(draw);
564 if (Anim)
565 glutIdleFunc(idle);
566 if (argc > 1 && strcmp(argv[1] , "-i") == 0)
567 Init(1);
568 else
569 Init(0);
570 glutMainLoop();
571 return 0;
572 }