UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
r_array.cpp
Go to the documentation of this file.
1 
7 /*
8 Copyright(c) 1997-2001 Id Software, Inc.
9 Copyright(c) 2002 The Quakeforge Project.
10 Copyright(c) 2006 Quake2World.
11 
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 as published by the Free Software Foundation; either version 2
15 of the License, or(at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 
21 See the GNU General Public License for more details.
22 
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, 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 
38 typedef struct r_array_state_s {
40  int arrays;
42 
44 
45 
52 static int R_ArraysMask (void)
53 {
54  int mask;
55 
57 
59  mask |= R_ARRAY_COLOR;
60 
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)
72  mask |= R_ARRAY_TEX_LIGHTMAP;
73 
74  return mask;
75 }
76 
82 static 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 */
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 
118 static 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 
153 void R_SetArrayState (const mBspModel_t* bsp)
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 
185 void R_ResetArrayState (void)
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);
195  /* vertex array */
196  R_BindDefaultArray(GL_VERTEX_ARRAY);
197 
198  /* color array */
200  R_BindDefaultArray(GL_COLOR_ARRAY);
201 
202  /* normals and tangents */
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 void R_SetVertexBufferState(const mBspModel_t *bsp, int mask)
Definition: r_array.cpp:118
cvar_t * r_bumpmap
Definition: r_main.cpp:103
#define GL_TANGENT_ARRAY
Definition: r_gl.h:76
#define R_ARRAY_ELEMENT
Definition: r_array.cpp:36
cvar_t * r_vertexbuffers
Definition: r_main.cpp:94
const mBspModel_t * bspmodel
Definition: r_array.cpp:39
unsigned int texcoord_buffer
void R_BindBuffer(GLenum target, GLenum type, GLuint id)
Definition: r_state.cpp:213
#define R_ARRAY_TEX_LIGHTMAP
Definition: r_array.cpp:35
float value
Definition: cvar.h:80
local graphics definitions
int integer
Definition: cvar.h:81
#define texunit_lightmap
Definition: r_state.h:69
#define R_ARRAY_NORMAL
Definition: r_array.cpp:32
bool lighting_enabled
Definition: r_state.h:154
#define R_ARRAY_TEX_DIFFUSE
Definition: r_array.cpp:34
static r_array_state_t r_array_state
Definition: r_array.cpp:43
#define R_ARRAY_TANGENT
Definition: r_array.cpp:33
unsigned int normal_buffer
float * texcoords
float * tangents
unsigned int tangent_buffer
void R_ResetArrayState(void)
Definition: r_array.cpp:185
struct r_array_state_s r_array_state_t
void R_BindDefaultArray(GLenum target)
Binds the appropriate shared vertex array to the specified target.
Definition: r_state.cpp:182
unsigned int vertex_buffer
bool color_array_enabled
Definition: r_state.h:151
unsigned int lmtexcoord_buffer
float * lmtexcoords
#define texunit_diffuse
Definition: r_state.h:68
float * verts
#define R_ARRAY_COLOR
Definition: r_array.cpp:31
brush model
void R_SetArrayState(const mBspModel_t *bsp)
Definition: r_array.cpp:153
rstate_t r_state
Definition: r_main.cpp:48
static void R_SetVertexArrayState(const mBspModel_t *bsp, int mask)
Definition: r_array.cpp:82
float * normals
bool modified
Definition: cvar.h:79
unsigned int index_buffer
#define R_ARRAY_VERTEX
Definition: r_array.cpp:30
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
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