⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 underwater.c

📁 学习c++必备
💻 C
📖 第 1 页 / 共 2 页
字号:
/* 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 },};voiddrawLightLocation(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 voiddrawFloor(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();}voiddrawObject(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);  }}voiddrawScene(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);  }}voiddisplay(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);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -