blob: 41010746eeabb4c151e6cbe4630fdf7298f80840 [file] [log] [blame]
Brianedf24e62007-02-03 11:36:16 -07001/**
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"
Brian2dca3372008-04-09 22:28:23 -060034#include "shaderutil.h"
Brianedf24e62007-02-03 11:36:16 -070035
36static const char *Demo = "texdemo1";
37
Brian Paulc0dd9122008-08-16 09:36:46 -060038static const char *ReflectVertFile = "reflect.vert";
39static const char *CubeFragFile = "cubemap.frag";
Brianedf24e62007-02-03 11:36:16 -070040
Brian Paulc0dd9122008-08-16 09:36:46 -060041static const char *SimpleVertFile = "simple.vert";
42static const char *SimpleTexFragFile = "shadowtex.frag";
Brianedf24e62007-02-03 11:36:16 -070043
44static const char *GroundImage = "../images/tile.rgb";
45
46static GLuint Program1, Program2;
47
48static GLfloat TexXrot = 0, TexYrot = 0;
49static GLfloat Xrot = 20.0, Yrot = 20.0, Zrot = 0.0;
50static GLfloat EyeDist = 10;
51static GLboolean Anim = GL_TRUE;
52
53
Brianedf24e62007-02-03 11:36:16 -070054static struct uniform_info ReflectUniforms[] = {
Brian2dca3372008-04-09 22:28:23 -060055 { "cubeTex", 1, GL_INT, { 0, 0, 0, 0 }, -1 },
56 { "lightPos", 3, GL_FLOAT, { 10, 10, 20, 0 }, -1 },
57 END_OF_UNIFORMS
Brianedf24e62007-02-03 11:36:16 -070058};
59
60static struct uniform_info SimpleUniforms[] = {
Brian2dca3372008-04-09 22:28:23 -060061 { "tex2d", 1, GL_INT, { 1, 0, 0, 0 }, -1 },
62 { "lightPos", 3, GL_FLOAT, { 10, 10, 20, 0 }, -1 },
63 END_OF_UNIFORMS
Brianedf24e62007-02-03 11:36:16 -070064};
65
66
67static void
Brianedf24e62007-02-03 11:36:16 -070068DrawGround(GLfloat size)
69{
70 glPushMatrix();
71 glRotatef(90, 1, 0, 0);
72 glNormal3f(0, 0, 1);
73 glBegin(GL_POLYGON);
74 glTexCoord2f(-2, -2); glVertex2f(-size, -size);
75 glTexCoord2f( 2, -2); glVertex2f( size, -size);
76 glTexCoord2f( 2, 2); glVertex2f( size, size);
77 glTexCoord2f(-2, 2); glVertex2f(-size, size);
78 glEnd();
79 glPopMatrix();
80}
81
82
83static void
84draw(void)
85{
86 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
87
88 glEnable(GL_TEXTURE_2D);
89
90 glPushMatrix(); /* modelview matrix */
91 glTranslatef(0.0, 0.0, -EyeDist);
92 glRotatef(Xrot, 1, 0, 0);
93 glRotatef(Yrot, 0, 1, 0);
94 glRotatef(Zrot, 0, 0, 1);
95
96 /* sphere w/ reflection map */
97 glPushMatrix();
98 glTranslatef(0, 1, 0);
99 glUseProgram_func(Program1);
100
101 /* setup texture matrix */
102 glActiveTexture(GL_TEXTURE0);
103 glMatrixMode(GL_TEXTURE);
104 glLoadIdentity();
105 glRotatef(-TexYrot, 0, 1, 0);
106 glRotatef(-TexXrot, 1, 0, 0);
107
108 glEnable(GL_TEXTURE_GEN_S);
109 glEnable(GL_TEXTURE_GEN_T);
110 glEnable(GL_TEXTURE_GEN_R);
111 glutSolidSphere(2.0, 20, 20);
112
113 glLoadIdentity(); /* texture matrix */
114 glMatrixMode(GL_MODELVIEW);
115 glPopMatrix();
116
117 /* ground */
118 glUseProgram_func(Program2);
119 glTranslatef(0, -1.0, 0);
120 DrawGround(5);
121
122 glPopMatrix();
123
124 glutSwapBuffers();
125}
126
127
128static void
129idle(void)
130{
131 GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
132 TexYrot = t;
133 glutPostRedisplay();
134}
135
136
137static void
138key(unsigned char k, int x, int y)
139{
140 (void) x;
141 (void) y;
142 switch (k) {
143 case ' ':
144 case 'a':
145 Anim = !Anim;
146 if (Anim)
147 glutIdleFunc(idle);
148 else
149 glutIdleFunc(NULL);
150 break;
151 case 'z':
152 EyeDist -= 0.5;
153 if (EyeDist < 6.0)
154 EyeDist = 6.0;
155 break;
156 case 'Z':
157 EyeDist += 0.5;
158 if (EyeDist > 90.0)
159 EyeDist = 90;
160 break;
161 case 27:
162 exit(0);
163 }
164 glutPostRedisplay();
165}
166
167
168static void
169specialkey(int key, int x, int y)
170{
171 GLfloat step = 2.0;
172 (void) x;
173 (void) y;
174 switch (key) {
175 case GLUT_KEY_UP:
176 Xrot += step;
177 break;
178 case GLUT_KEY_DOWN:
179 Xrot -= step;
180 break;
181 case GLUT_KEY_LEFT:
182 Yrot -= step;
183 break;
184 case GLUT_KEY_RIGHT:
185 Yrot += step;
186 break;
187 }
188 glutPostRedisplay();
189}
190
191
192/* new window size or exposure */
193static void
194Reshape(int width, int height)
195{
196 GLfloat ar = (float) width / (float) height;
197 glViewport(0, 0, (GLint)width, (GLint)height);
198 glMatrixMode(GL_PROJECTION);
199 glLoadIdentity();
200 glFrustum(-2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0);
201 glMatrixMode(GL_MODELVIEW);
202 glLoadIdentity();
203}
204
205
206static void
207InitCheckers(void)
208{
209#define CUBE_TEX_SIZE 64
210 GLubyte image[CUBE_TEX_SIZE][CUBE_TEX_SIZE][3];
211 static const GLubyte colors[6][3] = {
212 { 255, 0, 0 }, /* face 0 - red */
213 { 0, 255, 255 }, /* face 1 - cyan */
214 { 0, 255, 0 }, /* face 2 - green */
215 { 255, 0, 255 }, /* face 3 - purple */
216 { 0, 0, 255 }, /* face 4 - blue */
217 { 255, 255, 0 } /* face 5 - yellow */
218 };
219 static const GLenum targets[6] = {
220 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
221 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
222 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
223 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
224 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
225 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
226 };
227
228 GLint i, j, f;
229
230 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
231
232 /* make colored checkerboard cube faces */
233 for (f = 0; f < 6; f++) {
234 for (i = 0; i < CUBE_TEX_SIZE; i++) {
235 for (j = 0; j < CUBE_TEX_SIZE; j++) {
236 if ((i/4 + j/4) & 1) {
237 image[i][j][0] = colors[f][0];
238 image[i][j][1] = colors[f][1];
239 image[i][j][2] = colors[f][2];
240 }
241 else {
242 image[i][j][0] = 255;
243 image[i][j][1] = 255;
244 image[i][j][2] = 255;
245 }
246 }
247 }
248
249 glTexImage2D(targets[f], 0, GL_RGB, CUBE_TEX_SIZE, CUBE_TEX_SIZE, 0,
250 GL_RGB, GL_UNSIGNED_BYTE, image);
251 }
252}
253
254
255static void
256LoadFace(GLenum target, const char *filename,
257 GLboolean flipTB, GLboolean flipLR)
258{
259 GLint w, h;
260 GLenum format;
261 GLubyte *img = LoadRGBImage(filename, &w, &h, &format);
262 if (!img) {
263 printf("Error: couldn't load texture image %s\n", filename);
264 exit(1);
265 }
266 assert(format == GL_RGB);
267
268 /* <sigh> the way the texture cube mapping works, we have to flip
269 * images to make things look right.
270 */
271 if (flipTB) {
272 const int stride = 3 * w;
273 GLubyte temp[3*1024];
274 int i;
275 for (i = 0; i < h / 2; i++) {
276 memcpy(temp, img + i * stride, stride);
277 memcpy(img + i * stride, img + (h - i - 1) * stride, stride);
278 memcpy(img + (h - i - 1) * stride, temp, stride);
279 }
280 }
281 if (flipLR) {
282 const int stride = 3 * w;
283 GLubyte temp[3];
284 GLubyte *row;
285 int i, j;
286 for (i = 0; i < h; i++) {
287 row = img + i * stride;
288 for (j = 0; j < w / 2; j++) {
289 int k = w - j - 1;
290 temp[0] = row[j*3+0];
291 temp[1] = row[j*3+1];
292 temp[2] = row[j*3+2];
293 row[j*3+0] = row[k*3+0];
294 row[j*3+1] = row[k*3+1];
295 row[j*3+2] = row[k*3+2];
296 row[k*3+0] = temp[0];
297 row[k*3+1] = temp[1];
298 row[k*3+2] = temp[2];
299 }
300 }
301 }
302
303 gluBuild2DMipmaps(target, GL_RGB, w, h, format, GL_UNSIGNED_BYTE, img);
304 free(img);
305}
306
307
308static void
309LoadEnvmaps(void)
310{
311 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, "right.rgb", GL_TRUE, GL_FALSE);
312 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, "left.rgb", GL_TRUE, GL_FALSE);
313 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, "top.rgb", GL_FALSE, GL_TRUE);
314 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB, "bottom.rgb", GL_FALSE, GL_TRUE);
315 LoadFace(GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB, "front.rgb", GL_TRUE, GL_FALSE);
316 LoadFace(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB, "back.rgb", GL_TRUE, GL_FALSE);
317}
318
319
320static void
321InitTextures(GLboolean useImageFiles)
322{
323 GLenum filter;
324
325 /*
326 * Env map
327 */
328 glActiveTexture(GL_TEXTURE0);
329 glBindTexture(GL_TEXTURE_CUBE_MAP, 1);
330 if (useImageFiles) {
331 LoadEnvmaps();
332 filter = GL_LINEAR;
333 }
334 else {
335 InitCheckers();
336 filter = GL_NEAREST;
337 }
338 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, filter);
339 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, filter);
340 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
341 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
342
343 /*
344 * Ground texture
345 */
346 {
347 GLint imgWidth, imgHeight;
348 GLenum imgFormat;
349 GLubyte *image = NULL;
350
351 image = LoadRGBImage(GroundImage, &imgWidth, &imgHeight, &imgFormat);
352 if (!image) {
353 printf("Couldn't read %s\n", GroundImage);
354 exit(0);
355 }
356
357 glActiveTexture(GL_TEXTURE1);
358 glBindTexture(GL_TEXTURE_2D, 2);
359 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imgWidth, imgHeight,
360 imgFormat, GL_UNSIGNED_BYTE, image);
361 free(image);
362
363 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
364 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
365 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
366 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
367 }
368}
369
370
Brianedf24e62007-02-03 11:36:16 -0700371static GLuint
372CreateProgram(const char *vertProgFile, const char *fragProgFile,
373 struct uniform_info *uniforms)
374{
Brian2dca3372008-04-09 22:28:23 -0600375 GLuint fragShader, vertShader, program;
Brianedf24e62007-02-03 11:36:16 -0700376
Brian2dca3372008-04-09 22:28:23 -0600377 vertShader = CompileShaderFile(GL_VERTEX_SHADER, vertProgFile);
378 fragShader = CompileShaderFile(GL_FRAGMENT_SHADER, fragProgFile);
379 program = LinkShaders(vertShader, fragShader);
Brianedf24e62007-02-03 11:36:16 -0700380
381 glUseProgram_func(program);
382
Brian2dca3372008-04-09 22:28:23 -0600383 InitUniforms(program, uniforms);
Brianedf24e62007-02-03 11:36:16 -0700384
385 return program;
386}
387
388
389static void
390InitPrograms(void)
391{
392 Program1 = CreateProgram(ReflectVertFile, CubeFragFile, ReflectUniforms);
393 Program2 = CreateProgram(SimpleVertFile, SimpleTexFragFile, SimpleUniforms);
394}
395
396
397static void
398Init(GLboolean useImageFiles)
399{
400 const char *version = (const char *) glGetString(GL_VERSION);
401
402 if (version[0] != '2' || version[1] != '.') {
403 printf("Warning: this program expects OpenGL 2.0\n");
404 /*exit(1);*/
405 }
406 printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
407
408 GetExtensionFuncs();
409
410 InitTextures(useImageFiles);
411 InitPrograms();
412
413 glEnable(GL_DEPTH_TEST);
414
415 glClearColor(.6, .6, .9, 0);
416 glColor3f(1.0, 1.0, 1.0);
417}
418
419
420int
421main(int argc, char *argv[])
422{
423 glutInit(&argc, argv);
424 glutInitWindowSize(500, 400);
425 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
426 glutCreateWindow(Demo);
427 glutReshapeFunc(Reshape);
428 glutKeyboardFunc(key);
429 glutSpecialFunc(specialkey);
430 glutDisplayFunc(draw);
431 if (Anim)
432 glutIdleFunc(idle);
433 if (argc > 1 && strcmp(argv[1] , "-i") == 0)
434 Init(1);
435 else
436 Init(0);
437 glutMainLoop();
438 return 0;
439}