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

462 lines
11 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>
#define GLFW_INCLUDE_GLU
#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-11-17 17:29:28 -05:00
GLboolean locked = GL_FALSE;
int cursorX;
int cursorY;
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(GLFWwindow window)
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
glfwSwapBuffers(window);
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_ESCAPE:
2010-10-24 09:37:51 -04:00
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_PAGE_UP:
2010-11-17 17:33:46 -05:00
zoom -= 0.25f;
if (zoom < 0.f)
zoom = 0.f;
2010-10-24 09:37:51 -04:00
break;
case GLFW_KEY_PAGE_DOWN:
2010-11-17 17:33:46 -05:00
zoom += 0.25f;
2010-10-24 09:37:51 -04:00
break;
default:
break;
}
2010-09-07 11:34:51 -04:00
}
2010-11-17 17:29:28 -05:00
//========================================================================
// Callback function for mouse button events
//========================================================================
void mouse_button_callback(GLFWwindow window, int button, int action)
{
if (button != GLFW_MOUSE_BUTTON_LEFT)
return;
if (action == GLFW_PRESS)
{
glfwSetInputMode(window, GLFW_CURSOR_MODE, GLFW_CURSOR_CAPTURED);
2010-11-17 17:29:28 -05:00
locked = GL_TRUE;
}
else
{
locked = GL_FALSE;
glfwSetInputMode(window, GLFW_CURSOR_MODE, GLFW_CURSOR_NORMAL);
2010-11-17 17:29:28 -05:00
}
}
//========================================================================
// Callback function for cursor motion events
2010-11-17 17:29:28 -05:00
//========================================================================
void cursor_position_callback(GLFWwindow window, int x, int y)
2010-11-17 17:29:28 -05:00
{
if (locked)
{
alpha += (x - cursorX) / 10.f;
beta += (y - cursorY) / 10.f;
}
cursorX = x;
cursorY = y;
}
2010-11-17 17:33:46 -05:00
//========================================================================
// Callback function for scroll events
//========================================================================
2012-03-28 15:54:09 -04:00
void scroll_callback(GLFWwindow window, double x, double y)
2010-11-17 17:33:46 -05:00
{
2012-03-28 15:54:09 -04:00
zoom += (float) y / 4.f;
2010-11-17 17:33:46 -05:00
if (zoom < 0)
zoom = 0;
}
2010-10-24 09:37:51 -04:00
//========================================================================
// Callback function for window resize events
//========================================================================
void window_size_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
}
2012-08-03 10:20:52 -04:00
//========================================================================
// Callback function for window close events
//========================================================================
static int window_close_callback(GLFWwindow window)
{
running = GL_FALSE;
return GL_TRUE;
}
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;
int width, height;
2010-10-24 09:37:51 -04:00
2012-02-07 08:58:58 -05:00
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
2012-09-27 15:37:36 -04:00
window = glfwCreateWindow(640, 480, "Wave Simulation", NULL, NULL);
2010-10-24 09:37:51 -04:00
if (!window)
{
fprintf(stderr, "Could not open window\n");
exit(EXIT_FAILURE);
2010-09-07 11:34:51 -04:00
}
glfwSetKeyCallback(window, key_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwGetWindowSize(window, &width, &height);
window_size_callback(window, width, height);
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(window);
2010-09-07 11:34:51 -04:00
2010-10-24 09:37:51 -04:00
glfwPollEvents();
}
exit(EXIT_SUCCESS);
2010-09-07 11:34:51 -04:00
}
2010-10-24 09:37:51 -04:00