~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_StorageVA.cpp

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ***** BEGIN GPL LICENSE BLOCK *****
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 *
 
18
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 
19
 * All rights reserved.
 
20
 *
 
21
 * The Original Code is: all of this file.
 
22
 *
 
23
 * Contributor(s): none yet.
 
24
 *
 
25
 * ***** END GPL LICENSE BLOCK *****
 
26
 */
 
27
 
 
28
#include "RAS_StorageVA.h"
 
29
 
 
30
#include "GL/glew.h"
 
31
 
 
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),
 
35
        m_last_texco_num(0),
 
36
        m_last_attrib_num(0),
 
37
        m_texco(texco),
 
38
        m_attrib(attrib),
 
39
        m_attrib_layer(attrib_layer)
 
40
{
 
41
}
 
42
 
 
43
RAS_StorageVA::~RAS_StorageVA()
 
44
{
 
45
}
 
46
 
 
47
bool RAS_StorageVA::Init()
 
48
{
 
49
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
50
 
 
51
        return true;
 
52
}
 
53
 
 
54
void RAS_StorageVA::Exit()
 
55
{
 
56
}
 
57
 
 
58
void RAS_StorageVA::IndexPrimitives(RAS_MeshSlot& ms)
 
59
{
 
60
        static const GLsizei stride = sizeof(RAS_TexVert);
 
61
        bool wireframe = m_drawingmode <= RAS_IRasterizer::KX_WIREFRAME;
 
62
        RAS_MeshSlot::iterator it;
 
63
        GLenum drawmode;
 
64
 
 
65
        if (!wireframe)
 
66
                glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
67
        glEnableClientState(GL_VERTEX_ARRAY);
 
68
        glEnableClientState(GL_NORMAL_ARRAY);
 
69
 
 
70
        // use glDrawElements to draw each vertexarray
 
71
        for (ms.begin(it); !ms.end(it); ms.next(it)) {
 
72
                if (it.totindex == 0)
 
73
                        continue;
 
74
 
 
75
                // drawing mode
 
76
                if (it.array->m_type == RAS_DisplayArray::TRIANGLE)
 
77
                        drawmode = GL_TRIANGLES;
 
78
                else if (it.array->m_type == RAS_DisplayArray::QUAD)
 
79
                        drawmode = GL_QUADS;
 
80
                else
 
81
                        drawmode = GL_LINES;
 
82
 
 
83
                // colors
 
84
                if (drawmode != GL_LINES && !wireframe) {
 
85
                        if (ms.m_bObjectColor) {
 
86
                                const MT_Vector4& rgba = ms.m_RGBAcolor;
 
87
 
 
88
                                glDisableClientState(GL_COLOR_ARRAY);
 
89
                                glColor4d(rgba[0], rgba[1], rgba[2], rgba[3]);
 
90
                        }
 
91
                        else {
 
92
                                glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
 
93
                                glEnableClientState(GL_COLOR_ARRAY);
 
94
                        }
 
95
                }
 
96
                else
 
97
                        glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
 
98
 
 
99
                glVertexPointer(3, GL_FLOAT, stride, it.vertex->getXYZ());
 
100
                glNormalPointer(GL_FLOAT, stride, it.vertex->getNormal());
 
101
                if (!wireframe) {
 
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());
 
105
                }
 
106
 
 
107
                // here the actual drawing takes places
 
108
                glDrawElements(drawmode, it.totindex, GL_UNSIGNED_SHORT, it.index);
 
109
        }
 
110
        
 
111
        glDisableClientState(GL_VERTEX_ARRAY);
 
112
        glDisableClientState(GL_NORMAL_ARRAY);
 
113
        if (!wireframe) {
 
114
                glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 
115
                glDisableClientState(GL_COLOR_ARRAY);
 
116
        }
 
117
}
 
118
 
 
119
void RAS_StorageVA::IndexPrimitivesMulti(class RAS_MeshSlot& ms)
 
120
{
 
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;
 
124
        GLenum drawmode;
 
125
 
 
126
        if (!wireframe)
 
127
                EnableTextures(true);
 
128
        glEnableClientState(GL_VERTEX_ARRAY);
 
129
        glEnableClientState(GL_NORMAL_ARRAY);
 
130
 
 
131
        // use glDrawElements to draw each vertexarray
 
132
        for (ms.begin(it); !ms.end(it); ms.next(it)) {
 
133
                if (it.totindex == 0)
 
134
                        continue;
 
135
 
 
136
                // drawing mode
 
137
                if (it.array->m_type == RAS_DisplayArray::TRIANGLE)
 
138
                        drawmode = GL_TRIANGLES;
 
139
                else if (it.array->m_type == RAS_DisplayArray::QUAD)
 
140
                        drawmode = GL_QUADS;
 
141
                else
 
142
                        drawmode = GL_LINES;
 
143
 
 
144
                // colors
 
145
                if (drawmode != GL_LINES && !wireframe) {
 
146
                        if (ms.m_bObjectColor) {
 
147
                                const MT_Vector4& rgba = ms.m_RGBAcolor;
 
148
 
 
149
                                glDisableClientState(GL_COLOR_ARRAY);
 
150
                                glColor4d(rgba[0], rgba[1], rgba[2], rgba[3]);
 
151
                                use_color_array = false;
 
152
                        }
 
153
                        else {
 
154
                                glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
 
155
                                glEnableClientState(GL_COLOR_ARRAY);
 
156
                                use_color_array = true;
 
157
                        }
 
158
                }
 
159
                else
 
160
                        glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
 
161
 
 
162
                glVertexPointer(3, GL_FLOAT, stride, it.vertex->getXYZ());
 
163
                glNormalPointer(GL_FLOAT, stride, it.vertex->getNormal());
 
164
 
 
165
                if (!wireframe) {
 
166
                        TexCoordPtr(it.vertex);
 
167
                        if (use_color_array)
 
168
                                glColorPointer(4, GL_UNSIGNED_BYTE, stride, it.vertex->getRGBA());
 
169
                }
 
170
 
 
171
                // here the actual drawing takes places
 
172
                glDrawElements(drawmode, it.totindex, GL_UNSIGNED_SHORT, it.index);
 
173
        }
 
174
        
 
175
        glDisableClientState(GL_VERTEX_ARRAY);
 
176
        glDisableClientState(GL_NORMAL_ARRAY);
 
177
        if (!wireframe) {
 
178
                glDisableClientState(GL_COLOR_ARRAY);
 
179
                EnableTextures(false);
 
180
        }
 
181
}
 
