2
* ***** BEGIN GPL LICENSE BLOCK *****
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19
* All rights reserved.
21
* The Original Code is: all of this file.
23
* Contributor(s): none yet.
25
* ***** END GPL LICENSE BLOCK *****
28
#include "RAS_StorageVA.h"
32
RAS_StorageVA::RAS_StorageVA(int *texco_num, RAS_IRasterizer::TexCoGen *texco, int *attrib_num, RAS_IRasterizer::TexCoGen *attrib, int *attrib_layer) :
33
m_texco_num(texco_num),
34
m_attrib_num(attrib_num),
39
m_attrib_layer(attrib_layer)
43
RAS_StorageVA::~RAS_StorageVA()
47
bool RAS_StorageVA::Init()
49
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
54
void RAS_StorageVA::Exit()
58
void RAS_StorageVA::IndexPrimitives(RAS_MeshSlot& ms)
60
static const GLsizei stride = sizeof(RAS_TexVert);
61
bool wireframe = m_drawingmode <= RAS_IRasterizer::KX_WIREFRAME;
62
RAS_MeshSlot::iterator it;
66
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
67
glEnableClientState(GL_VERTEX_ARRAY);
68
glEnableClientState(GL_NORMAL_ARRAY);
70
// use glDrawElements to draw each vertexarray
71
for (ms.begin(it); !ms.end(it); ms.next(it)) {
76
if (it.array->m_type == RAS_DisplayArray::TRIANGLE)
77
drawmode = GL_TRIANGLES;
78
else if (it.array->m_type == RAS_DisplayArray::QUAD)
84
if (drawmode != GL_LINES && !wireframe) {
85
if (ms.m_bObjectColor) {
86
const MT_Vector4& rgba = ms.m_RGBAcolor;
88
glDisableClientState(GL_COLOR_ARRAY);
89
glColor4d(rgba[0], rgba[1], rgba[2], rgba[3]);
92
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
93
glEnableClientState(GL_COLOR_ARRAY);
97
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
99
glVertexPointer(3, GL_FLOAT, stride, it.vertex->getXYZ());
100
glNormalPointer(GL_FLOAT, stride, it.vertex->getNormal());
102
glTexCoordPointer(2, GL_FLOAT, stride, it.vertex->getUV(0));
103
if (glIsEnabled(GL_COLOR_ARRAY))
104
glColorPointer(4, GL_UNSIGNED_BYTE, stride, it.vertex->getRGBA());
107
// here the actual drawing takes places
108
glDrawElements(drawmode, it.totindex, GL_UNSIGNED_SHORT, it.index);
111
glDisableClientState(GL_VERTEX_ARRAY);
112
glDisableClientState(GL_NORMAL_ARRAY);
114
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
115
glDisableClientState(GL_COLOR_ARRAY);
119
void RAS_StorageVA::IndexPrimitivesMulti(class RAS_MeshSlot& ms)
121
static const GLsizei stride = sizeof(RAS_TexVert);
122
bool wireframe = m_drawingmode <= RAS_IRasterizer::KX_WIREFRAME, use_color_array;
123
RAS_MeshSlot::iterator it;
127
EnableTextures(true);
128
glEnableClientState(GL_VERTEX_ARRAY);
129
glEnableClientState(GL_NORMAL_ARRAY);
131
// use glDrawElements to draw each vertexarray
132
for (ms.begin(it); !ms.end(it); ms.next(it)) {
133
if (it.totindex == 0)
137
if (it.array->m_type == RAS_DisplayArray::TRIANGLE)
138
drawmode = GL_TRIANGLES;
139
else if (it.array->m_type == RAS_DisplayArray::QUAD)
145
if (drawmode != GL_LINES && !wireframe) {
146
if (ms.m_bObjectColor) {
147
const MT_Vector4& rgba = ms.m_RGBAcolor;
149
glDisableClientState(GL_COLOR_ARRAY);
150
glColor4d(rgba[0], rgba[1], rgba[2], rgba[3]);
151
use_color_array = false;
154
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
155
glEnableClientState(GL_COLOR_ARRAY);
156
use_color_array = true;
160
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
162
glVertexPointer(3, GL_FLOAT, stride, it.vertex->getXYZ());
163
glNormalPointer(GL_FLOAT, stride, it.vertex->getNormal());
166
TexCoordPtr(it.vertex);
168
glColorPointer(4, GL_UNSIGNED_BYTE, stride, it.vertex->getRGBA());
171
// here the actual drawing takes places
172
glDrawElements(drawmode, it.totindex, GL_UNSIGNED_SHORT, it.index);
175
glDisableClientState(GL_VERTEX_ARRAY);
176
glDisableClientState(GL_NORMAL_ARRAY);
178
glDisableClientState(GL_COLOR_ARRAY);
179
EnableTextures(false);
183
void RAS_StorageVA::TexCoordPtr(const RAS_TexVert *tv)
185
/* note: this function must closely match EnableTextures to enable/disable
186
* the right arrays, otherwise coordinate and attribute pointers from other
187
* materials can still be used and cause crashes */
190
if (GLEW_ARB_multitexture)
192
for (unit = 0; unit < *m_texco_num; unit++)
194
glClientActiveTextureARB(GL_TEXTURE0_ARB+unit);
195
switch (m_texco[unit]) {
196
case RAS_IRasterizer::RAS_TEXCO_ORCO:
197
case RAS_IRasterizer::RAS_TEXCO_GLOB:
198
glTexCoordPointer(3, GL_FLOAT, sizeof(RAS_TexVert),tv->getXYZ());
200
case RAS_IRasterizer::RAS_TEXCO_UV:
201
glTexCoordPointer(2, GL_FLOAT, sizeof(RAS_TexVert),tv->getUV(unit));
203
case RAS_IRasterizer::RAS_TEXCO_NORM:
204
glTexCoordPointer(3, GL_FLOAT, sizeof(RAS_TexVert),tv->getNormal());
206
case RAS_IRasterizer::RAS_TEXTANGENT:
207
glTexCoordPointer(4, GL_FLOAT, sizeof(RAS_TexVert),tv->getTangent());
214
glClientActiveTextureARB(GL_TEXTURE0_ARB);
217
if (GLEW_ARB_vertex_program) {
218
for (unit = 0; unit < *m_attrib_num; unit++) {
219
switch (m_attrib[unit]) {
220
case RAS_IRasterizer::RAS_TEXCO_ORCO:
221
case RAS_IRasterizer::RAS_TEXCO_GLOB:
222
glVertexAttribPointerARB(unit, 3, GL_FLOAT, GL_FALSE, sizeof(RAS_TexVert), tv->getXYZ());
224
case RAS_IRasterizer::RAS_TEXCO_UV:
225
glVertexAttribPointerARB(unit, 2, GL_FLOAT, GL_FALSE, sizeof(RAS_TexVert), tv->getUV(m_attrib_layer[unit]));
227
case RAS_IRasterizer::RAS_TEXCO_NORM:
228
glVertexAttribPointerARB(unit, 3, GL_FLOAT, GL_FALSE, sizeof(RAS_TexVert), tv->getNormal());
230
case RAS_IRasterizer::RAS_TEXTANGENT:
231
glVertexAttribPointerARB(unit, 4, GL_FLOAT, GL_FALSE, sizeof(RAS_TexVert), tv->getTangent());
233
case RAS_IRasterizer::RAS_TEXCO_VCOL:
234
glVertexAttribPointerARB(unit, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(RAS_TexVert), tv->getRGBA());
243
void RAS_StorageVA::EnableTextures(bool enable)
245
RAS_IRasterizer::TexCoGen *texco, *attrib;
246
int unit, texco_num, attrib_num;
248
/* we cache last texcoords and attribs to ensure we disable the ones that
249
* were actually last set */
252
texco_num = *m_texco_num;
254
attrib_num = *m_attrib_num;
256
memcpy(m_last_texco, m_texco, sizeof(RAS_IRasterizer::TexCoGen)*(*m_texco_num));
257
m_last_texco_num = *m_texco_num;
258
memcpy(m_last_attrib, m_attrib, sizeof(RAS_IRasterizer::TexCoGen)*(*m_attrib_num));
259
m_last_attrib_num = *m_attrib_num;
262
texco = m_last_texco;
263
texco_num = m_last_texco_num;
264
attrib = m_last_attrib;
265
attrib_num = m_last_attrib_num;
268
if (GLEW_ARB_multitexture) {
269
for (unit = 0; unit < texco_num; unit++) {
270
glClientActiveTextureARB(GL_TEXTURE0_ARB + unit);
272
switch (texco[unit]) {
273
case RAS_IRasterizer::RAS_TEXCO_ORCO:
274
case RAS_IRasterizer::RAS_TEXCO_GLOB:
275
case RAS_IRasterizer::RAS_TEXCO_UV:
276
case RAS_IRasterizer::RAS_TEXCO_NORM:
277
case RAS_IRasterizer::RAS_TEXTANGENT:
278
if (enable) glEnableClientState(GL_TEXTURE_COORD_ARRAY);
279
else glDisableClientState(GL_TEXTURE_COORD_ARRAY);
282
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
287
glClientActiveTextureARB(GL_TEXTURE0_ARB);
291
if (enable) glEnableClientState(GL_TEXTURE_COORD_ARRAY);
292
else glDisableClientState(GL_TEXTURE_COORD_ARRAY);
296
if (GLEW_ARB_vertex_program) {
297
for (unit = 0; unit < attrib_num; unit++) {
298
switch (attrib[unit]) {
299
case RAS_IRasterizer::RAS_TEXCO_ORCO:
300
case RAS_IRasterizer::RAS_TEXCO_GLOB:
301
case RAS_IRasterizer::RAS_TEXCO_UV:
302
case RAS_IRasterizer::RAS_TEXCO_NORM:
303
case RAS_IRasterizer::RAS_TEXTANGENT:
304
case RAS_IRasterizer::RAS_TEXCO_VCOL:
305
if (enable) glEnableVertexAttribArrayARB(unit);
306
else glDisableVertexAttribArrayARB(unit);
309
glDisableVertexAttribArrayARB(unit);
316
m_last_texco_num = 0;
317
m_last_attrib_num = 0;