1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
|
/* Copyright (c) Mark J. Kilgard, 1997. */
/* This program is freely distributable without licensing fees and is
provided without guarantee or warrantee expressed or implied. This
program is -not- in the public domain. */
/* X compile line: cc -o underwater underwater.c texload.c dino.c -lglut -lGLU -lGL -lXmu -lXext -lX11 -lm */
/* This code compiles and works with any of 1) OpenGL 1.0 with no
texture extensions, 2) OpenGL 1.0 with texture extensions, or 3)
OpenGL 1.1. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
#include "texload.h"
#include "dino.h"
#if 0 /* For debugging different OpenGL versions. */
#undef GL_VERSION_1_1
#undef GL_EXT_texture_object
#endif
/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/* Texture object compatibility. */
#if defined(GL_VERSION_1_1)
#define TEXTURE_OBJECT 1
#elif defined(GL_EXT_texture_object)
#define TEXTURE_OBJECT 1
#define glBindTexture(A,B) glBindTextureEXT(A,B)
#define glGenTextures(A,B) glGenTexturesEXT(A,B)
#define glDeleteTextures(A,B) glDeleteTexturesEXT(A,B)
#ifndef GL_REPLACE
#define GL_REPLACE GL_REPLACE_EXT
#endif
#else
/* Define to nothing; but HaveTexObj will not be true in this case. */
#define glBindTexture(A,B)
#define glGenTextures(A,B)
#define glDeleteTextures(A,B)
#endif
#define NUM_PATTERNS 32
#define FLOOR_FILE "floor.rgb"
enum {
PASS_NORMAL, PASS_CAUSTIC
};
enum {
M_POSITIONAL, M_DIRECTIONAL, M_GREENISH_LIGHT, M_WHITE_LIGHT,
M_NO_CAUSTICS, M_WITH_CAUSTICS, M_SWITCH_MODEL,
M_INCREASE_RIPPLE_SIZE, M_DECREASE_RIPPLE_SIZE
};
enum {
MODEL_SPHERE, MODEL_CUBE, MODEL_DINO
};
static GLboolean HaveTexObj = GL_FALSE;
static int object = MODEL_SPHERE;
static int reportSpeed = 0;
static int dinoDisplayList;
static GLfloat causticScale = 1.0;
static int fullscreen = 0;
static GLfloat lightPosition[4];
/* XXX Diffuse light color component > 1.0 to brighten caustics. */
static GLfloat lightDiffuseColor[] = {1.0, 1.5, 1.0, 1.0}; /* XXX Green = 1.5 */
static GLfloat defaultDiffuseMaterial[] = {0.8, 0.8, 0.8, 1.0};
static int directionalLight = 1;
static int showCaustics = 1, causticMotion = 1;
static int useMipmaps = 1;
static int currentCaustic = 0;
static int causticIncrement = 1;
static float lightAngle = 0.0, lightHeight = 20;
static GLfloat angle = -150; /* in degrees */
static GLfloat angle2 = 30; /* in degrees */
static int moving = 0, startx, starty;
static int lightMoving = 0, lightStartX, lightStartY;
static GLfloat floorVertices[4][3] = {
{ -20.0, 0.0, 20.0 },
{ 20.0, 0.0, 20.0 },
{ 20.0, 0.0, -20.0 },
{ -20.0, 0.0, -20.0 },
};
void
drawLightLocation(void)
{
glPushMatrix();
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glColor3f(1.0, 1.0, 0.0);
if (directionalLight) {
/* Draw an arrowhead. */
glDisable(GL_CULL_FACE);
glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);
glRotatef(lightAngle * -180.0 / M_PI, 0, 1, 0);
glRotatef(atan(lightHeight/12) * 180.0 / M_PI, 0, 0, 1);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0, 0, 0);
glVertex3f(2, 1, 1);
glVertex3f(2, -1, 1);
glVertex3f(2, -1, -1);
glVertex3f(2, 1, -1);
glVertex3f(2, 1, 1);
glEnd();
/* Draw a white line from light direction. */
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(5, 0, 0);
glEnd();
glEnable(GL_CULL_FACE);
} else {
/* Draw a yellow ball at the light source. */
glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);
glutSolidSphere(1.0, 5, 5);
}
glEnable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glPopMatrix();
}
/* Draw a floor (possibly textured). */
static void
drawFloor(int pass)
{
if (pass == PASS_NORMAL) {
if (HaveTexObj)
glBindTexture(GL_TEXTURE_2D, 100);
else
glCallList(100);
}
/* The glTexCoord2f calls get ignored when in texture generation
mode (ie, when modulating in caustics). */
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0, 0.0);
glTexCoord2f(0.0, 0.0);
glVertex3fv(floorVertices[0]);
glTexCoord2f(0.0, 2.0);
glVertex3fv(floorVertices[1]);
glTexCoord2f(2.0, 2.0);
glVertex3fv(floorVertices[2]);
glTexCoord2f(2.0, 0.0);
glVertex3fv(floorVertices[3]);
glEnd();
}
void
drawObject(int pass)
{
if (pass == PASS_NORMAL) {
/* The objects are not textured (they could be if someone
wanted them to be) so disable texture in the normal pass. In
the caustic pass, we want to avoid disabling texture though. */
glDisable(GL_TEXTURE_2D);
}
glPushMatrix();
/* If object is dino, put feet on the floor. */
glTranslatef(0.0, object == MODEL_DINO ? 8.0 : 12.0, 0.0);
switch (object) {
case MODEL_SPHERE:
glutSolidSphere(6.0, 12, 12);
break;
case MODEL_CUBE:
glutSolidCube(7.0);
break;
case MODEL_DINO:
glCallList(dinoDisplayList);
glMaterialfv(GL_FRONT, GL_DIFFUSE, defaultDiffuseMaterial);
break;
}
glPopMatrix();
if (pass == PASS_NORMAL) {
glEnable(GL_TEXTURE_2D);
}
}
void
drawScene(int pass)
{
/* The 0.03 in the Y column is just to shift the texture coordinates
a little based on Y (depth in the water) so that vertical faces
(like on the cube) do not get totally vertical caustics. */
GLfloat sPlane[4] = { 0.05, 0.03, 0.0, 0.0 };
GLfloat tPlane[4] = { 0.0, 0.03, 0.05, 0.0 };
/* The causticScale determines how large the caustic "ripples" will
be. See the "Increate/Decrease ripple size" menu options. */
sPlane[0] = 0.05 * causticScale;
sPlane[1] = 0.03 * causticScale;
tPlane[1] = 0.03 * causticScale;
tPlane[2] = 0.05 * causticScale;
if (pass == PASS_CAUSTIC) {
/* Set current color to "white" and disable lighting
to emulate OpenGL 1.1's GL_REPLACE texture environment. */
glColor3f(1.0, 1.0, 1.0);
glDisable(GL_LIGHTING);
/* Generate the S & T coordinates for the caustic textures
from the object coordinates. */
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(GL_S, GL_OBJECT_PLANE, sPlane);
glTexGenfv(GL_T, GL_OBJECT_PLANE, tPlane);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
if (HaveTexObj) {
glBindTexture(GL_TEXTURE_2D, currentCaustic+1);
} else {
glCallList(currentCaustic+101);
}
}
drawFloor(pass);
drawObject(pass);
if (pass == PASS_CAUSTIC) {
glEnable(GL_LIGHTING);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
}
}
void
display(void)
{
int startTime, endTime;
/* Simplistic benchmarking. Be careful about results. */
if (reportSpeed) {
startTime = glutGet(GLUT_ELAPSED_TIME);
}
/* Clear depth and color buffer. */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Reposition the light source. */
lightPosition[0] = 12*cos(lightAngle);
lightPosition[1] = lightHeight;
lightPosition[2] = 12*sin(lightAngle);
if (directionalLight) {
lightPosition[3] = 0.0;
} else {
lightPosition[3] = 1.0;
}
glPushMatrix();
/* Perform scene rotations based on user mouse input. */
glRotatef(angle2, 1.0, 0.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
/* Position the light again, after viewing rotation. */
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
/* Draw the light location. */
drawLightLocation();
/* Normal pass rendering the scene (caustics get added
after this pass). */
drawScene(PASS_NORMAL);
if (showCaustics) {
/* Disable depth buffer update and exactly match depth
buffer values for slightly faster rendering. */
glDepthMask(GL_FALSE);
glDepthFunc(GL_EQUAL);
/* Multiply the source color (from the caustic luminance
texture) with the previous color from the normal pass. The
caustics are modulated into the scene. */
glBlendFunc(GL_ZERO, GL_SRC_COLOR);
glEnable(GL_BLEND);
drawScene(PASS_CAUSTIC);
/* Restore fragment operations to normal. */
glDepthMask(GL_TRUE);
glDepthFunc(GL_LESS);
glDisable(GL_BLEND);
}
glPopMatrix();
glutSwapBuffers();
if (reportSpeed) {
glFinish();
endTime = glutGet(GLUT_ELAPSED_TIME);
printf("Speed %.3g frames/sec (%d ms)\n", 1000.0/(endTime-startTime), endTime-startTime);
fflush(stdout);
}
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, /* field of view in degree */
(GLfloat) w/(GLfloat) h, /* aspect ratio */
20.0, /* Z near */
100.0); /* Z far */
glMatrixMode(GL_MODELVIEW);
}
void
idle(void)
{
/* Advance the caustic pattern. */
currentCaustic = (currentCaustic + causticIncrement) % NUM_PATTERNS;
glutPostRedisplay();
}
void
updateIdleFunc(void)
{
/* Must be both displaying the caustic patterns and have the
caustics in rippling motion to need an idle callback. */
if (showCaustics && causticMotion) {
glutIdleFunc(idle);
} else {
glutIdleFunc(NULL);
}
}
void
visible(int vis)
{
/* Stop the animation when the window is not visible. */
if (vis == GLUT_VISIBLE)
updateIdleFunc();
else
glutIdleFunc(NULL);
}
/* ARGSUSED2 */
static void
mouse(int button, int state, int x, int y)
{
/* Rotate the scene with the left mouse button. */
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) {
moving = 1;
startx = x;
starty = y;
}
if (state == GLUT_UP) {
moving = 0;
}
}
/* Rotate the light position with the middle mouse button. */
if (button == GLUT_MIDDLE_BUTTON) {
if (state == GLUT_DOWN) {
lightMoving = 1;
lightStartX = x;
lightStartY = y;
}
if (state == GLUT_UP) {
lightMoving = 0;
}
}
}
/* ARGSUSED1 */
static void
motion(int x, int y)
{
if (moving) {
angle = angle + (x - startx);
angle2 = angle2 + (y - starty);
startx = x;
starty = y;
glutPostRedisplay();
}
if (lightMoving) {
lightAngle += (x - lightStartX)/40.0;
lightHeight += (lightStartY - y)/20.0;
lightStartX = x;
lightStartY = y;
glutPostRedisplay();
}
}
/* ARGSUSED1 */
static void
keyboard(unsigned char c, int x, int y)
{
switch (c) {
case 27: /* Escape quits. */
exit(0);
break;
case 'R': /* Simplistic benchmarking. */
case 'r':
reportSpeed = !reportSpeed;
break;
case ' ': /* Spacebar toggles caustic rippling motion. */
causticMotion = !causticMotion;
updateIdleFunc();
break;
}
}
void
menuSelect(int value)
{
switch (value) {
case M_POSITIONAL:
directionalLight = 0;
break;
case M_DIRECTIONAL:
directionalLight = 1;
break;
case M_GREENISH_LIGHT:
lightDiffuseColor[0] = 1.0;
lightDiffuseColor[1] = 1.5; /* XXX Green = 1.5 */
lightDiffuseColor[2] = 1.0;
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuseColor);
break;
case M_WHITE_LIGHT:
lightDiffuseColor[0] = 1.5; /* XXX Red = 1.5 */
lightDiffuseColor[1] = 1.5; /* XXX Green = 1.5 */
lightDiffuseColor[2] = 1.5; /* XXX Blue = 1.5 */
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuseColor);
break;
case M_WITH_CAUSTICS:
showCaustics = 1;
updateIdleFunc();
break;
case M_NO_CAUSTICS:
showCaustics = 0;
updateIdleFunc();
break;
case M_SWITCH_MODEL:
object = (object + 1) % 3;
break;
case M_INCREASE_RIPPLE_SIZE:
causticScale /= 1.5;
break;
case M_DECREASE_RIPPLE_SIZE:
causticScale *= 1.5;
break;
}
glutPostRedisplay();
}
int
main(int argc, char **argv)
{
int width, height;
int i;
GLubyte *imageData;
glutInit(&argc, argv);
for (i=1; i<argc; i++) {
if (!strcmp("-lesstex", argv[i])) {
/* Only use 16 caustic textures. Saves texture memory and works
better on slow machines. */
causticIncrement = 2;
} else if (!strcmp("-evenlesstex", argv[i])) {
/* Only use 8 caustic textures. Saves even more texture memory for
slow machines. Temporal rippling suffers. */
causticIncrement = 4;
} else if (!strcmp("-nomipmap", argv[i])) {
/* Don't use linear mipmap linear texture filtering; instead
use linear filtering. */
useMipmaps = 0;
} else if (!strcmp("-fullscreen", argv[i])) {
fullscreen = 1;
} else {
fprintf(stderr, "usage: caustics [-lesstex]\n");
fprintf(stderr, " -lesstex uses half the caustic textures.\n");
fprintf(stderr, " -evenlesstex uses one fourth of the caustic textures.\n");
exit(1);
}
}
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
if (fullscreen) {
glutGameModeString("800x600:16@60");
glutEnterGameMode();
} else {
glutCreateWindow("underwater");
}
/* Check that renderer has the GL_EXT_texture_object
extension or supports OpenGL 1.1 */
#ifdef TEXTURE_OBJECT
{
char *version = (char *) glGetString(GL_VERSION);
if (glutExtensionSupported("GL_EXT_texture_object")
|| strncmp(version, "1.1", 3) == 0) {
HaveTexObj = GL_TRUE;
}
}
#endif
glEnable(GL_TEXTURE_2D);
#ifdef TEXTURE_OBJECT /* Replace texture environment not in OpenGL 1.0. */
if (HaveTexObj)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
#endif
/* Load the caustic ripple textures. */
printf("loading caustics:");
for (i=0; i<NUM_PATTERNS; i += causticIncrement) {
char filename[80];
sprintf(filename, "caust%02d.bw", i);
printf(" %d", i);
fflush(stdout);
imageData = read_alpha_texture(filename, &width, &height);
if (imageData == NULL) {
fprintf(stderr, "\n%s: could not load image file\n", filename);
exit(1);
}
if (HaveTexObj)
glBindTexture(GL_TEXTURE_2D, i+1);
else
glNewList(i+101, GL_COMPILE);
if (useMipmaps) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_LUMINANCE, width, height,
GL_LUMINANCE, GL_UNSIGNED_BYTE, imageData);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, height, width, 0,
GL_LUMINANCE, GL_UNSIGNED_BYTE, imageData);
}
free(imageData);
if (!HaveTexObj)
glEndList();
}
printf(".\n");
/* Load an RGB file for the floor texture. */
printf("loading RGB textures: floor");
fflush(stdout);
imageData = read_rgb_texture(FLOOR_FILE, &width, &height);
if (imageData == NULL) {
fprintf(stderr, "%s: could not load image file\n", FLOOR_FILE);
exit(1);
}
printf(".\n");
if (HaveTexObj)
glBindTexture(GL_TEXTURE_2D, 100);
else
glNewList(100, GL_COMPILE);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
if (useMipmaps) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB,
GL_UNSIGNED_BYTE, imageData);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, imageData);
}
free(imageData);
if (!HaveTexObj)
glEndList();
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 8.0, 60.0, /* eye is at (0,8,60) */
0.0, 8.0, 0.0, /* center is at (0,8,0) */
0.0, 1.0, 0.); /* up is in postivie Y direction */
/* Setup initial OpenGL rendering state. */
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuseColor);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
/* Register assorted GLUT callback routines. */
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutVisibilityFunc(visible);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
/* Create a pop-up menu. */
if (!fullscreen) {
glutCreateMenu(menuSelect);
glutAddMenuEntry("Positional light", M_POSITIONAL);
glutAddMenuEntry("Directional light", M_DIRECTIONAL);
glutAddMenuEntry("Greenish light", M_GREENISH_LIGHT);
glutAddMenuEntry("White light", M_WHITE_LIGHT);
glutAddMenuEntry("With caustics", M_WITH_CAUSTICS);
glutAddMenuEntry("No caustics", M_NO_CAUSTICS);
glutAddMenuEntry("Switch model", M_SWITCH_MODEL);
glutAddMenuEntry("Increase ripple size", M_INCREASE_RIPPLE_SIZE);
glutAddMenuEntry("Decrease ripple size", M_DECREASE_RIPPLE_SIZE);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
/* For rendering the MODEL_DINO object. */
dinoDisplayList = makeDinosaur();
/* Enter GLUT's main loop; callback dispatching begins. */
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
|