UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 */
55  if (s->flags & (STAGE_SCALE_S | STAGE_SCALE_T))
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 
69 void R_DrawFlareSurfaces (const mBspSurfaces_t* surfs, glElementIndex_t* indexPtr)
70 {
71  if (!r_flares->integer)
72  return;
73 
74  if (!surfs->count)
75  return;
76 
77  bool oldblend = r_state.blend_enabled;
78 
79  R_EnableColorArray(true);
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];
98  mBspFlare_t* f;
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 
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 
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 }
vec_t VectorLength(const vec3_t v)
Calculate the length of a vector.
Definition: mathlib.cpp:434
float t
Definition: r_material.h:96
#define VectorCopy(src, dest)
Definition: vector.h:51
unsigned flags
Definition: r_material.h:133
memPool_t * vid_modelPool
Definition: cl_main.cpp:90
image_t * image
Definition: r_model_brush.h:71
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 const vec3_t scale
void R_CreateSurfaceFlare(mBspSurface_t *surf)
Definition: r_flare.cpp:24
GLfloat * vertex_array_3d
Definition: r_state.h:100
const image_t * image
Definition: r_model_brush.h:77
void R_BlendFunc(GLenum src, GLenum dest)
Definition: r_state.cpp:232
surfaces are assigned to arrays based on their primary rendering type and then sorted by world textur...
float vec_t
Definition: ufotypes.h:37
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
float value
Definition: cvar.h:80
local graphics definitions
int integer
Definition: cvar.h:81
#define VectorScale(in, scale, out)
Definition: vector.h:79
void getDiagonal(vec3_t diagonal) const
Definition: aabb.h:159
float fraction
Definition: tracing.h:58
void R_Color(const vec4_t rgba)
Change the color to given value.
Definition: r_state.cpp:1011
#define STAGE_FLARE
Definition: r_material.h:48
vec3_t viewOrigin
Definition: cl_renderer.h:172
materialStage_t * stages
Definition: r_material.h:164
#define DotProduct(x, y)
Returns the distance between two 3-dimensional vectors.
Definition: vector.h:44
cvar_t * r_flares
Definition: r_main.cpp:108
rendererData_t refdef
Definition: r_main.cpp:45
rlocals_t r_locals
Definition: r_main.cpp:49
mBspSurface_t ** surfaces
mBspFlare_t * flare
int frame
Definition: r_local.h:109
void R_ResetArrayState(void)
Definition: r_array.cpp:185
void R_EnableColorArray(bool enable)
Definition: r_state.cpp:332
GLuint glElementIndex_t
Definition: r_gl.h:57
vec3_t up
Definition: r_local.h:102
mBspTexInfo_t * texinfo
#define MASK_SOLID
Definition: defines.h:272
Definition: line.h:31
float alpha
Definition: r_entity.h:92
unsigned flags
Definition: r_material.h:157
vec3_t origin
Definition: r_model_brush.h:75
QGL_EXTERN GLfloat f
Definition: r_gl.h:114
void R_Trace(const Line &trLine, float size, int contentmask)
Moves the given mins/maxs volume through the world from start to end.
Definition: r_lightmap.cpp:317
#define VectorAdd(a, b, dest)
Definition: vector.h:47
#define STAGE_COLOR
Definition: r_material.h:35
#define texunit_diffuse
Definition: r_state.h:68
image_t * r_flaretextures[NUM_FLARETEXTURES]
Definition: r_image.cpp:53
#define STAGE_SCALE_S
Definition: r_material.h:41
QGL_EXTERN GLint i
Definition: r_gl.h:113
void R_EnableBlend(bool enable)
Definition: r_state.cpp:261
vec_t VectorNormalize(vec3_t v)
Calculate unit vector for a given vec3_t.
Definition: mathlib.cpp:745
void R_DrawArrays(GLint first, GLsizei count)
Definition: r_gl.h:35
vec3_t color
Definition: r_model_brush.h:78
float s
Definition: r_material.h:96
vec_t vec3_t[3]
Definition: ufotypes.h:39
vec_t vec2_t[2]
Definition: ufotypes.h:38
#define STAGE_SCALE_T
Definition: r_material.h:42
vec3_t right
Definition: r_local.h:104
rstate_t r_state
Definition: r_main.cpp:48
GLfloat * color_array
Definition: r_state.h:102
#define Mem_PoolAllocType(type, pool)
Definition: mem.h:43
struct image_s * image
Definition: r_material.h:134
bool blend_enabled
Definition: r_state.h:149
material_t material
Definition: r_image.h:68
static struct mdfour * m
Definition: md4.cpp:35
const vec2_t default_texcoords[4]
Definition: r_state.cpp:30
#define VectorSubtract(a, b, dest)
Definition: vector.h:45
#define R_BindTexture(tn)
Definition: r_state.h:184
struct materialStage_s * next
Definition: r_material.h:147
GLuint texnum
Definition: r_image.h:66