Platformer in OpenGL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1068 lines
35 KiB

5 years ago
  1. //========================================================================
  2. // A simple particle engine with threaded physics
  3. // Copyright (c) Marcus Geelnard
  4. // Copyright (c) Camilla Berglund <elmindreda@glfw.org>
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would
  17. // be appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not
  20. // be misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. //
  25. //========================================================================
  26. #if defined(_MSC_VER)
  27. // Make MS math.h define M_PI
  28. #define _USE_MATH_DEFINES
  29. #endif
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <math.h>
  34. #include <time.h>
  35. #include <tinycthread.h>
  36. #include <getopt.h>
  37. #include <linmath.h>
  38. #include <glad/glad.h>
  39. #include <GLFW/glfw3.h>
  40. // Define tokens for GL_EXT_separate_specular_color if not already defined
  41. #ifndef GL_EXT_separate_specular_color
  42. #define GL_LIGHT_MODEL_COLOR_CONTROL_EXT 0x81F8
  43. #define GL_SINGLE_COLOR_EXT 0x81F9
  44. #define GL_SEPARATE_SPECULAR_COLOR_EXT 0x81FA
  45. #endif // GL_EXT_separate_specular_color
  46. //========================================================================
  47. // Type definitions
  48. //========================================================================
  49. typedef struct
  50. {
  51. float x, y, z;
  52. } Vec3;
  53. // This structure is used for interleaved vertex arrays (see the
  54. // draw_particles function)
  55. //
  56. // NOTE: This structure SHOULD be packed on most systems. It uses 32-bit fields
  57. // on 32-bit boundaries, and is a multiple of 64 bits in total (6x32=3x64). If
  58. // it does not work, try using pragmas or whatever to force the structure to be
  59. // packed.
  60. typedef struct
  61. {
  62. GLfloat s, t; // Texture coordinates
  63. GLuint rgba; // Color (four ubytes packed into an uint)
  64. GLfloat x, y, z; // Vertex coordinates
  65. } Vertex;
  66. //========================================================================
  67. // Program control global variables
  68. //========================================================================
  69. // Window dimensions
  70. float aspect_ratio;
  71. // "wireframe" flag (true if we use wireframe view)
  72. int wireframe;
  73. // Thread synchronization
  74. struct {
  75. double t; // Time (s)
  76. float dt; // Time since last frame (s)
  77. int p_frame; // Particle physics frame number
  78. int d_frame; // Particle draw frame number
  79. cnd_t p_done; // Condition: particle physics done
  80. cnd_t d_done; // Condition: particle draw done
  81. mtx_t particles_lock; // Particles data sharing mutex
  82. } thread_sync;
  83. //========================================================================
  84. // Texture declarations (we hard-code them into the source code, since
  85. // they are so simple)
  86. //========================================================================
  87. #define P_TEX_WIDTH 8 // Particle texture dimensions
  88. #define P_TEX_HEIGHT 8
  89. #define F_TEX_WIDTH 16 // Floor texture dimensions
  90. #define F_TEX_HEIGHT 16
  91. // Texture object IDs
  92. GLuint particle_tex_id, floor_tex_id;
  93. // Particle texture (a simple spot)
  94. const unsigned char particle_texture[ P_TEX_WIDTH * P_TEX_HEIGHT ] = {
  95. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  96. 0x00, 0x00, 0x11, 0x22, 0x22, 0x11, 0x00, 0x00,
  97. 0x00, 0x11, 0x33, 0x88, 0x77, 0x33, 0x11, 0x00,
  98. 0x00, 0x22, 0x88, 0xff, 0xee, 0x77, 0x22, 0x00,
  99. 0x00, 0x22, 0x77, 0xee, 0xff, 0x88, 0x22, 0x00,
  100. 0x00, 0x11, 0x33, 0x77, 0x88, 0x33, 0x11, 0x00,
  101. 0x00, 0x00, 0x11, 0x33, 0x22, 0x11, 0x00, 0x00,
  102. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  103. };
  104. // Floor texture (your basic checkered floor)
  105. const unsigned char floor_texture[ F_TEX_WIDTH * F_TEX_HEIGHT ] = {
  106. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  107. 0xff, 0xf0, 0xcc, 0xf0, 0xf0, 0xf0, 0xff, 0xf0, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  108. 0xf0, 0xcc, 0xee, 0xff, 0xf0, 0xf0, 0xf0, 0xf0, 0x30, 0x66, 0x30, 0x30, 0x30, 0x20, 0x30, 0x30,
  109. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xee, 0xf0, 0xf0, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  110. 0xf0, 0xf0, 0xf0, 0xf0, 0xcc, 0xf0, 0xf0, 0xf0, 0x30, 0x30, 0x55, 0x30, 0x30, 0x44, 0x30, 0x30,
  111. 0xf0, 0xdd, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0x33, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  112. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xff, 0xf0, 0xf0, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x60, 0x30,
  113. 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0x33, 0x33, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  114. 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x33, 0x30, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  115. 0x30, 0x30, 0x30, 0x30, 0x30, 0x20, 0x30, 0x30, 0xf0, 0xff, 0xf0, 0xf0, 0xdd, 0xf0, 0xf0, 0xff,
  116. 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x55, 0x33, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xff, 0xf0, 0xf0,
  117. 0x30, 0x44, 0x66, 0x30, 0x30, 0x30, 0x30, 0x30, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  118. 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xf0, 0xf0, 0xf0, 0xaa, 0xf0, 0xf0, 0xcc, 0xf0,
  119. 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xff, 0xf0, 0xf0, 0xf0, 0xff, 0xf0, 0xdd, 0xf0,
  120. 0x30, 0x30, 0x30, 0x77, 0x30, 0x30, 0x30, 0x30, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  121. 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
  122. };
  123. //========================================================================
  124. // These are fixed constants that control the particle engine. In a
  125. // modular world, these values should be variables...
  126. //========================================================================
  127. // Maximum number of particles
  128. #define MAX_PARTICLES 3000
  129. // Life span of a particle (in seconds)
  130. #define LIFE_SPAN 8.f
  131. // A new particle is born every [BIRTH_INTERVAL] second
  132. #define BIRTH_INTERVAL (LIFE_SPAN/(float)MAX_PARTICLES)
  133. // Particle size (meters)
  134. #define PARTICLE_SIZE 0.7f
  135. // Gravitational constant (m/s^2)
  136. #define GRAVITY 9.8f
  137. // Base initial velocity (m/s)
  138. #define VELOCITY 8.f
  139. // Bounce friction (1.0 = no friction, 0.0 = maximum friction)
  140. #define FRICTION 0.75f
  141. // "Fountain" height (m)
  142. #define FOUNTAIN_HEIGHT 3.f
  143. // Fountain radius (m)
  144. #define FOUNTAIN_RADIUS 1.6f
  145. // Minimum delta-time for particle phisics (s)
  146. #define MIN_DELTA_T (BIRTH_INTERVAL * 0.5f)
  147. //========================================================================
  148. // Particle system global variables
  149. //========================================================================
  150. // This structure holds all state for a single particle
  151. typedef struct {
  152. float x,y,z; // Position in space
  153. float vx,vy,vz; // Velocity vector
  154. float r,g,b; // Color of particle
  155. float life; // Life of particle (1.0 = newborn, < 0.0 = dead)
  156. int active; // Tells if this particle is active
  157. } PARTICLE;
  158. // Global vectors holding all particles. We use two vectors for double
  159. // buffering.
  160. static PARTICLE particles[MAX_PARTICLES];
  161. // Global variable holding the age of the youngest particle
  162. static float min_age;
  163. // Color of latest born particle (used for fountain lighting)
  164. static float glow_color[4];
  165. // Position of latest born particle (used for fountain lighting)
  166. static float glow_pos[4];
  167. //========================================================================
  168. // Object material and fog configuration constants
  169. //========================================================================
  170. const GLfloat fountain_diffuse[4] = { 0.7f, 1.f, 1.f, 1.f };
  171. const GLfloat fountain_specular[4] = { 1.f, 1.f, 1.f, 1.f };
  172. const GLfloat fountain_shininess = 12.f;
  173. const GLfloat floor_diffuse[4] = { 1.f, 0.6f, 0.6f, 1.f };
  174. const GLfloat floor_specular[4] = { 0.6f, 0.6f, 0.6f, 1.f };
  175. const GLfloat floor_shininess = 18.f;
  176. const GLfloat fog_color[4] = { 0.1f, 0.1f, 0.1f, 1.f };
  177. //========================================================================
  178. // Print usage information
  179. //========================================================================
  180. static void usage(void)
  181. {
  182. printf("Usage: particles [-bfhs]\n");
  183. printf("Options:\n");
  184. printf(" -f Run in full screen\n");
  185. printf(" -h Display this help\n");
  186. printf(" -s Run program as single thread (default is to use two threads)\n");
  187. printf("\n");
  188. printf("Program runtime controls:\n");
  189. printf(" W Toggle wireframe mode\n");
  190. printf(" Esc Exit program\n");
  191. }
  192. //========================================================================
  193. // Initialize a new particle
  194. //========================================================================
  195. static void init_particle(PARTICLE *p, double t)
  196. {
  197. float xy_angle, velocity;
  198. // Start position of particle is at the fountain blow-out
  199. p->x = 0.f;
  200. p->y = 0.f;
  201. p->z = FOUNTAIN_HEIGHT;
  202. // Start velocity is up (Z)...
  203. p->vz = 0.7f + (0.3f / 4096.f) * (float) (rand() & 4095);
  204. // ...and a randomly chosen X/Y direction
  205. xy_angle = (2.f * (float) M_PI / 4096.f) * (float) (rand() & 4095);
  206. p->vx = 0.4f * (float) cos(xy_angle);
  207. p->vy = 0.4f * (float) sin(xy_angle);
  208. // Scale velocity vector according to a time-varying velocity
  209. velocity = VELOCITY * (0.8f + 0.1f * (float) (sin(0.5 * t) + sin(1.31 * t)));
  210. p->vx *= velocity;
  211. p->vy *= velocity;
  212. p->vz *= velocity;
  213. // Color is time-varying
  214. p->r = 0.7f + 0.3f * (float) sin(0.34 * t + 0.1);
  215. p->g = 0.6f + 0.4f * (float) sin(0.63 * t + 1.1);
  216. p->b = 0.6f + 0.4f * (float) sin(0.91 * t + 2.1);
  217. // Store settings for fountain glow lighting
  218. glow_pos[0] = 0.4f * (float) sin(1.34 * t);
  219. glow_pos[1] = 0.4f * (float) sin(3.11 * t);
  220. glow_pos[2] = FOUNTAIN_HEIGHT + 1.f;
  221. glow_pos[3] = 1.f;
  222. glow_color[0] = p->r;
  223. glow_color[1] = p->g;
  224. glow_color[2] = p->b;
  225. glow_color[3] = 1.f;
  226. // The particle is new-born and active
  227. p->life = 1.f;
  228. p->active = 1;
  229. }
  230. //========================================================================
  231. // Update a particle
  232. //========================================================================
  233. #define FOUNTAIN_R2 (FOUNTAIN_RADIUS+PARTICLE_SIZE/2)*(FOUNTAIN_RADIUS+PARTICLE_SIZE/2)
  234. static void update_particle(PARTICLE *p, float dt)
  235. {
  236. // If the particle is not active, we need not do anything
  237. if (!p->active)
  238. return;
  239. // The particle is getting older...
  240. p->life -= dt * (1.f / LIFE_SPAN);
  241. // Did the particle die?
  242. if (p->life <= 0.f)
  243. {
  244. p->active = 0;
  245. return;
  246. }
  247. // Apply gravity
  248. p->vz = p->vz - GRAVITY * dt;
  249. // Update particle position
  250. p->x = p->x + p->vx * dt;
  251. p->y = p->y + p->vy * dt;
  252. p->z = p->z + p->vz * dt;
  253. // Simple collision detection + response
  254. if (p->vz < 0.f)
  255. {
  256. // Particles should bounce on the fountain (with friction)
  257. if ((p->x * p->x + p->y * p->y) < FOUNTAIN_R2 &&
  258. p->z < (FOUNTAIN_HEIGHT + PARTICLE_SIZE / 2))
  259. {
  260. p->vz = -FRICTION * p->vz;
  261. p->z = FOUNTAIN_HEIGHT + PARTICLE_SIZE / 2 +
  262. FRICTION * (FOUNTAIN_HEIGHT +
  263. PARTICLE_SIZE / 2 - p->z);
  264. }
  265. // Particles should bounce on the floor (with friction)
  266. else if (p->z < PARTICLE_SIZE / 2)
  267. {
  268. p->vz = -FRICTION * p->vz;
  269. p->z = PARTICLE_SIZE / 2 +
  270. FRICTION * (PARTICLE_SIZE / 2 - p->z);
  271. }
  272. }
  273. }
  274. //========================================================================
  275. // The main frame for the particle engine. Called once per frame.
  276. //========================================================================
  277. static void particle_engine(double t, float dt)
  278. {
  279. int i;
  280. float dt2;
  281. // Update particles (iterated several times per frame if dt is too large)
  282. while (dt > 0.f)
  283. {
  284. // Calculate delta time for this iteration
  285. dt2 = dt < MIN_DELTA_T ? dt : MIN_DELTA_T;
  286. for (i = 0; i < MAX_PARTICLES; i++)
  287. update_particle(&particles[i], dt2);
  288. min_age += dt2;
  289. // Should we create any new particle(s)?
  290. while (min_age >= BIRTH_INTERVAL)
  291. {
  292. min_age -= BIRTH_INTERVAL;
  293. // Find a dead particle to replace with a new one
  294. for (i = 0; i < MAX_PARTICLES; i++)
  295. {
  296. if (!particles[i].active)
  297. {
  298. init_particle(&particles[i], t + min_age);
  299. update_particle(&particles[i], min_age);
  300. break;
  301. }
  302. }
  303. }
  304. dt -= dt2;
  305. }
  306. }
  307. //========================================================================
  308. // Draw all active particles. We use OpenGL 1.1 vertex
  309. // arrays for this in order to accelerate the drawing.
  310. //========================================================================
  311. #define BATCH_PARTICLES 70 // Number of particles to draw in each batch
  312. // (70 corresponds to 7.5 KB = will not blow
  313. // the L1 data cache on most CPUs)
  314. #define PARTICLE_VERTS 4 // Number of vertices per particle
  315. static void draw_particles(GLFWwindow* window, double t, float dt)
  316. {
  317. int i, particle_count;
  318. Vertex vertex_array[BATCH_PARTICLES * PARTICLE_VERTS];
  319. Vertex* vptr;
  320. float alpha;
  321. GLuint rgba;
  322. Vec3 quad_lower_left, quad_lower_right;
  323. GLfloat mat[16];
  324. PARTICLE* pptr;
  325. // Here comes the real trick with flat single primitive objects (s.c.
  326. // "billboards"): We must rotate the textured primitive so that it
  327. // always faces the viewer (is coplanar with the view-plane).
  328. // We:
  329. // 1) Create the primitive around origo (0,0,0)
  330. // 2) Rotate it so that it is coplanar with the view plane
  331. // 3) Translate it according to the particle position
  332. // Note that 1) and 2) is the same for all particles (done only once).
  333. // Get modelview matrix. We will only use the upper left 3x3 part of
  334. // the matrix, which represents the rotation.
  335. glGetFloatv(GL_MODELVIEW_MATRIX, mat);
  336. // 1) & 2) We do it in one swift step:
  337. // Although not obvious, the following six lines represent two matrix/
  338. // vector multiplications. The matrix is the inverse 3x3 rotation
  339. // matrix (i.e. the transpose of the same matrix), and the two vectors
  340. // represent the lower left corner of the quad, PARTICLE_SIZE/2 *
  341. // (-1,-1,0), and the lower right corner, PARTICLE_SIZE/2 * (1,-1,0).
  342. // The upper left/right corners of the quad is always the negative of
  343. // the opposite corners (regardless of rotation).
  344. quad_lower_left.x = (-PARTICLE_SIZE / 2) * (mat[0] + mat[1]);
  345. quad_lower_left.y = (-PARTICLE_SIZE / 2) * (mat[4] + mat[5]);
  346. quad_lower_left.z = (-PARTICLE_SIZE / 2) * (mat[8] + mat[9]);
  347. quad_lower_right.x = (PARTICLE_SIZE / 2) * (mat[0] - mat[1]);
  348. quad_lower_right.y = (PARTICLE_SIZE / 2) * (mat[4] - mat[5]);
  349. quad_lower_right.z = (PARTICLE_SIZE / 2) * (mat[8] - mat[9]);
  350. // Don't update z-buffer, since all particles are transparent!
  351. glDepthMask(GL_FALSE);
  352. glEnable(GL_BLEND);
  353. glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  354. // Select particle texture
  355. if (!wireframe)
  356. {
  357. glEnable(GL_TEXTURE_2D);
  358. glBindTexture(GL_TEXTURE_2D, particle_tex_id);
  359. }
  360. // Set up vertex arrays. We use interleaved arrays, which is easier to
  361. // handle (in most situations) and it gives a linear memeory access
  362. // access pattern (which may give better performance in some
  363. // situations). GL_T2F_C4UB_V3F means: 2 floats for texture coords,
  364. // 4 ubytes for color and 3 floats for vertex coord (in that order).
  365. // Most OpenGL cards / drivers are optimized for this format.
  366. glInterleavedArrays(GL_T2F_C4UB_V3F, 0, vertex_array);
  367. // Wait for particle physics thread to be done
  368. mtx_lock(&thread_sync.particles_lock);
  369. while (!glfwWindowShouldClose(window) &&
  370. thread_sync.p_frame <= thread_sync.d_frame)
  371. {
  372. struct timespec ts;
  373. clock_gettime(CLOCK_REALTIME, &ts);
  374. ts.tv_nsec += 100000000;
  375. cnd_timedwait(&thread_sync.p_done, &thread_sync.particles_lock, &ts);
  376. }
  377. // Store the frame time and delta time for the physics thread
  378. thread_sync.t = t;
  379. thread_sync.dt = dt;
  380. // Update frame counter
  381. thread_sync.d_frame++;
  382. // Loop through all particles and build vertex arrays.
  383. particle_count = 0;
  384. vptr = vertex_array;
  385. pptr = particles;
  386. for (i = 0; i < MAX_PARTICLES; i++)
  387. {
  388. if (pptr->active)
  389. {
  390. // Calculate particle intensity (we set it to max during 75%
  391. // of its life, then it fades out)
  392. alpha = 4.f * pptr->life;
  393. if (alpha > 1.f)
  394. alpha = 1.f;
  395. // Convert color from float to 8-bit (store it in a 32-bit
  396. // integer using endian independent type casting)
  397. ((GLubyte*) &rgba)[0] = (GLubyte)(pptr->r * 255.f);
  398. ((GLubyte*) &rgba)[1] = (GLubyte)(pptr->g * 255.f);
  399. ((GLubyte*) &rgba)[2] = (GLubyte)(pptr->b * 255.f);
  400. ((GLubyte*) &rgba)[3] = (GLubyte)(alpha * 255.f);
  401. // 3) Translate the quad to the correct position in modelview
  402. // space and store its parameters in vertex arrays (we also
  403. // store texture coord and color information for each vertex).
  404. // Lower left corner
  405. vptr->s = 0.f;
  406. vptr->t = 0.f;
  407. vptr->rgba = rgba;
  408. vptr->x = pptr->x + quad_lower_left.x;
  409. vptr->y = pptr->y + quad_lower_left.y;
  410. vptr->z = pptr->z + quad_lower_left.z;
  411. vptr ++;
  412. // Lower right corner
  413. vptr->s = 1.f;
  414. vptr->t = 0.f;
  415. vptr->rgba = rgba;
  416. vptr->x = pptr->x + quad_lower_right.x;
  417. vptr->y = pptr->y + quad_lower_right.y;
  418. vptr->z = pptr->z + quad_lower_right.z;
  419. vptr ++;
  420. // Upper right corner
  421. vptr->s = 1.f;
  422. vptr->t = 1.f;
  423. vptr->rgba = rgba;
  424. vptr->x = pptr->x - quad_lower_left.x;
  425. vptr->y = pptr->y - quad_lower_left.y;
  426. vptr->z = pptr->z - quad_lower_left.z;
  427. vptr ++;
  428. // Upper left corner
  429. vptr->s = 0.f;
  430. vptr->t = 1.f;
  431. vptr->rgba = rgba;
  432. vptr->x = pptr->x - quad_lower_right.x;
  433. vptr->y = pptr->y - quad_lower_right.y;
  434. vptr->z = pptr->z - quad_lower_right.z;
  435. vptr ++;
  436. // Increase count of drawable particles
  437. particle_count ++;
  438. }
  439. // If we have filled up one batch of particles, draw it as a set
  440. // of quads using glDrawArrays.
  441. if (particle_count >= BATCH_PARTICLES)
  442. {
  443. // The first argument tells which primitive type we use (QUAD)
  444. // The second argument tells the index of the first vertex (0)
  445. // The last argument is the vertex count
  446. glDrawArrays(GL_QUADS, 0, PARTICLE_VERTS * particle_count);
  447. particle_count = 0;
  448. vptr = vertex_array;
  449. }
  450. // Next particle
  451. pptr++;
  452. }
  453. // We are done with the particle data
  454. mtx_unlock(&thread_sync.particles_lock);
  455. cnd_signal(&thread_sync.d_done);
  456. // Draw final batch of particles (if any)
  457. glDrawArrays(GL_QUADS, 0, PARTICLE_VERTS * particle_count);
  458. // Disable vertex arrays (Note: glInterleavedArrays implicitly called
  459. // glEnableClientState for vertex, texture coord and color arrays)
  460. glDisableClientState(GL_VERTEX_ARRAY);
  461. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  462. glDisableClientState(GL_COLOR_ARRAY);
  463. glDisable(GL_TEXTURE_2D);
  464. glDisable(GL_BLEND);
  465. glDepthMask(GL_TRUE);
  466. }
  467. //========================================================================
  468. // Fountain geometry specification
  469. //========================================================================
  470. #define FOUNTAIN_SIDE_POINTS 14
  471. #define FOUNTAIN_SWEEP_STEPS 32
  472. static const float fountain_side[FOUNTAIN_SIDE_POINTS * 2] =
  473. {
  474. 1.2f, 0.f, 1.f, 0.2f, 0.41f, 0.3f, 0.4f, 0.35f,
  475. 0.4f, 1.95f, 0.41f, 2.f, 0.8f, 2.2f, 1.2f, 2.4f,
  476. 1.5f, 2.7f, 1.55f,2.95f, 1.6f, 3.f, 1.f, 3.f,
  477. 0.5f, 3.f, 0.f, 3.f
  478. };
  479. static const float fountain_normal[FOUNTAIN_SIDE_POINTS * 2] =
  480. {
  481. 1.0000f, 0.0000f, 0.6428f, 0.7660f, 0.3420f, 0.9397f, 1.0000f, 0.0000f,
  482. 1.0000f, 0.0000f, 0.3420f,-0.9397f, 0.4226f,-0.9063f, 0.5000f,-0.8660f,
  483. 0.7660f,-0.6428f, 0.9063f,-0.4226f, 0.0000f,1.00000f, 0.0000f,1.00000f,
  484. 0.0000f,1.00000f, 0.0000f,1.00000f
  485. };
  486. //========================================================================
  487. // Draw a fountain
  488. //========================================================================
  489. static void draw_fountain(void)
  490. {
  491. static GLuint fountain_list = 0;
  492. double angle;
  493. float x, y;
  494. int m, n;
  495. // The first time, we build the fountain display list
  496. if (!fountain_list)
  497. {
  498. fountain_list = glGenLists(1);
  499. glNewList(fountain_list, GL_COMPILE_AND_EXECUTE);
  500. glMaterialfv(GL_FRONT, GL_DIFFUSE, fountain_diffuse);
  501. glMaterialfv(GL_FRONT, GL_SPECULAR, fountain_specular);
  502. glMaterialf(GL_FRONT, GL_SHININESS, fountain_shininess);
  503. // Build fountain using triangle strips
  504. for (n = 0; n < FOUNTAIN_SIDE_POINTS - 1; n++)
  505. {
  506. glBegin(GL_TRIANGLE_STRIP);
  507. for (m = 0; m <= FOUNTAIN_SWEEP_STEPS; m++)
  508. {
  509. angle = (double) m * (2.0 * M_PI / (double) FOUNTAIN_SWEEP_STEPS);
  510. x = (float) cos(angle);
  511. y = (float) sin(angle);
  512. // Draw triangle strip
  513. glNormal3f(x * fountain_normal[n * 2 + 2],
  514. y * fountain_normal[n * 2 + 2],
  515. fountain_normal[n * 2 + 3]);
  516. glVertex3f(x * fountain_side[n * 2 + 2],
  517. y * fountain_side[n * 2 + 2],
  518. fountain_side[n * 2 +3 ]);
  519. glNormal3f(x * fountain_normal[n * 2],
  520. y * fountain_normal[n * 2],
  521. fountain_normal[n * 2 + 1]);
  522. glVertex3f(x * fountain_side[n * 2],
  523. y * fountain_side[n * 2],
  524. fountain_side[n * 2 + 1]);
  525. }
  526. glEnd();
  527. }
  528. glEndList();
  529. }
  530. else
  531. glCallList(fountain_list);
  532. }
  533. //========================================================================
  534. // Recursive function for building variable tesselated floor
  535. //========================================================================
  536. static void tessellate_floor(float x1, float y1, float x2, float y2, int depth)
  537. {
  538. float delta, x, y;
  539. // Last recursion?
  540. if (depth >= 5)
  541. delta = 999999.f;
  542. else
  543. {
  544. x = (float) (fabs(x1) < fabs(x2) ? fabs(x1) : fabs(x2));
  545. y = (float) (fabs(y1) < fabs(y2) ? fabs(y1) : fabs(y2));
  546. delta = x*x + y*y;
  547. }
  548. // Recurse further?
  549. if (delta < 0.1f)
  550. {
  551. x = (x1 + x2) * 0.5f;
  552. y = (y1 + y2) * 0.5f;
  553. tessellate_floor(x1, y1, x, y, depth + 1);
  554. tessellate_floor(x, y1, x2, y, depth + 1);
  555. tessellate_floor(x1, y, x, y2, depth + 1);
  556. tessellate_floor(x, y, x2, y2, depth + 1);
  557. }
  558. else
  559. {
  560. glTexCoord2f(x1 * 30.f, y1 * 30.f);
  561. glVertex3f( x1 * 80.f, y1 * 80.f, 0.f);
  562. glTexCoord2f(x2 * 30.f, y1 * 30.f);
  563. glVertex3f( x2 * 80.f, y1 * 80.f, 0.f);
  564. glTexCoord2f(x2 * 30.f, y2 * 30.f);
  565. glVertex3f( x2 * 80.f, y2 * 80.f, 0.f);
  566. glTexCoord2f(x1 * 30.f, y2 * 30.f);
  567. glVertex3f( x1 * 80.f, y2 * 80.f, 0.f);
  568. }
  569. }
  570. //========================================================================
  571. // Draw floor. We build the floor recursively and let the tessellation in the
  572. // center (near x,y=0,0) be high, while the tessellation around the edges be
  573. // low.
  574. //========================================================================
  575. static void draw_floor(void)
  576. {
  577. static GLuint floor_list = 0;
  578. if (!wireframe)
  579. {
  580. glEnable(GL_TEXTURE_2D);
  581. glBindTexture(GL_TEXTURE_2D, floor_tex_id);
  582. }
  583. // The first time, we build the floor display list
  584. if (!floor_list)
  585. {
  586. floor_list = glGenLists(1);
  587. glNewList(floor_list, GL_COMPILE_AND_EXECUTE);
  588. glMaterialfv(GL_FRONT, GL_DIFFUSE, floor_diffuse);
  589. glMaterialfv(GL_FRONT, GL_SPECULAR, floor_specular);
  590. glMaterialf(GL_FRONT, GL_SHININESS, floor_shininess);
  591. // Draw floor as a bunch of triangle strips (high tesselation
  592. // improves lighting)
  593. glNormal3f(0.f, 0.f, 1.f);
  594. glBegin(GL_QUADS);
  595. tessellate_floor(-1.f, -1.f, 0.f, 0.f, 0);
  596. tessellate_floor( 0.f, -1.f, 1.f, 0.f, 0);
  597. tessellate_floor( 0.f, 0.f, 1.f, 1.f, 0);
  598. tessellate_floor(-1.f, 0.f, 0.f, 1.f, 0);
  599. glEnd();
  600. glEndList();
  601. }
  602. else
  603. glCallList(floor_list);
  604. glDisable(GL_TEXTURE_2D);
  605. }
  606. //========================================================================
  607. // Position and configure light sources
  608. //========================================================================
  609. static void setup_lights(void)
  610. {
  611. float l1pos[4], l1amb[4], l1dif[4], l1spec[4];
  612. float l2pos[4], l2amb[4], l2dif[4], l2spec[4];
  613. // Set light source 1 parameters
  614. l1pos[0] = 0.f; l1pos[1] = -9.f; l1pos[2] = 8.f; l1pos[3] = 1.f;
  615. l1amb[0] = 0.2f; l1amb[1] = 0.2f; l1amb[2] = 0.2f; l1amb[3] = 1.f;
  616. l1dif[0] = 0.8f; l1dif[1] = 0.4f; l1dif[2] = 0.2f; l1dif[3] = 1.f;
  617. l1spec[0] = 1.f; l1spec[1] = 0.6f; l1spec[2] = 0.2f; l1spec[3] = 0.f;
  618. // Set light source 2 parameters
  619. l2pos[0] = -15.f; l2pos[1] = 12.f; l2pos[2] = 1.5f; l2pos[3] = 1.f;
  620. l2amb[0] = 0.f; l2amb[1] = 0.f; l2amb[2] = 0.f; l2amb[3] = 1.f;
  621. l2dif[0] = 0.2f; l2dif[1] = 0.4f; l2dif[2] = 0.8f; l2dif[3] = 1.f;
  622. l2spec[0] = 0.2f; l2spec[1] = 0.6f; l2spec[2] = 1.f; l2spec[3] = 0.f;
  623. glLightfv(GL_LIGHT1, GL_POSITION, l1pos);
  624. glLightfv(GL_LIGHT1, GL_AMBIENT, l1amb);
  625. glLightfv(GL_LIGHT1, GL_DIFFUSE, l1dif);
  626. glLightfv(GL_LIGHT1, GL_SPECULAR, l1spec);
  627. glLightfv(GL_LIGHT2, GL_POSITION, l2pos);
  628. glLightfv(GL_LIGHT2, GL_AMBIENT, l2amb);
  629. glLightfv(GL_LIGHT2, GL_DIFFUSE, l2dif);
  630. glLightfv(GL_LIGHT2, GL_SPECULAR, l2spec);
  631. glLightfv(GL_LIGHT3, GL_POSITION, glow_pos);
  632. glLightfv(GL_LIGHT3, GL_DIFFUSE, glow_color);
  633. glLightfv(GL_LIGHT3, GL_SPECULAR, glow_color);
  634. glEnable(GL_LIGHT1);
  635. glEnable(GL_LIGHT2);
  636. glEnable(GL_LIGHT3);
  637. }
  638. //========================================================================
  639. // Main rendering function
  640. //========================================================================
  641. static void draw_scene(GLFWwindow* window, double t)
  642. {
  643. double xpos, ypos, zpos, angle_x, angle_y, angle_z;
  644. static double t_old = 0.0;
  645. float dt;
  646. mat4x4 projection;
  647. // Calculate frame-to-frame delta time
  648. dt = (float) (t - t_old);
  649. t_old = t;
  650. mat4x4_perspective(projection,
  651. 65.f * (float) M_PI / 180.f,
  652. aspect_ratio,
  653. 1.0, 60.0);
  654. glClearColor(0.1f, 0.1f, 0.1f, 1.f);
  655. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  656. glMatrixMode(GL_PROJECTION);
  657. glLoadMatrixf((const GLfloat*) projection);
  658. // Setup camera
  659. glMatrixMode(GL_MODELVIEW);
  660. glLoadIdentity();
  661. // Rotate camera
  662. angle_x = 90.0 - 10.0;
  663. angle_y = 10.0 * sin(0.3 * t);
  664. angle_z = 10.0 * t;
  665. glRotated(-angle_x, 1.0, 0.0, 0.0);
  666. glRotated(-angle_y, 0.0, 1.0, 0.0);
  667. glRotated(-angle_z, 0.0, 0.0, 1.0);
  668. // Translate camera
  669. xpos = 15.0 * sin((M_PI / 180.0) * angle_z) +
  670. 2.0 * sin((M_PI / 180.0) * 3.1 * t);
  671. ypos = -15.0 * cos((M_PI / 180.0) * angle_z) +
  672. 2.0 * cos((M_PI / 180.0) * 2.9 * t);
  673. zpos = 4.0 + 2.0 * cos((M_PI / 180.0) * 4.9 * t);
  674. glTranslated(-xpos, -ypos, -zpos);
  675. glFrontFace(GL_CCW);
  676. glCullFace(GL_BACK);
  677. glEnable(GL_CULL_FACE);
  678. setup_lights();
  679. glEnable(GL_LIGHTING);
  680. glEnable(GL_FOG);
  681. glFogi(GL_FOG_MODE, GL_EXP);
  682. glFogf(GL_FOG_DENSITY, 0.05f);
  683. glFogfv(GL_FOG_COLOR, fog_color);
  684. draw_floor();
  685. glEnable(GL_DEPTH_TEST);
  686. glDepthFunc(GL_LEQUAL);
  687. glDepthMask(GL_TRUE);
  688. draw_fountain();
  689. glDisable(GL_LIGHTING);
  690. glDisable(GL_FOG);
  691. // Particles must be drawn after all solid objects have been drawn
  692. draw_particles(window, t, dt);
  693. // Z-buffer not needed anymore
  694. glDisable(GL_DEPTH_TEST);
  695. }
  696. //========================================================================
  697. // Window resize callback function
  698. //========================================================================
  699. static void resize_callback(GLFWwindow* window, int width, int height)
  700. {
  701. glViewport(0, 0, width, height);
  702. aspect_ratio = height ? width / (float) height : 1.f;
  703. }
  704. //========================================================================
  705. // Key callback functions
  706. //========================================================================
  707. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  708. {
  709. if (action == GLFW_PRESS)
  710. {
  711. switch (key)
  712. {
  713. case GLFW_KEY_ESCAPE:
  714. glfwSetWindowShouldClose(window, GLFW_TRUE);
  715. break;
  716. case GLFW_KEY_W:
  717. wireframe = !wireframe;
  718. glPolygonMode(GL_FRONT_AND_BACK,
  719. wireframe ? GL_LINE : GL_FILL);
  720. break;
  721. default:
  722. break;
  723. }
  724. }
  725. }
  726. //========================================================================
  727. // Thread for updating particle physics
  728. //========================================================================
  729. static int physics_thread_main(void* arg)
  730. {
  731. GLFWwindow* window = arg;
  732. for (;;)
  733. {
  734. mtx_lock(&thread_sync.particles_lock);
  735. // Wait for particle drawing to be done
  736. while (!glfwWindowShouldClose(window) &&
  737. thread_sync.p_frame > thread_sync.d_frame)
  738. {
  739. struct timespec ts;
  740. clock_gettime(CLOCK_REALTIME, &ts);
  741. ts.tv_nsec += 100000000;
  742. cnd_timedwait(&thread_sync.d_done, &thread_sync.particles_lock, &ts);
  743. }
  744. if (glfwWindowShouldClose(window))
  745. break;
  746. // Update particles
  747. particle_engine(thread_sync.t, thread_sync.dt);
  748. // Update frame counter
  749. thread_sync.p_frame++;
  750. // Unlock mutex and signal drawing thread
  751. mtx_unlock(&thread_sync.particles_lock);
  752. cnd_signal(&thread_sync.p_done);
  753. }
  754. return 0;
  755. }
  756. //========================================================================
  757. // main
  758. //========================================================================
  759. int main(int argc, char** argv)
  760. {
  761. int ch, width, height;
  762. thrd_t physics_thread = 0;
  763. GLFWwindow* window;
  764. GLFWmonitor* monitor = NULL;
  765. if (!glfwInit())
  766. {
  767. fprintf(stderr, "Failed to initialize GLFW\n");
  768. exit(EXIT_FAILURE);
  769. }
  770. while ((ch = getopt(argc, argv, "fh")) != -1)
  771. {
  772. switch (ch)
  773. {
  774. case 'f':
  775. monitor = glfwGetPrimaryMonitor();
  776. break;
  777. case 'h':
  778. usage();
  779. exit(EXIT_SUCCESS);
  780. }
  781. }
  782. if (monitor)
  783. {
  784. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  785. glfwWindowHint(GLFW_RED_BITS, mode->redBits);
  786. glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
  787. glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
  788. glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
  789. width = mode->width;
  790. height = mode->height;
  791. }
  792. else
  793. {
  794. width = 640;
  795. height = 480;
  796. }
  797. window = glfwCreateWindow(width, height, "Particle Engine", monitor, NULL);
  798. if (!window)
  799. {
  800. fprintf(stderr, "Failed to create GLFW window\n");
  801. glfwTerminate();
  802. exit(EXIT_FAILURE);
  803. }
  804. if (monitor)
  805. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  806. glfwMakeContextCurrent(window);
  807. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  808. glfwSwapInterval(1);
  809. glfwSetFramebufferSizeCallback(window, resize_callback);
  810. glfwSetKeyCallback(window, key_callback);
  811. // Set initial aspect ratio
  812. glfwGetFramebufferSize(window, &width, &height);
  813. resize_callback(window, width, height);
  814. // Upload particle texture
  815. glGenTextures(1, &particle_tex_id);
  816. glBindTexture(GL_TEXTURE_2D, particle_tex_id);
  817. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  818. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  819. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  820. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  821. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  822. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, P_TEX_WIDTH, P_TEX_HEIGHT,
  823. 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, particle_texture);
  824. // Upload floor texture
  825. glGenTextures(1, &floor_tex_id);
  826. glBindTexture(GL_TEXTURE_2D, floor_tex_id);
  827. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  828. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  829. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  830. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  831. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  832. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, F_TEX_WIDTH, F_TEX_HEIGHT,
  833. 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, floor_texture);
  834. if (glfwExtensionSupported("GL_EXT_separate_specular_color"))
  835. {
  836. glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL_EXT,
  837. GL_SEPARATE_SPECULAR_COLOR_EXT);
  838. }
  839. // Set filled polygon mode as default (not wireframe)
  840. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  841. wireframe = 0;
  842. // Set initial times
  843. thread_sync.t = 0.0;
  844. thread_sync.dt = 0.001f;
  845. thread_sync.p_frame = 0;
  846. thread_sync.d_frame = 0;
  847. mtx_init(&thread_sync.particles_lock, mtx_timed);
  848. cnd_init(&thread_sync.p_done);
  849. cnd_init(&thread_sync.d_done);
  850. if (thrd_create(&physics_thread, physics_thread_main, window) != thrd_success)
  851. {
  852. glfwTerminate();
  853. exit(EXIT_FAILURE);
  854. }
  855. glfwSetTime(0.0);
  856. while (!glfwWindowShouldClose(window))
  857. {
  858. draw_scene(window, glfwGetTime());
  859. glfwSwapBuffers(window);
  860. glfwPollEvents();
  861. }
  862. thrd_join(physics_thread, NULL);
  863. glfwDestroyWindow(window);
  864. glfwTerminate();
  865. exit(EXIT_SUCCESS);
  866. }