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