1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-30 05:58:52 -05:00
glfw/examples/wave.c

391 lines
9.4 KiB
C
Raw Normal View History

2010-09-07 11:34:51 -04:00
/*****************************************************************************
* 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
2010-10-24 09:38:59 -04:00
* 2010-10-24: Formatting and cleanup - Camilla Berglund
2010-09-07 11:34:51 -04:00
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glfw3.h>
2010-09-07 11:34:51 -04:00
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif
2010-10-24 09:42:07 -04:00
// Maximum delta T to allow for differential calculations
2010-09-07 11:34:51 -04:00
#define MAX_DELTA_T 0.01
2010-10-24 09:42:07 -04:00
// Animation speed (10.0 looks good)
2010-09-07 11:34:51 -04:00
#define ANIMATION_SPEED 10.0
2010-10-24 09:37:51 -04:00
GLfloat alpha = 210.f, beta = -70.f;
GLfloat zoom = 2.f;
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
GLboolean running = GL_TRUE;
2010-09-07 11:34:51 -04:00
struct Vertex
{
2010-10-24 09:37:51 -04:00
GLfloat x, y, z;
GLfloat r, g, b;
2010-09-07 11:34:51 -04:00
};
#define GRIDW 50
#define GRIDH 50
#define VERTEXNUM (GRIDW*GRIDH)
2010-10-24 09:37:51 -04:00
#define QUADW (GRIDW - 1)
#define QUADH (GRIDH - 1)
2010-09-07 11:34:51 -04:00
#define QUADNUM (QUADW*QUADH)
2010-10-24 09:37:51 -04:00
GLuint quad[4 * QUADNUM];
2010-09-07 11:34:51 -04:00
struct Vertex vertex[VERTEXNUM];
/* The grid will look like this:
*
* 3 4 5
* *---*---*
* | | |
* | 0 | 1 |
* | | |
* *---*---*
* 0 1 2
*/
2010-10-24 09:37:51 -04:00
//========================================================================
// Initialize grid geometry
//========================================================================
void init_vertices(void)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
int x, y, p;
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
// Place the vertices in a grid
for (y = 0; y < GRIDH; y++)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
for (x = 0; x < GRIDW; x++)
{
p = y * GRIDW + x;
vertex[p].x = (GLfloat) (x - GRIDW / 2) / (GLfloat) (GRIDW / 2);
vertex[p].y = (GLfloat) (y - GRIDH / 2) / (GLfloat) (GRIDH / 2);
vertex[p].z = 0;
if ((x % 4 < 2) ^ (y % 4 < 2))
vertex[p].r = 0.0;
else
vertex[p].r = 1.0;
vertex[p].g = (GLfloat) y / (GLfloat) GRIDH;
vertex[p].b = 1.f - ((GLfloat) x / (GLfloat) GRIDW + (GLfloat) y / (GLfloat) GRIDH) / 2.f;
}
2010-09-07 11:34:51 -04:00
}
2010-10-24 09:37:51 -04:00
for (y = 0; y < QUADH; y++)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
for (x = 0; x < QUADW; x++)
{
p = 4 * (y * QUADW + x);
quad[p + 0] = y * GRIDW + x; // Some point
quad[p + 1] = y * GRIDW + x + 1; // Neighbor at the right side
quad[p + 2] = (y + 1) * GRIDW + x + 1; // Upper right neighbor
quad[p + 3] = (y + 1) * GRIDW + x; // Upper neighbor
}
2010-09-07 11:34:51 -04:00
}
}
double dt;
double p[GRIDW][GRIDH];
double vx[GRIDW][GRIDH], vy[GRIDW][GRIDH];
double ax[GRIDW][GRIDH], ay[GRIDW][GRIDH];
2010-10-24 09:37:51 -04:00
//========================================================================
// Initialize grid
//========================================================================
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
void init_grid(void)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
int x, y;
double dx, dy, d;
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
for (y = 0; y < GRIDH; y++)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
for (x = 0; x < GRIDW; x++)
{
dx = (double) (x - GRIDW / 2);
dy = (double) (y - GRIDH / 2);
d = sqrt(dx * dx + dy * dy);
if (d < 0.1 * (double) (GRIDW / 2))
{
2010-10-24 09:40:57 -04:00
d = d * 10.0;
p[x][y] = -cos(d * (M_PI / (double)(GRIDW * 4))) * 100.0;
2010-10-24 09:37:51 -04:00
}
else
p[x][y] = 0.0;
vx[x][y] = 0.0;
vy[x][y] = 0.0;
}
2010-09-07 11:34:51 -04:00
}
}
2010-10-24 09:37:51 -04:00
//========================================================================
// Draw scene
//========================================================================
void draw_scene(void)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
// Clear the color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
// We don't want to modify the projection matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
// Move back
glTranslatef(0.0, 0.0, -zoom);
// Rotate the view
glRotatef(beta, 1.0, 0.0, 0.0);
glRotatef(alpha, 0.0, 0.0, 1.0);
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
glDrawElements(GL_QUADS, 4 * QUADNUM, GL_UNSIGNED_INT, quad);
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
glfwSwapBuffers();
2010-09-07 11:34:51 -04:00
}
2010-10-24 09:37:51 -04:00
//========================================================================
// Initialize Miscellaneous OpenGL state
//========================================================================
void init_opengl(void)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
// Use Gouraud (smooth) shading
glShadeModel(GL_SMOOTH);
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
// Switch on the z-buffer
glEnable(GL_DEPTH_TEST);
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(struct Vertex), vertex);
glColorPointer(3, GL_FLOAT, sizeof(struct Vertex), &vertex[0].r); // Pointer to the first color
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
glPointSize(2.0);
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
// Background color is black
glClearColor(0, 0, 0, 0);
2010-09-07 11:34:51 -04:00
}
2010-10-24 09:37:51 -04:00
//========================================================================
// Modify the height of each vertex according to the pressure
//========================================================================
void adjust_grid(void)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
int pos;
int x, y;
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
for (y = 0; y < GRIDH; y++)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
for (x = 0; x < GRIDW; x++)
{
pos = y * GRIDW + x;
vertex[pos].z = (float) (p[x][y] * (1.0 / 50.0));
}
2010-09-07 11:34:51 -04:00
}
}
2010-10-24 09:37:51 -04:00
//========================================================================
// Calculate wave propagation
//========================================================================
void calc_grid(void)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
int x, y, x2, y2;
double time_step = dt * ANIMATION_SPEED;
// Compute accelerations
for (x = 0; x < GRIDW; x++)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
x2 = (x + 1) % GRIDW;
for(y = 0; y < GRIDH; y++)
ax[x][y] = p[x][y] - p[x2][y];
2010-09-07 11:34:51 -04:00
}
2010-10-24 09:37:51 -04:00
for (y = 0; y < GRIDH; y++)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
y2 = (y + 1) % GRIDH;
for(x = 0; x < GRIDW; x++)
ay[x][y] = p[x][y] - p[x][y2];
2010-09-07 11:34:51 -04:00
}
2010-10-24 09:37:51 -04:00
// Compute speeds
for (x = 0; x < GRIDW; x++)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
for (y = 0; y < GRIDH; y++)
{
vx[x][y] = vx[x][y] + ax[x][y] * time_step;
vy[x][y] = vy[x][y] + ay[x][y] * time_step;
}
2010-09-07 11:34:51 -04:00
}
2010-10-24 09:37:51 -04:00
// Compute pressure
for (x = 1; x < GRIDW; x++)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
x2 = x - 1;
for (y = 1; y < GRIDH; y++)
{
y2 = y - 1;
p[x][y] = p[x][y] + (vx[x2][y] - vx[x][y] + vy[x][y2] - vy[x][y]) * time_step;
}
2010-09-07 11:34:51 -04:00
}
}
2010-10-24 09:37:51 -04:00
//========================================================================
// Handle key strokes
//========================================================================
2010-10-24 09:50:39 -04:00
void key_callback(GLFWwindow window, int key, int action)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
if (action != GLFW_PRESS)
return;
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:
2010-10-24 09:42:07 -04:00
zoom += 1;
2010-10-24 09:37:51 -04:00
break;
default:
break;
}
2010-09-07 11:34:51 -04:00
}
2010-10-24 09:37:51 -04:00
//========================================================================
// Callback function for window resize events
//========================================================================
2010-10-24 09:50:39 -04:00
void window_resize_callback(GLFWwindow window, int width, int height)
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
float ratio = 1.f;
if (height > 0)
ratio = (float) width / (float) 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);
2010-09-07 11:34:51 -04:00
}
2010-10-24 09:37:51 -04:00
//========================================================================
// main
//========================================================================
2010-09-07 11:34:51 -04:00
int main(int argc, char* argv[])
{
2010-10-24 09:37:51 -04:00
GLFWwindow window;
double t, dt_total, t_old;
if (!glfwInit())
2010-09-07 11:34:51 -04:00
{
2010-10-24 09:37:51 -04:00
fprintf(stderr, "GLFW initialization failed\n");
exit(EXIT_FAILURE);
}
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
window = glfwOpenWindow(640, 480, GLFW_WINDOWED, "Wave Simulation", NULL);
if (!window)
{
fprintf(stderr, "Could not open window\n");
exit(EXIT_FAILURE);
2010-09-07 11:34:51 -04:00
}
2010-10-24 09:37:51 -04:00
glfwSwapInterval(1);
// Keyboard handler
2010-10-24 12:28:55 -04:00
glfwSetKeyCallback(key_callback);
2010-10-24 09:37:51 -04:00
glfwEnable(window, GLFW_KEY_REPEAT);
// Window resize handler
2010-10-24 12:28:55 -04:00
glfwSetWindowSizeCallback(window_resize_callback);
2010-10-24 09:37:51 -04:00
// Initialize OpenGL
init_opengl();
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
// Initialize simulation
init_vertices();
init_grid();
adjust_grid();
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
// Initialize timer
t_old = glfwGetTime() - 0.01;
2010-10-24 09:37:51 -04:00
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();
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
// Draw wave grid to OpenGL display
draw_scene();
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
glfwPollEvents();
// Still running?
running = running && glfwIsWindow(window);
}
exit(EXIT_SUCCESS);
2010-09-07 11:34:51 -04:00
}
2010-10-24 09:37:51 -04:00