UFO: Alien Invasion
Loading...
Searching...
No Matches
r_surface.cpp
Go to the documentation of this file.
1
5
6/*
7Copyright (C) 1997-2001 Id Software, Inc.
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18See the GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, 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
32void R_SetSurfaceBumpMappingParameters (const mBspSurface_t* surf, const image_t* normalMap, const image_t* specularMap)
33{
34 if (!r_state.lighting_enabled)
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
59static 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
88
90}
91
98void 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 */
163 if (r_state.active_normalmap)
164 R_EnableBumpmap(nullptr);
165
166 R_EnableGlowMap(nullptr);
167
168 R_Color(nullptr);
169}
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
#define SURF_BLEND66
Definition defines.h:258
#define SURF_BLEND33
Definition defines.h:257
Error checking function.
#define R_CheckError()
Definition r_error.h:30
#define GL_ELEMENT_INDEX_TYPE
Definition r_gl.h:58
GLuint glElementIndex_t
Definition r_gl.h:57
lightmap definitions
local graphics definitions
cvar_t * r_bumpmap
Definition r_main.cpp:103
rlocals_t r_locals
Definition r_main.cpp:49
rstate_t r_state
Definition r_main.cpp:48
#define MSURF_LIGHTMAP
void R_EnableGlowMap(const image_t *image)
Definition r_state.cpp:664
void R_BindLightmapTexture(GLuint texnum)
Definition r_state.cpp:90
void R_BindDeluxemapTexture(GLuint texnum)
Definition r_state.cpp:95
void R_UseMaterial(const material_t *material)
Definition r_state.cpp:105
void R_EnableBumpmap(const image_t *normalmap)
Enables bumpmapping and binds the given normalmap.
Definition r_state.cpp:465
void R_EnableSpecularMap(const image_t *image, bool enable)
Definition r_state.cpp:707
#define texunit_lightmap
Definition r_state.h:69
#define R_BindTexture(tn)
Definition r_state.h:184
void R_DrawSurfaces(const mBspSurfaces_t *surfs, glElementIndex_t *indexPtr)
General surface drawing function, that draw the surface chains.
Definition r_surface.cpp:98
void R_SetSurfaceBumpMappingParameters(const mBspSurface_t *surf, const image_t *normalMap, const image_t *specularMap)
Definition r_surface.cpp:32
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
struct image_s * specularmap
Definition r_image.h:71
GLuint texnum
Definition r_image.h:66
struct image_s * normalmap
Definition r_image.h:69
struct image_s * glowmap
Definition r_image.h:70
material_t material
Definition r_image.h:68
mBspTexInfo_t * texinfo
unsigned int numTriangles
surfaces are assigned to arrays based on their primary rendering type and then sorted by world textur...
mBspSurface_t ** surfaces
image_t * image
vec_t vec4_t[4]
Definition ufotypes.h:40