]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - r_explosion.c
changed most #define macros to use (,,,) format instead of {;;;} so they will gobble...
[xonotic/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 EXPLOSIONSTARTRADIUS (20.0f)
28 #define EXPLOSIONSTARTVELOCITY (256.0f)
29 #define EXPLOSIONRANDOMVELOCITY (64.0f)
30 #define EXPLOSIONFADESTART (1.5f)
31 #define EXPLOSIONFADERATE (4.5f)
32 /*
33 #define MAX_EXPLOSIONGAS (MAX_EXPLOSIONS * EXPLOSIONGAS)
34 #define EXPLOSIONGAS 8
35 #define EXPLOSIONGASSTARTRADIUS (15.0f)
36 #define EXPLOSIONGASSTARTVELOCITY (50.0f)
37 #define GASDENSITY_SCALER (32768.0f / EXPLOSIONGAS)
38 #define GASFADERATE (GASDENSITY_SCALER * EXPLOSIONGAS * 2)
39
40 typedef struct explosiongas_s
41 {
42         float pressure;
43         vec3_t origin;
44         vec3_t velocity;
45 }
46 explosiongas_t;
47
48 explosiongas_t explosiongas[MAX_EXPLOSIONGAS];
49 */
50
51 vec3_t explosionspherevert[EXPLOSIONVERTS];
52 vec3_t explosionspherevertvel[EXPLOSIONVERTS];
53 float explosiontexcoords[EXPLOSIONVERTS][2];
54 int explosiontris[EXPLOSIONTRIS][3];
55 int explosionnoiseindex[EXPLOSIONVERTS];
56 vec3_t explosionpoint[EXPLOSIONVERTS];
57
58 typedef struct explosion_s
59 {
60         float starttime;
61         float alpha;
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         explosionnoiseindex[i] = (row % EXPLOSIONGRID) * EXPLOSIONGRID + (column % EXPLOSIONGRID);
132         explosiontexcoords[i][0] = (float) column / (float) EXPLOSIONGRID;
133         explosiontexcoords[i][1] = (float) row / (float) EXPLOSIONGRID;
134         return i;
135 }
136
137 void R_Explosion_Init(void)
138 {
139         int i, x, y;
140         i = 0;
141         for (y = 0;y < EXPLOSIONGRID;y++)
142         {
143                 for (x = 0;x < EXPLOSIONGRID;x++)
144                 {
145                         explosiontris[i][0] = R_ExplosionVert(x    , y    );
146                         explosiontris[i][1] = R_ExplosionVert(x + 1, y    );
147                         explosiontris[i][2] = R_ExplosionVert(x    , y + 1);
148                         i++;
149                         explosiontris[i][0] = R_ExplosionVert(x + 1, y    );
150                         explosiontris[i][1] = R_ExplosionVert(x + 1, y + 1);
151                         explosiontris[i][2] = R_ExplosionVert(x    , y + 1);
152                         i++;
153                 }
154         }
155         for (i = 0;i < EXPLOSIONVERTS;i++)
156         {
157                 explosionspherevert[i][0] = explosionpoint[i][0] * EXPLOSIONSTARTRADIUS;
158                 explosionspherevert[i][1] = explosionpoint[i][1] * EXPLOSIONSTARTRADIUS;
159                 explosionspherevert[i][2] = explosionpoint[i][2] * EXPLOSIONSTARTRADIUS;
160                 explosionspherevertvel[i][0] = explosionpoint[i][0] * EXPLOSIONSTARTVELOCITY;
161                 explosionspherevertvel[i][1] = explosionpoint[i][1] * EXPLOSIONSTARTVELOCITY;
162                 explosionspherevertvel[i][2] = explosionpoint[i][2] * EXPLOSIONSTARTVELOCITY;
163         }
164
165         Cvar_RegisterVariable(&r_explosionclip);
166         Cvar_RegisterVariable(&r_drawexplosions);
167
168         R_RegisterModule("R_Explosions", r_explosion_start, r_explosion_shutdown, r_explosion_newmap);
169 }
170
171 void R_NewExplosion(vec3_t org)
172 {
173         int i, j;
174         float dist, v[3], normal[3];
175         byte noise[4][EXPLOSIONGRID*EXPLOSIONGRID];
176         fractalnoise(noise[0], EXPLOSIONGRID, 4);
177         fractalnoise(noise[1], EXPLOSIONGRID, 2);
178         fractalnoise(noise[2], EXPLOSIONGRID, 2);
179         fractalnoise(noise[3], EXPLOSIONGRID, 2);
180         for (i = 0;i < MAX_EXPLOSIONS;i++)
181         {
182                 if (explosion[i].alpha <= 0.0f)
183                 {
184                         explosion[i].alpha = EXPLOSIONFADESTART;
185                         for (j = 0;j < EXPLOSIONVERTS;j++)
186                         {
187                                 dist = noise[3][explosionnoiseindex[j]] * (1.0f / 256.0f) + 0.5;
188                                 VectorMA(org, dist, explosionspherevert[j], v);
189                                 TraceLine(org, v, explosion[i].vert[j], normal, 0);
190                                 VectorAdd(explosion[i].vert[j], normal, explosion[i].vert[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);
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], fog, ifog, alpha;
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 = alpha;
242         if (fogenabled)
243         {
244                 m.color = &c[0][0];
245                 m.colorstep = sizeof(float[4]);
246                 for (i = 0;i < EXPLOSIONVERTS;i++)
247                 {
248                         // use inverse fog alpha as color
249                         VectorSubtract(e->vert[i], r_origin, diff);
250                         ifog = 1 - exp(fogdensity/DotProduct(diff,diff));
251                         if (ifog < 0)
252                                 ifog = 0;
253                         c[i][0] = ifog;
254                         c[i][1] = ifog;
255                         c[i][2] = ifog;
256                         c[i][3] = alpha;
257                 }
258         }
259         m.tex[0] = R_GetTexture(explosiontexture);
260         m.texcoords[0] = &explosiontexcoords[0][0];
261         m.texcoordstep[0] = sizeof(float[2]);
262
263         R_Mesh_Draw(&m);
264
265         if (fogenabled)
266         {
267                 m.blendfunc1 = GL_SRC_ALPHA;
268                 m.blendfunc2 = GL_ONE;
269                 for (i = 0;i < EXPLOSIONVERTS;i++)
270                 {
271                         VectorSubtract(e->vert[i], r_origin, diff);
272                         fog = exp(fogdensity/DotProduct(diff,diff));
273                         c[i][0] = fogcolor[0];
274                         c[i][1] = fogcolor[1];
275                         c[i][2] = fogcolor[2];
276                         c[i][3] = alpha * fog;
277                 }
278                 //m.color = &c[0][0];
279                 //m.colorstep = sizeof(float[4]);
280                 m.tex[0] = R_GetTexture(explosiontexturefog);
281                 R_Mesh_Draw(&m);
282         }
283 }
284
285 void R_MoveExplosion(explosion_t *e, /*explosiongas_t **list, explosiongas_t **listend, */float frametime)
286 {
287         int i;
288         float f, dot, frictionscale, end[3], impact[3], normal[3];
289         /*
290         vec3_t diff;
291         vec_t dist;
292         explosiongas_t **l;
293         */
294         e->alpha -= frametime * EXPLOSIONFADERATE;
295         frictionscale = 1 - frametime;
296         frictionscale = bound(0, frictionscale, 1);
297         for (i = 0;i < EXPLOSIONVERTS;i++)
298         {
299                 if (e->vertvel[i][0] || e->vertvel[i][1] || e->vertvel[i][2])
300                 {
301                         end[0] = e->vert[i][0] + frametime * e->vertvel[i][0];
302                         end[1] = e->vert[i][1] + frametime * e->vertvel[i][1];
303                         end[2] = e->vert[i][2] + frametime * e->vertvel[i][2];
304                         if (r_explosionclip.integer)
305                         {
306                                 f = TraceLine(e->vert[i], end, impact, normal, 0);
307                                 VectorCopy(impact, e->vert[i]);
308                                 if (f < 1)
309                                 {
310                                         // clip velocity against the wall
311                                         dot = DotProduct(e->vertvel[i], normal) * 1.125f;
312                                         e->vertvel[i][0] -= normal[0] * dot;
313                                         e->vertvel[i][1] -= normal[1] * dot;
314                                         e->vertvel[i][2] -= normal[2] * dot;
315                                 }
316                         }
317                         else
318                         {
319                                 VectorCopy(end, e->vert[i]);
320                         }
321                         e->vertvel[i][2] += sv_gravity.value * frametime * -0.25f;
322                         VectorScale(e->vertvel[i], frictionscale, e->vertvel[i]);
323                 }
324                 /*
325                 for (l = list;l < listend;l++)
326                 {
327                         VectorSubtract(e->vert[i], (*l)->origin, diff);
328                         dist = DotProduct(diff, diff);
329                         if (dist < 4096 && dist >= 1)
330                         {
331                                 dist = (*l)->pressure * frametime / dist;
332                                 VectorMA(e->vertvel[i], dist, diff, e->vertvel[i]);
333                         }
334                 }
335                 */
336         }
337 }
338
339 /*
340 void R_MoveExplosionGas(explosiongas_t *e, explosiongas_t **list, explosiongas_t **listend, float frametime)
341 {
342         vec3_t end, diff;
343         vec_t dist, frictionscale;
344         explosiongas_t **l;
345         frictionscale = 1 - frametime;
346         frictionscale = bound(0, frictionscale, 1);
347         if (e->velocity[0] || e->velocity[1] || e->velocity[2])
348         {
349                 end[0] = e->origin[0] + frametime * e->velocity[0];
350                 end[1] = e->origin[1] + frametime * e->velocity[1];
351                 end[2] = e->origin[2] + frametime * e->velocity[2];
352                 if (r_explosionclip.integer)
353                 {
354                         float f, dot;
355                         vec3_t impact, normal;
356                         f = TraceLine(e->origin, end, impact, normal, 0);
357                         VectorCopy(impact, e->origin);
358                         if (f < 1)
359                         {
360                                 // clip velocity against the wall
361                                 dot = DotProduct(e->velocity, normal) * -1.3f;
362                                 e->velocity[0] += normal[0] * dot;
363                                 e->velocity[1] += normal[1] * dot;
364                                 e->velocity[2] += normal[2] * dot;
365                         }
366                 }
367                 else
368                 {
369                         VectorCopy(end, e->origin);
370                 }
371                 e->velocity[2] += sv_gravity.value * frametime;
372                 VectorScale(e->velocity, frictionscale, e->velocity);
373         }
374         for (l = list;l < listend;l++)
375         {
376                 if (*l != e)
377                 {
378                         VectorSubtract(e->origin, (*l)->origin, diff);
379                         dist = DotProduct(diff, diff);
380                         if (dist < 4096 && dist >= 1)
381                         {
382                                 dist = (*l)->pressure * frametime / dist;
383                                 VectorMA(e->velocity, dist, diff, e->velocity);
384                         }
385                 }
386         }
387 }
388 */
389
390 void R_MoveExplosions(void)
391 {
392         int i;
393         float frametime;
394 //      explosiongas_t *gaslist[MAX_EXPLOSIONGAS], **l, **end;
395         frametime = cl.time - cl.oldtime;
396         /*
397         l = &gaslist[0];
398         for (i = 0;i < MAX_EXPLOSIONGAS;i++)
399         {
400                 if (explosiongas[i].pressure > 0)
401                 {
402                         explosiongas[i].pressure -= frametime * GASFADERATE;
403                         if (explosiongas[i].pressure > 0)
404                                 *l++ = &explosiongas[i];
405                 }
406         }
407         end = l;
408         for (l = gaslist;l < end;l++)
409                 R_MoveExplosionGas(*l, gaslist, end, frametime);
410         */
411
412         for (i = 0;i < MAX_EXPLOSIONS;i++)
413         {
414                 if (explosion[i].alpha > 0.0f)
415                 {
416                         if (explosion[i].starttime > cl.time)
417                         {
418                                 explosion[i].alpha = 0;
419                                 continue;
420                         }
421                         R_MoveExplosion(&explosion[i], /*gaslist, end, */frametime);
422                 }
423         }
424 }
425
426 void R_DrawExplosions(void)
427 {
428         int i;
429         if (!r_drawexplosions.integer)
430                 return;
431         for (i = 0;i < MAX_EXPLOSIONS;i++)
432         {
433                 if (explosion[i].alpha > 0.0f)
434                 {
435                         R_DrawExplosion(&explosion[i]);
436                 }
437         }
438 }