UFO: Alien Invasion
Loading...
Searching...
No Matches
r_flare.cpp
Go to the documentation of this file.
1/*
2 * Copyright(c) 1997-2001 Id Software, Inc.
3 * Copyright(c) 2002 The Quakeforge Project.
4 * Copyright(c) 2006 Quake2World.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 *
15 * See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22#include "r_local.h"
23
25{
26 material_t* m = &surf->texinfo->image->material;
27
28 if (!(m->flags & STAGE_FLARE)) /* surface is not flared */
29 return;
30
32
33 /* move the flare away from the surface, into the level */
34 VectorMA(surf->center, 2, surf->normal, surf->flare->origin);
35
36 /* calculate the flare radius based on surface size */
37 vec3_t span;
38 surf->mbsBox.getDiagonal(span);
39 surf->flare->radius = VectorLength(span);
40
41 const materialStage_t* s = m->stages; /* resolve the flare stage */
42 for (;;) {
43 if (s->flags & STAGE_FLARE)
44 break;
45 s = s->next;
46 }
47
48 /* resolve flare color */
49 if (s->flags & STAGE_COLOR)
50 VectorCopy(s->color, surf->flare->color);
51 else
52 VectorCopy(surf->lightColor, surf->flare->color);
53
54 /* and scaled radius */
56 surf->flare->radius *= (s->scale.s ? s->scale.s : s->scale.t);
57
58 /* and image */
59 surf->flare->image = s->image;
60}
61
70{
71 if (!r_flares->integer)
72 return;
73
74 if (!surfs->count)
75 return;
76
77 bool oldblend = r_state.blend_enabled;
78
80
82
84 glDisable(GL_DEPTH_TEST);
85 R_EnableBlend(true);
86 R_BlendFunc(GL_SRC_ALPHA, GL_ONE);
87
88 const image_t* image = r_flaretextures[0];
89 R_BindTexture(image->texnum);
90
91 int i, j, k, l;
92 j = k = l = 0;
93 for (i = 0; i < surfs->count; i++) {
94 vec3_t view, verts[4];
95 vec3_t right, up, upright, downright;
96 float dot, dist, scale, alpha;
97 mBspSurface_t* surf = surfs->surfaces[i];
99
100 if (surf->frame != r_locals.frame)
101 continue;
102
103 f = surf->flare;
104
105 /* bind the flare's texture */
106 if (f->image != image) {
107 R_DrawArrays(0, l / 3);
108 j = k = l = 0;
109
110 refdef.batchCount++;
111
112 image = f->image;
113 R_BindTexture(image->texnum);
114 }
115
116 /* periodically test visibility to ramp alpha */
117 if (refdef.time - f->time > 0.02) {
118 if (refdef.time - f->time > 0.5) /* reset old flares */
119 f->alpha = 0;
120
121 R_Trace(Line(refdef.viewOrigin, f->origin), 0, MASK_SOLID);
122 bool visible = refdef.trace.fraction == 1.0;
123
124 f->alpha += (visible ? 0.03 : -0.15); /* ramp */
125
126 if (f->alpha > 0.75) /* clamp */
127 f->alpha = 0.75;
128 else if (f->alpha < 0)
129 f->alpha = 0.0;
130
131 f->time = refdef.time;
132 }
133
134 VectorSubtract(f->origin, refdef.viewOrigin, view);
135 dist = VectorNormalize(view);
136
137 /* fade according to angle */
138 dot = DotProduct(surf->normal, view);
139 if (dot > 0)
140 continue;
141
142 alpha = 0.1 + -dot * r_flares->value;
143
144 if (alpha > 1.0)
145 alpha = 1.0;
146
147 alpha = f->alpha * alpha;
148
149 /* scale according to distance */
150 scale = f->radius + (f->radius * dist * .0005);
151
152 VectorScale(r_locals.right, scale, right);
153 VectorScale(r_locals.up, scale, up);
154
155 VectorAdd(up, right, upright);
156 VectorSubtract(right, up, downright);
157
158 VectorSubtract(f->origin, downright, verts[0]);
159 VectorAdd(f->origin, upright, verts[1]);
160 VectorAdd(f->origin, downright, verts[2]);
161 VectorSubtract(f->origin, upright, verts[3]);
162
163 for (int m = 0; m < 4; m++) { /* duplicate color data to all 4 verts */
164 memcpy(&r_state.color_array[j], f->color, sizeof(vec3_t));
165 r_state.color_array[j + 3] = alpha;
166 j += 4;
167 }
168
169 /* copy texcoord info */
170 memcpy(&texunit_diffuse.texcoord_array[k], default_texcoords, sizeof(vec2_t) * 4);
171 k += sizeof(vec2_t) / sizeof(vec_t) * 4;
172
173 /* and lastly copy the 4 verts */
174 memcpy(&r_state.vertex_array_3d[l], verts, sizeof(vec3_t) * 4);
175 l += sizeof(vec3_t) / sizeof(vec_t) * 4;
176 }
177
178 R_DrawArrays(0, l / 3);
179
180 refdef.batchCount++;
181
182 R_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
183 R_EnableBlend(oldblend);
184 glEnable(GL_DEPTH_TEST);
185
186 R_EnableColorArray(false);
187
188 R_Color(nullptr);
189}
memPool_t * vid_modelPool
Definition cl_main.cpp:90
rendererData_t refdef
Definition r_main.cpp:45
void R_Color(const vec4_t rgba)
Change the color to given value.
Definition r_state.cpp:1011
void getDiagonal(vec3_t diagonal) const
Definition aabb.h:159
Definition line.h:31
#define MASK_SOLID
Definition defines.h:272
vec_t VectorNormalize(vec3_t v)
Calculate unit vector for a given vec3_t.
Definition mathlib.cpp:745
vec_t VectorLength(const vec3_t v)
Calculate the length of a vector.
Definition mathlib.cpp:434
void VectorMA(const vec3_t veca, const float scale, const vec3_t vecb, vec3_t outVector)
Sets vector_out (vc) to vevtor1 (va) + scale * vector2 (vb).
Definition mathlib.cpp:261
static struct mdfour * m
Definition md4.cpp:35
#define Mem_PoolAllocType(type, pool)
Definition mem.h:43
void R_ResetArrayState(void)
Definition r_array.cpp:185
void R_CreateSurfaceFlare(mBspSurface_t *surf)
Definition r_flare.cpp:24
void R_DrawFlareSurfaces(const mBspSurfaces_t *surfs, glElementIndex_t *indexPtr)
Flares are batched by their texture. Usually, this means one draw operation for all flares in view....
Definition r_flare.cpp:69
QGL_EXTERN GLfloat f
Definition r_gl.h:114
QGL_EXTERN GLint i
Definition r_gl.h:113
GLuint glElementIndex_t
Definition r_gl.h:57
void R_DrawArrays(GLint first, GLsizei count)
Definition r_gl.h:35
image_t * r_flaretextures[NUM_FLARETEXTURES]
Definition r_image.cpp:53
void R_Trace(const Line &trLine, float size, int contentmask)
Moves the given mins/maxs volume through the world from start to end.
local graphics definitions
cvar_t * r_flares
Definition r_main.cpp:108
rlocals_t r_locals
Definition r_main.cpp:49
rstate_t r_state
Definition r_main.cpp:48
#define STAGE_FLARE
Definition r_material.h:48
#define STAGE_SCALE_T
Definition r_material.h:42
#define STAGE_SCALE_S
Definition r_material.h:41
#define STAGE_COLOR
Definition r_material.h:35
void R_BlendFunc(GLenum src, GLenum dest)
Definition r_state.cpp:232
void R_EnableBlend(bool enable)
Definition r_state.cpp:261
const vec2_t default_texcoords[4]
Definition r_state.cpp:30
void R_EnableColorArray(bool enable)
Definition r_state.cpp:332
#define texunit_diffuse
Definition r_state.h:68
#define R_BindTexture(tn)
Definition r_state.h:184
GLuint texnum
Definition r_image.h:66
material_t material
Definition r_image.h:68
const image_t * image
mBspTexInfo_t * texinfo
mBspFlare_t * flare
surfaces are assigned to arrays based on their primary rendering type and then sorted by world textur...
mBspSurface_t ** surfaces
image_t * image
struct image_s * image
Definition r_material.h:134
struct materialStage_s * next
Definition r_material.h:147
float s
Definition r_material.h:96
float t
Definition r_material.h:96
float vec_t
Definition ufotypes.h:37
vec_t vec3_t[3]
Definition ufotypes.h:39
vec_t vec2_t[2]
Definition ufotypes.h:38
static const vec3_t scale
#define VectorSubtract(a, b, dest)
Definition vector.h:45
#define VectorCopy(src, dest)
Definition vector.h:51
#define VectorAdd(a, b, dest)
Definition vector.h:47
#define DotProduct(x, y)
Returns the distance between two 3-dimensional vectors.
Definition vector.h:44
#define VectorScale(in, scale, out)
Definition vector.h:79