rewrote RecursiveHullCheck, no longer gets stuck on angle changes, and is generally...
[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 16
25 #define EXPLOSIONVERTS ((EXPLOSIONGRID+1)*(EXPLOSIONGRID+1))
26 #define EXPLOSIONTRIS (EXPLOSIONVERTS*2)
27 #define EXPLOSIONSTARTRADIUS (20.0f)
28 #define EXPLOSIONSTARTVELOCITY (256.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 vec3_t explosionspherevert[EXPLOSIONVERTS];
51 vec3_t explosionspherevertvel[EXPLOSIONVERTS];
52 float explosiontexcoords[EXPLOSIONVERTS][2];
53 int explosiontris[EXPLOSIONTRIS][3];
54 int explosionnoiseindex[EXPLOSIONVERTS];
55 vec3_t explosionpoint[EXPLOSIONVERTS];
56
57 typedef struct explosion_s
58 {
59         float starttime;
60         float alpha;
61         vec3_t vert[EXPLOSIONVERTS];
62         vec3_t vertvel[EXPLOSIONVERTS];
63 }
64 explosion_t;
65
66 explosion_t explosion[MAX_EXPLOSIONS];
67
68 rtexture_t      *explosiontexture;
69 rtexture_t      *explosiontexturefog;
70
71 cvar_t r_explosionclip = {CVAR_SAVE, "r_explosionclip", "1"};
72 cvar_t r_drawexplosions = {0, "r_drawexplosions", "1"};
73
74 int R_ExplosionVert(int column, int row)
75 {
76         int i;
77         float a, b, c;
78         i = row * (EXPLOSIONGRID + 1) + column;
79         a = row * M_PI * 2 / EXPLOSIONGRID;
80         b = column * M_PI * 2 / EXPLOSIONGRID;
81         c = cos(b);
82         explosionpoint[i][0] = cos(a) * c;
83         explosionpoint[i][1] = sin(a) * c;
84         explosionpoint[i][2] = -sin(b);
85         explosionnoiseindex[i] = (row & (EXPLOSIONGRID - 1)) * EXPLOSIONGRID + (column & (EXPLOSIONGRID - 1));
86         explosiontexcoords[i][0] = (float) column / (float) EXPLOSIONGRID;
87         explosiontexcoords[i][1] = (float) row / (float) EXPLOSIONGRID;
88         return i;
89 }
90
91 void r_explosion_start(void)
92 {
93         int x, y;
94         byte noise1[128][128], noise2[128][128], noise3[128][128], data[128][128][4];
95         fractalnoise(&noise1[0][0], 128, 32);
96         fractalnoise(&noise2[0][0], 128, 4);
97         fractalnoise(&noise3[0][0], 128, 4);
98         for (y = 0;y < 128;y++)
99         {
100                 for (x = 0;x < 128;x++)
101                 {
102                         int j, r, g, b, a;
103                         j = (noise1[y][x] * noise2[y][x]) * 3 / 256 - 128;
104                         r = (j * 512) / 256;
105                         g = (j * 256) / 256;
106                         b = (j * 128) / 256;
107                         a = noise3[y][x] * 3 - 128;
108                         data[y][x][0] = bound(0, r, 255);
109                         data[y][x][1] = bound(0, g, 255);
110                         data[y][x][2] = bound(0, b, 255);
111                         data[y][x][3] = bound(0, a, 255);
112                 }
113         }
114         explosiontexture = R_LoadTexture ("explosiontexture", 128, 128, &data[0][0][0], TEXF_MIPMAP | TEXF_ALPHA | TEXF_RGBA | TEXF_PRECACHE);
115         for (y = 0;y < 128;y++)
116                 for (x = 0;x < 128;x++)
117                         data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
118         explosiontexturefog = R_LoadTexture ("explosiontexturefog", 128, 128, &data[0][0][0], TEXF_MIPMAP | TEXF_ALPHA | TEXF_RGBA | TEXF_PRECACHE);
119 }
120
121 void r_explosion_shutdown(void)
122 {
123 }
124
125 void r_explosion_newmap(void)
126 {
127         memset(explosion, 0, sizeof(explosion));
128 //      memset(explosiongas, 0, sizeof(explosiongas));
129 }
130
131 void R_Explosion_Init(void)
132 {
133         int i, x, y;
134         i = 0;
135         for (y = 0;y < EXPLOSIONGRID;y++)
136         {
137                 for (x = 0;x < EXPLOSIONGRID;x++)
138                 {
139                         explosiontris[i][0] = R_ExplosionVert(x    , y    );
140                         explosiontris[i][1] = R_ExplosionVert(x + 1, y    );
141                         explosiontris[i][2] = R_ExplosionVert(x    , y + 1);
142                         i++;
143                         explosiontris[i][0] = R_ExplosionVert(x + 1, y    );
144                         explosiontris[i][1] = R_ExplosionVert(x + 1, y + 1);
145                         explosiontris[i][2] = R_ExplosionVert(x    , y + 1);
146                         i++;
147                 }
148         }
149         for (i = 0;i < EXPLOSIONVERTS;i++)
150         {
151                 explosionspherevert[i][0] = explosionpoint[i][0] * EXPLOSIONSTARTRADIUS;
152                 explosionspherevert[i][1] = explosionpoint[i][1] * EXPLOSIONSTARTRADIUS;
153                 explosionspherevert[i][2] = explosionpoint[i][2] * EXPLOSIONSTARTRADIUS;
154                 explosionspherevertvel[i][0] = explosionpoint[i][0] * EXPLOSIONSTARTVELOCITY;
155                 explosionspherevertvel[i][1] = explosionpoint[i][1] * EXPLOSIONSTARTVELOCITY;
156                 explosionspherevertvel[i][2] = explosionpoint[i][2] * EXPLOSIONSTARTVELOCITY;
157         }
158
159         Cvar_RegisterVariable(&r_explosionclip);
160         Cvar_RegisterVariable(&r_drawexplosions);
161
162         R_RegisterModule("R_Explosions", r_explosion_start, r_explosion_shutdown, r_explosion_newmap);
163 }
164
165 void R_NewExplosion(vec3_t org)
166 {
167         int i, j;
168         float dist, v[3];
169         byte noise[EXPLOSIONGRID*EXPLOSIONGRID];
170         fractalnoise(noise, EXPLOSIONGRID, 4);
171         for (i = 0;i < MAX_EXPLOSIONS;i++)
172         {
173                 if (explosion[i].alpha <= 0.0f)
174                 {
175                         explosion[i].alpha = EXPLOSIONFADESTART;
176                         for (j = 0;j < EXPLOSIONVERTS;j++)
177                         {
178                                 dist = noise[explosionnoiseindex[j]] * (1.0f / 256.0f) + 0.5;
179                                 VectorMA(org, dist, explosionspherevert[j], v);
180                                 TraceLine(org, v, explosion[i].vert[j], NULL, 0);
181                                 VectorScale(explosionspherevertvel[j], dist, explosion[i].vertvel[j]);
182                         }
183                         break;
184                 }
185         }
186
187         /*
188         i = 0;
189         j = EXPLOSIONGAS;
190         while (i < MAX_EXPLOSIONGAS && j > 0)
191         {
192                 while (explosiongas[i].pressure > 0)
193                 {
194                         i++;
195                         if (i >= MAX_EXPLOSIONGAS)
196                                 return;
197                 }
198                 VectorRandom(v);
199                 VectorMA(org, EXPLOSIONGASSTARTRADIUS, v, v);
200                 TraceLine(org, v, explosiongas[i].origin, NULL, 0);
201                 VectorRandom(v);
202                 VectorScale(v, EXPLOSIONGASSTARTVELOCITY, explosiongas[i].velocity);
203                 explosiongas[i].pressure = j * GASDENSITY_SCALER;
204                 j--;
205         }
206         */
207 }
208
209 void R_DrawExplosion(explosion_t *e)
210 {
211         int i, index, *indexlist = &explosiontris[0][0], alpha = bound(0, e->alpha * 128.0f, 128), texnum, fogtexnum;
212         float s, t;
213 //      s = cl.time * 1;
214 //      t = cl.time * 0.75;
215 //      s -= (int) s;
216 //      t -= (int) t;
217         s = 0;
218         t = 0;
219         /*
220         glColor4f(1,1,1,e->alpha);
221         glDisable(GL_TEXTURE_2D);
222 //      glBindTexture(GL_TEXTURE_2D, explosiontexture);
223         glVertexPointer(3, GL_FLOAT, sizeof(float[3]), (float *) &e->vert[0][0]);
224 //      glTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), (float *) &explosiontexcoords[0][0]);
225         glEnableClientState(GL_VERTEX_ARRAY);
226 //      glEnableClientState(GL_TEXTURE_COORD_ARRAY);
227         glDrawElements(GL_TRIANGLES, EXPLOSIONTRIS, GL_UNSIGNED_INT, indexlist);
228 //      glDisableClientState(GL_TEXTURE_COORD_ARRAY);
229         glDisableClientState(GL_VERTEX_ARRAY);
230         glEnable(GL_TEXTURE_2D);
231         */
232         texnum = R_GetTexture(explosiontexture);
233         fogtexnum = R_GetTexture(explosiontexturefog);
234         for (i = 0;i < EXPLOSIONTRIS;i++)
235         {
236                 transpolybegin(texnum, 0, fogtexnum, TPOLYTYPE_ALPHA);
237                 index = *indexlist++;transpolyvert(e->vert[index][0], e->vert[index][1], e->vert[index][2], explosiontexcoords[index][0] + s, explosiontexcoords[index][1] + t, 255, 255, 255, alpha);
238                 index = *indexlist++;transpolyvert(e->vert[index][0], e->vert[index][1], e->vert[index][2], explosiontexcoords[index][0] + s, explosiontexcoords[index][1] + t, 255, 255, 255, alpha);
239                 index = *indexlist++;transpolyvert(e->vert[index][0], e->vert[index][1], e->vert[index][2], explosiontexcoords[index][0] + s, explosiontexcoords[index][1] + t, 255, 255, 255, alpha);
240                 transpolyend();
241         }
242 }
243
244 void R_MoveExplosion(explosion_t *e, /*explosiongas_t **list, explosiongas_t **listend, */float frametime)
245 {
246         int i;
247         vec3_t end;
248         vec_t frictionscale;
249         /*
250         vec3_t diff;
251         vec_t dist;
252         explosiongas_t **l;
253         */
254         e->alpha -= frametime * EXPLOSIONFADERATE;
255         frictionscale = 1 - frametime;
256         frictionscale = bound(0, frictionscale, 1);
257         for (i = 0;i < EXPLOSIONVERTS;i++)
258         {
259                 if (e->vertvel[i][0] || e->vertvel[i][1] || e->vertvel[i][2])
260                 {
261                         end[0] = e->vert[i][0] + frametime * e->vertvel[i][0];
262                         end[1] = e->vert[i][1] + frametime * e->vertvel[i][1];
263                         end[2] = e->vert[i][2] + frametime * e->vertvel[i][2];
264                         if (r_explosionclip.value)
265                         {
266                                 float f, dot;
267                                 vec3_t impact, normal;
268                                 f = TraceLine(e->vert[i], end, impact, normal, 0);
269                                 VectorCopy(impact, e->vert[i]);
270                                 if (f < 1)
271                                 {
272                                         // clip velocity against the wall
273                                         dot = DotProduct(e->vertvel[i], normal) * -1.125f;
274                                         e->vertvel[i][0] += normal[0] * dot;
275                                         e->vertvel[i][1] += normal[1] * dot;
276                                         e->vertvel[i][2] += normal[2] * dot;
277                                 }
278                         }
279                         else
280                         {
281                                 VectorCopy(end, e->vert[i]);
282                         }
283                         e->vertvel[i][2] += sv_gravity.value * frametime * -0.25f;
284                         VectorScale(e->vertvel[i], frictionscale, e->vertvel[i]);
285                 }
286                 /*
287                 for (l = list;l < listend;l++)
288                 {
289                         VectorSubtract(e->vert[i], (*l)->origin, diff);
290                         dist = DotProduct(diff, diff);
291                         if (dist < 4096 && dist >= 1)
292                         {
293                                 dist = (*l)->pressure * frametime / dist;
294                                 VectorMA(e->vertvel[i], dist, diff, e->vertvel[i]);
295                         }
296                 }
297                 */
298         }
299 }
300
301 /*
302 void R_MoveExplosionGas(explosiongas_t *e, explosiongas_t **list, explosiongas_t **listend, float frametime)
303 {
304         vec3_t end, diff;
305         vec_t dist, frictionscale;
306         explosiongas_t **l;
307         frictionscale = 1 - frametime;
308         frictionscale = bound(0, frictionscale, 1);
309         if (e->velocity[0] || e->velocity[1] || e->velocity[2])
310         {
311                 end[0] = e->origin[0] + frametime * e->velocity[0];
312                 end[1] = e->origin[1] + frametime * e->velocity[1];
313                 end[2] = e->origin[2] + frametime * e->velocity[2];
314                 if (r_explosionclip.value)
315                 {
316                         float f, dot;
317                         vec3_t impact, normal;
318                         f = TraceLine(e->origin, end, impact, normal, 0);
319                         VectorCopy(impact, e->origin);
320                         if (f < 1)
321                         {
322                                 // clip velocity against the wall
323                                 dot = DotProduct(e->velocity, normal) * -1.3f;
324                                 e->velocity[0] += normal[0] * dot;
325                                 e->velocity[1] += normal[1] * dot;
326                                 e->velocity[2] += normal[2] * dot;
327                         }
328                 }
329                 else
330                 {
331                         VectorCopy(end, e->origin);
332                 }
333                 e->velocity[2] += sv_gravity.value * frametime;
334                 VectorScale(e->velocity, frictionscale, e->velocity);
335         }
336         for (l = list;l < listend;l++)
337         {
338                 if (*l != e)
339                 {
340                         VectorSubtract(e->origin, (*l)->origin, diff);
341                         dist = DotProduct(diff, diff);
342                         if (dist < 4096 && dist >= 1)
343                         {
344                                 dist = (*l)->pressure * frametime / dist;
345                                 VectorMA(e->velocity, dist, diff, e->velocity);
346                         }
347                 }
348         }
349 }
350 */
351
352 void R_MoveExplosions(void)
353 {
354         int i;
355         float frametime;
356 //      explosiongas_t *gaslist[MAX_EXPLOSIONGAS], **l, **end;
357         frametime = cl.time - cl.oldtime;
358         /*
359         l = &gaslist[0];
360         for (i = 0;i < MAX_EXPLOSIONGAS;i++)
361         {
362                 if (explosiongas[i].pressure > 0)
363                 {
364                         explosiongas[i].pressure -= frametime * GASFADERATE;
365                         if (explosiongas[i].pressure > 0)
366                                 *l++ = &explosiongas[i];
367                 }
368         }
369         end = l;
370         for (l = gaslist;l < end;l++)
371                 R_MoveExplosionGas(*l, gaslist, end, frametime);
372         */
373
374         for (i = 0;i < MAX_EXPLOSIONS;i++)
375         {
376                 if (explosion[i].alpha > 0.0f)
377                 {
378                         if (explosion[i].starttime > cl.time)
379                         {
380                                 explosion[i].alpha = 0;
381                                 continue;
382                         }
383                         R_MoveExplosion(&explosion[i], /*gaslist, end, */frametime);
384                 }
385         }
386 }
387
388 void R_DrawExplosions(void)
389 {
390         int i;
391         if (!r_drawexplosions.value)
392                 return;
393         for (i = 0;i < MAX_EXPLOSIONS;i++)
394         {
395                 if (explosion[i].alpha > 0.0f)
396                 {
397                         R_DrawExplosion(&explosion[i]);
398                 }
399         }
400 }