textpsys
view src/psys.cc @ 1:57c6f7b70126
renamed to textpsys
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 19 Aug 2015 09:17:20 +0300 |
parents | |
children | 4b1360a5d54d |
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::explode(const Vector3 &c, float force, float dur, float life)
61 {
62 expl_dur = dur;
63 expl_force = force;
64 expl_cent = c;
65 expl_life = life;
66 expl = true;
67 }
69 bool ParticleSystem::alive() const
70 {
71 return active || pcount > 0;
72 }
74 void ParticleSystem::update(float dt)
75 {
76 if(pp.spawn_map && !smcache) {
77 gen_spawnmap(MAX_SPAWNMAP_SAMPLES);
78 }
80 if(active) {
81 active_time += dt;
82 }
84 if(expl) {
85 expl = false;
86 //active = false;
88 Vector3 cent = expl_cent + pos;
90 Particle *p = plist;
91 while(p) {
92 p->max_life = expl_dur;
93 Vector3 dir = p->pos - cent;
94 p->vel += (normalize(dir + Vector3((frand() - 0.5) * 0.5, frand() - 0.5, (frand() - 0.5) * 0.5))) * expl_force;
95 p = p->next;
96 }
97 }
99 if(expl_life > 0.0) {
100 expl_life -= dt;
101 if(expl_life <= 0.0) {
102 expl_life = 0.0;
103 active = false;
104 }
105 }
107 // update active particles
108 Particle *p = plist;
109 while(p) {
110 p->life += dt;
111 if(p->life < p->max_life) {
112 float t = p->life / p->max_life;
114 p->pos = p->pos + p->vel * dt;
115 p->vel = p->vel + pp.gravity * dt;
117 if(t < 0.5) {
118 t *= 2.0;
119 p->color = lerp(pp.pcolor_start, pp.pcolor_mid, t);
120 p->alpha = lerp(pp.palpha_start, pp.palpha_mid, t);
121 p->scale = lerp(pp.pscale_start, pp.pscale_mid, t);
122 } else {
123 t = (t - 0.5) * 2.0;
124 p->color = lerp(pp.pcolor_mid, pp.pcolor_end, t);
125 p->alpha = lerp(pp.palpha_mid, pp.palpha_end, t);
126 p->scale = lerp(pp.pscale_mid, pp.pscale_end, t);
127 }
129 } else {
130 p->life = -1.0;
131 }
132 p = p->next;
133 }
135 // remove dead particles
136 Particle dummy;
137 dummy.next = plist;
138 p = &dummy;
139 while(p->next) {
140 if(p->next->life < 0.0) {
141 Particle *tmp = p->next;
142 p->next = tmp->next;
143 pfree(tmp);
144 --pcount;
145 } else {
146 p = p->next;
147 }
148 }
149 plist = dummy.next;
151 float spawn_rate = pp.spawn_rate;
152 if(pp.spawn_map && pp.spawn_map_speed > 0.0) {
153 float s = active_time * pp.spawn_map_speed;
154 if(s > 1.0) s = 1.0;
155 spawn_rate *= s;
156 }
158 // spawn particles as needed
159 if(active) {
160 spawn_pending += spawn_rate * dt;
162 while(spawn_pending >= 1.0f) {
163 spawn_pending -= 1.0f;
164 spawn_particle();
165 }
166 }
167 }
169 void ParticleSystem::draw() const
170 {
171 int cur_sdr = 0;
172 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr);
173 if(cur_sdr) {
174 glUseProgram(0);
175 }
177 glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
178 glDisable(GL_LIGHTING);
179 glEnable(GL_BLEND);
180 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
182 if(pp.pimg) {
183 if(!pp.pimg->texture) {
184 pp.pimg->gen_texture();
185 }
186 glEnable(GL_TEXTURE_2D);
187 glBindTexture(GL_TEXTURE_2D, pp.pimg->texture);
188 }
190 glBegin(GL_QUADS);
191 Particle *p = plist;
192 while(p) {
193 float hsz = p->size * p->scale * 0.5;
194 glColor4f(p->color.x, p->color.y, p->color.z, p->alpha);
195 glTexCoord2f(0, 0); glVertex3f(p->pos.x - hsz, p->pos.y - hsz, p->pos.z);
196 glTexCoord2f(1, 0); glVertex3f(p->pos.x + hsz, p->pos.y - hsz, p->pos.z);
197 glTexCoord2f(1, 1); glVertex3f(p->pos.x + hsz, p->pos.y + hsz, p->pos.z);
198 glTexCoord2f(0, 1); glVertex3f(p->pos.x - hsz, p->pos.y + hsz, p->pos.z);
199 p = p->next;
200 }
201 glEnd();
203 glPopAttrib();
205 if(cur_sdr) {
206 glUseProgram(cur_sdr);
207 }
208 }
210 void ParticleSystem::gen_spawnmap(int count)
211 {
212 Image *img = pp.spawn_map;
213 if(!img) return;
215 delete [] smcache;
216 smcache = new Vector3[count];
218 float umax = (float)img->width;
219 float vmax = (float)img->height;
220 float aspect = umax / vmax;
222 // first generate a bunch of random samples by rejection sampling
223 printf("generating %d random spawnmap samples\n", count);
224 for(int i=0; i<count; i++) {
225 float u, v;
226 unsigned char val, ord;
228 do {
229 u = (double)rand() / (double)RAND_MAX;
230 v = (double)rand() / (double)RAND_MAX;
232 int x = (int)(u * umax);
233 int y = (int)(v * vmax);
235 unsigned char *pptr = img->pixels + (y * img->width + x) * 3;
236 val = pptr[0];
237 ord = pptr[1];
238 } while(val < 192);
240 smcache[i] = Vector3(u * 2.0 - 1.0, (1.0 - v * 2.0) / aspect, ord / 255.0);
241 }
243 // then order by z
244 std::sort(smcache, smcache + count,
245 [](const Vector3 &a, const Vector3 &b) { return a.z < b.z; });
247 // precalculate the bounds of each slot
248 smcache_max[0] = 0;
249 for(int i=1; i<255; i++) {
250 float maxval = (float)i / 255.0;
252 int idx = smcache_max[i - 1];
253 while(++idx < count && smcache[idx].z < maxval);
254 smcache_max[i] = idx;
255 }
256 smcache_max[255] = count;
257 }
259 static double frand()
260 {
261 return (double)rand() / (double)RAND_MAX;
262 }
264 static float rndval(float x, float range)
265 {
266 if(fabs(range) < 1e-6) {
267 return x;
268 }
269 return x + (frand() * range - range * 0.5);
270 }
272 void ParticleSystem::spawn_particle()
273 {
274 Particle *p = palloc();
275 p->pos = Vector3(rndval(pos.x, pp.spawn_range),
276 rndval(pos.y, pp.spawn_range),
277 rndval(pos.z, pp.spawn_range));
278 p->vel = Vector3(0, 0, 0);
279 p->color = pp.pcolor_start;
280 p->alpha = pp.palpha_start;
281 p->life = 0.0;
282 p->max_life = rndval(pp.life, pp.life_range);
283 p->size = rndval(pp.size, pp.size_range);
284 p->scale = pp.pscale_start;
286 if(pp.spawn_map) {
287 float maxz = pp.spawn_map_speed > 0.0 ? active_time * pp.spawn_map_speed : 1.0;
288 int max_idx = (int)(maxz * 255.0);
289 if(max_idx > 255) max_idx = 255;
290 if(max_idx < 1) max_idx = 1;
292 int idx = rand() % smcache_max[max_idx];
294 p->pos.x += smcache[idx].x;
295 p->pos.y += smcache[idx].y;
296 }
298 p->next = plist;
299 plist = p;
300 ++pcount;
301 }
303 // particle allocator
304 #define MAX_POOL_SIZE 8192
305 static Particle *ppool;
306 static int ppool_size;
308 static Particle *palloc()
309 {
310 if(ppool) {
311 Particle *p = ppool;
312 ppool = ppool->next;
313 --ppool_size;
314 return p;
315 }
316 return new Particle;
317 }
319 static void pfree(Particle *p)
320 {
321 if(!p) return;
323 if(ppool_size < MAX_POOL_SIZE) {
324 p->next = ppool;
325 ppool = p;
326 ++ppool_size;
327 } else {
328 delete p;
329 }
330 }
332 static void pfreelist(Particle *p)
333 {
334 if(!p) return;
336 Particle *it = p;
337 int new_pool_size = ppool_size;
339 while(it->next && new_pool_size < MAX_POOL_SIZE) {
340 it = it->next;
341 ++new_pool_size;
342 }
344 Particle *last = it;
345 it = it->next;
347 // add the first lot to the pool
348 last->next = ppool;
349 ppool = p;
350 ppool_size = new_pool_size;
352 // delete the rest;
353 while(it) {
354 p = it;
355 it = it->next;
356 delete p;
357 }
358 }