182
 
 
183
void RAS_StorageVA::TexCoordPtr(const RAS_TexVert *tv)
 
184
{
 
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 */
 
188
        int unit;
 
189
 
 
190
        if (GLEW_ARB_multitexture)
 
191
        {
 
192
                for (unit = 0; unit < *m_texco_num; unit++)
 
193
                {
 
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());
 
199
                                        break;
 
200
                                case RAS_IRasterizer::RAS_TEXCO_UV:
 
201
                                        glTexCoordPointer(2, GL_FLOAT, sizeof(RAS_TexVert),tv->getUV(unit));
 
202
                                        break;
 
203
                                case RAS_IRasterizer::RAS_TEXCO_NORM:
 
204
                                        glTexCoordPointer(3, GL_FLOAT, sizeof(RAS_TexVert),tv->getNormal());
 
205
                                        break;
 
206
                                case RAS_IRasterizer::RAS_TEXTANGENT:
 
207
                                        glTexCoordPointer(4, GL_FLOAT, sizeof(RAS_TexVert),tv->getTangent());
 
208
                                        break;
 
209
                                default:
 
210
                                        break;
 
211
                        }
 
212
                }
 
213
 
 
214
                glClientActiveTextureARB(GL_TEXTURE0_ARB);
 
215
        }
 
216
 
 
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());
 
223
                                        break;
 
224
                                case RAS_IRasterizer::RAS_TEXCO_UV:
 
225
                                        glVertexAttribPointerARB(unit, 2, GL_FLOAT, GL_FALSE, sizeof(RAS_TexVert), tv->getUV(m_attrib_layer[unit]));
 
226
                                        break;
 
227
                                case RAS_IRasterizer::RAS_TEXCO_NORM:
 
228
                                        glVertexAttribPointerARB(unit, 3, GL_FLOAT, GL_FALSE, sizeof(RAS_TexVert), tv->getNormal());
 
229
                                        break;
 
230
                                case RAS_IRasterizer::RAS_TEXTANGENT:
 
231
                                        glVertexAttribPointerARB(unit, 4, GL_FLOAT, GL_FALSE, sizeof(RAS_TexVert), tv->getTangent());
 
232
                                        break;
 
233
                                case RAS_IRasterizer::RAS_TEXCO_VCOL:
 
234
                                        glVertexAttribPointerARB(unit, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(RAS_TexVert), tv->getRGBA());
 
235
                                        break;
 
236
                                default:
 
237
                                        break;
 
238
                        }
 
239
                }
 
240
        }
 
241
}
 
242
 
 
243
void RAS_StorageVA::EnableTextures(bool enable)
 
244
{
 
245
        RAS_IRasterizer::TexCoGen *texco, *attrib;
 
246
        int unit, texco_num, attrib_num;
 
247
 
 
248
        /* we cache last texcoords and attribs to ensure we disable the ones that
 
249
         * were actually last set */
 
250
        if (enable) {
 
251
                texco = m_texco;
 
252
                texco_num = *m_texco_num;
 
253
                attrib = m_attrib;
 
254
                attrib_num = *m_attrib_num;
 
255
                
 
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;
 
260
        }
 
261
        else {
 
262
                texco = m_last_texco;
 
263
                texco_num = m_last_texco_num;
 
264
                attrib = m_last_attrib;
 
265
                attrib_num = m_last_attrib_num;
 
266
        }
 
267
 
 
268
        if (GLEW_ARB_multitexture) {
 
269
                for (unit = 0; unit < texco_num; unit++) {
 
270
                        glClientActiveTextureARB(GL_TEXTURE0_ARB + unit);
 
271
 
 
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);
 
280
                                        break;
 
281
                                default:
 
282
                                        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 
283
                                        break;
 
284
                        }
 
285
                }
 
286
 
 
287
                glClientActiveTextureARB(GL_TEXTURE0_ARB);
 
288
        }
 
289
        else {
 
290
                if (texco_num) {
 
291
                        if (enable) glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
292
                        else glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 
293
                }
 
294
        }
 
295
 
 
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);
 
307
                                        break;
 
308
                                default:
 
309
                                        glDisableVertexAttribArrayARB(unit);
 
310
                                        break;
 
311
                        }
 
312
                }
 
313
        }
 
314
 
 
315
        if (!enable) {
 
316
                m_last_texco_num = 0;
 
317
                m_last_attrib_num = 0;
 
318
        }
 
319
}
 
320