Switch to using floats instead of shorts for normal data - they're supposed to be...
[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 #include <OpenGL/gl.h>
42 #else
43 #include <GL/gl.h>
44 #endif
45
46 #include "STR_String.h"
47 #include "RAS_TexVert.h"
48 #include "MT_CmMatrix4x4.h"
49 #include "RAS_IRenderTools.h" // rendering text
50
51 #include "RAS_GLExtensionManager.h"
52         
53
54 using namespace bgl;
55
56 RAS_VAOpenGLRasterizer::RAS_VAOpenGLRasterizer(RAS_ICanvas* canvas)
57 :RAS_OpenGLRasterizer(canvas)
58 {
59 }
60
61
62
63 RAS_VAOpenGLRasterizer::~RAS_VAOpenGLRasterizer()
64 {
65 }
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 {
142         unsigned char* mypointer=NULL;
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         // use glDrawElements to draw each vertexarray
185         for (vt=0;vt<vertexarrays.size();vt++)
186         {
187                 vertexarray = &((*vertexarrays[vt]) [0]);
188                 const KX_IndexArray & indexarray = (*indexarrays[vt]);
189                 numindices = indexarray.size();
190                 int numverts = vertexarrays[vt]->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                 //glLockArraysEXT(0,numverts);
200                 // here the actual drawing takes places
201                 glDrawElements(drawmode,numindices,GL_UNSIGNED_SHORT,&(indexarray[0]));
202                 //glUnlockArraysEXT();
203         }
204 }
205
206
207 void RAS_VAOpenGLRasterizer::EnableTextures(bool enable)
208 {
209         if (enable)
210                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
211         else
212                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
213 }
214
215 bool RAS_VAOpenGLRasterizer::Stereo()
216 {
217 /*
218         if(m_stereomode == RAS_STEREO_NOSTEREO)
219                 return false;
220         else
221                 return true;
222 */
223         return false;
224 }
225