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