/***************************************************************************** * Wave Simulation in OpenGL * (C) 2002 Jakob Thomsen * http://home.in.tum.de/~thomsen * Modified for GLFW by Sylvain Hellegouarch - sh@programmationworld.com * Modified for variable frame rate by Marcus Geelnard * 2003-Jan-31: Minor cleanups and speedups / MG *****************************************************************************/ #include #include #include #include #ifndef M_PI #define M_PI 3.1415926535897932384626433832795 #endif /* Maximum delta T to allow for differential calculations */ #define MAX_DELTA_T 0.01 /* Animation speed (10.0 looks good) */ #define ANIMATION_SPEED 10.0 GLfloat alpha = 210.0f, beta = -70.0f; GLfloat zoom = 2.0f; int running = 1; struct Vertex { GLfloat x,y,z; GLfloat r,g,b; }; #define GRIDW 50 #define GRIDH 50 #define VERTEXNUM (GRIDW*GRIDH) #define QUADW (GRIDW-1) #define QUADH (GRIDH-1) #define QUADNUM (QUADW*QUADH) GLuint quad[4*QUADNUM]; struct Vertex vertex[VERTEXNUM]; /* The grid will look like this: * * 3 4 5 * *---*---* * | | | * | 0 | 1 | * | | | * *---*---* * 0 1 2 */ void initVertices( void ) { int x,y,p; /* place the vertices in a grid */ for(y=0;y1) zoom-=1; break; case GLFW_KEY_PAGEDOWN: zoom+=1; break; default: break; } } /* Callback function for window resize events */ void handle_resize( int width, int height ) { float ratio = 1.0f; if( height > 0 ) { ratio = (float) width / (float) height; } /* Setup viewport (Place where the stuff will appear in the main window). */ glViewport(0, 0, width, height); /* * Change to the projection matrix and set * our viewing volume. */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, ratio, 1.0, 1024.0); } /* Program entry point */ int main(int argc, char* argv[]) { /* Dimensions of our window. */ int width, height; /* Style of our window. */ int mode; /* Frame time */ double t, t_old, dt_total; /* Initialize GLFW */ if(glfwInit() == GL_FALSE) { fprintf(stderr, "GLFW initialization failed\n"); exit(-1); } /* Desired window properties */ width = 640; height = 480; mode = GLFW_WINDOW; /* Open window */ if( glfwOpenWindow(width,height,0,0,0,0,16,0,mode) == GL_FALSE ) { fprintf(stderr, "Could not open window\n"); glfwTerminate(); exit(-1); } /* Set title */ glfwSetWindowTitle( "Wave Simulation" ); glfwSwapInterval( 1 ); /* Keyboard handler */ glfwSetKeyCallback( handle_key_down ); glfwEnable( GLFW_KEY_REPEAT ); /* Window resize handler */ glfwSetWindowSizeCallback( handle_resize ); /* Initialize OpenGL */ setup_opengl(); /* Initialize simulation */ initVertices(); initSurface(); adjustGrid(); /* Initialize timer */ t_old = glfwGetTime() - 0.01; /* Main loop */ while(running) { /* Timing */ t = glfwGetTime(); dt_total = t - t_old; t_old = t; /* Safety - iterate if dt_total is too large */ while( dt_total > 0.0f ) { /* Select iteration time step */ dt = dt_total > MAX_DELTA_T ? MAX_DELTA_T : dt_total; dt_total -= dt; /* Calculate wave propagation */ calc(); } /* Compute height of each vertex */ adjustGrid(); /* Draw wave grid to OpenGL display */ draw_screen(); /* Still running? */ running = running && glfwGetWindowParam( GLFW_OPENED ); } glfwTerminate(); return 0; }