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