UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
r_surface.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 1997-2001 Id Software, Inc.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24 */
25 
26 #include "r_local.h"
27 #include "r_lightmap.h"
28 #include "r_light.h"
29 #include "r_error.h"
30 #include "r_draw.h"
31 
32 void R_SetSurfaceBumpMappingParameters (const mBspSurface_t* surf, const image_t* normalMap, const image_t* specularMap)
33 {
35  return;
36 
37  if (!r_bumpmap->value)
38  return;
39 
40  assert(surf);
41 
42  if (normalMap && (surf->flags & MSURF_LIGHTMAP)) {
43  const image_t* image = surf->texinfo->image;
45  R_EnableBumpmap(normalMap);
46  R_EnableSpecularMap(specularMap, true);
47  R_UseMaterial(&image->material);
48  } else {
49  R_EnableBumpmap(nullptr);
50  R_EnableSpecularMap(nullptr, false);
51  R_UseMaterial(nullptr);
52  }
53 }
54 
59 static void R_SetSurfaceState (const mBspSurface_t* surf)
60 {
61  image_t* image;
62 
63  if (r_state.blend_enabled) { /* alpha blend */
64  vec4_t color = {1.0, 1.0, 1.0, 1.0};
65  switch (surf->texinfo->flags & (SURF_BLEND33 | SURF_BLEND66)) {
66  case SURF_BLEND33:
67  color[3] = 0.33;
68  break;
69  case SURF_BLEND66:
70  color[3] = 0.66;
71  break;
72  }
73 
74  R_Color(color);
75  }
76 
77  image = surf->texinfo->image;
78  R_BindTexture(image->texnum); /* texture */
79 
80  if (texunit_lightmap.enabled) { /* lightmap */
81  if (surf->flags & MSURF_LIGHTMAP)
83  }
84 
86 
87  R_EnableGlowMap(image->glowmap);
88 
89  R_CheckError();
90 }
91 
98 void R_DrawSurfaces (const mBspSurfaces_t* surfs, glElementIndex_t* indexPtr)
99 {
100  int numSurfaces = surfs->count;
101  mBspSurface_t** surfPtrList = surfs->surfaces;
102  const int frame = r_locals.frame;
103 
104  int lastLightMap = 0, lastDeluxeMap = 0;
105  image_t* lastTexture = nullptr;
106  uint32_t lastFlags = ~0;
107 
108  int batchStart = 0, batchSize = 0; /* in triangles */
109 
110  while (numSurfaces--) {
111  const mBspSurface_t* surf = *surfPtrList++;
112  const int numTriangles = surf->numTriangles;
113  mBspTexInfo_t* texInfo;
114  int texFlags;
115  bool newBatch = false;
116 
117  if (surf->frame != frame)
118  continue;
119  if (numTriangles <= 0)
120  continue;
121 
122  refdef.brushCount += numTriangles;
123 
124  if (batchStart + batchSize != surf->firstTriangle) {
125  /* Cannot continue assembling the batch, draw it and start a new one*/
126  if (batchSize > 0) {
127  glDrawElements(GL_TRIANGLES, batchSize * 3, GL_ELEMENT_INDEX_TYPE, indexPtr + batchStart * 3);
128  refdef.batchCount++;
129  }
130  batchStart = surf->firstTriangle;
131  batchSize = 0;
132  newBatch = true;
133  }
134 
136  texInfo = surf->texinfo;
137  texFlags = texInfo->flags & (SURF_BLEND33 | SURF_BLEND66); /* should match flags that affect R_SetSurfaceState behavior */
138  if (texInfo->image != lastTexture || surf->lightmap_texnum != lastLightMap || surf->deluxemap_texnum != lastDeluxeMap || texFlags != lastFlags) {
139  if (!newBatch) {
140  /* changes in texturing require new batch */
141  glDrawElements(GL_TRIANGLES, batchSize * 3, GL_ELEMENT_INDEX_TYPE, indexPtr + batchStart * 3);
142  refdef.batchCount++;
143  batchStart = surf->firstTriangle;
144  batchSize = 0;
145  }
146  lastTexture = texInfo->image;
147  lastLightMap = surf->lightmap_texnum;
148  lastDeluxeMap = surf->deluxemap_texnum;
149  lastFlags = texFlags;
150  R_SetSurfaceState(surf);
151  }
152 
153  batchSize += numTriangles;
154  }
155 
156  /* finish uncomplete batch, if any */
157  if (batchSize > 0) {
158  glDrawElements(GL_TRIANGLES, batchSize * 3, GL_ELEMENT_INDEX_TYPE, indexPtr + batchStart * 3);
159  refdef.batchCount++;
160  }
161 
162  /* reset state */
164  R_EnableBumpmap(nullptr);
165 
166  R_EnableGlowMap(nullptr);
167 
168  R_Color(nullptr);
169 }
cvar_t * r_bumpmap
Definition: r_main.cpp:103
void R_EnableBumpmap(const image_t *normalmap)
Enables bumpmapping and binds the given normalmap.
Definition: r_state.cpp:465
image_t * image
Definition: r_model_brush.h:71
void R_BindLightmapTexture(GLuint texnum)
Definition: r_state.cpp:90
surfaces are assigned to arrays based on their primary rendering type and then sorted by world textur...
unsigned int numTriangles
float value
Definition: cvar.h:80
static void R_SetSurfaceState(const mBspSurface_t *surf)
Set the surface state according to surface flags and bind the texture.
Definition: r_surface.cpp:59
local graphics definitions
#define texunit_lightmap
Definition: r_state.h:69
void R_SetSurfaceBumpMappingParameters(const mBspSurface_t *surf, const image_t *normalMap, const image_t *specularMap)
Definition: r_surface.cpp:32
void R_BindDeluxemapTexture(GLuint texnum)
Definition: r_state.cpp:95
void R_Color(const vec4_t rgba)
Change the color to given value.
Definition: r_state.cpp:1011
bool lighting_enabled
Definition: r_state.h:154
lightmap definitions
#define SURF_BLEND33
Definition: defines.h:257
#define GL_ELEMENT_INDEX_TYPE
Definition: r_gl.h:58
rendererData_t refdef
Definition: r_main.cpp:45
rlocals_t r_locals
Definition: r_main.cpp:49
mBspSurface_t ** surfaces
int frame
Definition: r_local.h:109
GLuint glElementIndex_t
Definition: r_gl.h:57
mBspTexInfo_t * texinfo
struct image_s * glowmap
Definition: r_image.h:70
struct image_s * specularmap
Definition: r_image.h:71
uint32_t flags
Definition: r_model_brush.h:70
void R_UseMaterial(const material_t *material)
Definition: r_state.cpp:105
Error checking function.
rstate_t r_state
Definition: r_main.cpp:48
#define R_CheckError()
Definition: r_error.h:30
struct image_s * normalmap
Definition: r_image.h:69
#define SURF_BLEND66
Definition: defines.h:258
void R_DrawSurfaces(const mBspSurfaces_t *surfs, glElementIndex_t *indexPtr)
General surface drawing function, that draw the surface chains.
Definition: r_surface.cpp:98
bool blend_enabled
Definition: r_state.h:149
material_t material
Definition: r_image.h:68
void R_EnableSpecularMap(const image_t *image, bool enable)
Definition: r_state.cpp:707
#define R_BindTexture(tn)
Definition: r_state.h:184
const struct image_s * active_normalmap
Definition: r_state.h:166
void R_EnableGlowMap(const image_t *image)
Definition: r_state.cpp:664
#define MSURF_LIGHTMAP
Definition: r_model_brush.h:51
GLuint texnum
Definition: r_image.h:66
vec_t vec4_t[4]
Definition: ufotypes.h:40