2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
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)
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)
39 typedef struct explosiongas_s
47 explosiongas_t explosiongas[MAX_EXPLOSIONGAS];
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];
57 typedef struct explosion_s
61 vec3_t vert[EXPLOSIONVERTS];
62 vec3_t vertvel[EXPLOSIONVERTS];
66 explosion_t explosion[MAX_EXPLOSIONS];
68 rtexture_t *explosiontexture;
69 rtexture_t *explosiontexturefog;
71 cvar_t r_explosionclip = {CVAR_SAVE, "r_explosionclip", "1"};
72 cvar_t r_drawexplosions = {0, "r_drawexplosions", "1"};
74 int R_ExplosionVert(int column, int row)
78 i = row * (EXPLOSIONGRID + 1) + column;
79 a = row * M_PI * 2 / EXPLOSIONGRID;
80 b = column * M_PI * 2 / EXPLOSIONGRID;
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;
91 void r_explosion_start(void)
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++)
100 for (x = 0;x < 128;x++)
103 j = (noise1[y][x] * noise2[y][x]) * 3 / 256 - 128;
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);
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);
121 void r_explosion_shutdown(void)
125 void r_explosion_newmap(void)
127 memset(explosion, 0, sizeof(explosion));
128 // memset(explosiongas, 0, sizeof(explosiongas));
131 void R_Explosion_Init(void)
135 for (y = 0;y < EXPLOSIONGRID;y++)
137 for (x = 0;x < EXPLOSIONGRID;x++)
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);
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);
149 for (i = 0;i < EXPLOSIONVERTS;i++)
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;
159 Cvar_RegisterVariable(&r_explosionclip);
160 Cvar_RegisterVariable(&r_drawexplosions);
162 R_RegisterModule("R_Explosions", r_explosion_start, r_explosion_shutdown, r_explosion_newmap);
165 void R_NewExplosion(vec3_t org)
169 byte noise[EXPLOSIONGRID*EXPLOSIONGRID];
170 fractalnoise(noise, EXPLOSIONGRID, 4);
171 for (i = 0;i < MAX_EXPLOSIONS;i++)
173 if (explosion[i].alpha <= 0.0f)
175 explosion[i].alpha = EXPLOSIONFADESTART;
176 for (j = 0;j < EXPLOSIONVERTS;j++)
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]);
190 while (i < MAX_EXPLOSIONGAS && j > 0)
192 while (explosiongas[i].pressure > 0)
195 if (i >= MAX_EXPLOSIONGAS)
199 VectorMA(org, EXPLOSIONGASSTARTRADIUS, v, v);
200 TraceLine(org, v, explosiongas[i].origin, NULL, 0);
202 VectorScale(v, EXPLOSIONGASSTARTVELOCITY, explosiongas[i].velocity);
203 explosiongas[i].pressure = j * GASDENSITY_SCALER;
209 void R_DrawExplosion(explosion_t *e)
211 int i, index, *indexlist = &explosiontris[0][0], alpha = bound(0, e->alpha * 128.0f, 128), texnum, fogtexnum;
214 // t = cl.time * 0.75;
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);
232 texnum = R_GetTexture(explosiontexture);
233 fogtexnum = R_GetTexture(explosiontexturefog);
234 for (i = 0;i < EXPLOSIONTRIS;i++)
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);
244 void R_MoveExplosion(explosion_t *e, /*explosiongas_t **list, explosiongas_t **listend, */float frametime)
254 e->alpha -= frametime * EXPLOSIONFADERATE;
255 frictionscale = 1 - frametime;
256 frictionscale = bound(0, frictionscale, 1);
257 for (i = 0;i < EXPLOSIONVERTS;i++)
259 if (e->vertvel[i][0] || e->vertvel[i][1] || e->vertvel[i][2])
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)
267 vec3_t impact, normal;
268 f = TraceLine(e->vert[i], end, impact, normal, 0);
269 VectorCopy(impact, e->vert[i]);
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;
281 VectorCopy(end, e->vert[i]);
283 e->vertvel[i][2] += sv_gravity.value * frametime * -0.25f;
284 VectorScale(e->vertvel[i], frictionscale, e->vertvel[i]);
287 for (l = list;l < listend;l++)
289 VectorSubtract(e->vert[i], (*l)->origin, diff);
290 dist = DotProduct(diff, diff);
291 if (dist < 4096 && dist >= 1)
293 dist = (*l)->pressure * frametime / dist;
294 VectorMA(e->vertvel[i], dist, diff, e->vertvel[i]);
302 void R_MoveExplosionGas(explosiongas_t *e, explosiongas_t **list, explosiongas_t **listend, float frametime)
305 vec_t dist, frictionscale;
307 frictionscale = 1 - frametime;
308 frictionscale = bound(0, frictionscale, 1);
309 if (e->velocity[0] || e->velocity[1] || e->velocity[2])
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)
317 vec3_t impact, normal;
318 f = TraceLine(e->origin, end, impact, normal, 0);
319 VectorCopy(impact, e->origin);
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;
331 VectorCopy(end, e->origin);
333 e->velocity[2] += sv_gravity.value * frametime;
334 VectorScale(e->velocity, frictionscale, e->velocity);
336 for (l = list;l < listend;l++)
340 VectorSubtract(e->origin, (*l)->origin, diff);
341 dist = DotProduct(diff, diff);
342 if (dist < 4096 && dist >= 1)
344 dist = (*l)->pressure * frametime / dist;
345 VectorMA(e->velocity, dist, diff, e->velocity);
352 void R_MoveExplosions(void)
356 // explosiongas_t *gaslist[MAX_EXPLOSIONGAS], **l, **end;
357 frametime = cl.time - cl.oldtime;
360 for (i = 0;i < MAX_EXPLOSIONGAS;i++)
362 if (explosiongas[i].pressure > 0)
364 explosiongas[i].pressure -= frametime * GASFADERATE;
365 if (explosiongas[i].pressure > 0)
366 *l++ = &explosiongas[i];
370 for (l = gaslist;l < end;l++)
371 R_MoveExplosionGas(*l, gaslist, end, frametime);
374 for (i = 0;i < MAX_EXPLOSIONS;i++)
376 if (explosion[i].alpha > 0.0f)
378 if (explosion[i].starttime > cl.time)
380 explosion[i].alpha = 0;
383 R_MoveExplosion(&explosion[i], /*gaslist, end, */frametime);
388 void R_DrawExplosions(void)
391 if (!r_drawexplosions.value)
393 for (i = 0;i < MAX_EXPLOSIONS;i++)
395 if (explosion[i].alpha > 0.0f)
397 R_DrawExplosion(&explosion[i]);