DarkPlaces
Game engine based on the Quake 1 engine by id Software, developed by LadyHavoc
 
r_explosion.c
Go to the documentation of this file.
1/*
2Copyright (C) 1996-1997 Id Software, Inc.
3
4This program is free software; you can redistribute it and/or
5modify it under the terms of the GNU General Public License
6as published by the Free Software Foundation; either version 2
7of the License, or (at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19*/
20
21#include "quakedef.h"
22#include "cl_collision.h"
23
24#ifdef MAX_EXPLOSIONS
25#define EXPLOSIONGRID 8
26#define EXPLOSIONVERTS ((EXPLOSIONGRID+1)*(EXPLOSIONGRID+1))
27#define EXPLOSIONTRIS (EXPLOSIONGRID*EXPLOSIONGRID*2)
28
29static int numexplosions = 0;
30
32static unsigned short explosiontris[EXPLOSIONTRIS][3];
35
36typedef struct explosion_s
37{
38 float starttime;
39 float endtime;
40 float time;
41 float alpha;
42 float fade;
47}
49
51
53//static rtexture_t *explosiontexturefog;
54
56#endif
57
58cvar_t r_explosionclip = {CF_CLIENT | CF_ARCHIVE, "r_explosionclip", "1", "enables collision detection for explosion shell (so that it flattens against walls and floors)"};
59#ifdef MAX_EXPLOSIONS
60static cvar_t r_drawexplosions = {CF_CLIENT, "r_drawexplosions", "1", "enables rendering of explosion shells (see also cl_particles_explosions_shell)"};
61
62//extern qbool r_loadfog;
63static void r_explosion_start(void)
64{
65 int x, y;
66 static unsigned char noise1[128][128], noise2[128][128], noise3[128][128], data[128][128][4];
69 //explosiontexturefog = NULL;
70 fractalnoise(&noise1[0][0], 128, 32);
71 fractalnoise(&noise2[0][0], 128, 4);
72 fractalnoise(&noise3[0][0], 128, 4);
73 for (y = 0;y < 128;y++)
74 {
75 for (x = 0;x < 128;x++)
76 {
77 int j, r, g, b, a;
78 j = (noise1[y][x] * noise2[y][x]) * 3 / 256 - 128;
79 r = (j * 512) / 256;
80 g = (j * 256) / 256;
81 b = (j * 128) / 256;
82 a = noise3[y][x] * 3 - 128;
83 data[y][x][2] = bound(0, r, 255);
84 data[y][x][1] = bound(0, g, 255);
85 data[y][x][0] = bound(0, b, 255);
86 data[y][x][3] = bound(0, a, 255);
87 }
88 }
90// if (r_loadfog)
91// {
92// for (y = 0;y < 128;y++)
93// for (x = 0;x < 128;x++)
94// data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
95// explosiontexturefog = R_LoadTexture2D(explosiontexturepool, "explosiontexture_fog", 128, 128, &data[0][0][0], TEXTYPE_BGRA, TEXF_MIPMAP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL);
96// }
97 // note that explosions survive the restart
98}
99
104
105static void r_explosion_newmap(void)
106{
107 numexplosions = 0;
108 memset(explosion, 0, sizeof(explosion));
109}
110
111static int R_ExplosionVert(int column, int row)
112{
113 int i;
114 float yaw, pitch;
115 // top and bottom rows are all one position...
116 if (row == 0 || row == EXPLOSIONGRID)
117 column = 0;
118 i = row * (EXPLOSIONGRID + 1) + column;
119 yaw = ((double) column / EXPLOSIONGRID) * M_PI * 2;
120 pitch = (((double) row / EXPLOSIONGRID) - 0.5) * M_PI;
121 explosionpoint[i][0] = cos(yaw) * cos(pitch);
122 explosionpoint[i][1] = sin(yaw) * cos(pitch);
123 explosionpoint[i][2] = 1 * -sin(pitch);
124 explosiontexcoord2f[i][0] = (float) column / (float) EXPLOSIONGRID;
125 explosiontexcoord2f[i][1] = (float) row / (float) EXPLOSIONGRID;
127 return i;
128}
129#endif
130
132{
133#ifdef MAX_EXPLOSIONS
134 int i, x, y;
135 i = 0;
136 for (y = 0;y < EXPLOSIONGRID;y++)
137 {
138 for (x = 0;x < EXPLOSIONGRID;x++)
139 {
141 explosiontris[i][1] = R_ExplosionVert(x + 1, y );
142 explosiontris[i][2] = R_ExplosionVert(x , y + 1);
143 i++;
144 explosiontris[i][0] = R_ExplosionVert(x + 1, y );
145 explosiontris[i][1] = R_ExplosionVert(x + 1, y + 1);
146 explosiontris[i][2] = R_ExplosionVert(x , y + 1);
147 i++;
148 }
149 }
150
151#endif
153#ifdef MAX_EXPLOSIONS
155
157#endif
158}
159
160void R_NewExplosion(const vec3_t org)
161{
162#ifdef MAX_EXPLOSIONS
163 int i, j;
164 float dist, n;
165 explosion_t *e;
166 trace_t trace;
167 unsigned char noise[EXPLOSIONGRID*EXPLOSIONGRID];
168 fractalnoisequick(noise, EXPLOSIONGRID, 4); // adjust noise grid size according to explosion
169 for (i = 0, e = explosion;i < MAX_EXPLOSIONS;i++, e++)
170 {
171 if (!e->alpha)
172 {
174 e->starttime = cl.time;
176 e->time = e->starttime;
180 VectorCopy(org, e->origin);
181 for (j = 0;j < EXPLOSIONVERTS;j++)
182 {
183 // calculate start origin and velocity
184 n = noise[explosionnoiseindex[j]] * (1.0f / 255.0f) + 0.5;
186 VectorMA(e->origin, dist, explosionpoint[j], e->vert[j]);
188 VectorScale(explosionpoint[j], dist, e->vertvel[j]);
189 // clip start origin
190 if (e->clipping)
191 {
192 trace = CL_TraceLine(e->origin, e->vert[j], MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, 0, 0, collision_extendmovelength.value, true, false, NULL, false, false);
193 VectorCopy(trace.endpos, e->vert[i]);
194 }
195 }
196 break;
197 }
198 }
199#endif
200}
201
202#ifdef MAX_EXPLOSIONS
203static void R_DrawExplosion_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
204{
205 int surfacelistindex = 0;
206 const int numtriangles = EXPLOSIONTRIS, numverts = EXPLOSIONVERTS;
208 GL_DepthMask(false);
209 GL_DepthRange(0, 1);
211 GL_DepthTest(true);
214
215// R_Mesh_ResetTextureState();
216 R_SetupShader_Generic(explosiontexture, false, false, false);
217 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
218 {
219 const explosion_t *e = explosion + surfacelist[surfacelistindex];
220 // FIXME: this can't properly handle r_refdef.view.colorscale > 1
223 R_Mesh_Draw(0, numverts, 0, numtriangles, NULL, NULL, 0, explosiontris[0], NULL, 0);
224 }
225}
226
228{
229 int i;
230 float dot, end[3], frametime;
231 trace_t trace;
232
233 frametime = cl.time - e->time;
234 e->time = cl.time;
235 e->alpha = e->alpha - (e->fade * frametime);
236 if (e->alpha < 0 || cl.time > e->endtime)
237 {
238 e->alpha = 0;
239 return;
240 }
241 for (i = 0;i < EXPLOSIONVERTS;i++)
242 {
243 if (e->vertvel[i][0] || e->vertvel[i][1] || e->vertvel[i][2])
244 {
245 VectorMA(e->vert[i], frametime, e->vertvel[i], end);
246 if (e->clipping)
247 {
248 trace = CL_TraceLine(e->vert[i], end, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, 0, 0, collision_extendmovelength.value, true, false, NULL, false, false);
249 if (trace.fraction < 1)
250 {
251 // clip velocity against the wall
252 dot = -DotProduct(e->vertvel[i], trace.plane.normal);
253 VectorMA(e->vertvel[i], dot, trace.plane.normal, e->vertvel[i]);
254 }
255 VectorCopy(trace.endpos, e->vert[i]);
256 }
257 else
258 VectorCopy(end, e->vert[i]);
259 }
260 }
261}
262#endif
263
265{
266#ifdef MAX_EXPLOSIONS
267 int i;
268
270 return;
271
272 for (i = 0;i < numexplosions;i++)
273 {
274 if (explosion[i].alpha)
275 {
277 if (explosion[i].alpha)
279 }
280 }
281 while (numexplosions > 0 && explosion[i-1].alpha <= 0)
283#endif
284}
285
#define SUPERCONTENTS_SOLID
Definition bspfile.h:196
trace_t CL_TraceLine(const vec3_t start, const vec3_t end, int type, prvm_edict_t *passedict, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask, float extend, qbool hitnetworkbrushmodels, qbool hitnetworkplayers, int *hitnetworkentity, qbool hitcsqcentities, qbool hitsurfaces)
cvar_t cl_explosions_lifetime
Definition cl_main.c:73
cvar_t cl_explosions_size_start
Definition cl_main.c:71
cvar_t cl_explosions_size_end
Definition cl_main.c:72
client_state_t cl
Definition cl_main.c:117
cvar_t cl_explosions_alpha_start
Definition cl_main.c:69
cvar_t cl_explosions_alpha_end
Definition cl_main.c:70
#define CF_CLIENT
cvar/command that only the client can change/execute
Definition cmd.h:48
#define CF_ARCHIVE
cvar should have its set value saved to config.cfg and persist across sessions
Definition cmd.h:53
cvar_t collision_extendmovelength
Definition collision.c:14
float frametime
vector origin
void Cvar_RegisterVariable(cvar_t *variable)
registers a cvar that already has the name, string, and optionally the archive elements set.
Definition cvar.c:599
float alpha
void fractalnoise(unsigned char *noise, int size, int startgrid)
Definition fractalnoise.c:4
#define n(x, y)
void fractalnoisequick(unsigned char *noise, int size, int startgrid)
void GL_CullFace(int state)
void GL_DepthMask(int state)
void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const r_meshbuffer_t *element3i_indexbuffer, int element3i_bufferoffset, const unsigned short *element3s, const r_meshbuffer_t *element3s_indexbuffer, int element3s_bufferoffset)
void GL_DepthTest(int state)
void GL_DepthRange(float nearfrac, float farfrac)
void GL_PolygonOffset(float planeoffset, float depthoffset)
void GL_Color(float cr, float cg, float cb, float ca)
void GL_BlendFunc(int blendfunc1, int blendfunc2)
void R_Mesh_PrepareVertices_Generic_Arrays(int numvertices, const float *vertex3f, const float *color4f, const float *texcoord2f)
void R_EntityMatrix(const matrix4x4_t *matrix)
Definition gl_rmain.c:4417
void R_SetupShader_Generic(rtexture_t *t, qbool usegamma, qbool notrippy, qbool suppresstexalpha)
Definition gl_rmain.c:1461
r_refdef_t r_refdef
Definition gl_rmain.c:57
void R_FreeTexturePool(rtexturepool_t **rtexturepool)
rtexturepool_t * R_AllocTexturePool(void)
rtexture_t * R_LoadTexture2D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, const unsigned char *data, textype_t textype, int flags, int miplevel, const unsigned int *palette)
#define GL_SRC_ALPHA
Definition glquake.h:79
GLint GLenum GLint GLint y
Definition glquake.h:651
GLint GLenum GLint x
Definition glquake.h:651
GLsizeiptr const GLvoid * data
Definition glquake.h:639
#define GL_ONE
Definition glquake.h:74
#define max(A, B)
Definition mathlib.h:38
#define bound(min, num, max)
Definition mathlib.h:34
#define DotProduct(a, b)
Definition mathlib.h:98
#define VectorCopy(in, out)
Definition mathlib.h:101
#define VectorScale(in, scale, out)
Definition mathlib.h:111
#define M_PI
Definition mathlib.h:28
#define VectorMA(a, scale, b, out)
Definition mathlib.h:114
const matrix4x4_t identitymatrix
Definition matrixlib.c:9
float cos(float f)
float sin(float f)
void R_MeshQueue_AddTransparent(dptransparentsortcategory_t category, const vec3_t center, void(*callback)(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist), const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight)
Definition meshqueue.c:33
string noise
Definition progsdefs.qc:209
string noise1
Definition progsdefs.qc:209
string noise3
Definition progsdefs.qc:209
string noise2
Definition progsdefs.qc:209
int i
#define MAX_EXPLOSIONS
max number of explosion shell effects active at once (not particle related)
Definition qdefs.h:131
#define NULL
Definition qtypes.h:12
vec_t vec3_t[3]
Definition qtypes.h:71
bool qbool
Definition qtypes.h:9
static rtexturepool_t * explosiontexturepool
Definition r_explosion.c:55
static void R_DrawExplosion_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
#define EXPLOSIONGRID
Definition r_explosion.c:25
static void r_explosion_newmap(void)
static void r_explosion_shutdown(void)
static int numexplosions
Definition r_explosion.c:29
void R_DrawExplosions(void)
static int R_ExplosionVert(int column, int row)
void R_Explosion_Init(void)
static void r_explosion_start(void)
Definition r_explosion.c:63
static rtexture_t * explosiontexture
Definition r_explosion.c:52
static float explosiontexcoord2f[EXPLOSIONVERTS][2]
Definition r_explosion.c:31
static cvar_t r_drawexplosions
Definition r_explosion.c:60
cvar_t r_explosionclip
Definition r_explosion.c:58
static int explosionnoiseindex[EXPLOSIONVERTS]
Definition r_explosion.c:33
static vec3_t explosionpoint[EXPLOSIONVERTS]
Definition r_explosion.c:34
void R_NewExplosion(const vec3_t org)
static unsigned short explosiontris[EXPLOSIONTRIS][3]
Definition r_explosion.c:32
#define EXPLOSIONVERTS
Definition r_explosion.c:26
static void R_MoveExplosion(explosion_t *e)
static explosion_t explosion[MAX_EXPLOSIONS]
Definition r_explosion.c:50
#define EXPLOSIONTRIS
Definition r_explosion.c:27
void R_RegisterModule(const char *name, void(*start)(void), void(*shutdown)(void), void(*newmap)(void), void(*devicelost)(void), void(*devicerestored)(void))
Definition r_modules.c:25
@ TRANSPARENTSORT_DISTANCE
Definition r_qshader.h:194
#define TEXF_ALPHA
Definition r_textures.h:9
#define TEXF_FORCELINEAR
Definition r_textures.h:19
#define TEXF_MIPMAP
Definition r_textures.h:11
@ TEXTYPE_BGRA
Definition r_textures.h:53
dp_FragColor r
dp_FragColor g
precision highp float
Definition shader_glsl.h:53
dp_FragColor b
ret a
double time
Definition client.h:868
Definition cvar.h:66
float value
Definition cvar.h:74
int integer
Definition cvar.h:73
float endtime
Definition r_explosion.c:39
vec3_t origin
Definition r_explosion.c:43
vec3_t vertvel[EXPLOSIONVERTS]
Definition r_explosion.c:45
vec3_t vert[EXPLOSIONVERTS]
Definition r_explosion.c:44
qbool clipping
Definition r_explosion.c:46
float starttime
Definition r_explosion.c:38
float polygonoffset
Definition render.h:459
r_refdef_view_t view
Definition render.h:406
float polygonfactor
Definition render.h:458
float colorscale
global RGB color multiplier for rendering
Definition render.h:302
int cullface_back
Definition render.h:318
double fraction
Definition collision.h:40
double endpos[3]
Definition collision.h:42
plane_t plane
Definition collision.h:44
vec3_t normal
Definition collision.h:13
#define MOVE_NOMONSTERS
Definition world.h:29