textpsys
view src/psys.cc @ 3:b1c8d2784c72
made the timer internal to the effect, fx_draw doesn't take a time value any more
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 20 Aug 2015 06:40:23 +0300 |
parents | a4ffd9e6984c |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <vector>
4 #include <algorithm>
5 #include "opengl.h"
6 #include "psys.h"
8 #define MAX_SPAWNMAP_SAMPLES 2048
10 static double frand();
11 static float rndval(float x, float range);
12 static Particle *palloc();
13 static void pfree(Particle *p);
14 static void pfreelist(Particle *p);
16 void psys_default(PSysParam *pp)
17 {
18 // default parameters
19 pp->spawn_rate = 10.0;
20 pp->spawn_range = 0.0;
21 pp->life = 1.0;
22 pp->life_range = 0.0;
23 pp->size = 1.0;
24 pp->size_range = 0.0;
25 pp->spawn_map = 0;
26 pp->spawn_map_speed = 0.0;
28 pp->gravity = Vector3(0, -9.2, 0);
30 pp->pimg = 0;
31 pp->pcolor_start = pp->pcolor_mid = pp->pcolor_end = Vector3(1, 1, 1);
32 pp->palpha_start = 1.0;
33 pp->palpha_mid = 0.5;
34 pp->palpha_end = 0.0;
35 pp->pscale_start = pp->pscale_mid = pp->pscale_end = 1.0;
36 }
38 ParticleSystem::ParticleSystem()
39 {
40 active = true;
41 active_time = 0.0f;
42 spawn_pending = 0.0f;
43 plist = 0;
44 pcount = 0;
45 smcache = 0;
47 expl = false;
48 expl_force = expl_dur = 0.0f;
49 expl_life = 0.0f;
51 psys_default(&pp);
52 }
54 ParticleSystem::~ParticleSystem()
55 {
56 pfreelist(plist);
57 delete [] smcache;
58 }
60 void ParticleSystem::reset()
61 {
62 pfreelist(plist);
63 delete [] smcache;
64 smcache = 0;
66 active = true;
67 active_time = 0.0f;
68 spawn_pending = 0.0f;
70 psys_default(&pp);
71 }
73 void ParticleSystem::explode(const Vector3 &c, float force, float dur, float life)
74 {
75 if(expl) return;
77 expl_dur = dur;
78 expl_force = force;
79 expl_cent = c;
80 expl_life = life;
81 expl = true;
82 }
84 bool ParticleSystem::alive() const
85 {
86 return active || pcount > 0;
87 }
89 void ParticleSystem::update(float dt)
90 {
91 if(pp.spawn_map && !smcache) {
92 gen_spawnmap(MAX_SPAWNMAP_SAMPLES);
93 }
95 if(active) {
96 active_time += dt;
97 }
99 if(expl) {
100 expl = false;
101 //active = false;
103 Vector3 cent = expl_cent + pos;
105 Particle *p = plist;
106 while(p) {
107 p->max_life = expl_dur;
108 Vector3 dir = p->pos - cent;
109 p->vel += (normalize(dir + Vector3((frand() - 0.5) * 0.5, frand() - 0.5, (frand() - 0.5) * 0.5))) * expl_force;
110 p = p->next;
111 }
112 }
114 if(expl_life > 0.0) {
115 expl_life -= dt;
116 if(expl_life <= 0.0) {
117 expl_life = 0.0;
118 active = false;
119 }
120 }
122 // update active particles
123 Particle *p = plist;
124 while(p) {
125 p->life += dt;
126 if(p->life < p->max_life) {
127 float t = p->life / p->max_life;
129 p->pos = p->pos + p->vel * dt;
130 p->vel = p->vel + pp.gravity * dt;
132 if(t < 0.5) {
133 t *= 2.0;
134 p->color = lerp(pp.pcolor_start, pp.pcolor_mid, t);
135 p->alpha = lerp(pp.palpha_start, pp.palpha_mid, t);
136 p->scale = lerp(pp.pscale_start, pp.pscale_mid, t);
137 } else {
138 t = (t - 0.5) * 2.0;
139 p->color = lerp(pp.pcolor_mid, pp.pcolor_end, t);
140 p->alpha = lerp(pp.palpha_mid, pp.palpha_end, t);
141 p->scale = lerp(pp.pscale_mid, pp.pscale_end, t);
142 }
144 } else {
145 p->life = -1.0;
146 }
147 p = p->next;
148 }
150 // remove dead particles
151 Particle dummy;
152 dummy.next = plist;
153 p = &dummy;
154 while(p->next) {
155 if(p->next->life < 0.0) {
156 Particle *tmp = p->next;
157 p->next = tmp->next;
158 pfree(tmp);
159 --pcount;
160 } else {
161 p = p->next;
162 }
163 }
164 plist = dummy.next;
166 float spawn_rate = pp.spawn_rate;
167 if(pp.spawn_map && pp.spawn_map_speed > 0.0) {
168 float s = active_time * pp.spawn_map_speed;
169 if(s > 1.0) s = 1.0;
170 spawn_rate *= s;
171 }
173 // spawn particles as needed
174 if(active) {
175 spawn_pending += spawn_rate * dt;
177 while(spawn_pending >= 1.0f) {
178 spawn_pending -= 1.0f;
179 spawn_particle();
180 }
181 }
182 }
184 void ParticleSystem::draw() const
185 {
186 int cur_sdr = 0;
187 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
188 if(cur_sdr) {
189 glUseProgram(0);
190 }
192 glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
193 glDisable(GL_LIGHTING);
194 glEnable(GL_BLEND);
195 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
197 if(pp.pimg) {
198 if(!pp.pimg->texture) {
199 pp.pimg->gen_texture();
200 }
201 glEnable(GL_TEXTURE_2D);
202 glBindTexture(GL_TEXTURE_2D, pp.pimg->texture);
203 }
205 glBegin(GL_QUADS);
206 Particle *p = plist;
207 while(p) {
208 float hsz = p->size * p->scale * 0.5;
209 glColor4f(p->color.x, p->color.y, p->color.z, p->alpha);
210 glTexCoord2f(0, 0); glVertex3f(p->pos.x - hsz, p->pos.y - hsz, p->pos.z);
211 glTexCoord2f(1, 0); glVertex3f(p->pos.x + hsz, p->pos.y - hsz, p->pos.z);
212 glTexCoord2f(1, 1); glVertex3f(p->pos.x + hsz, p->pos.y + hsz, p->pos.z);
213 glTexCoord2f(0, 1); glVertex3f(p->pos.x - hsz, p->pos.y + hsz, p->pos.z);
214 p = p->next;
215 }
216 glEnd();
218 glPopAttrib();
220 if(cur_sdr) {
221 glUseProgram(cur_sdr);
222 }
223 }
225 void ParticleSystem::gen_spawnmap(int count)
226 {
227 Image *img = pp.spawn_map;
228 if(!img) return;
230 delete [] smcache;
231 smcache = new Vector3[count];
233 float umax = (float)img->width;
234 float vmax = (float)img->height;
235 float aspect = umax / vmax;
237 // first generate a bunch of random samples by rejection sampling
238 printf("generating %d random spawnmap samples\n", count);
239 for(int i=0; i<count; i++) {
240 float u, v;
241 unsigned char val, ord;
243 do {
244 u = (double)rand() / (double)RAND_MAX;
245 v = (double)rand() / (double)RAND_MAX;
247 int x = (int)(u * umax);
248 int y = (int)(v * vmax);
250 unsigned char *pptr = img->pixels + (y * img->width + x) * 3;
251 val = pptr[0];
252 ord = pptr[1];
253 } while(val < 192);
255 smcache[i] = Vector3(u * 2.0 - 1.0, (1.0 - v * 2.0) / aspect, ord / 255.0);
256 }
258 // then order by z
259 std::sort(smcache, smcache + count,
260 [](const Vector3 &a, const Vector3 &b) { return a.z < b.z; });
262 // precalculate the bounds of each slot
263 smcache_max[0] = 0;
264 for(int i=1; i<255; i++) {
265 float maxval = (float)i / 255.0;
267 int idx = smcache_max[i - 1];
268 while(++idx < count && smcache[idx].z < maxval);
269 smcache_max[i] = idx;
270 }
271 smcache_max[255] = count;
272 }
274 static double frand()
275 {
276 return (double)rand() / (double)RAND_MAX;
277 }
279 static float rndval(float x, float range)
280 {
281 if(fabs(range) < 1e-6) {
282 return x;
283 }
284 return x + (frand() * range - range * 0.5);
285 }
287 void ParticleSystem::spawn_particle()
288 {
289 Particle *p = palloc();
290 p->pos = Vector3(rndval(pos.x, pp.spawn_range),
291 rndval(pos.y, pp.spawn_range),
292 rndval(pos.z, pp.spawn_range));
293 p->vel = Vector3(0, 0, 0);
294 p->color = pp.pcolor_start;
295 p->alpha = pp.palpha_start;
296 p->life = 0.0;
297 p->max_life = rndval(pp.life, pp.life_range);
298 p->size = rndval(pp.size, pp.size_range);
299 p->scale = pp.pscale_start;
301 if(pp.spawn_map) {
302 float maxz = pp.spawn_map_speed > 0.0 ? active_time * pp.spawn_map_speed : 1.0;
303 int max_idx = (int)(maxz * 255.0);
304 if(max_idx > 255) max_idx = 255;
305 if(max_idx < 1) max_idx = 1;
307 int idx = rand() % smcache_max[max_idx];
309 p->pos.x += smcache[idx].x;
310 p->pos.y += smcache[idx].y;
311 }
313 p->next = plist;
314 plist = p;
315 ++pcount;
316 }
318 // particle allocator
319 #define MAX_POOL_SIZE 8192
320 static Particle *ppool;
321 static int ppool_size;
323 static Particle *palloc()
324 {
325 if(ppool) {
326 Particle *p = ppool;
327 ppool = ppool->next;
328 --ppool_size;
329 return p;
330 }
331 return new Particle;
332 }
334 static void pfree(Particle *p)
335 {
336 if(!p) return;
338 if(ppool_size < MAX_POOL_SIZE) {
339 p->next = ppool;
340 ppool = p;
341 ++ppool_size;
342 } else {
343 delete p;
344 }
345 }
347 static void pfreelist(Particle *p)
348 {
349 if(!p) return;
351 Particle *it = p;
352 int new_pool_size = ppool_size;
354 while(it->next && new_pool_size < MAX_POOL_SIZE) {
355 it = it->next;
356 ++new_pool_size;
357 }
359 Particle *last = it;
360 it = it->next;
362 // add the first lot to the pool
363 last->next = ppool;
364 ppool = p;
365 ppool_size = new_pool_size;
367 // delete the rest;
368 while(it) {
369 p = it;
370 it = it->next;
371 delete p;
372 }
373 }