glfw/examples/particles.c

1075 lines
35 KiB
C
Raw Normal View History

2014-01-11 18:37:01 +00:00
//========================================================================
2014-01-11 19:46:24 +00:00
// A simple particle engine with threaded physics
// Copyright (c) Marcus Geelnard
2016-11-21 15:23:59 +00:00
// Copyright (c) Camilla Löwy <elmindreda@glfw.org>
2014-01-11 18:37:01 +00:00
//
2014-01-11 19:46:24 +00:00
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
2014-01-11 18:37:01 +00:00
//
2014-01-11 19:46:24 +00:00
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
2014-01-11 18:37:01 +00:00
//
2014-01-11 19:46:24 +00:00
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
2014-01-11 18:37:01 +00:00
//
2014-01-11 19:46:24 +00:00
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
2014-01-11 18:37:01 +00:00
//
2014-01-11 19:46:24 +00:00
// 3. This notice may not be removed or altered from any source
// distribution.
2014-01-11 18:37:01 +00:00
//
//========================================================================
#if defined(_MSC_VER)
// Make MS math.h define M_PI
#define _USE_MATH_DEFINES
#endif
2014-01-11 18:37:01 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <tinycthread.h>
#include <getopt.h>
2015-08-09 13:44:20 +00:00
#include <linmath.h>
2014-01-11 18:37:01 +00:00
#define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h>
#define GLFW_INCLUDE_NONE
2014-08-15 10:53:47 +00:00
#include <GLFW/glfw3.h>
2014-01-11 18:37:01 +00:00
// Define tokens for GL_EXT_separate_specular_color if not already defined
#ifndef GL_EXT_separate_specular_color
#define GL_LIGHT_MODEL_COLOR_CONTROL_EXT 0x81F8
#define GL_SINGLE_COLOR_EXT 0x81F9
#define GL_SEPARATE_SPECULAR_COLOR_EXT 0x81FA
#endif // GL_EXT_separate_specular_color
//========================================================================
// Type definitions
//========================================================================
2014-01-11 19:30:37 +00:00
typedef struct
{
float x, y, z;
} Vec3;
2014-01-11 18:37:01 +00:00
// This structure is used for interleaved vertex arrays (see the
2014-01-11 19:30:37 +00:00
// draw_particles function)
//
// NOTE: This structure SHOULD be packed on most systems. It uses 32-bit fields
// on 32-bit boundaries, and is a multiple of 64 bits in total (6x32=3x64). If
// it does not work, try using pragmas or whatever to force the structure to be
// packed.
typedef struct
{
2014-01-11 18:37:01 +00:00
GLfloat s, t; // Texture coordinates
GLuint rgba; // Color (four ubytes packed into an uint)
GLfloat x, y, z; // Vertex coordinates
2014-01-11 19:30:37 +00:00
} Vertex;
2014-01-11 18:37:01 +00:00
//========================================================================
// Program control global variables
//========================================================================
// Window dimensions
2014-01-11 19:46:24 +00:00
float aspect_ratio;
2014-01-11 18:37:01 +00:00
// "wireframe" flag (true if we use wireframe view)
int wireframe;
// Thread synchronization
struct {
double t; // Time (s)
float dt; // Time since last frame (s)
int p_frame; // Particle physics frame number
int d_frame; // Particle draw frame number
cnd_t p_done; // Condition: particle physics done
cnd_t d_done; // Condition: particle draw done
mtx_t particles_lock; // Particles data sharing mutex
2014-01-11 18:37:01 +00:00
} thread_sync;
//========================================================================
// Texture declarations (we hard-code them into the source code, since
// they are so simple)
//========================================================================
#define P_TEX_WIDTH 8 // Particle texture dimensions
#define P_TEX_HEIGHT 8
#define F_TEX_WIDTH 16 // Floor texture dimensions
#define F_TEX_HEIGHT 16
// Texture object IDs
GLuint particle_tex_id, floor_tex_id;
// Particle texture (a simple spot)
const unsigned char particle_texture[ P_TEX_WIDTH * P_TEX_HEIGHT ] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x11, 0x22, 0x22, 0x11, 0x00, 0x00,
0x00, 0x11, 0x33, 0x88, 0x77, 0x33, 0x11, 0x00,
0x00, 0x22, 0x88, 0xff, 0xee, 0x77, 0x22, 0x00,
0x00, 0x22, 0x77, 0xee, 0xff, 0x88, 0x22, 0x00,
0x00, 0x11, 0x33, 0x77, 0x88, 0x33, 0x11, 0x00,
0x00, 0x00, 0x11, 0x33, 0x22, 0x11, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// Floor texture (your basic checkered floor)
const unsigned char floor_texture[ F_TEX_WIDTH * F_TEX_HEIGHT ] = {
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0xff, 0xf0, 0xcc, 0xf0, 0xf0, 0xf0, 0xff, 0xf0, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0xf0, 0xcc, 0xee, 0xff, 0xf0, 0xf0, 0xf0, 0xf0, 0x30, 0x66, 0x30, 0x30, 0x30, 0x20, 0x30, 0x30,
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xee, 0xf0, 0xf0, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0xf0, 0xf0, 0xf0, 0xf0, 0xcc, 0xf0, 0xf0, 0xf0, 0x30, 0x30, 0x55, 0x30, 0x30, 0x44, 0x30, 0x30,
0xf0, 0xdd, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0x33, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xff, 0xf0, 0xf0, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x60, 0x30,
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0x33, 0x33, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33, 0x30, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
0x30, 0x30, 0x30, 0x30, 0x30, 0x20, 0x30, 0x30, 0xf0, 0xff, 0xf0, 0xf0, 0xdd, 0xf0, 0xf0, 0xff,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x55, 0x33, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xff, 0xf0, 0xf0,
0x30, 0x44, 0x66, 0x30, 0x30, 0x30, 0x30, 0x30, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xf0, 0xf0, 0xf0, 0xaa, 0xf0, 0xf0, 0xcc, 0xf0,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xff, 0xf0, 0xf0, 0xf0, 0xff, 0xf0, 0xdd, 0xf0,
0x30, 0x30, 0x30, 0x77, 0x30, 0x30, 0x30, 0x30, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
};
//========================================================================
// These are fixed constants that control the particle engine. In a
// modular world, these values should be variables...
//========================================================================
// Maximum number of particles
#define MAX_PARTICLES 3000
// Life span of a particle (in seconds)
2014-01-11 19:30:37 +00:00
#define LIFE_SPAN 8.f
2014-01-11 18:37:01 +00:00
// A new particle is born every [BIRTH_INTERVAL] second
#define BIRTH_INTERVAL (LIFE_SPAN/(float)MAX_PARTICLES)
// Particle size (meters)
#define PARTICLE_SIZE 0.7f
// Gravitational constant (m/s^2)
#define GRAVITY 9.8f
// Base initial velocity (m/s)
2014-01-11 19:30:37 +00:00
#define VELOCITY 8.f
2014-01-11 18:37:01 +00:00
// Bounce friction (1.0 = no friction, 0.0 = maximum friction)
#define FRICTION 0.75f
// "Fountain" height (m)
2014-01-11 19:30:37 +00:00
#define FOUNTAIN_HEIGHT 3.f
2014-01-11 18:37:01 +00:00
// Fountain radius (m)
#define FOUNTAIN_RADIUS 1.6f
// Minimum delta-time for particle phisics (s)
#define MIN_DELTA_T (BIRTH_INTERVAL * 0.5f)
//========================================================================
// Particle system global variables
//========================================================================
// This structure holds all state for a single particle
typedef struct {
float x,y,z; // Position in space
float vx,vy,vz; // Velocity vector
float r,g,b; // Color of particle
float life; // Life of particle (1.0 = newborn, < 0.0 = dead)
int active; // Tells if this particle is active
} PARTICLE;
// Global vectors holding all particles. We use two vectors for double
// buffering.
2014-01-11 19:30:37 +00:00
static PARTICLE particles[MAX_PARTICLES];
2014-01-11 18:37:01 +00:00
// Global variable holding the age of the youngest particle
static float min_age;
// Color of latest born particle (used for fountain lighting)
static float glow_color[4];
// Position of latest born particle (used for fountain lighting)
static float glow_pos[4];
//========================================================================
// Object material and fog configuration constants
//========================================================================
2014-01-11 19:30:37 +00:00
const GLfloat fountain_diffuse[4] = { 0.7f, 1.f, 1.f, 1.f };
const GLfloat fountain_specular[4] = { 1.f, 1.f, 1.f, 1.f };
const GLfloat fountain_shininess = 12.f;
const GLfloat floor_diffuse[4] = { 1.f, 0.6f, 0.6f, 1.f };
const GLfloat floor_specular[4] = { 0.6f, 0.6f, 0.6f, 1.f };
const GLfloat floor_shininess = 18.f;
const GLfloat fog_color[4] = { 0.1f, 0.1f, 0.1f, 1.f };
2014-01-11 18:37:01 +00:00
//========================================================================
// Print usage information
//========================================================================
static void usage(void)
{
2014-01-11 19:46:24 +00:00
printf("Usage: particles [-bfhs]\n");
printf("Options:\n");
2014-01-11 19:46:24 +00:00
printf(" -f Run in full screen\n");
printf(" -h Display this help\n");
2014-01-11 19:46:24 +00:00
printf(" -s Run program as single thread (default is to use two threads)\n");
printf("\n");
printf("Program runtime controls:\n");
printf(" W Toggle wireframe mode\n");
printf(" Esc Exit program\n");
}
2014-01-11 18:37:01 +00:00
//========================================================================
2014-01-11 19:30:37 +00:00
// Initialize a new particle
2014-01-11 18:37:01 +00:00
//========================================================================
2014-01-11 19:30:37 +00:00
static void init_particle(PARTICLE *p, double t)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
float xy_angle, velocity;
2014-01-11 18:37:01 +00:00
// Start position of particle is at the fountain blow-out
2014-01-11 19:30:37 +00:00
p->x = 0.f;
p->y = 0.f;
2014-01-11 18:37:01 +00:00
p->z = FOUNTAIN_HEIGHT;
// Start velocity is up (Z)...
2014-01-11 19:30:37 +00:00
p->vz = 0.7f + (0.3f / 4096.f) * (float) (rand() & 4095);
2014-01-11 18:37:01 +00:00
// ...and a randomly chosen X/Y direction
2014-01-11 19:30:37 +00:00
xy_angle = (2.f * (float) M_PI / 4096.f) * (float) (rand() & 4095);
p->vx = 0.4f * (float) cos(xy_angle);
p->vy = 0.4f * (float) sin(xy_angle);
2014-01-11 18:37:01 +00:00
// Scale velocity vector according to a time-varying velocity
2014-01-11 19:30:37 +00:00
velocity = VELOCITY * (0.8f + 0.1f * (float) (sin(0.5 * t) + sin(1.31 * t)));
2014-01-11 18:37:01 +00:00
p->vx *= velocity;
p->vy *= velocity;
p->vz *= velocity;
// Color is time-varying
2014-01-11 19:30:37 +00:00
p->r = 0.7f + 0.3f * (float) sin(0.34 * t + 0.1);
p->g = 0.6f + 0.4f * (float) sin(0.63 * t + 1.1);
p->b = 0.6f + 0.4f * (float) sin(0.91 * t + 2.1);
2014-01-11 18:37:01 +00:00
// Store settings for fountain glow lighting
2014-01-11 19:30:37 +00:00
glow_pos[0] = 0.4f * (float) sin(1.34 * t);
glow_pos[1] = 0.4f * (float) sin(3.11 * t);
glow_pos[2] = FOUNTAIN_HEIGHT + 1.f;
glow_pos[3] = 1.f;
2014-01-11 18:37:01 +00:00
glow_color[0] = p->r;
glow_color[1] = p->g;
glow_color[2] = p->b;
2014-01-11 19:30:37 +00:00
glow_color[3] = 1.f;
2014-01-11 18:37:01 +00:00
// The particle is new-born and active
2014-01-11 19:30:37 +00:00
p->life = 1.f;
2014-01-11 18:37:01 +00:00
p->active = 1;
}
//========================================================================
2014-01-11 19:30:37 +00:00
// Update a particle
2014-01-11 18:37:01 +00:00
//========================================================================
#define FOUNTAIN_R2 (FOUNTAIN_RADIUS+PARTICLE_SIZE/2)*(FOUNTAIN_RADIUS+PARTICLE_SIZE/2)
2014-01-11 19:30:37 +00:00
static void update_particle(PARTICLE *p, float dt)
2014-01-11 18:37:01 +00:00
{
// If the particle is not active, we need not do anything
2014-01-11 19:30:37 +00:00
if (!p->active)
2014-01-11 18:37:01 +00:00
return;
// The particle is getting older...
2014-01-11 19:30:37 +00:00
p->life -= dt * (1.f / LIFE_SPAN);
2014-01-11 18:37:01 +00:00
// Did the particle die?
2014-01-11 19:30:37 +00:00
if (p->life <= 0.f)
2014-01-11 18:37:01 +00:00
{
p->active = 0;
return;
}
2014-01-11 19:30:37 +00:00
// Apply gravity
2014-01-11 18:37:01 +00:00
p->vz = p->vz - GRAVITY * dt;
// Update particle position
p->x = p->x + p->vx * dt;
p->y = p->y + p->vy * dt;
p->z = p->z + p->vz * dt;
// Simple collision detection + response
2014-01-11 19:30:37 +00:00
if (p->vz < 0.f)
2014-01-11 18:37:01 +00:00
{
// Particles should bounce on the fountain (with friction)
2014-01-11 19:30:37 +00:00
if ((p->x * p->x + p->y * p->y) < FOUNTAIN_R2 &&
p->z < (FOUNTAIN_HEIGHT + PARTICLE_SIZE / 2))
2014-01-11 18:37:01 +00:00
{
p->vz = -FRICTION * p->vz;
2014-01-11 19:30:37 +00:00
p->z = FOUNTAIN_HEIGHT + PARTICLE_SIZE / 2 +
FRICTION * (FOUNTAIN_HEIGHT +
PARTICLE_SIZE / 2 - p->z);
2014-01-11 18:37:01 +00:00
}
// Particles should bounce on the floor (with friction)
2014-01-11 19:30:37 +00:00
else if (p->z < PARTICLE_SIZE / 2)
2014-01-11 18:37:01 +00:00
{
p->vz = -FRICTION * p->vz;
2014-01-11 19:30:37 +00:00
p->z = PARTICLE_SIZE / 2 +
FRICTION * (PARTICLE_SIZE / 2 - p->z);
2014-01-11 18:37:01 +00:00
}
}
}
//========================================================================
2014-01-11 19:30:37 +00:00
// The main frame for the particle engine. Called once per frame.
2014-01-11 18:37:01 +00:00
//========================================================================
2014-01-11 19:30:37 +00:00
static void particle_engine(double t, float dt)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
int i;
float dt2;
2014-01-11 18:37:01 +00:00
2014-01-11 19:30:37 +00:00
// Update particles (iterated several times per frame if dt is too large)
while (dt > 0.f)
2014-01-11 18:37:01 +00:00
{
// Calculate delta time for this iteration
dt2 = dt < MIN_DELTA_T ? dt : MIN_DELTA_T;
2014-01-11 19:30:37 +00:00
for (i = 0; i < MAX_PARTICLES; i++)
update_particle(&particles[i], dt2);
2014-01-11 18:37:01 +00:00
min_age += dt2;
// Should we create any new particle(s)?
2014-01-11 19:30:37 +00:00
while (min_age >= BIRTH_INTERVAL)
2014-01-11 18:37:01 +00:00
{
min_age -= BIRTH_INTERVAL;
// Find a dead particle to replace with a new one
2014-01-11 19:30:37 +00:00
for (i = 0; i < MAX_PARTICLES; i++)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
if (!particles[i].active)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
init_particle(&particles[i], t + min_age);
update_particle(&particles[i], min_age);
2014-01-11 18:37:01 +00:00
break;
}
}
}
dt -= dt2;
}
}
//========================================================================
2014-01-11 19:30:37 +00:00
// Draw all active particles. We use OpenGL 1.1 vertex
2014-01-11 18:37:01 +00:00
// arrays for this in order to accelerate the drawing.
//========================================================================
#define BATCH_PARTICLES 70 // Number of particles to draw in each batch
// (70 corresponds to 7.5 KB = will not blow
// the L1 data cache on most CPUs)
#define PARTICLE_VERTS 4 // Number of vertices per particle
2014-01-11 19:46:24 +00:00
static void draw_particles(GLFWwindow* window, double t, float dt)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
int i, particle_count;
Vertex vertex_array[BATCH_PARTICLES * PARTICLE_VERTS];
Vertex* vptr;
float alpha;
GLuint rgba;
Vec3 quad_lower_left, quad_lower_right;
GLfloat mat[16];
PARTICLE* pptr;
2014-01-11 18:37:01 +00:00
// Here comes the real trick with flat single primitive objects (s.c.
// "billboards"): We must rotate the textured primitive so that it
// always faces the viewer (is coplanar with the view-plane).
// We:
// 1) Create the primitive around origo (0,0,0)
// 2) Rotate it so that it is coplanar with the view plane
// 3) Translate it according to the particle position
// Note that 1) and 2) is the same for all particles (done only once).
// Get modelview matrix. We will only use the upper left 3x3 part of
// the matrix, which represents the rotation.
2014-01-11 19:30:37 +00:00
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
2014-01-11 18:37:01 +00:00
// 1) & 2) We do it in one swift step:
// Although not obvious, the following six lines represent two matrix/
// vector multiplications. The matrix is the inverse 3x3 rotation
// matrix (i.e. the transpose of the same matrix), and the two vectors
// represent the lower left corner of the quad, PARTICLE_SIZE/2 *
// (-1,-1,0), and the lower right corner, PARTICLE_SIZE/2 * (1,-1,0).
// The upper left/right corners of the quad is always the negative of
// the opposite corners (regardless of rotation).
2014-01-11 19:30:37 +00:00
quad_lower_left.x = (-PARTICLE_SIZE / 2) * (mat[0] + mat[1]);
quad_lower_left.y = (-PARTICLE_SIZE / 2) * (mat[4] + mat[5]);
quad_lower_left.z = (-PARTICLE_SIZE / 2) * (mat[8] + mat[9]);
quad_lower_right.x = (PARTICLE_SIZE / 2) * (mat[0] - mat[1]);
quad_lower_right.y = (PARTICLE_SIZE / 2) * (mat[4] - mat[5]);
quad_lower_right.z = (PARTICLE_SIZE / 2) * (mat[8] - mat[9]);
2014-01-11 18:37:01 +00:00
// Don't update z-buffer, since all particles are transparent!
2014-01-11 19:30:37 +00:00
glDepthMask(GL_FALSE);
2014-01-11 18:37:01 +00:00
2014-01-11 19:30:37 +00:00
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
2014-01-11 18:37:01 +00:00
// Select particle texture
2014-01-11 19:30:37 +00:00
if (!wireframe)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, particle_tex_id);
2014-01-11 18:37:01 +00:00
}
// Set up vertex arrays. We use interleaved arrays, which is easier to
// handle (in most situations) and it gives a linear memory access
2014-01-11 18:37:01 +00:00
// access pattern (which may give better performance in some
// situations). GL_T2F_C4UB_V3F means: 2 floats for texture coords,
// 4 ubytes for color and 3 floats for vertex coord (in that order).
// Most OpenGL cards / drivers are optimized for this format.
2014-01-11 19:30:37 +00:00
glInterleavedArrays(GL_T2F_C4UB_V3F, 0, vertex_array);
2014-01-11 18:37:01 +00:00
2014-01-11 19:46:24 +00:00
// Wait for particle physics thread to be done
mtx_lock(&thread_sync.particles_lock);
while (!glfwWindowShouldClose(window) &&
thread_sync.p_frame <= thread_sync.d_frame)
2014-01-11 18:37:01 +00:00
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_nsec += 100 * 1000 * 1000;
ts.tv_sec += ts.tv_nsec / (1000 * 1000 * 1000);
ts.tv_nsec %= 1000 * 1000 * 1000;
2014-01-11 19:46:24 +00:00
cnd_timedwait(&thread_sync.p_done, &thread_sync.particles_lock, &ts);
}
2014-01-11 18:37:01 +00:00
2014-01-11 19:46:24 +00:00
// Store the frame time and delta time for the physics thread
thread_sync.t = t;
thread_sync.dt = dt;
2014-01-11 18:37:01 +00:00
2014-01-11 19:46:24 +00:00
// Update frame counter
thread_sync.d_frame++;
2014-01-11 18:37:01 +00:00
// Loop through all particles and build vertex arrays.
particle_count = 0;
vptr = vertex_array;
pptr = particles;
2014-01-11 19:30:37 +00:00
for (i = 0; i < MAX_PARTICLES; i++)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
if (pptr->active)
2014-01-11 18:37:01 +00:00
{
// Calculate particle intensity (we set it to max during 75%
// of its life, then it fades out)
2014-01-11 19:30:37 +00:00
alpha = 4.f * pptr->life;
if (alpha > 1.f)
alpha = 1.f;
2014-01-11 18:37:01 +00:00
// Convert color from float to 8-bit (store it in a 32-bit
// integer using endian independent type casting)
2014-01-11 19:30:37 +00:00
((GLubyte*) &rgba)[0] = (GLubyte)(pptr->r * 255.f);
((GLubyte*) &rgba)[1] = (GLubyte)(pptr->g * 255.f);
((GLubyte*) &rgba)[2] = (GLubyte)(pptr->b * 255.f);
((GLubyte*) &rgba)[3] = (GLubyte)(alpha * 255.f);
2014-01-11 18:37:01 +00:00
// 3) Translate the quad to the correct position in modelview
// space and store its parameters in vertex arrays (we also
// store texture coord and color information for each vertex).
// Lower left corner
2014-01-11 19:30:37 +00:00
vptr->s = 0.f;
vptr->t = 0.f;
2014-01-11 18:37:01 +00:00
vptr->rgba = rgba;
vptr->x = pptr->x + quad_lower_left.x;
vptr->y = pptr->y + quad_lower_left.y;
vptr->z = pptr->z + quad_lower_left.z;
vptr ++;
// Lower right corner
2014-01-11 19:30:37 +00:00
vptr->s = 1.f;
vptr->t = 0.f;
2014-01-11 18:37:01 +00:00
vptr->rgba = rgba;
vptr->x = pptr->x + quad_lower_right.x;
vptr->y = pptr->y + quad_lower_right.y;
vptr->z = pptr->z + quad_lower_right.z;
vptr ++;
// Upper right corner
2014-01-11 19:30:37 +00:00
vptr->s = 1.f;
vptr->t = 1.f;
2014-01-11 18:37:01 +00:00
vptr->rgba = rgba;
vptr->x = pptr->x - quad_lower_left.x;
vptr->y = pptr->y - quad_lower_left.y;
vptr->z = pptr->z - quad_lower_left.z;
vptr ++;
// Upper left corner
2014-01-11 19:30:37 +00:00
vptr->s = 0.f;
vptr->t = 1.f;
2014-01-11 18:37:01 +00:00
vptr->rgba = rgba;
vptr->x = pptr->x - quad_lower_right.x;
vptr->y = pptr->y - quad_lower_right.y;
vptr->z = pptr->z - quad_lower_right.z;
vptr ++;
// Increase count of drawable particles
particle_count ++;
}
// If we have filled up one batch of particles, draw it as a set
// of quads using glDrawArrays.
2014-01-11 19:30:37 +00:00
if (particle_count >= BATCH_PARTICLES)
2014-01-11 18:37:01 +00:00
{
// The first argument tells which primitive type we use (QUAD)
// The second argument tells the index of the first vertex (0)
// The last argument is the vertex count
2014-01-11 19:30:37 +00:00
glDrawArrays(GL_QUADS, 0, PARTICLE_VERTS * particle_count);
2014-01-11 18:37:01 +00:00
particle_count = 0;
vptr = vertex_array;
}
// Next particle
2014-01-11 19:30:37 +00:00
pptr++;
2014-01-11 18:37:01 +00:00
}
2014-01-11 19:46:24 +00:00
// We are done with the particle data
mtx_unlock(&thread_sync.particles_lock);
cnd_signal(&thread_sync.d_done);
2014-01-11 18:37:01 +00:00
// Draw final batch of particles (if any)
2014-01-11 19:30:37 +00:00
glDrawArrays(GL_QUADS, 0, PARTICLE_VERTS * particle_count);
2014-01-11 18:37:01 +00:00
// Disable vertex arrays (Note: glInterleavedArrays implicitly called
// glEnableClientState for vertex, texture coord and color arrays)
2014-01-11 19:30:37 +00:00
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
2014-01-11 18:37:01 +00:00
2014-01-11 19:30:37 +00:00
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
2014-01-11 18:37:01 +00:00
2014-01-11 19:30:37 +00:00
glDepthMask(GL_TRUE);
2014-01-11 18:37:01 +00:00
}
//========================================================================
// Fountain geometry specification
//========================================================================
#define FOUNTAIN_SIDE_POINTS 14
#define FOUNTAIN_SWEEP_STEPS 32
2014-01-11 19:30:37 +00:00
static const float fountain_side[FOUNTAIN_SIDE_POINTS * 2] =
{
1.2f, 0.f, 1.f, 0.2f, 0.41f, 0.3f, 0.4f, 0.35f,
0.4f, 1.95f, 0.41f, 2.f, 0.8f, 2.2f, 1.2f, 2.4f,
1.5f, 2.7f, 1.55f,2.95f, 1.6f, 3.f, 1.f, 3.f,
0.5f, 3.f, 0.f, 3.f
2014-01-11 18:37:01 +00:00
};
2014-01-11 19:30:37 +00:00
static const float fountain_normal[FOUNTAIN_SIDE_POINTS * 2] =
{
2014-01-11 18:37:01 +00:00
1.0000f, 0.0000f, 0.6428f, 0.7660f, 0.3420f, 0.9397f, 1.0000f, 0.0000f,
1.0000f, 0.0000f, 0.3420f,-0.9397f, 0.4226f,-0.9063f, 0.5000f,-0.8660f,
0.7660f,-0.6428f, 0.9063f,-0.4226f, 0.0000f,1.00000f, 0.0000f,1.00000f,
0.0000f,1.00000f, 0.0000f,1.00000f
};
//========================================================================
2014-01-11 19:30:37 +00:00
// Draw a fountain
2014-01-11 18:37:01 +00:00
//========================================================================
2014-01-11 19:30:37 +00:00
static void draw_fountain(void)
2014-01-11 18:37:01 +00:00
{
static GLuint fountain_list = 0;
double angle;
float x, y;
int m, n;
// The first time, we build the fountain display list
2014-01-11 19:30:37 +00:00
if (!fountain_list)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
fountain_list = glGenLists(1);
glNewList(fountain_list, GL_COMPILE_AND_EXECUTE);
2014-01-11 18:37:01 +00:00
2014-01-11 19:30:37 +00:00
glMaterialfv(GL_FRONT, GL_DIFFUSE, fountain_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, fountain_specular);
glMaterialf(GL_FRONT, GL_SHININESS, fountain_shininess);
2014-01-11 18:37:01 +00:00
// Build fountain using triangle strips
2014-01-11 19:30:37 +00:00
for (n = 0; n < FOUNTAIN_SIDE_POINTS - 1; n++)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
glBegin(GL_TRIANGLE_STRIP);
for (m = 0; m <= FOUNTAIN_SWEEP_STEPS; m++)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
angle = (double) m * (2.0 * M_PI / (double) FOUNTAIN_SWEEP_STEPS);
x = (float) cos(angle);
y = (float) sin(angle);
2014-01-11 18:37:01 +00:00
// Draw triangle strip
2014-01-11 19:30:37 +00:00
glNormal3f(x * fountain_normal[n * 2 + 2],
y * fountain_normal[n * 2 + 2],
fountain_normal[n * 2 + 3]);
glVertex3f(x * fountain_side[n * 2 + 2],
y * fountain_side[n * 2 + 2],
fountain_side[n * 2 +3 ]);
glNormal3f(x * fountain_normal[n * 2],
y * fountain_normal[n * 2],
fountain_normal[n * 2 + 1]);
glVertex3f(x * fountain_side[n * 2],
y * fountain_side[n * 2],
fountain_side[n * 2 + 1]);
2014-01-11 18:37:01 +00:00
}
2014-01-11 19:30:37 +00:00
2014-01-11 18:37:01 +00:00
glEnd();
}
glEndList();
}
else
2014-01-11 19:30:37 +00:00
glCallList(fountain_list);
2014-01-11 18:37:01 +00:00
}
//========================================================================
// Recursive function for building variable tessellated floor
2014-01-11 18:37:01 +00:00
//========================================================================
2014-01-11 19:30:37 +00:00
static void tessellate_floor(float x1, float y1, float x2, float y2, int depth)
2014-01-11 18:37:01 +00:00
{
float delta, x, y;
// Last recursion?
2014-01-11 19:30:37 +00:00
if (depth >= 5)
delta = 999999.f;
2014-01-11 18:37:01 +00:00
else
{
x = (float) (fabs(x1) < fabs(x2) ? fabs(x1) : fabs(x2));
y = (float) (fabs(y1) < fabs(y2) ? fabs(y1) : fabs(y2));
delta = x*x + y*y;
}
// Recurse further?
2014-01-11 19:30:37 +00:00
if (delta < 0.1f)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
x = (x1 + x2) * 0.5f;
y = (y1 + y2) * 0.5f;
tessellate_floor(x1, y1, x, y, depth + 1);
tessellate_floor(x, y1, x2, y, depth + 1);
tessellate_floor(x1, y, x, y2, depth + 1);
tessellate_floor(x, y, x2, y2, depth + 1);
2014-01-11 18:37:01 +00:00
}
else
{
2014-01-11 19:30:37 +00:00
glTexCoord2f(x1 * 30.f, y1 * 30.f);
glVertex3f( x1 * 80.f, y1 * 80.f, 0.f);
glTexCoord2f(x2 * 30.f, y1 * 30.f);
glVertex3f( x2 * 80.f, y1 * 80.f, 0.f);
glTexCoord2f(x2 * 30.f, y2 * 30.f);
glVertex3f( x2 * 80.f, y2 * 80.f, 0.f);
glTexCoord2f(x1 * 30.f, y2 * 30.f);
glVertex3f( x1 * 80.f, y2 * 80.f, 0.f);
2014-01-11 18:37:01 +00:00
}
}
//========================================================================
2014-01-11 19:30:37 +00:00
// Draw floor. We build the floor recursively and let the tessellation in the
// center (near x,y=0,0) be high, while the tessellation around the edges be
// low.
2014-01-11 18:37:01 +00:00
//========================================================================
2014-01-11 19:30:37 +00:00
static void draw_floor(void)
2014-01-11 18:37:01 +00:00
{
static GLuint floor_list = 0;
2014-01-11 19:30:37 +00:00
if (!wireframe)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, floor_tex_id);
2014-01-11 18:37:01 +00:00
}
// The first time, we build the floor display list
2014-01-11 19:30:37 +00:00
if (!floor_list)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
floor_list = glGenLists(1);
glNewList(floor_list, GL_COMPILE_AND_EXECUTE);
2014-01-11 18:37:01 +00:00
2014-01-11 19:30:37 +00:00
glMaterialfv(GL_FRONT, GL_DIFFUSE, floor_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, floor_specular);
glMaterialf(GL_FRONT, GL_SHININESS, floor_shininess);
2014-01-11 18:37:01 +00:00
// Draw floor as a bunch of triangle strips (high tessellation
2014-01-11 18:37:01 +00:00
// improves lighting)
2014-01-11 19:30:37 +00:00
glNormal3f(0.f, 0.f, 1.f);
glBegin(GL_QUADS);
tessellate_floor(-1.f, -1.f, 0.f, 0.f, 0);
tessellate_floor( 0.f, -1.f, 1.f, 0.f, 0);
tessellate_floor( 0.f, 0.f, 1.f, 1.f, 0);
tessellate_floor(-1.f, 0.f, 0.f, 1.f, 0);
2014-01-11 18:37:01 +00:00
glEnd();
glEndList();
}
else
2014-01-11 19:30:37 +00:00
glCallList(floor_list);
2014-01-11 18:37:01 +00:00
2014-01-11 19:30:37 +00:00
glDisable(GL_TEXTURE_2D);
2014-01-11 18:37:01 +00:00
}
//========================================================================
2014-01-11 19:30:37 +00:00
// Position and configure light sources
2014-01-11 18:37:01 +00:00
//========================================================================
2014-01-11 19:30:37 +00:00
static void setup_lights(void)
2014-01-11 18:37:01 +00:00
{
float l1pos[4], l1amb[4], l1dif[4], l1spec[4];
float l2pos[4], l2amb[4], l2dif[4], l2spec[4];
// Set light source 1 parameters
2014-01-11 19:30:37 +00:00
l1pos[0] = 0.f; l1pos[1] = -9.f; l1pos[2] = 8.f; l1pos[3] = 1.f;
l1amb[0] = 0.2f; l1amb[1] = 0.2f; l1amb[2] = 0.2f; l1amb[3] = 1.f;
l1dif[0] = 0.8f; l1dif[1] = 0.4f; l1dif[2] = 0.2f; l1dif[3] = 1.f;
l1spec[0] = 1.f; l1spec[1] = 0.6f; l1spec[2] = 0.2f; l1spec[3] = 0.f;
2014-01-11 18:37:01 +00:00
// Set light source 2 parameters
2014-01-11 19:30:37 +00:00
l2pos[0] = -15.f; l2pos[1] = 12.f; l2pos[2] = 1.5f; l2pos[3] = 1.f;
l2amb[0] = 0.f; l2amb[1] = 0.f; l2amb[2] = 0.f; l2amb[3] = 1.f;
l2dif[0] = 0.2f; l2dif[1] = 0.4f; l2dif[2] = 0.8f; l2dif[3] = 1.f;
l2spec[0] = 0.2f; l2spec[1] = 0.6f; l2spec[2] = 1.f; l2spec[3] = 0.f;
glLightfv(GL_LIGHT1, GL_POSITION, l1pos);
glLightfv(GL_LIGHT1, GL_AMBIENT, l1amb);
glLightfv(GL_LIGHT1, GL_DIFFUSE, l1dif);
glLightfv(GL_LIGHT1, GL_SPECULAR, l1spec);
glLightfv(GL_LIGHT2, GL_POSITION, l2pos);
glLightfv(GL_LIGHT2, GL_AMBIENT, l2amb);
glLightfv(GL_LIGHT2, GL_DIFFUSE, l2dif);
glLightfv(GL_LIGHT2, GL_SPECULAR, l2spec);
glLightfv(GL_LIGHT3, GL_POSITION, glow_pos);
glLightfv(GL_LIGHT3, GL_DIFFUSE, glow_color);
glLightfv(GL_LIGHT3, GL_SPECULAR, glow_color);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_LIGHT3);
2014-01-11 18:37:01 +00:00
}
//========================================================================
2014-01-11 19:30:37 +00:00
// Main rendering function
2014-01-11 18:37:01 +00:00
//========================================================================
2014-01-11 19:46:24 +00:00
static void draw_scene(GLFWwindow* window, double t)
2014-01-11 18:37:01 +00:00
{
double xpos, ypos, zpos, angle_x, angle_y, angle_z;
static double t_old = 0.0;
2014-01-11 19:30:37 +00:00
float dt;
2015-08-09 13:44:20 +00:00
mat4x4 projection;
2014-01-11 18:37:01 +00:00
// Calculate frame-to-frame delta time
2014-01-11 19:30:37 +00:00
dt = (float) (t - t_old);
2014-01-11 18:37:01 +00:00
t_old = t;
2015-08-09 13:44:20 +00:00
mat4x4_perspective(projection,
65.f * (float) M_PI / 180.f,
aspect_ratio,
1.0, 60.0);
2014-01-11 19:30:37 +00:00
glClearColor(0.1f, 0.1f, 0.1f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2014-01-11 18:37:01 +00:00
2014-01-11 19:30:37 +00:00
glMatrixMode(GL_PROJECTION);
2015-08-09 13:44:20 +00:00
glLoadMatrixf((const GLfloat*) projection);
2014-01-11 18:37:01 +00:00
// Setup camera
2014-01-11 19:30:37 +00:00
glMatrixMode(GL_MODELVIEW);
2014-01-11 18:37:01 +00:00
glLoadIdentity();
// Rotate camera
angle_x = 90.0 - 10.0;
2014-01-11 19:30:37 +00:00
angle_y = 10.0 * sin(0.3 * t);
2014-01-11 18:37:01 +00:00
angle_z = 10.0 * t;
2014-01-11 19:30:37 +00:00
glRotated(-angle_x, 1.0, 0.0, 0.0);
glRotated(-angle_y, 0.0, 1.0, 0.0);
glRotated(-angle_z, 0.0, 0.0, 1.0);
2014-01-11 18:37:01 +00:00
// Translate camera
2014-01-11 19:30:37 +00:00
xpos = 15.0 * sin((M_PI / 180.0) * angle_z) +
2.0 * sin((M_PI / 180.0) * 3.1 * t);
ypos = -15.0 * cos((M_PI / 180.0) * angle_z) +
2.0 * cos((M_PI / 180.0) * 2.9 * t);
zpos = 4.0 + 2.0 * cos((M_PI / 180.0) * 4.9 * t);
glTranslated(-xpos, -ypos, -zpos);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
setup_lights();
glEnable(GL_LIGHTING);
glEnable(GL_FOG);
glFogi(GL_FOG_MODE, GL_EXP);
glFogf(GL_FOG_DENSITY, 0.05f);
glFogfv(GL_FOG_COLOR, fog_color);
draw_floor();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
draw_fountain();
glDisable(GL_LIGHTING);
glDisable(GL_FOG);
// Particles must be drawn after all solid objects have been drawn
2014-01-11 19:46:24 +00:00
draw_particles(window, t, dt);
2014-01-11 18:37:01 +00:00
// Z-buffer not needed anymore
2014-01-11 19:30:37 +00:00
glDisable(GL_DEPTH_TEST);
2014-01-11 18:37:01 +00:00
}
//========================================================================
2014-01-11 19:30:37 +00:00
// Window resize callback function
2014-01-11 18:37:01 +00:00
//========================================================================
2014-01-11 19:46:24 +00:00
static void resize_callback(GLFWwindow* window, int width, int height)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:46:24 +00:00
glViewport(0, 0, width, height);
aspect_ratio = height ? width / (float) height : 1.f;
2014-01-11 18:37:01 +00:00
}
//========================================================================
2014-01-11 19:30:37 +00:00
// Key callback functions
2014-01-11 18:37:01 +00:00
//========================================================================
2014-01-11 19:30:37 +00:00
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
if (action == GLFW_PRESS)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
switch (key)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
case GLFW_KEY_ESCAPE:
2015-08-23 17:30:04 +00:00
glfwSetWindowShouldClose(window, GLFW_TRUE);
2014-01-11 19:30:37 +00:00
break;
case GLFW_KEY_W:
wireframe = !wireframe;
glPolygonMode(GL_FRONT_AND_BACK,
wireframe ? GL_LINE : GL_FILL);
break;
default:
break;
2014-01-11 18:37:01 +00:00
}
}
}
//========================================================================
2014-01-11 19:30:37 +00:00
// Thread for updating particle physics
2014-01-11 18:37:01 +00:00
//========================================================================
2014-01-11 19:30:37 +00:00
static int physics_thread_main(void* arg)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:46:24 +00:00
GLFWwindow* window = arg;
2014-01-11 19:30:37 +00:00
for (;;)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
mtx_lock(&thread_sync.particles_lock);
2014-01-11 18:37:01 +00:00
// Wait for particle drawing to be done
2014-01-11 19:46:24 +00:00
while (!glfwWindowShouldClose(window) &&
thread_sync.p_frame > thread_sync.d_frame)
2014-01-11 18:37:01 +00:00
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_nsec += 100 * 1000 * 1000;
ts.tv_sec += ts.tv_nsec / (1000 * 1000 * 1000);
ts.tv_nsec %= 1000 * 1000 * 1000;
2014-01-11 19:30:37 +00:00
cnd_timedwait(&thread_sync.d_done, &thread_sync.particles_lock, &ts);
2014-01-11 18:37:01 +00:00
}
2014-01-11 19:46:24 +00:00
if (glfwWindowShouldClose(window))
2014-01-11 18:37:01 +00:00
break;
// Update particles
2014-01-11 19:30:37 +00:00
particle_engine(thread_sync.t, thread_sync.dt);
2014-01-11 18:37:01 +00:00
// Update frame counter
2014-01-11 19:30:37 +00:00
thread_sync.p_frame++;
2014-01-11 18:37:01 +00:00
// Unlock mutex and signal drawing thread
2014-01-11 19:30:37 +00:00
mtx_unlock(&thread_sync.particles_lock);
cnd_signal(&thread_sync.p_done);
2014-01-11 18:37:01 +00:00
}
return 0;
2014-01-11 18:37:01 +00:00
}
//========================================================================
2014-01-11 19:30:37 +00:00
// main
2014-01-11 18:37:01 +00:00
//========================================================================
2014-01-11 19:30:37 +00:00
int main(int argc, char** argv)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:46:24 +00:00
int ch, width, height;
2014-01-11 19:30:37 +00:00
thrd_t physics_thread = 0;
GLFWwindow* window;
2014-01-11 19:46:24 +00:00
GLFWmonitor* monitor = NULL;
2014-01-11 18:37:01 +00:00
2014-01-11 19:46:24 +00:00
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
exit(EXIT_FAILURE);
}
2014-01-11 18:37:01 +00:00
2014-02-12 19:56:42 +00:00
while ((ch = getopt(argc, argv, "fh")) != -1)
2014-01-11 18:37:01 +00:00
{
switch (ch)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:46:24 +00:00
case 'f':
monitor = glfwGetPrimaryMonitor();
break;
case 'h':
usage();
exit(EXIT_SUCCESS);
2014-01-11 18:37:01 +00:00
}
}
2014-01-11 19:46:24 +00:00
if (monitor)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:46:24 +00:00
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
width = mode->width;
height = mode->height;
}
else
{
width = 640;
height = 480;
2014-01-11 18:37:01 +00:00
}
2014-01-11 19:46:24 +00:00
window = glfwCreateWindow(width, height, "Particle Engine", monitor, NULL);
2014-01-11 19:30:37 +00:00
if (!window)
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:46:24 +00:00
fprintf(stderr, "Failed to create GLFW window\n");
2014-01-11 18:37:01 +00:00
glfwTerminate();
2014-01-11 19:30:37 +00:00
exit(EXIT_FAILURE);
2014-01-11 18:37:01 +00:00
}
2014-01-11 19:46:24 +00:00
if (monitor)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
2014-01-11 18:37:01 +00:00
glfwMakeContextCurrent(window);
gladLoadGL(glfwGetProcAddress);
2014-01-11 19:46:24 +00:00
glfwSwapInterval(1);
2014-01-11 18:37:01 +00:00
glfwSetFramebufferSizeCallback(window, resize_callback);
2014-01-11 19:30:37 +00:00
glfwSetKeyCallback(window, key_callback);
2014-01-11 18:37:01 +00:00
2014-01-11 19:46:24 +00:00
// Set initial aspect ratio
glfwGetFramebufferSize(window, &width, &height);
2014-01-11 19:46:24 +00:00
resize_callback(window, width, height);
2014-01-11 18:37:01 +00:00
// Upload particle texture
2014-01-11 19:30:37 +00:00
glGenTextures(1, &particle_tex_id);
glBindTexture(GL_TEXTURE_2D, particle_tex_id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, P_TEX_WIDTH, P_TEX_HEIGHT,
0, GL_LUMINANCE, GL_UNSIGNED_BYTE, particle_texture);
2014-01-11 18:37:01 +00:00
// Upload floor texture
2014-01-11 19:30:37 +00:00
glGenTextures(1, &floor_tex_id);
glBindTexture(GL_TEXTURE_2D, floor_tex_id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, F_TEX_WIDTH, F_TEX_HEIGHT,
0, GL_LUMINANCE, GL_UNSIGNED_BYTE, floor_texture);
if (glfwExtensionSupported("GL_EXT_separate_specular_color"))
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:30:37 +00:00
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL_EXT,
GL_SEPARATE_SPECULAR_COLOR_EXT);
2014-01-11 18:37:01 +00:00
}
// Set filled polygon mode as default (not wireframe)
2014-01-11 19:30:37 +00:00
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
2014-01-11 18:37:01 +00:00
wireframe = 0;
// Set initial times
thread_sync.t = 0.0;
thread_sync.dt = 0.001f;
2014-01-11 19:46:24 +00:00
thread_sync.p_frame = 0;
thread_sync.d_frame = 0;
2014-01-11 18:37:01 +00:00
2014-01-11 19:46:24 +00:00
mtx_init(&thread_sync.particles_lock, mtx_timed);
cnd_init(&thread_sync.p_done);
cnd_init(&thread_sync.d_done);
2014-01-11 19:46:24 +00:00
if (thrd_create(&physics_thread, physics_thread_main, window) != thrd_success)
{
glfwTerminate();
exit(EXIT_FAILURE);
2014-01-11 18:37:01 +00:00
}
2014-01-11 19:46:24 +00:00
glfwSetTime(0.0);
2014-01-11 19:30:37 +00:00
2014-01-11 19:46:24 +00:00
while (!glfwWindowShouldClose(window))
2014-01-11 18:37:01 +00:00
{
2014-01-11 19:46:24 +00:00
draw_scene(window, glfwGetTime());
2014-01-11 18:37:01 +00:00
glfwSwapBuffers(window);
glfwPollEvents();
2014-01-11 18:37:01 +00:00
}
2014-01-11 19:30:37 +00:00
2014-01-11 19:46:24 +00:00
thrd_join(physics_thread, NULL);
2014-01-11 18:37:01 +00:00
glfwDestroyWindow(window);
2014-01-11 18:37:01 +00:00
glfwTerminate();
2014-01-11 19:30:37 +00:00
exit(EXIT_SUCCESS);
2014-01-11 18:37:01 +00:00
}