diff --git a/examples/wave.c b/examples/wave.c index 2c81abc8..97783d0d 100644 --- a/examples/wave.c +++ b/examples/wave.c @@ -22,27 +22,26 @@ /* Animation speed (10.0 looks good) */ #define ANIMATION_SPEED 10.0 +GLfloat alpha = 210.f, beta = -70.f; +GLfloat zoom = 2.f; -GLfloat alpha = 210.0f, beta = -70.0f; -GLfloat zoom = 2.0f; - -int running = 1; +GLboolean running = GL_TRUE; struct Vertex { - GLfloat x,y,z; - GLfloat r,g,b; + 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 QUADW (GRIDW - 1) +#define QUADH (GRIDH - 1) #define QUADNUM (QUADW*QUADH) -GLuint quad[4*QUADNUM]; +GLuint quad[4 * QUADNUM]; struct Vertex vertex[VERTEXNUM]; /* The grid will look like this: @@ -56,47 +55,47 @@ struct Vertex vertex[VERTEXNUM]; * 0 1 2 */ -void initVertices( void ) +//======================================================================== +// Initialize grid geometry +//======================================================================== + +void init_vertices(void) { - int x,y,p; + 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; - } + switch (key) + { + case GLFW_KEY_ESC: + running = 0; + break; + case GLFW_KEY_SPACE: + init_grid(); + break; + case GLFW_KEY_LEFT: + alpha += 5; + break; + case GLFW_KEY_RIGHT: + alpha -= 5; + break; + case GLFW_KEY_UP: + beta -= 5; + break; + case GLFW_KEY_DOWN: + beta += 5; + break; + case GLFW_KEY_PAGEUP: + if (zoom > 1) + zoom -= 1; + break; + case GLFW_KEY_PAGEDOWN: + zoom+=1; + break; + default: + break; + } } -/* Callback function for window resize events */ -void handle_resize( GLFWwindow window, int width, int height ) +//======================================================================== +// Callback function for window resize events +//======================================================================== + +void handle_resize(GLFWwindow window, int width, int height) { - float ratio = 1.0f; + float ratio = 1.f; - if( height > 0 ) - { - ratio = (float) width / (float) height; - } + 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); + // Setup viewport + 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); + // 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 */ +//======================================================================== +// main +//======================================================================== + 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; - GLFWwindow window; + GLFWwindow window; + double t, dt_total, t_old; - /* Initialize GLFW */ - if(glfwInit() == GL_FALSE) - { - fprintf(stderr, "GLFW initialization failed\n"); - exit(-1); - } - - /* Desired window properties */ - width = 640; - height = 480; - mode = GLFW_WINDOWED; - - glfwOpenWindowHint(GLFW_DEPTH_BITS, 16); - - /* Open window */ - window = glfwOpenWindow(width, height, mode, "Wave Simulation", NULL); - if (!window) - { - fprintf(stderr, "Could not open window\n"); - glfwTerminate(); - exit(-1); - } - - glfwSwapInterval( 1 ); - - /* Keyboard handler */ - glfwSetKeyCallback( window, handle_key_down ); - glfwEnable( window, GLFW_KEY_REPEAT ); - - /* Window resize handler */ - glfwSetWindowSizeCallback( window, 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 ) + if (!glfwInit()) { - /* Select iteration time step */ - dt = dt_total > MAX_DELTA_T ? MAX_DELTA_T : dt_total; - dt_total -= dt; - - /* Calculate wave propagation */ - calc(); + fprintf(stderr, "GLFW initialization failed\n"); + exit(EXIT_FAILURE); } - /* Compute height of each vertex */ - adjustGrid(); + window = glfwOpenWindow(640, 480, GLFW_WINDOWED, "Wave Simulation", NULL); + if (!window) + { + fprintf(stderr, "Could not open window\n"); + exit(EXIT_FAILURE); + } - /* Draw wave grid to OpenGL display */ - draw_screen(); + glfwSwapInterval(1); - glfwPollEvents(); + // Keyboard handler + glfwSetKeyCallback(window, handle_key_down); + glfwEnable(window, GLFW_KEY_REPEAT); - /* Still running? */ - running = running && glfwIsWindow( window ); - } + // Window resize handler + glfwSetWindowSizeCallback(window, handle_resize); - glfwTerminate(); + // Initialize OpenGL + init_opengl(); - return 0; + // Initialize simulation + init_vertices(); + init_grid(); + adjust_grid(); + + // Initialize timer + t_old = glfwGetTime() - 0.01; + + while (running) + { + t = glfwGetTime(); + dt_total = t - t_old; + t_old = t; + + // Safety - iterate if dt_total is too large + while (dt_total > 0.f) + { + // Select iteration time step + dt = dt_total > MAX_DELTA_T ? MAX_DELTA_T : dt_total; + dt_total -= dt; + + // Calculate wave propagation + calc_grid(); + } + + // Compute height of each vertex + adjust_grid(); + + // Draw wave grid to OpenGL display + draw_scene(); + + glfwPollEvents(); + + // Still running? + running = running && glfwIsWindow(window); + } + + exit(EXIT_SUCCESS); } +