1). Fix for cube maps in the player.
[blender.git] / source / gameengine / Rasterizer / RAS_OpenGLRasterizer / RAS_VAOpenGLRasterizer.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include "RAS_VAOpenGLRasterizer.h"
36
37 #ifdef WIN32
38 #include <windows.h>
39 #endif // WIN32
40 #ifdef __APPLE__
41 #define GL_GLEXT_LEGACY 1
42 #include <OpenGL/gl.h>
43 #else
44 #include <GL/gl.h>
45 #endif
46
47 #include "STR_String.h"
48 #include "RAS_TexVert.h"
49 #include "MT_CmMatrix4x4.h"
50 #include "RAS_IRenderTools.h" // rendering text
51
52 #include "RAS_GLExtensionManager.h"
53         
54
55 using namespace bgl;
56
57 RAS_VAOpenGLRasterizer::RAS_VAOpenGLRasterizer(RAS_ICanvas* canvas, bool lock)
58 :       RAS_OpenGLRasterizer(canvas),
59         m_Lock(lock && RAS_EXT_support._EXT_compiled_vertex_array)      
60 {
61 }
62
63
64
65 RAS_VAOpenGLRasterizer::~RAS_VAOpenGLRasterizer()
66 {
67 }
68
69 bool RAS_VAOpenGLRasterizer::Init(void)
70 {
71         
72         bool result = RAS_OpenGLRasterizer::Init();
73         
74         if (result)
75         {
76                 glEnableClientState(GL_VERTEX_ARRAY);
77                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
78                 glEnableClientState(GL_NORMAL_ARRAY);
79                 glDisableClientState(GL_COLOR_ARRAY);
80
81                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
82         }
83
84         return result;
85 }
86
87
88
89 void RAS_VAOpenGLRasterizer::SetDrawingMode(int drawingmode)
90 {
91         m_drawingmode = drawingmode;
92
93                 switch (m_drawingmode)
94         {
95         case KX_BOUNDINGBOX:
96                 {
97                 }
98         case KX_WIREFRAME:
99                 {
100                         glDisable (GL_CULL_FACE);
101                         break;
102                 }
103         case KX_TEXTURED:
104                 {
105                 }
106         case KX_SHADED:
107                 {
108                         glEnableClientState(GL_COLOR_ARRAY);
109                 }
110         case KX_SOLID:
111                 {
112                         break;
113                 }
114         default:
115                 {
116                 }
117         }
118 }
119
120
121
122 void RAS_VAOpenGLRasterizer::Exit()
123 {
124         glDisableClientState(GL_VERTEX_ARRAY);
125         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
126         glDisableClientState(GL_COLOR_ARRAY);
127         glDisableClientState(GL_NORMAL_ARRAY);
128
129         RAS_OpenGLRasterizer::Exit();
130 }
131
132
133
134 void RAS_VAOpenGLRasterizer::IndexPrimitives( const vecVertexArray& vertexarrays,
135                                                         const vecIndexArrays & indexarrays,
136                                                         int mode,
137                                                         class RAS_IPolyMaterial* polymat,
138                                                         class RAS_IRenderTools* rendertools,
139                                                         bool useObjectColor,
140                                                         const MT_Vector4& rgbacolor,
141                                                         class KX_ListSlot** slot)
142 {
143         static const GLsizei vtxstride = sizeof(RAS_TexVert);
144         GLenum drawmode;
145         switch (mode)
146         {
147         case 0:
148                 {
149                 drawmode = GL_TRIANGLES;
150                 break;
151                 }
152         case 2:
153                 {
154                 drawmode = GL_QUADS;
155                 break;
156                 }
157         case 1: //lines
158                 {
159                 }
160         default:
161                 {
162                 drawmode = GL_LINES;
163                 break;
164                 }
165         }
166         const RAS_TexVert* vertexarray;
167         unsigned int numindices, vt;
168         if (drawmode != GL_LINES)
169         {
170                 if (useObjectColor)
171                 {
172                         glDisableClientState(GL_COLOR_ARRAY);
173                         glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
174                 } else
175                 {
176                         glColor4d(0,0,0,1.0);
177                         glEnableClientState(GL_COLOR_ARRAY);
178                 }
179         }
180         else
181         {
182                 glColor3d(0,0,0);
183         }
184
185         // use glDrawElements to draw each vertexarray
186         for (vt=0;vt<vertexarrays.size();vt++)
187         {
188                 vertexarray = &((*vertexarrays[vt]) [0]);
189                 const KX_IndexArray & indexarray = (*indexarrays[vt]);
190                 numindices = indexarray.size();
191
192                 if (!numindices)
193                         continue;
194                 
195                 glVertexPointer(3,GL_FLOAT,vtxstride,vertexarray->getLocalXYZ());
196                 glTexCoordPointer(2,GL_FLOAT,vtxstride,vertexarray->getUV1());
197                 glColorPointer(4,GL_UNSIGNED_BYTE,vtxstride,vertexarray->getRGBA());
198                 glNormalPointer(GL_FLOAT,vtxstride,vertexarray->getNormal());
199
200                 //if(m_Lock)
201                 //      local->Begin(vertexarrays[vt]->size());
202
203                 // here the actual drawing takes places
204                 glDrawElements(drawmode,numindices,GL_UNSIGNED_SHORT,&(indexarray[0]));
205
206                 //if(m_Lock)
207                 //      local->End();
208
209
210         }
211 }
212
213
214 void RAS_VAOpenGLRasterizer::IndexPrimitivesMulti( const vecVertexArray& vertexarrays,
215                                                         const vecIndexArrays & indexarrays,
216                                                         int mode,
217                                                         class RAS_IPolyMaterial* polymat,
218                                                         class RAS_IRenderTools* rendertools,
219                                                         bool useObjectColor,
220                                                         const MT_Vector4& rgbacolor,
221                                                         class KX_ListSlot** slot)
222 {
223         static const GLsizei vtxstride = sizeof(RAS_TexVert);
224         GLenum drawmode;
225         switch (mode)
226         {
227         case 0:
228                 {
229                 drawmode = GL_TRIANGLES;
230                 break;
231                 }
232         case 2:
233                 {
234                 drawmode = GL_QUADS;
235                 break;
236                 }
237         case 1: //lines
238                 {
239                 }
240         default:
241                 {
242                 drawmode = GL_LINES;
243                 break;
244                 }
245         }
246         const RAS_TexVert* vertexarray;
247         unsigned int numindices, vt;
248         const unsigned int enabled = polymat->GetEnabled();
249
250         if (drawmode != GL_LINES)
251         {
252                 if (useObjectColor)
253                 {
254                         glDisableClientState(GL_COLOR_ARRAY);
255                         glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
256                 } else
257                 {
258                         glColor4d(0,0,0,1.0);
259                         glEnableClientState(GL_COLOR_ARRAY);
260                 }
261         }
262         else
263         {
264                 glColor3d(0,0,0);
265         }
266
267         // use glDrawElements to draw each vertexarray
268         for (vt=0;vt<vertexarrays.size();vt++)
269         {
270                 vertexarray = &((*vertexarrays[vt]) [0]);
271                 const KX_IndexArray & indexarray = (*indexarrays[vt]);
272                 numindices = indexarray.size();
273
274                 if (!numindices)
275                         continue;
276                 
277                 glVertexPointer(3,GL_FLOAT,vtxstride,vertexarray->getLocalXYZ());
278                 TexCoordPtr(vertexarray, enabled);
279
280                 //glTexCoordPointer(2,GL_FLOAT,vtxstride,vertexarray->getUV1());
281                 glColorPointer(4,GL_UNSIGNED_BYTE,vtxstride,vertexarray->getRGBA());
282                 glNormalPointer(GL_FLOAT,vtxstride,vertexarray->getNormal());
283
284                 //if(m_Lock)
285                 //      local->Begin(vertexarrays[vt]->size());
286
287                 // here the actual drawing takes places
288                 glDrawElements(drawmode,numindices,GL_UNSIGNED_SHORT,&(indexarray[0]));
289                 
290                 //if(m_Lock)
291                 //      local->End();
292         }
293 }
294
295 void RAS_VAOpenGLRasterizer::TexCoordPtr(const RAS_TexVert *tv, int enabled)
296 {
297 #ifdef GL_ARB_multitexture
298         if(bgl::RAS_EXT_support._ARB_multitexture)
299         {
300                 for(int unit=0; unit<enabled; unit++)
301                 {
302                         bgl::blClientActiveTextureARB(GL_TEXTURE0_ARB+unit);
303
304                         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
305                         if( tv->getFlag() & TV_2NDUV && tv->getUnit() == unit ) {
306                                 glTexCoordPointer(2, GL_FLOAT, sizeof(RAS_TexVert), tv->getUV2());
307                                 continue;
308                         }
309                         switch(m_texco[unit])
310                         {
311                         case RAS_TEXCO_DISABLE:
312                         case RAS_TEXCO_OBJECT:
313                         case RAS_TEXCO_GEN:
314                                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
315                                 break;
316                         case RAS_TEXCO_ORCO:
317                         case RAS_TEXCO_GLOB:
318                                 glTexCoordPointer(3, GL_FLOAT, sizeof(RAS_TexVert),tv->getLocalXYZ());
319                                 break;
320                         case RAS_TEXCO_UV1:
321                                 glTexCoordPointer(2, GL_FLOAT, sizeof(RAS_TexVert),tv->getUV1());
322                                 break;
323                         case RAS_TEXCO_NORM:
324                                 glTexCoordPointer(3, GL_FLOAT, sizeof(RAS_TexVert),tv->getNormal());
325                                 break;
326                         case RAS_TEXTANGENT:
327                                 glTexCoordPointer(4, GL_FLOAT, sizeof(RAS_TexVert),tv->getTangent());
328                                 break;
329                         case RAS_TEXCO_UV2:
330                                 glTexCoordPointer(2, GL_FLOAT, sizeof(RAS_TexVert),tv->getUV2());
331                                 break;
332                         }
333                 }
334         }
335
336 #ifdef GL_ARB_vertex_program
337         if(m_useTang && bgl::RAS_EXT_support._ARB_vertex_program)
338                 bgl::blVertexAttrib4fvARB(1/*tangent*/, tv->getTangent());
339 #endif
340
341 #endif
342 }
343
344
345 void RAS_VAOpenGLRasterizer::EnableTextures(bool enable)
346 {
347         if (enable)
348                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
349         else
350                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
351 }
352