UFO: Alien Invasion
Loading...
Searching...
No Matches
r_array.cpp
Go to the documentation of this file.
1
6
7/*
8Copyright(c) 1997-2001 Id Software, Inc.
9Copyright(c) 2002 The Quakeforge Project.
10Copyright(c) 2006 Quake2World.
11
12This program is free software; you can redistribute it and/or
13modify it under the terms of the GNU General Public License
14as published by the Free Software Foundation; either version 2
15of the License, or(at your option) any later version.
16
17This program is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20
21See the GNU General Public License for more details.
22
23You should have received a copy of the GNU General Public License
24along with this program; if not, write to the Free Software
25Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26*/
27
28#include "r_local.h"
29
30#define R_ARRAY_VERTEX 0x1
31#define R_ARRAY_COLOR 0x2
32#define R_ARRAY_NORMAL 0x4
33#define R_ARRAY_TANGENT 0x8
34#define R_ARRAY_TEX_DIFFUSE 0x10
35#define R_ARRAY_TEX_LIGHTMAP 0x20
36#define R_ARRAY_ELEMENT 0x1000
37
38typedef struct r_array_state_s {
40 int arrays;
42
44
45
52static int R_ArraysMask (void)
53{
54 int mask;
55
57
58 if (r_state.color_array_enabled)
59 mask |= R_ARRAY_COLOR;
60
61 if (r_state.lighting_enabled) {
62 mask |= R_ARRAY_NORMAL;
63
64 if (r_bumpmap->value > 0.0)
65 mask |= R_ARRAY_TANGENT;
66 }
67
68 if (texunit_diffuse.enabled)
69 mask |= R_ARRAY_TEX_DIFFUSE;
70
71 if (texunit_lightmap.enabled)
73
74 return mask;
75}
76
82static inline void R_SetVertexArrayState (const mBspModel_t* bsp, int mask)
83{
84 /* vertex array */
85 if (mask & R_ARRAY_VERTEX)
86 R_BindArray(GL_VERTEX_ARRAY, GL_FLOAT, bsp->verts);
87
88 /* normals and tangents for lighting */
89 if (r_state.lighting_enabled) {
90 if (mask & R_ARRAY_NORMAL)
91 R_BindArray(GL_NORMAL_ARRAY, GL_FLOAT, bsp->normals);
92
93 if (mask & R_ARRAY_TANGENT)
94 R_BindArray(GL_TANGENT_ARRAY, GL_FLOAT, bsp->tangents);
95 }
96
97 /* diffuse texcoords */
98 if (texunit_diffuse.enabled) {
99 if (mask & R_ARRAY_TEX_DIFFUSE)
100 R_BindArray(GL_TEXTURE_COORD_ARRAY, GL_FLOAT, bsp->texcoords);
101 }
102
103 if (texunit_lightmap.enabled) { /* lightmap texcoords */
104 if (mask & R_ARRAY_TEX_LIGHTMAP) {
106 R_BindArray(GL_TEXTURE_COORD_ARRAY, GL_FLOAT, bsp->lmtexcoords);
108 }
109 }
110}
111
118static inline void R_SetVertexBufferState (const mBspModel_t* bsp, int mask)
119{
120 /* vertex array */
121 if (mask & R_ARRAY_VERTEX)
122 R_BindBuffer(GL_VERTEX_ARRAY, GL_FLOAT, bsp->vertex_buffer);
123
124 /* index array */
125 if (mask & R_ARRAY_ELEMENT)
126 R_BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0, bsp->index_buffer);
127 /* R_BindBuffer ignores the type parameter for GL_ELEMENT_ARRAY_BUFFER, and GL_INT is not defined in GLES */
128
129 if (r_state.lighting_enabled) { /* normals and tangents for lighting */
130 if (mask & R_ARRAY_NORMAL)
131 R_BindBuffer(GL_NORMAL_ARRAY, GL_FLOAT, bsp->normal_buffer);
132
133 if (mask & R_ARRAY_TANGENT)
135 }
136
137 /* diffuse texcoords */
138 if (texunit_diffuse.enabled) {
139 if (mask & R_ARRAY_TEX_DIFFUSE)
140 R_BindBuffer(GL_TEXTURE_COORD_ARRAY, GL_FLOAT, bsp->texcoord_buffer);
141 }
142
143 /* lightmap texcoords */
144 if (texunit_lightmap.enabled) {
145 if (mask & R_ARRAY_TEX_LIGHTMAP) {
147 R_BindBuffer(GL_TEXTURE_COORD_ARRAY, GL_FLOAT, bsp->lmtexcoord_buffer);
149 }
150 }
151}
152
154{
155 int arrays, mask;
156
157 if (r_vertexbuffers->modified) { /* force a full re-bind */
158 r_array_state.bspmodel = nullptr;
159 r_array_state.arrays = 0xFFFF;
160 r_vertexbuffers->modified = false;
161 }
162
163 /* resolve the desired arrays mask */
164 mask = 0xFFFF, arrays = R_ArraysMask();
165
166 /* try to save some binds */
167 if (r_array_state.bspmodel == bsp) {
168 const int xorR = r_array_state.arrays ^ arrays;
169 if (!xorR) /* no changes, we're done */
170 return;
171
172 /* resolve what's left to turn on */
173 mask = arrays & xorR;
174 }
175
176 if (r_vertexbuffers->integer && qglGenBuffers) /* use vbo */
177 R_SetVertexBufferState(bsp, mask);
178 else /* or arrays */
179 R_SetVertexArrayState(bsp, mask);
180
181 r_array_state.bspmodel = bsp;
182 r_array_state.arrays = arrays;
183}
184
186{
187 r_array_state.bspmodel = nullptr;
188 r_array_state.arrays = 0;
189
190 /* vbo */
191 R_BindBuffer(0, 0, 0);
192 if (qglBindBuffer && r_vertexbuffers->integer)
193 qglBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
194
195 /* vertex array */
196 R_BindDefaultArray(GL_VERTEX_ARRAY);
197
198 /* color array */
199 if (r_state.color_array_enabled)
200 R_BindDefaultArray(GL_COLOR_ARRAY);
201
202 /* normals and tangents */
203 if (r_state.lighting_enabled) {
204 R_BindDefaultArray(GL_NORMAL_ARRAY);
205
207 }
208
209 /* diffuse texcoords */
210 if (texunit_diffuse.enabled)
211 R_BindDefaultArray(GL_TEXTURE_COORD_ARRAY);
212
213 /* lightmap texcoords */
214 if (texunit_lightmap.enabled) {
216 R_BindDefaultArray(GL_TEXTURE_COORD_ARRAY);
218 }
219}
static int R_ArraysMask(void)
This function is consulted to determine whether or not array bindings are up to date.
Definition r_array.cpp:52
#define R_ARRAY_TEX_DIFFUSE
Definition r_array.cpp:34
#define R_ARRAY_VERTEX
Definition r_array.cpp:30
#define R_ARRAY_ELEMENT
Definition r_array.cpp:36
void R_SetArrayState(const mBspModel_t *bsp)
Definition r_array.cpp:153
static void R_SetVertexArrayState(const mBspModel_t *bsp, int mask)
Definition r_array.cpp:82
#define R_ARRAY_TANGENT
Definition r_array.cpp:33
void R_ResetArrayState(void)
Definition r_array.cpp:185
static r_array_state_t r_array_state
Definition r_array.cpp:43
static void R_SetVertexBufferState(const mBspModel_t *bsp, int mask)
Definition r_array.cpp:118
#define R_ARRAY_COLOR
Definition r_array.cpp:31
#define R_ARRAY_TEX_LIGHTMAP
Definition r_array.cpp:35
#define R_ARRAY_NORMAL
Definition r_array.cpp:32
#define GL_TANGENT_ARRAY
Definition r_gl.h:76
local graphics definitions
cvar_t * r_bumpmap
Definition r_main.cpp:103
cvar_t * r_vertexbuffers
Definition r_main.cpp:94
rstate_t r_state
Definition r_main.cpp:48
void R_BindBuffer(GLenum target, GLenum type, GLuint id)
Definition r_state.cpp:213
void R_BindDefaultArray(GLenum target)
Binds the appropriate shared vertex array to the specified target.
Definition r_state.cpp:182
bool R_SelectTexture(gltexunit_t *texunit)
Returns false if the texunit is not supported.
Definition r_state.cpp:40
void R_BindArray(GLenum target, GLenum type, const void *array)
Definition r_state.cpp:148
#define texunit_diffuse
Definition r_state.h:68
#define texunit_lightmap
Definition r_state.h:69
brush model
unsigned int tangent_buffer
unsigned int normal_buffer
float * normals
unsigned int index_buffer
unsigned int vertex_buffer
unsigned int texcoord_buffer
unsigned int lmtexcoord_buffer
float * lmtexcoords
float * texcoords
float * tangents
const mBspModel_t * bspmodel
Definition r_array.cpp:39