particles.c 35 KB

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