]> icculus.org git repositories - divverent/darkplaces.git/blob - r_explosion.c
smoke is alpha again (not additive), rather dark in appearance, lingers longer
[divverent/darkplaces.git] / r_explosion.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "quakedef.h"
22
23 #define MAX_EXPLOSIONS 64
24 #define EXPLOSIONGRID 8
25 #define EXPLOSIONVERTS ((EXPLOSIONGRID+1)*(EXPLOSIONGRID+1))
26 #define EXPLOSIONTRIS (EXPLOSIONGRID*EXPLOSIONGRID*2)
27 #define EXPLOSIONSTARTVELOCITY (256.0f)
28 //#define EXPLOSIONSTARTVELOCITY (384.0f)
29 //#define EXPLOSIONRANDOMVELOCITY (32.0f)
30 #define EXPLOSIONFADESTART (1.5f)
31 //#define EXPLOSIONFADERATE (4.5f)
32 #define EXPLOSIONFADERATE (3.0f)
33 /*
34 #define MAX_EXPLOSIONGAS (MAX_EXPLOSIONS * EXPLOSIONGAS)
35 #define EXPLOSIONGAS 8
36 #define EXPLOSIONGASSTARTRADIUS (15.0f)
37 #define EXPLOSIONGASSTARTVELOCITY (50.0f)
38 #define GASDENSITY_SCALER (32768.0f / EXPLOSIONGAS)
39 #define GASFADERATE (GASDENSITY_SCALER * EXPLOSIONGAS * 2)
40
41 typedef struct explosiongas_s
42 {
43         float pressure;
44         vec3_t origin;
45         vec3_t velocity;
46 }
47 explosiongas_t;
48
49 explosiongas_t explosiongas[MAX_EXPLOSIONGAS];
50 */
51
52 float explosiontexcoords[EXPLOSIONVERTS][2];
53 int explosiontris[EXPLOSIONTRIS][3];
54 int explosionnoiseindex[EXPLOSIONVERTS];
55 vec3_t explosionpoint[EXPLOSIONVERTS];
56 vec3_t explosionspherevertvel[EXPLOSIONVERTS];
57
58 typedef struct explosion_s
59 {
60         float starttime;
61         float time;
62         float alpha;
63         vec3_t origin;
64         vec3_t vert[EXPLOSIONVERTS];
65         vec3_t vertvel[EXPLOSIONVERTS];
66 }
67 explosion_t;
68
69 explosion_t explosion[MAX_EXPLOSIONS];
70
71 rtexture_t      *explosiontexture;
72 rtexture_t      *explosiontexturefog;
73
74 rtexturepool_t  *explosiontexturepool;
75
76 cvar_t r_explosionclip = {CVAR_SAVE, "r_explosionclip", "1"};
77 cvar_t r_drawexplosions = {0, "r_drawexplosions", "1"};
78
79 void r_explosion_start(void)
80 {
81         int x, y;
82         qbyte noise1[128][128], noise2[128][128], noise3[128][128], data[128][128][4];
83         explosiontexturepool = R_AllocTexturePool();
84         fractalnoise(&noise1[0][0], 128, 32);
85         fractalnoise(&noise2[0][0], 128, 4);
86         fractalnoise(&noise3[0][0], 128, 4);
87         for (y = 0;y < 128;y++)
88         {
89                 for (x = 0;x < 128;x++)
90                 {
91                         int j, r, g, b, a;
92                         j = (noise1[y][x] * noise2[y][x]) * 3 / 256 - 128;
93                         r = (j * 512) / 256;
94                         g = (j * 256) / 256;
95                         b = (j * 128) / 256;
96                         a = noise3[y][x] * 3 - 128;
97                         data[y][x][0] = bound(0, r, 255);
98                         data[y][x][1] = bound(0, g, 255);
99                         data[y][x][2] = bound(0, b, 255);
100                         data[y][x][3] = bound(0, a, 255);
101                 }
102         }
103         explosiontexture = R_LoadTexture (explosiontexturepool, "explosiontexture", 128, 128, &data[0][0][0], TEXTYPE_RGBA, TEXF_MIPMAP | TEXF_ALPHA | TEXF_PRECACHE);
104         for (y = 0;y < 128;y++)
105                 for (x = 0;x < 128;x++)
106                         data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
107         explosiontexturefog = R_LoadTexture (explosiontexturepool, "explosiontexturefog", 128, 128, &data[0][0][0], TEXTYPE_RGBA, TEXF_MIPMAP | TEXF_ALPHA | TEXF_PRECACHE);
108         // note that explosions survive the restart
109 }
110
111 void r_explosion_shutdown(void)
112 {
113         R_FreeTexturePool(&explosiontexturepool);
114 }
115
116 void r_explosion_newmap(void)
117 {
118         memset(explosion, 0, sizeof(explosion));
119 //      memset(explosiongas, 0, sizeof(explosiongas));
120 }
121
122 int R_ExplosionVert(int column, int row)
123 {
124         int i;
125         float a, b, c;
126         i = row * (EXPLOSIONGRID + 1) + column;
127         a = row * M_PI * 2 / EXPLOSIONGRID;
128         b = column * M_PI * 2 / EXPLOSIONGRID;
129         c = cos(b);
130         explosionpoint[i][0] = cos(a) * c;
131         explosionpoint[i][1] = sin(a) * c;
132         explosionpoint[i][2] = -sin(b);
133         explosionspherevertvel[i][0] = explosionpoint[i][0] * EXPLOSIONSTARTVELOCITY;
134         explosionspherevertvel[i][1] = explosionpoint[i][1] * EXPLOSIONSTARTVELOCITY;
135         explosionspherevertvel[i][2] = explosionpoint[i][2] * EXPLOSIONSTARTVELOCITY;
136         explosiontexcoords[i][0] = (float) column / (float) EXPLOSIONGRID;
137         explosiontexcoords[i][1] = (float) row / (float) EXPLOSIONGRID;
138         // top and bottom rows are all one position...
139         if (row == 0 || row == EXPLOSIONGRID)
140                 column = 0;
141         explosionnoiseindex[i] = (row % EXPLOSIONGRID) * EXPLOSIONGRID + (column % EXPLOSIONGRID);
142         return i;
143 }
144
145 void R_Explosion_Init(void)
146 {
147         int i, x, y;
148         i = 0;
149         for (y = 0;y < EXPLOSIONGRID;y++)
150         {
151                 for (x = 0;x < EXPLOSIONGRID;x++)
152                 {
153                         explosiontris[i][0] = R_ExplosionVert(x    , y    );
154                         explosiontris[i][1] = R_ExplosionVert(x + 1, y    );
155                         explosiontris[i][2] = R_ExplosionVert(x    , y + 1);
156                         i++;
157                         explosiontris[i][0] = R_ExplosionVert(x + 1, y    );
158                         explosiontris[i][1] = R_ExplosionVert(x + 1, y + 1);
159                         explosiontris[i][2] = R_ExplosionVert(x    , y + 1);
160                         i++;
161                 }
162         }
163
164         Cvar_RegisterVariable(&r_explosionclip);
165         Cvar_RegisterVariable(&r_drawexplosions);
166
167         R_RegisterModule("R_Explosions", r_explosion_start, r_explosion_shutdown, r_explosion_newmap);
168 }
169
170 void R_NewExplosion(vec3_t org)
171 {
172         int i, j;
173         float dist;
174         qbyte noise[EXPLOSIONGRID*EXPLOSIONGRID];
175         fractalnoisequick(noise, EXPLOSIONGRID, 4); // adjust noise grid size according to explosion
176         for (i = 0;i < MAX_EXPLOSIONS;i++)
177         {
178                 if (explosion[i].alpha <= 0.01f)
179                 {
180                         explosion[i].starttime = cl.time;
181                         explosion[i].time = explosion[i].starttime - 0.1;
182                         explosion[i].alpha = EXPLOSIONFADESTART;
183                         VectorCopy(org, explosion[i].origin);
184                         for (j = 0;j < EXPLOSIONVERTS;j++)
185                         {
186                                 // calculate start
187                                 VectorCopy(explosion[i].origin, explosion[i].vert[j]);
188                                 // calculate velocity
189                                 dist = noise[explosionnoiseindex[j]] * (1.0f / 255.0f) + 0.5;
190                                 VectorScale(explosionspherevertvel[j], dist, explosion[i].vertvel[j]);
191                                 //explosion[i].vertvel[j][0] = explosionspherevertvel[j][0] * dist; + (((float) noise[0][explosionnoiseindex[j]] - 128.0f) * (EXPLOSIONRANDOMVELOCITY / 128.0f));
192                                 //explosion[i].vertvel[j][1] = explosionspherevertvel[j][1] * dist; + (((float) noise[1][explosionnoiseindex[j]] - 128.0f) * (EXPLOSIONRANDOMVELOCITY / 128.0f));
193                                 //explosion[i].vertvel[j][2] = explosionspherevertvel[j][2] * dist; + (((float) noise[2][explosionnoiseindex[j]] - 128.0f) * (EXPLOSIONRANDOMVELOCITY / 128.0f));
194                         }
195                         break;
196                 }
197         }
198
199         /*
200         i = 0;
201         j = EXPLOSIONGAS;
202         while (i < MAX_EXPLOSIONGAS && j > 0)
203         {
204                 while (explosiongas[i].pressure > 0)
205                 {
206                         i++;
207                         if (i >= MAX_EXPLOSIONGAS)
208                                 return;
209                 }
210                 VectorRandom(v);
211                 VectorMA(org, EXPLOSIONGASSTARTRADIUS, v, v);
212                 TraceLine(org, v, explosiongas[i].origin, NULL, 0, true);
213                 VectorRandom(v);
214                 VectorScale(v, EXPLOSIONGASSTARTVELOCITY, explosiongas[i].velocity);
215                 explosiongas[i].pressure = j * GASDENSITY_SCALER;
216                 j--;
217         }
218         */
219 }
220
221 void R_DrawExplosion(explosion_t *e)
222 {
223         int i;
224         float c[EXPLOSIONVERTS][4], diff[3], centerdir[3], /*fog, */ifog, alpha, dist/*, centerdist, size, scale*/;
225         rmeshinfo_t m;
226         memset(&m, 0, sizeof(m));
227         m.transparent = true;
228         m.blendfunc1 = GL_SRC_ALPHA;
229         m.blendfunc2 = GL_ONE; //_MINUS_SRC_ALPHA;
230         m.numtriangles = EXPLOSIONTRIS;
231         m.index = &explosiontris[0][0];
232         m.numverts = EXPLOSIONVERTS;
233         m.vertex = &e->vert[0][0];
234         m.vertexstep = sizeof(float[3]);
235         alpha = e->alpha;
236         //if (alpha > 1)
237         //      alpha = 1;
238         m.cr = 1;
239         m.cg = 1;
240         m.cb = 1;
241         m.ca = 1; //alpha;
242         m.color = &c[0][0];
243         m.colorstep = sizeof(float[4]);
244         VectorSubtract(r_origin, e->origin, centerdir);
245         VectorNormalizeFast(centerdir);
246         /*
247         centerdist = DotProduct(e->origin, centerdir);
248         size = 0;
249         for (i = 0;i < EXPLOSIONVERTS;i++)
250         {
251                 dist = DotProduct(e->vert[i], centerdir) - centerdist;
252                 if (size < dist)
253                         size = dist;
254         }
255         scale = 4.0f / size;
256         */
257         if (fogenabled)
258         {
259                 for (i = 0;i < EXPLOSIONVERTS;i++)
260                 {
261                         //dist = (DotProduct(e->vert[i], centerdir) - centerdist) * scale - 2.0f;
262                         VectorSubtract(e->vert[i], e->origin, diff);
263                         VectorNormalizeFast(diff);
264                         dist = DotProduct(diff, centerdir) * 6.0f - 4.0f;
265                         if (dist > 0)
266                         {
267                                 // use inverse fog alpha as color
268                                 VectorSubtract(e->vert[i], r_origin, diff);
269                                 ifog = 1 - exp(fogdensity/DotProduct(diff,diff));
270                                 if (ifog < 0)
271                                         ifog = 0;
272                                 c[i][0] = c[i][1] = c[i][2] = dist * alpha * ifog;
273                         }
274                         else
275                                 c[i][0] = c[i][1] = c[i][2] = 0;
276                         c[i][3] = 1;
277                 }
278         }
279         else
280         {
281                 for (i = 0;i < EXPLOSIONVERTS;i++)
282                 {
283                         //dist = (DotProduct(e->vert[i], centerdir) - centerdist) * scale - 2.0f;
284                         VectorSubtract(e->vert[i], e->origin, diff);
285                         VectorNormalizeFast(diff);
286                         dist = DotProduct(diff, centerdir) * 6.0f - 4.0f;
287                         if (dist > 0)
288                                 c[i][0] = c[i][1] = c[i][2] = dist * alpha;
289                         else
290                                 c[i][0] = c[i][1] = c[i][2] = 0;
291                         c[i][3] = 1;
292                 }
293         }
294         /*
295         if (fogenabled)
296         {
297                 m.color = &c[0][0];
298                 m.colorstep = sizeof(float[4]);
299                 for (i = 0;i < EXPLOSIONVERTS;i++)
300                 {
301                         // use inverse fog alpha as color
302                         VectorSubtract(e->vert[i], r_origin, diff);
303                         ifog = 1 - exp(fogdensity/DotProduct(diff,diff));
304                         if (ifog < 0)
305                                 ifog = 0;
306                         c[i][0] = ifog;
307                         c[i][1] = ifog;
308                         c[i][2] = ifog;
309                         c[i][3] = alpha;
310                 }
311         }
312         */
313         m.tex[0] = R_GetTexture(explosiontexture);
314         m.texcoords[0] = &explosiontexcoords[0][0];
315         m.texcoordstep[0] = sizeof(float[2]);
316
317         R_Mesh_Draw(&m);
318
319         /*
320         if (fogenabled)
321         {
322                 m.blendfunc1 = GL_SRC_ALPHA;
323                 m.blendfunc2 = GL_ONE;
324                 for (i = 0;i < EXPLOSIONVERTS;i++)
325                 {
326                         VectorSubtract(e->vert[i], r_origin, diff);
327                         fog = exp(fogdensity/DotProduct(diff,diff));
328                         c[i][0] = fogcolor[0];
329                         c[i][1] = fogcolor[1];
330                         c[i][2] = fogcolor[2];
331                         c[i][3] = alpha * fog;
332                 }
333                 //m.color = &c[0][0];
334                 //m.colorstep = sizeof(float[4]);
335                 m.tex[0] = R_GetTexture(explosiontexturefog);
336                 R_Mesh_Draw(&m);
337         }
338         */
339 }
340
341 void R_MoveExplosion(explosion_t *e/*, explosiongas_t **list, explosiongas_t **listend, */)
342 {
343         int i;
344         float dot, frictionscale, end[3], impact[3], normal[3], frametime;
345         /*
346         vec3_t diff;
347         vec_t dist;
348         explosiongas_t **l;
349         */
350         frametime = cl.time - e->time;
351         e->time = cl.time;
352         e->alpha = EXPLOSIONFADESTART - (cl.time - e->starttime) * EXPLOSIONFADERATE;
353         if (e->alpha <= 0.01f)
354         {
355                 e->alpha = -1;
356                 return;
357         }
358         frictionscale = 1 - frametime;
359         frictionscale = bound(0, frictionscale, 1);
360         for (i = 0;i < EXPLOSIONVERTS;i++)
361         {
362                 if (e->vertvel[i][0] || e->vertvel[i][1] || e->vertvel[i][2])
363                 {
364                         //e->vertvel[i][2] += sv_gravity.value * frametime * -0.25f;
365                         VectorScale(e->vertvel[i], frictionscale, e->vertvel[i]);
366                         VectorMA(e->vert[i], frametime, e->vertvel[i], end);
367                         if (r_explosionclip.integer)
368                         {
369                                 if (TraceLine(e->vert[i], end, impact, normal, 0, true) < 1)
370                                 {
371                                         // clip velocity against the wall
372                                         dot = DotProduct(e->vertvel[i], normal) * -1.125f;
373                                         VectorMA(e->vertvel[i], dot, normal, e->vertvel[i]);
374                                 }
375                                 VectorCopy(impact, e->vert[i]);
376                         }
377                         else
378                                 VectorCopy(end, e->vert[i]);
379                 }
380                 /*
381                 for (l = list;l < listend;l++)
382                 {
383                         VectorSubtract(e->vert[i], (*l)->origin, diff);
384                         dist = DotProduct(diff, diff);
385                         if (dist < 4096 && dist >= 1)
386                         {
387                                 dist = (*l)->pressure * frametime / dist;
388                                 VectorMA(e->vertvel[i], dist, diff, e->vertvel[i]);
389                         }
390                 }
391                 */
392         }
393         for (i = 0;i < EXPLOSIONGRID;i++)
394                 VectorCopy(e->vert[i * (EXPLOSIONGRID + 1)], e->vert[i * (EXPLOSIONGRID + 1) + EXPLOSIONGRID]);
395         memcpy(e->vert[EXPLOSIONGRID * (EXPLOSIONGRID + 1)], e->vert[0], sizeof(float[3]) * (EXPLOSIONGRID + 1));
396 }
397
398 /*
399 void R_MoveExplosionGas(explosiongas_t *e, explosiongas_t **list, explosiongas_t **listend, float frametime)
400 {
401         vec3_t end, diff;
402         vec_t dist, frictionscale;
403         explosiongas_t **l;
404         frictionscale = 1 - frametime;
405         frictionscale = bound(0, frictionscale, 1);
406         if (e->velocity[0] || e->velocity[1] || e->velocity[2])
407         {
408                 end[0] = e->origin[0] + frametime * e->velocity[0];
409                 end[1] = e->origin[1] + frametime * e->velocity[1];
410                 end[2] = e->origin[2] + frametime * e->velocity[2];
411                 if (r_explosionclip.integer)
412                 {
413                         float f, dot;
414                         vec3_t impact, normal;
415                         f = TraceLine(e->origin, end, impact, normal, 0, true);
416                         VectorCopy(impact, e->origin);
417                         if (f < 1)
418                         {
419                                 // clip velocity against the wall
420                                 dot = DotProduct(e->velocity, normal) * -1.3f;
421                                 e->velocity[0] += normal[0] * dot;
422                                 e->velocity[1] += normal[1] * dot;
423                                 e->velocity[2] += normal[2] * dot;
424                         }
425                 }
426                 else
427                 {
428                         VectorCopy(end, e->origin);
429                 }
430                 e->velocity[2] += sv_gravity.value * frametime;
431                 VectorScale(e->velocity, frictionscale, e->velocity);
432         }
433         for (l = list;l < listend;l++)
434         {
435                 if (*l != e)
436                 {
437                         VectorSubtract(e->origin, (*l)->origin, diff);
438                         dist = DotProduct(diff, diff);
439                         if (dist < 4096 && dist >= 1)
440                         {
441                                 dist = (*l)->pressure * frametime / dist;
442                                 VectorMA(e->velocity, dist, diff, e->velocity);
443                         }
444                 }
445         }
446 }
447 */
448
449 void R_MoveExplosions(void)
450 {
451         int i;
452         float frametime;
453 //      explosiongas_t *gaslist[MAX_EXPLOSIONGAS], **l, **end;
454         frametime = cl.time - cl.oldtime;
455         /*
456         l = &gaslist[0];
457         for (i = 0;i < MAX_EXPLOSIONGAS;i++)
458         {
459                 if (explosiongas[i].pressure > 0)
460                 {
461                         explosiongas[i].pressure -= frametime * GASFADERATE;
462                         if (explosiongas[i].pressure > 0)
463                                 *l++ = &explosiongas[i];
464                 }
465         }
466         end = l;
467         for (l = gaslist;l < end;l++)
468                 R_MoveExplosionGas(*l, gaslist, end, frametime);
469         */
470
471         for (i = 0;i < MAX_EXPLOSIONS;i++)
472                 if (explosion[i].alpha > 0.01f)
473                         R_MoveExplosion(&explosion[i]/*, gaslist, end, */);
474 }
475
476 void R_DrawExplosions(void)
477 {
478         int i;
479         if (!r_drawexplosions.integer)
480                 return;
481         for (i = 0;i < MAX_EXPLOSIONS;i++)
482                 if (explosion[i].alpha > 0.01f)
483                         R_DrawExplosion(&explosion[i]);
484 }