svn merge -r 13452:14721 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / gameengine / Rasterizer / RAS_OpenGLRasterizer / RAS_OpenGLRasterizer.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL 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.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28  
29 #include <math.h>
30 #include <stdlib.h>
31  
32 #include "RAS_OpenGLRasterizer.h"
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #ifdef WIN32
39 #include <windows.h>
40 #endif // WIN32
41 #ifdef __APPLE__
42 #define GL_GLEXT_LEGACY 1
43 #include <OpenGL/gl.h>
44 #include <OpenGL/glu.h>
45 #else
46 #include <GL/gl.h>
47 #if defined(__sun__) && !defined(__sparc__)
48 #include <mesa/glu.h>
49 #else
50 #include <GL/glu.h>
51 #endif
52 #endif
53
54 #include "RAS_Rect.h"
55 #include "RAS_TexVert.h"
56 #include "MT_CmMatrix4x4.h"
57 #include "RAS_IRenderTools.h" // rendering text
58
59 #include "RAS_GLExtensionManager.h"
60
61 /**
62  *  32x32 bit masks for vinterlace stereo mode
63  */
64 static GLuint left_eye_vinterlace_mask[32];
65 static GLuint right_eye_vinterlace_mask[32];
66
67 /**
68  *  32x32 bit masks for hinterlace stereo mode.
69  *  Left eye = &hinterlace_mask[0]
70  *  Right eye = &hinterlace_mask[1]
71  */
72 static GLuint hinterlace_mask[33];
73
74 RAS_OpenGLRasterizer::RAS_OpenGLRasterizer(RAS_ICanvas* canvas)
75         :RAS_IRasterizer(canvas),
76         m_2DCanvas(canvas),
77         m_fogenabled(false),
78         m_time(0.0),
79         m_stereomode(RAS_STEREO_NOSTEREO),
80         m_curreye(RAS_STEREO_LEFTEYE),
81         m_eyeseparation(0.0),
82         m_seteyesep(false),
83         m_focallength(0.0),
84         m_setfocallength(false),
85         m_noOfScanlines(32),
86         m_useTang(false),
87         m_materialCachingInfo(0),
88         m_motionblur(0),
89         m_motionblurvalue(-1.0)
90 {
91         m_viewmatrix.Identity();
92         
93         for (int i = 0; i < 32; i++)
94         {
95                 left_eye_vinterlace_mask[i] = 0x55555555;
96                 right_eye_vinterlace_mask[i] = 0xAAAAAAAA;
97                 hinterlace_mask[i] = (i&1)*0xFFFFFFFF;
98         }
99         hinterlace_mask[32] = 0;
100 }
101
102
103
104 RAS_OpenGLRasterizer::~RAS_OpenGLRasterizer()
105 {
106 }
107
108
109
110 static void Myinit_gl_stuff(void)       
111 {
112         float mat_specular[] = { 0.5, 0.5, 0.5, 1.0 };
113         float mat_shininess[] = { 35.0 };
114 /*      float one= 1.0; */
115         int a, x, y;
116         GLubyte pat[32*32];
117         const GLubyte *patc= pat;
118                 
119         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_specular);
120         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
121         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
122
123
124 #if defined(__FreeBSD) || defined(__linux__)
125         glDisable(GL_DITHER);   /* op sgi/sun hardware && 12 bits */
126 #endif
127         
128         /* no local viewer, looks ugly in ortho mode */
129         /* glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, &one); */
130         
131         glDepthFunc(GL_LEQUAL);
132         /* scaling matrices */
133         glEnable(GL_NORMALIZE);
134
135         glShadeModel(GL_FLAT);
136
137         glDisable(GL_ALPHA_TEST);
138         glDisable(GL_BLEND);
139         glDisable(GL_DEPTH_TEST);
140         glDisable(GL_FOG);
141         glDisable(GL_LIGHTING);
142         glDisable(GL_LOGIC_OP);
143         glDisable(GL_STENCIL_TEST);
144         glDisable(GL_TEXTURE_1D);
145         glDisable(GL_TEXTURE_2D);
146
147         glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
148         glPixelTransferi(GL_RED_SCALE, 1);
149         glPixelTransferi(GL_RED_BIAS, 0);
150         glPixelTransferi(GL_GREEN_SCALE, 1);
151         glPixelTransferi(GL_GREEN_BIAS, 0);
152         glPixelTransferi(GL_BLUE_SCALE, 1);
153         glPixelTransferi(GL_BLUE_BIAS, 0);
154         glPixelTransferi(GL_ALPHA_SCALE, 1);
155         glPixelTransferi(GL_ALPHA_BIAS, 0);
156
157         a = 0;
158         for(x=0; x<32; x++)
159         {
160                 for(y=0; y<4; y++)
161                 {
162                         if( (x) & 1) pat[a++]= 0x88;
163                         else pat[a++]= 0x22;
164                 }
165         }
166         
167         glPolygonStipple(patc);
168         
169         glFrontFace(GL_CCW);
170         glCullFace(GL_BACK);
171         glEnable(GL_CULL_FACE);
172 }
173
174
175
176 bool RAS_OpenGLRasterizer::Init()
177 {
178
179         Myinit_gl_stuff();
180
181         m_redback = 0.4375;
182         m_greenback = 0.4375;
183         m_blueback = 0.4375;
184         m_alphaback = 0.0;
185         
186         m_ambr = 0.0f;
187         m_ambg = 0.0f;
188         m_ambb = 0.0f;
189
190         glClearColor(m_redback,m_greenback,m_blueback,m_alphaback);
191         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
192         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
193
194         glShadeModel(GL_SMOOTH);
195
196         return true;
197 }
198
199
200 void RAS_OpenGLRasterizer::SetAmbientColor(float red, float green, float blue)
201 {
202         m_ambr = red;
203         m_ambg = green;
204         m_ambb = blue;
205 }
206
207
208 void RAS_OpenGLRasterizer::SetAlphaTest(bool enable)
209 {
210         if (enable)
211         {
212                 glEnable(GL_ALPHA_TEST);
213                 glAlphaFunc(GL_GREATER, 0.6f);
214         }
215         else glDisable(GL_ALPHA_TEST);
216 }
217
218
219
220 void RAS_OpenGLRasterizer::SetAmbient(float factor)
221 {
222         float ambient[] = { m_ambr*factor, m_ambg*factor, m_ambb*factor, 1.0f };
223         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
224 }
225
226
227 void RAS_OpenGLRasterizer::SetBackColor(float red,
228                                                                                 float green,
229                                                                                 float blue,
230                                                                                 float alpha)
231 {
232         m_redback = red;
233         m_greenback = green;
234         m_blueback = blue;
235         m_alphaback = alpha;
236 }
237
238
239
240 void RAS_OpenGLRasterizer::SetFogColor(float r,
241                                                                            float g,
242                                                                            float b)
243 {
244         m_fogr = r;
245         m_fogg = g;
246         m_fogb = b;
247         m_fogenabled = true;
248 }
249
250
251
252 void RAS_OpenGLRasterizer::SetFogStart(float start)
253 {
254         m_fogstart = start;
255         m_fogenabled = true;
256 }
257
258
259
260 void RAS_OpenGLRasterizer::SetFogEnd(float fogend)
261 {
262         m_fogdist = fogend;
263         m_fogenabled = true;
264 }
265
266
267
268 void RAS_OpenGLRasterizer::SetFog(float start,
269                                                                   float dist,
270                                                                   float r,
271                                                                   float g,
272                                                                   float b)
273 {
274         m_fogstart = start;
275         m_fogdist = dist;
276         m_fogr = r;
277         m_fogg = g;
278         m_fogb = b;
279         m_fogenabled = true;
280 }
281
282
283
284 void RAS_OpenGLRasterizer::DisableFog()
285 {
286         m_fogenabled = false;
287 }
288
289
290
291 void RAS_OpenGLRasterizer::DisplayFog()
292 {
293         if ((m_drawingmode >= KX_SOLID) && m_fogenabled)
294         {
295                 float params[5];
296                 glFogi(GL_FOG_MODE, GL_LINEAR);
297                 glFogf(GL_FOG_DENSITY, 0.1f);
298                 glFogf(GL_FOG_START, m_fogstart);
299                 glFogf(GL_FOG_END, m_fogstart + m_fogdist);
300                 params[0]= m_fogr;
301                 params[1]= m_fogg;
302                 params[2]= m_fogb;
303                 params[3]= 0.0;
304                 glFogfv(GL_FOG_COLOR, params); 
305                 glEnable(GL_FOG);
306         } 
307         else
308         {
309                 glDisable(GL_FOG);
310         }
311 }
312
313
314
315 bool RAS_OpenGLRasterizer::SetMaterial(const RAS_IPolyMaterial& mat)
316 {
317         return mat.Activate(this, m_materialCachingInfo);
318 }
319
320
321
322 void RAS_OpenGLRasterizer::Exit()
323 {
324
325         glEnable(GL_CULL_FACE);
326         glEnable(GL_DEPTH_TEST);
327         glClearDepth(1.0); 
328         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
329         glClearColor(m_redback, m_greenback, m_blueback, m_alphaback);
330         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
331         glDepthMask (GL_TRUE);
332         glDepthFunc(GL_LEQUAL);
333         glBlendFunc(GL_ONE, GL_ZERO);
334         
335         glDisable(GL_POLYGON_STIPPLE);
336         
337         glDisable(GL_LIGHTING);
338         if (bgl::QueryExtension(bgl::_GL_EXT_separate_specular_color) || bgl::QueryVersion(1, 2))
339                 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
340         
341         EndFrame();
342 }
343
344 bool RAS_OpenGLRasterizer::InterlacedStereo() const
345 {
346         return m_stereomode == RAS_STEREO_VINTERLACE || m_stereomode == RAS_STEREO_INTERLACED;
347 }
348
349 bool RAS_OpenGLRasterizer::BeginFrame(int drawingmode, double time)
350 {
351         m_time = time;
352         m_drawingmode = drawingmode;
353         
354         if (!InterlacedStereo() || m_curreye == RAS_STEREO_LEFTEYE)
355         {
356                 m_2DCanvas->ClearColor(m_redback,m_greenback,m_blueback,m_alphaback);
357                 m_2DCanvas->ClearBuffer(RAS_ICanvas::COLOR_BUFFER);
358         }
359
360         // Blender camera routine destroys the settings
361         if (m_drawingmode < KX_SOLID)
362         {
363                 glDisable (GL_CULL_FACE);
364                 glDisable (GL_DEPTH_TEST);
365         }
366         else
367         {
368                 glEnable(GL_DEPTH_TEST);
369                 glEnable (GL_CULL_FACE);
370         }
371
372         glShadeModel(GL_SMOOTH);
373
374         m_2DCanvas->BeginFrame();
375         
376         return true;
377 }
378
379
380
381 void RAS_OpenGLRasterizer::SetDrawingMode(int drawingmode)
382 {
383         m_drawingmode = drawingmode;
384
385         switch (m_drawingmode)
386         {
387         case KX_BOUNDINGBOX:
388                 {
389                 }
390         case KX_WIREFRAME:
391                 {
392                         glDisable (GL_CULL_FACE);
393                         break;
394                 }
395         case KX_TEXTURED:
396                 {
397                 }
398         case KX_SHADED:
399                 {
400                 }
401         case KX_SOLID:
402                 {
403                 }
404         default:
405                 {
406                 }
407         }
408 }
409
410
411
412 int RAS_OpenGLRasterizer::GetDrawingMode()
413 {
414         return m_drawingmode;
415 }
416
417
418
419 void RAS_OpenGLRasterizer::SetDepthMask(DepthMask depthmask)
420 {
421         glDepthMask(depthmask == KX_DEPTHMASK_DISABLED ? GL_FALSE : GL_TRUE);
422 }
423
424
425
426 void RAS_OpenGLRasterizer::ClearDepthBuffer()
427 {
428         m_2DCanvas->ClearBuffer(RAS_ICanvas::DEPTH_BUFFER);
429 }
430
431
432 void RAS_OpenGLRasterizer::ClearCachingInfo(void)
433 {
434         m_materialCachingInfo = 0;
435 }
436
437
438 void RAS_OpenGLRasterizer::EndFrame()
439 {
440         glDisable(GL_LIGHTING);
441         glDisable(GL_TEXTURE_2D);
442
443         //DrawDebugLines
444         glBegin(GL_LINES);
445         for (unsigned int i=0;i<m_debugLines.size();i++)
446         {
447                 
448
449                 glColor4f(m_debugLines[i].m_color[0],m_debugLines[i].m_color[1],m_debugLines[i].m_color[2],1.f);
450                 const MT_Scalar* fromPtr = &m_debugLines[i].m_from.x();
451                 const MT_Scalar* toPtr= &m_debugLines[i].m_to.x();
452
453                 glVertex3dv(fromPtr);
454                 glVertex3dv(toPtr);
455         }
456         glEnd();
457
458         m_debugLines.clear();
459
460         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
461         m_2DCanvas->EndFrame();
462 }       
463
464 void RAS_OpenGLRasterizer::SetRenderArea()
465 {
466         // only above/below stereo method needs viewport adjustment
467         switch (m_stereomode)
468         {
469                 case RAS_STEREO_ABOVEBELOW:
470                         switch(m_curreye)
471                         {
472                                 case RAS_STEREO_LEFTEYE:
473                                         // upper half of window
474                                         m_2DCanvas->GetDisplayArea().SetLeft(0);
475                                         m_2DCanvas->GetDisplayArea().SetBottom(m_2DCanvas->GetHeight() -
476                                                 int(m_2DCanvas->GetHeight() - m_noOfScanlines) / 2);
477         
478                                         m_2DCanvas->GetDisplayArea().SetRight(int(m_2DCanvas->GetWidth()));
479                                         m_2DCanvas->GetDisplayArea().SetTop(int(m_2DCanvas->GetHeight()));
480                                         break;
481                                 case RAS_STEREO_RIGHTEYE:
482                                         // lower half of window
483                                         m_2DCanvas->GetDisplayArea().SetLeft(0);
484                                         m_2DCanvas->GetDisplayArea().SetBottom(0);
485                                         m_2DCanvas->GetDisplayArea().SetRight(int(m_2DCanvas->GetWidth()));
486                                         m_2DCanvas->GetDisplayArea().SetTop(int(m_2DCanvas->GetHeight() - m_noOfScanlines) / 2);
487                                         break;
488                         }
489                         break;
490                 case RAS_STEREO_SIDEBYSIDE:
491                         switch (m_curreye)
492                         {
493                                 case RAS_STEREO_LEFTEYE:
494                                         // Left half of window
495                                         m_2DCanvas->GetDisplayArea().SetLeft(0);
496                                         m_2DCanvas->GetDisplayArea().SetBottom(0);
497                                         m_2DCanvas->GetDisplayArea().SetRight(m_2DCanvas->GetWidth()/2);
498                                         m_2DCanvas->GetDisplayArea().SetTop(m_2DCanvas->GetHeight());
499                                         break;
500                                 case RAS_STEREO_RIGHTEYE:
501                                         // Right half of window
502                                         m_2DCanvas->GetDisplayArea().SetLeft(m_2DCanvas->GetWidth()/2);
503                                         m_2DCanvas->GetDisplayArea().SetBottom(0);
504                                         m_2DCanvas->GetDisplayArea().SetRight(m_2DCanvas->GetWidth());
505                                         m_2DCanvas->GetDisplayArea().SetTop(m_2DCanvas->GetHeight());
506                                         break;
507                         }
508                         break;
509                 default:
510                         // every available pixel
511                         m_2DCanvas->GetDisplayArea().SetLeft(0);
512                         m_2DCanvas->GetDisplayArea().SetBottom(0);
513                         m_2DCanvas->GetDisplayArea().SetRight(int(m_2DCanvas->GetWidth()));
514                         m_2DCanvas->GetDisplayArea().SetTop(int(m_2DCanvas->GetHeight()));
515                         break;
516         }
517 }
518
519         
520 void RAS_OpenGLRasterizer::SetStereoMode(const StereoMode stereomode)
521 {
522         m_stereomode = stereomode;
523 }
524
525
526
527 bool RAS_OpenGLRasterizer::Stereo()
528 {
529         if(m_stereomode == RAS_STEREO_NOSTEREO)
530                 return false;
531         else
532                 return true;
533 }
534
535
536 void RAS_OpenGLRasterizer::SetEye(const StereoEye eye)
537 {
538         m_curreye = eye;
539         switch (m_stereomode)
540         {
541                 case RAS_STEREO_QUADBUFFERED:
542                         glDrawBuffer(m_curreye == RAS_STEREO_LEFTEYE ? GL_BACK_LEFT : GL_BACK_RIGHT);
543                         break;
544                 case RAS_STEREO_ANAGLYPH:
545                         if (m_curreye == RAS_STEREO_LEFTEYE)
546                         {
547                                 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_FALSE);
548                         } else {
549                                 //glAccum(GL_LOAD, 1.0);
550                                 glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
551                                 ClearDepthBuffer();
552                         }
553                         break;
554                 case RAS_STEREO_VINTERLACE:
555                 {
556                         glEnable(GL_POLYGON_STIPPLE);
557                         glPolygonStipple((const GLubyte*) ((m_curreye == RAS_STEREO_LEFTEYE) ? left_eye_vinterlace_mask : right_eye_vinterlace_mask));
558                         if (m_curreye == RAS_STEREO_RIGHTEYE)
559                                 ClearDepthBuffer();
560                         break;
561                 }
562                 case RAS_STEREO_INTERLACED:
563                 {
564                         glEnable(GL_POLYGON_STIPPLE);
565                         glPolygonStipple((const GLubyte*) &hinterlace_mask[m_curreye == RAS_STEREO_LEFTEYE?0:1]);
566                         if (m_curreye == RAS_STEREO_RIGHTEYE)
567                                 ClearDepthBuffer();
568                         break;
569                 }
570                 default:
571                         break;
572         }
573 }
574
575 RAS_IRasterizer::StereoEye RAS_OpenGLRasterizer::GetEye()
576 {
577         return m_curreye;
578 }
579
580
581 void RAS_OpenGLRasterizer::SetEyeSeparation(const float eyeseparation)
582 {
583         m_eyeseparation = eyeseparation;
584         m_seteyesep = true;
585 }
586
587 float RAS_OpenGLRasterizer::GetEyeSeparation()
588 {
589         return m_eyeseparation;
590 }
591
592 void RAS_OpenGLRasterizer::SetFocalLength(const float focallength)
593 {
594         m_focallength = focallength;
595         m_setfocallength = true;
596 }
597
598 float RAS_OpenGLRasterizer::GetFocalLength()
599 {
600         return m_focallength;
601 }
602
603
604 void RAS_OpenGLRasterizer::SwapBuffers()
605 {
606         m_2DCanvas->SwapBuffers();
607 }
608
609
610
611 void RAS_OpenGLRasterizer::GetViewMatrix(MT_Matrix4x4 &mat) const
612 {
613         float viewmat[16];
614         glGetFloatv(GL_MODELVIEW_MATRIX, viewmat);
615         mat.setValue(viewmat);
616 }
617
618
619
620 void RAS_OpenGLRasterizer::IndexPrimitives(const vecVertexArray & vertexarrays,
621                                                                         const vecIndexArrays & indexarrays,
622                                                                         int mode,
623                                                                         class RAS_IPolyMaterial* polymat,
624                                                                         class RAS_IRenderTools* rendertools,
625                                                                         bool useObjectColor,
626                                                                         const MT_Vector4& rgbacolor,
627                                                                         class KX_ListSlot** slot
628                                                                         )
629
630         GLenum drawmode;
631         switch (mode)
632         {
633         case 0:
634                 drawmode = GL_TRIANGLES;
635                 break;
636         case 1:
637                 drawmode = GL_LINES;
638                 break;
639         case 2:
640                 drawmode = GL_QUADS;
641                 break;
642         default:
643                 drawmode = GL_LINES;
644                 break;
645         }
646         
647         const RAS_TexVert* vertexarray ;
648         unsigned int numindices,vt;
649
650         for (vt=0;vt<vertexarrays.size();vt++)
651         {
652                 vertexarray = &((*vertexarrays[vt]) [0]);
653                 const KX_IndexArray & indexarray = (*indexarrays[vt]);
654                 numindices = indexarray.size();
655                 
656                 if (!numindices)
657                         break;
658                 
659                 int vindex=0;
660                 switch (mode)
661                 {
662                 case 1:
663                         {
664                                 glBegin(GL_LINES);
665                                 vindex=0;
666                                 for (unsigned int i=0;i<numindices;i+=2)
667                                 {
668                                         glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
669                                         glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
670                                 }
671                                 glEnd();
672                         }
673                         break;
674                 case 2:
675                         {
676                                 glBegin(GL_QUADS);
677                                 vindex=0;
678                                 if (useObjectColor)
679                                 {
680                                         for (unsigned int i=0;i<numindices;i+=4)
681                                         {
682
683                                                 glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
684
685                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
686                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
687                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
688                                                 vindex++;
689                                                 
690                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
691                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
692                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
693                                                 vindex++;
694                                                 
695                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
696                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
697                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
698                                                 vindex++;
699                                                 
700                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
701                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
702                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
703                                                 vindex++;
704                                         }
705                                 }
706                                 else
707                                 {
708                                         for (unsigned int i=0;i<numindices;i+=4)
709                                         {
710                                                 // This looks curiously endian unsafe to me.
711                                                 // However it depends on the way the colors are packed into 
712                                                 // the m_rgba field of RAS_TexVert
713
714                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
715                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
716                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
717                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
718                                                 vindex++;
719                                                 
720                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
721                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
722                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
723                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
724                                                 vindex++;
725                                                 
726                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
727                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
728                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
729                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
730                                                 vindex++;
731                                                 
732                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
733                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
734                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
735                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
736                                                 vindex++;
737                                         }
738                                 }
739                                 glEnd();        
740                                 break;
741                         }
742                 case 0:
743                         {
744                                 glBegin(GL_TRIANGLES);
745                                 vindex=0;
746                                 if (useObjectColor)
747                                 {
748                                         for (unsigned int i=0;i<numindices;i+=3)
749                                         {
750
751                                                 glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
752
753                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
754                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
755                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
756                                                 vindex++;
757                                                 
758                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
759                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
760                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
761                                                 vindex++;
762                                                 
763                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
764                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
765                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
766                                                 vindex++;
767                                         }
768                                 }
769                                 else 
770                                 {
771                                         for (unsigned int i=0;i<numindices;i+=3)
772                                         {
773
774                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
775                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
776                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
777                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
778                                                 vindex++;
779                                                 
780                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
781                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
782                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
783                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
784                                                 vindex++;
785                                                 
786                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
787                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
788                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
789                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
790                                                 vindex++;
791                                         }
792                                 }
793                                 glEnd();        
794                                 break;
795                         }
796                 default:
797                         {
798                         }
799                         
800                 } // switch
801         } // for each vertexarray
802
803 }
804
805 void RAS_OpenGLRasterizer::IndexPrimitives_Ex(const vecVertexArray & vertexarrays,
806                                                                         const vecIndexArrays & indexarrays,
807                                                                         int mode,
808                                                                         class RAS_IPolyMaterial* polymat,
809                                                                         class RAS_IRenderTools* rendertools,
810                                                                         bool useObjectColor,
811                                                                         const MT_Vector4& rgbacolor
812                                                                         )
813
814         bool    recalc;
815         GLenum drawmode;
816         switch (mode)
817         {
818         case 0:
819                 drawmode = GL_TRIANGLES;
820                 break;
821         case 1:
822                 drawmode = GL_LINES;
823                 break;
824         case 2:
825                 drawmode = GL_QUADS;
826                 break;
827         default:
828                 drawmode = GL_LINES;
829                 break;
830         }
831         
832         const RAS_TexVert* vertexarray ;
833         unsigned int numindices,vt;
834
835         for (vt=0;vt<vertexarrays.size();vt++)
836         {
837                 vertexarray = &((*vertexarrays[vt]) [0]);
838                 const KX_IndexArray & indexarray = (*indexarrays[vt]);
839                 numindices = indexarray.size();
840                 
841                 if (!numindices)
842                         continue;
843                 
844                 int vindex=0;
845                 switch (mode)
846                 {
847                 case 1:
848                         {
849                                 glBegin(GL_LINES);
850                                 vindex=0;
851                                 for (unsigned int i=0;i<numindices;i+=2)
852                                 {
853                                         glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
854                                         glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
855                                 }
856                                 glEnd();
857                         }
858                         break;
859                 case 2:
860                         {
861                                 glBegin(GL_QUADS);
862                                 vindex=0;
863                                 if (useObjectColor)
864                                 {
865                                         for (unsigned int i=0;i<numindices;i+=4)
866                                         {
867                                                 MT_Point3 mv1, mv2, mv3, mv4, fnor;
868                                                 /* Calc a new face normal */
869
870                                                 if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL)
871                                                         recalc= true;
872                                                 else
873                                                         recalc=false;
874
875                                                 if (recalc){
876                                                         mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ());
877                                                         mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ());
878                                                         mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
879                                                         mv4 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
880                                                         
881                                                         fnor = (((mv2-mv1).cross(mv3-mv2))+((mv4-mv3).cross(mv1-mv4))).safe_normalized();
882
883                                                         glNormal3f(fnor[0], fnor[1], fnor[2]);
884                                                 }
885
886                                                 glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
887
888                                                 if (!recalc)
889                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
890                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
891                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
892                                                 vindex++;
893                                                 
894                                                 if (!recalc)
895                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
896                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
897                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
898                                                 vindex++;
899                                                 
900                                                 if (!recalc)
901                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());                                             
902                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
903                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
904                                                 vindex++;
905                                                 
906                                                 if (!recalc)
907                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
908                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
909                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
910                                                 vindex++;
911                                         }
912                                 }
913                                 else
914                                 {
915                                         for (unsigned int i=0;i<numindices;i+=4)
916                                         {
917                                                 // This looks curiously endian unsafe to me.
918                                                 // However it depends on the way the colors are packed into 
919                                                 // the m_rgba field of RAS_TexVert
920                                                 MT_Point3 mv1, mv2, mv3, mv4, fnor;
921                                                 /* Calc a new face normal */
922
923                                                 if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL)
924                                                         recalc= true;
925                                                 else
926                                                         recalc=false;
927
928
929                                                 if (recalc){
930                                                         mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ());
931                                                         mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ());
932                                                         mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
933                                                         mv4 = MT_Point3(vertexarray[(indexarray[vindex+3])].getLocalXYZ());
934                                                         
935                                                         fnor = (((mv2-mv1).cross(mv3-mv2))+((mv4-mv3).cross(mv1-mv4))).safe_normalized();
936
937                                                         glNormal3f(fnor[0], fnor[1], fnor[2]);
938                                                 }
939
940                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
941                                                 if (!recalc)
942                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
943                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
944                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
945                                                 vindex++;
946                                                 
947                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
948                                                 if (!recalc)
949                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
950                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
951                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
952                                                 vindex++;
953                                                 
954                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
955                                                 if (!recalc)
956                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
957                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
958                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
959                                                 vindex++;
960                                                 
961                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
962                                                 if (!recalc)
963                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
964                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
965                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
966                                                 vindex++;
967                                         }
968                                 }
969                                 glEnd();        
970                                 break;
971                         }
972                 case 0:
973                         {
974                                 glBegin(GL_TRIANGLES);
975                                 vindex=0;
976                                 if (useObjectColor)
977                                 {
978                                         for (unsigned int i=0;i<numindices;i+=3)
979                                         {
980                                                 MT_Point3 mv1, mv2, mv3, fnor;
981                                                 /* Calc a new face normal */
982
983                                                 if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL)
984                                                         recalc= true;
985                                                 else
986                                                         recalc=false;
987
988                                                 if (recalc){
989                                                         mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ());
990                                                         mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ());
991                                                         mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
992                                                         
993                                                         fnor = ((mv2-mv1).cross(mv3-mv2)).safe_normalized();
994                                                         glNormal3f(fnor[0], fnor[1], fnor[2]);
995                                                 }
996
997                                                 glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
998
999                                                 if (!recalc)
1000                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1001                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
1002                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1003                                                 vindex++;
1004                                                 
1005                                                 if (!recalc)
1006                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1007                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
1008                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1009                                                 vindex++;
1010                                                 
1011                                                 if (!recalc)
1012                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1013                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
1014                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1015                                                 vindex++;
1016                                         }
1017                                 }
1018                                 else 
1019                                 {
1020                                         for (unsigned int i=0;i<numindices;i+=3)
1021                                         {
1022                                                 MT_Point3 mv1, mv2, mv3, fnor;
1023                                                 /* Calc a new face normal */
1024
1025                                                 if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL)
1026                                                         recalc= true;
1027                                                 else
1028                                                         recalc=false;
1029
1030
1031                                                 if (recalc){
1032                                                         mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ());
1033                                                         mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ());
1034                                                         mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
1035                                                         
1036                                                         fnor = ((mv2-mv1).cross(mv3-mv2)).safe_normalized();
1037                                                         glNormal3f(fnor[0], fnor[1], fnor[2]);
1038                                                 }
1039
1040                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1041                                                 if (!recalc)
1042                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1043                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
1044                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1045                                                 vindex++;
1046                                                 
1047                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1048                                                 if (!recalc)
1049                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1050                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
1051                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1052                                                 vindex++;
1053                                                 
1054                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1055                                                 if (!recalc)
1056                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1057                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
1058                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1059                                                 vindex++;
1060                                         }
1061                                 }
1062                                 glEnd();        
1063                                 break;
1064                         }
1065                 default:
1066                         {
1067                         }
1068                         
1069                 } // switch
1070         } // for each vertexarray
1071
1072 }
1073
1074
1075
1076 void RAS_OpenGLRasterizer::IndexPrimitives_3DText(const vecVertexArray & vertexarrays,
1077                                                                         const vecIndexArrays & indexarrays,
1078                                                                         int mode,
1079                                                                         class RAS_IPolyMaterial* polymat,
1080                                                                         class RAS_IRenderTools* rendertools,
1081                                                                         bool useObjectColor,
1082                                                                         const MT_Vector4& rgbacolor
1083                                                                         )
1084
1085         GLenum drawmode;
1086         switch (mode)
1087         {
1088         case 0:
1089                 drawmode = GL_TRIANGLES;
1090                 break;
1091         case 1:
1092                 drawmode = GL_LINES;
1093                 break;
1094         case 2:
1095                 drawmode = GL_QUADS;
1096                 break;
1097         default:
1098                 drawmode = GL_LINES;
1099                 break;
1100         }
1101         
1102         const RAS_TexVert* vertexarray ;
1103         
1104         unsigned int numindices, vt;
1105         
1106         if (useObjectColor)
1107         {
1108                 glDisableClientState(GL_COLOR_ARRAY);
1109                 glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
1110         }
1111         else
1112         {
1113                 glEnableClientState(GL_COLOR_ARRAY);
1114         }
1115         
1116         for (vt=0;vt<vertexarrays.size();vt++)
1117         {
1118                 vertexarray = &((*vertexarrays[vt]) [0]);
1119                 const KX_IndexArray & indexarray = (*indexarrays[vt]);
1120                 numindices = indexarray.size();
1121                 
1122                 if (!numindices)
1123                         break;
1124                 
1125                 int vindex=0;
1126                 switch (mode)
1127                 {
1128                 case 1:
1129                         {
1130                                 glBegin(GL_LINES);
1131                                 vindex=0;
1132                                 for (unsigned int i=0;i<numindices;i+=2)
1133                                 {
1134                                         glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
1135                                         glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
1136                                 }
1137                                 glEnd();
1138                         }
1139                         break;
1140                 case 2:
1141                         {
1142                                 vindex=0;
1143                                 for (unsigned int i=0;i<numindices;i+=4)
1144                                 {
1145                                         float v1[3],v2[3],v3[3],v4[3];
1146                                         
1147                                         v1[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0];
1148                                         v1[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1];
1149                                         v1[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2];
1150                                         vindex++;
1151                                         
1152                                         v2[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0];
1153                                         v2[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1];
1154                                         v2[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2];
1155                                         vindex++;
1156                                         
1157                                         v3[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0];
1158                                         v3[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1];
1159                                         v3[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2];
1160                                         vindex++;
1161                                         
1162                                         v4[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0];
1163                                         v4[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1];
1164                                         v4[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2];
1165                                         
1166                                         vindex++;
1167                                         
1168                                         rendertools->RenderText(polymat->GetDrawingMode(),polymat,v1,v2,v3,v4);
1169                                         ClearCachingInfo();
1170                                 }
1171                                 break;
1172                         }
1173                 case 0:
1174                         {
1175                                 glBegin(GL_TRIANGLES);
1176                                 vindex=0;
1177                                 for (unsigned int i=0;i<numindices;i+=3)
1178                                 {
1179                                         float v1[3],v2[3],v3[3];
1180                                         
1181                                         v1[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0];
1182                                         v1[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1];
1183                                         v1[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2];
1184                                         vindex++;
1185                                         
1186                                         v2[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0];
1187                                         v2[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1];
1188                                         v2[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2];
1189                                         vindex++;
1190                                         
1191                                         v3[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0];
1192                                         v3[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1];
1193                                         v3[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2];
1194                                         vindex++;
1195                                         
1196                                         rendertools->RenderText(polymat->GetDrawingMode(),polymat,v1,v2,v3,NULL);               
1197                                         ClearCachingInfo();
1198                                 }
1199                                 glEnd();        
1200                                 break;
1201                         }
1202                 default:
1203                         {
1204                         }
1205                 }       //switch
1206         }       //for each vertexarray
1207 }
1208
1209 void RAS_OpenGLRasterizer::SetTexCoords(TexCoGen coords,int unit)
1210 {
1211         // this changes from material to material
1212         if(unit < RAS_MAX)
1213                 m_texco[unit] = coords;
1214 }
1215
1216 void RAS_OpenGLRasterizer::SetAttrib(int type)
1217 {
1218         if(type == RAS_TEXTANGENT) m_useTang=true;
1219 }
1220
1221 void RAS_OpenGLRasterizer::TexCoord(const RAS_TexVert &tv, int enabled)
1222 {
1223 #if defined(GL_ARB_multitexture) && defined(WITH_GLEXT)
1224         if (!getenv("WITHOUT_GLEXT")) {
1225                 if(bgl::RAS_EXT_support._ARB_multitexture) {
1226                         for(int unit=0; unit<enabled; unit++) {
1227                                 if( tv.getFlag() & TV_2NDUV && tv.getUnit() == unit ) {
1228                                         bgl::blMultiTexCoord2fvARB(GL_TEXTURE0_ARB+unit, tv.getUV2());
1229                                         continue;
1230                                 }
1231                                 switch(m_texco[unit]) {
1232                                 case RAS_TEXCO_DISABLE:
1233                                 case RAS_TEXCO_OBJECT:
1234                                 case RAS_TEXCO_GEN:
1235                                         break;
1236                                 case RAS_TEXCO_ORCO:
1237                                 case RAS_TEXCO_GLOB:
1238                                         bgl::blMultiTexCoord3fvARB(GL_TEXTURE0_ARB+unit, tv.getLocalXYZ());
1239                                         break;
1240                                 case RAS_TEXCO_UV1:
1241                                         bgl::blMultiTexCoord2fvARB(GL_TEXTURE0_ARB+unit, tv.getUV1());
1242                                         break;
1243                                 case RAS_TEXCO_NORM:
1244                                         bgl::blMultiTexCoord3fvARB(GL_TEXTURE0_ARB+unit, tv.getNormal());
1245                                         break;
1246                                 case RAS_TEXTANGENT:
1247                                         bgl::blMultiTexCoord4fvARB(GL_TEXTURE0_ARB+unit, tv.getTangent());
1248                                         break;
1249                                 case RAS_TEXCO_UV2:
1250                                         bgl::blMultiTexCoord2fvARB(GL_TEXTURE0_ARB+unit, tv.getUV2());
1251                                         break;
1252                                 }
1253                         }
1254                 }
1255         }
1256 #endif
1257
1258 #ifdef GL_ARB_vertex_program
1259         if(m_useTang && bgl::RAS_EXT_support._ARB_vertex_program)
1260                 bgl::blVertexAttrib4fvARB(1/*tangent*/, tv.getTangent());
1261 #endif
1262
1263 }
1264 void RAS_OpenGLRasterizer::Tangent(     const RAS_TexVert& v1,
1265                                                                         const RAS_TexVert& v2,
1266                                                                         const RAS_TexVert& v3,
1267                                                                         const MT_Vector3 &no)
1268 {
1269 #if defined(GL_ARB_multitexture) && defined(WITH_GLEXT)
1270         if (!getenv("WITHOUT_GLEXT")) {
1271                 // TODO: set for deformer... 
1272                 MT_Vector3 x1(v1.getLocalXYZ()), x2(v2.getLocalXYZ()), x3(v3.getLocalXYZ());
1273                 MT_Vector2 uv1(v1.getUV1()), uv2(v2.getUV1()), uv3(v3.getUV1());
1274                 MT_Vector3 dx1(x2 - x1), dx2(x3 - x1);
1275                 MT_Vector2 duv1(uv2 - uv1), duv2(uv3 - uv1);
1276
1277                 MT_Scalar r = 1.0 / (duv1.x() * duv2.y() - duv2.x() * duv1.y());
1278                 duv1 *= r;
1279                 duv2 *= r;
1280                 MT_Vector3 sdir(duv2.y() * dx1 - duv1.y() * dx2);
1281                 MT_Vector3 tdir(duv1.x() * dx2 - duv2.x() * dx1);
1282
1283                 // Gram-Schmidt orthogonalize
1284                 MT_Vector3 t(sdir - no.cross(no.cross(sdir)));
1285                 if (!MT_fuzzyZero(t)) t /= t.length();
1286
1287                 float tangent[4];
1288                 t.getValue(tangent);
1289                 // Calculate handedness
1290                 tangent[3] = no.dot(sdir.cross(tdir)) < 0.0 ? -1.0 : 1.0;
1291         }
1292 #endif
1293 }
1294
1295
1296 void RAS_OpenGLRasterizer::IndexPrimitivesMulti(
1297                 const vecVertexArray& vertexarrays,
1298                 const vecIndexArrays & indexarrays,
1299                 int mode,
1300                 class RAS_IPolyMaterial* polymat,
1301                 class RAS_IRenderTools* rendertools,
1302                 bool useObjectColor,
1303                 const MT_Vector4& rgbacolor,
1304                 class KX_ListSlot** slot
1305                 )
1306
1307 #if defined(GL_ARB_multitexture) && defined(WITH_GLEXT)
1308         if (!getenv("WITHOUT_GLEXT")) {
1309                 GLenum drawmode;
1310                 switch (mode)
1311                 {
1312                 case 0:
1313                         drawmode = GL_TRIANGLES;
1314                         break;
1315                 case 1:
1316                         drawmode = GL_LINES;
1317                         break;
1318                 case 2:
1319                         drawmode = GL_QUADS;
1320                         break;
1321                 default:
1322                         drawmode = GL_LINES;
1323                         break;
1324                 }
1325         
1326                 const RAS_TexVert* vertexarray ;
1327                 unsigned int numindices,vt;
1328
1329                 for (vt=0;vt<vertexarrays.size();vt++)
1330                 {
1331                         vertexarray = &((*vertexarrays[vt]) [0]);
1332                         const KX_IndexArray & indexarray = (*indexarrays[vt]);
1333                         numindices = indexarray.size();
1334                         const unsigned int enabled = polymat->GetEnabled();
1335
1336                         if (!numindices)
1337                                 break;
1338
1339                         int vindex=0;
1340                         switch (mode)
1341                         {
1342                         case 1:
1343                                 {
1344                                         glBegin(GL_LINES);
1345                                         vindex=0;
1346                                         for (unsigned int i=0;i<numindices;i+=2)
1347                                         {
1348                                                 glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
1349                                                 glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
1350                                         }
1351                                         glEnd();
1352                                 }
1353                                 break;
1354                         case 2:
1355                                 {
1356                                         glBegin(GL_QUADS);
1357                                         vindex=0;
1358                                         if (useObjectColor)
1359                                         {
1360                                                 for (unsigned int i=0;i<numindices;i+=4)
1361                                                 {
1362
1363                                                         glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
1364
1365                                                         //
1366                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1367                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1368                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1369                                                         vindex++;
1370
1371                                                         //
1372                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1373                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1374                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1375                                                         vindex++;
1376
1377                                                         //
1378                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1379                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1380                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1381                                                         vindex++;
1382
1383                                                         //
1384                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1385                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1386                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1387                                                         vindex++;
1388                                                 }
1389                                         }
1390                                         else
1391                                         {
1392                                                 for (unsigned int i=0;i<numindices;i+=4)
1393                                                 {
1394                                                         // This looks curiously endian unsafe to me.
1395                                                         // However it depends on the way the colors are packed into 
1396                                                         // the m_rgba field of RAS_TexVert
1397                                         
1398                                                         //
1399                                                         glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1400                                         
1401                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1402                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1403                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1404                                                         vindex++;
1405                                                 
1406                                                         //
1407                                                         glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1408                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1409                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1410                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1411                                                         vindex++;
1412
1413                                                         //
1414                                                         glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1415                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1416                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1417                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1418                                                         vindex++;
1419
1420                                                         //
1421                                                         glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1422                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1423                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1424                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1425                                                         vindex++;
1426                                                 }
1427                                         }
1428                                         glEnd();        
1429                                         break;
1430                                 }
1431                         case 0:
1432                                 {
1433                                         glBegin(GL_TRIANGLES);
1434                                         vindex=0;
1435                                         if (useObjectColor)
1436                                         {
1437                                                 for (unsigned int i=0;i<numindices;i+=3)
1438                                                 {
1439
1440                                                         glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
1441                                                         //
1442                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1443                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1444                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1445                                                         vindex++;
1446
1447                                                         //
1448                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1449                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1450                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1451                                                         vindex++;
1452
1453                                                         //
1454                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1455                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1456                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1457                                                         vindex++;
1458                                                 }
1459                                         }
1460                                         else 
1461                                         {
1462                                                 for (unsigned int i=0;i<numindices;i+=3)
1463                                                 {
1464                                                         //
1465                                                         glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1466                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1467                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1468                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1469                                                         vindex++;
1470                                         
1471                                                         //
1472                                                         glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1473                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1474                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1475                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1476                                                         vindex++;
1477
1478                                                         //
1479                                                         glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1480                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1481                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1482                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1483                                                         vindex++;
1484                                                 }
1485                                         }
1486                                         glEnd();        
1487                                         break;
1488                                 }
1489                         default:
1490                                 {
1491                                 }
1492                         } // switch
1493                 } // for each vertexarray
1494         }
1495 #endif// GL_ARB_multitexture
1496 }
1497
1498 void RAS_OpenGLRasterizer::IndexPrimitivesMulti_Ex(
1499                                                                         const vecVertexArray & vertexarrays,
1500                                                                         const vecIndexArrays & indexarrays,
1501                                                                         int mode,
1502                                                                         class RAS_IPolyMaterial* polymat,
1503                                                                         class RAS_IRenderTools* rendertools,
1504                                                                         bool useObjectColor,
1505                                                                         const MT_Vector4& rgbacolor)
1506
1507 #if defined(GL_ARB_multitexture) && defined(WITH_GLEXT)
1508         if (!getenv("WITHOUT_GLEXT")) {
1509                 bool    recalc;
1510                 GLenum drawmode;
1511                 switch (mode)
1512                 {
1513                 case 0:
1514                         drawmode = GL_TRIANGLES;
1515                         break;
1516                 case 1:
1517                         drawmode = GL_LINES;
1518                         break;
1519                 case 2:
1520                         drawmode = GL_QUADS;
1521                         break;
1522                 default:
1523                         drawmode = GL_LINES;
1524                         break;
1525                 }
1526         
1527                 const RAS_TexVert* vertexarray ;
1528                 unsigned int numindices,vt;
1529
1530                 for (vt=0;vt<vertexarrays.size();vt++)
1531                 {
1532                         vertexarray = &((*vertexarrays[vt]) [0]);
1533                         const KX_IndexArray & indexarray = (*indexarrays[vt]);
1534                         numindices = indexarray.size();
1535                         const unsigned int enabled = polymat->GetEnabled();
1536                 
1537                         if (!numindices)
1538                                 continue;
1539                 
1540                         int vindex=0;
1541                         switch (mode)
1542                         {
1543                         case 1:
1544                                 {
1545                                         glBegin(GL_LINES);
1546                                         vindex=0;
1547                                         for (unsigned int i=0;i<numindices;i+=2)
1548                                         {
1549                                                 glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
1550                                                 glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
1551                                         }
1552                                         glEnd();
1553                                 }
1554                                 break;
1555                         case 2:
1556                                 {
1557                                         glBegin(GL_QUADS);
1558                                         vindex=0;
1559                                         if (useObjectColor)
1560                                         {
1561                                                 for (unsigned int i=0;i<numindices;i+=4)
1562                                                 {
1563                                                         MT_Point3 mv1, mv2, mv3, mv4, fnor;
1564                                                         /* Calc a new face normal */
1565
1566                                                         if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL)
1567                                                                 recalc= true;
1568                                                         else
1569                                                                 recalc=false;
1570
1571                                                         if (recalc){
1572                                                                 mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ());
1573                                                                 mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ());
1574                                                                 mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
1575                                                                 mv4 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
1576                                                 
1577                                                                 fnor = (((mv2-mv1).cross(mv3-mv2))+((mv4-mv3).cross(mv1-mv4))).safe_normalized();
1578
1579                                                                 glNormal3f(fnor[0], fnor[1], fnor[2]);
1580                                                         }
1581
1582                                                         glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
1583
1584                                                         if (!recalc)
1585                                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1586
1587                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1588                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1589                                                         vindex++;
1590                                         
1591                                                         if (!recalc)
1592                                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1593
1594                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1595                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1596                                                         vindex++;
1597                                                 
1598                                                         if (!recalc)
1599                                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());                                             
1600                                                 
1601                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1602                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1603                                                         vindex++;
1604                                         
1605                                                         if (!recalc)
1606                                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1607                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1608                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1609                                                         vindex++;
1610                                                 }
1611                                         }
1612                                         else
1613                                         {
1614                                                 for (unsigned int i=0;i<numindices;i+=4)
1615                                                 {
1616                                                         // This looks curiously endian unsafe to me.
1617                                                         // However it depends on the way the colors are packed into 
1618                                                         // the m_rgba field of RAS_TexVert
1619                                                         MT_Point3 mv1, mv2, mv3, mv4, fnor;
1620                                                         /* Calc a new face normal */
1621
1622                                                         if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL)
1623                                                                 recalc= true;
1624                                                         else
1625                                                                 recalc=false;
1626
1627
1628                                                         if (recalc){
1629                                                                 mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ());
1630                                                                 mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ());
1631                                                                 mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
1632                                                                 mv4 = MT_Point3(vertexarray[(indexarray[vindex+3])].getLocalXYZ());
1633                                                         
1634                                                                 fnor = (((mv2-mv1).cross(mv3-mv2))+((mv4-mv3).cross(mv1-mv4))).safe_normalized();
1635
1636                                                                 glNormal3f(fnor[0], fnor[1], fnor[2]);
1637                                                         }
1638
1639                                                         glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1640                                                         if (!recalc)
1641                                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1642
1643                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1644                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1645                                                         vindex++;
1646                                                 
1647                                                         glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1648                                                         if (!recalc)
1649                                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1650                                         
1651                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1652                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1653                                                         vindex++;
1654                                                 
1655                                                         glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1656                                                         if (!recalc)
1657                                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1658                                         
1659                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1660                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1661                                                         vindex++;
1662                                                 
1663                                                         glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1664                                                         if (!recalc)
1665                                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1666
1667                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1668                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1669                                                         vindex++;
1670                                                 }
1671                                         }
1672                                         glEnd();        
1673                                         break;
1674                                 }
1675                         case 0:
1676                                 {
1677                                         glBegin(GL_TRIANGLES);
1678                                         vindex=0;
1679                                         if (useObjectColor)
1680                                         {
1681                                                 for (unsigned int i=0;i<numindices;i+=3)
1682                                                 {
1683                                                         MT_Point3 mv1, mv2, mv3, fnor;
1684                                                         /* Calc a new face normal */
1685
1686                                                         if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL)
1687                                                                 recalc= true;
1688                                                         else
1689                                                                 recalc=false;
1690
1691                                                         if (recalc){
1692                                                                 mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ());
1693                                                                 mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ());
1694                                                                 mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
1695                                                         
1696                                                                 fnor = ((mv2-mv1).cross(mv3-mv2)).safe_normalized();
1697                                                                 glNormal3f(fnor[0], fnor[1], fnor[2]);
1698                                                         }
1699
1700                                                         glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
1701
1702                                                         if (!recalc)
1703                                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1704                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1705                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1706                                                         vindex++;
1707                                                         
1708                                                         if (!recalc)
1709                                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1710                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1711                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1712                                                         vindex++;
1713                                                 
1714                                                         if (!recalc)
1715                                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1716                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1717                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1718                                                         vindex++;
1719                                                 }
1720                                         }
1721                                         else 
1722                                         {
1723                                                 for (unsigned int i=0;i<numindices;i+=3)
1724                                                 {
1725                                                         MT_Point3 mv1, mv2, mv3, fnor;
1726                                                         /* Calc a new face normal */
1727
1728                                                         if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL)
1729                                                                 recalc= true;
1730                                                         else
1731                                                                 recalc=false;
1732
1733
1734                                                         if (recalc){
1735                                                                 mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ());
1736                                                                 mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ());
1737                                                                 mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
1738                                                         
1739                                                                 fnor = ((mv2-mv1).cross(mv3-mv2)).safe_normalized();
1740                                                                 glNormal3f(fnor[0], fnor[1], fnor[2]);
1741                                                         }
1742
1743                                                         glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1744                                                         if (!recalc)
1745                                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1746                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1747                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1748                                                         vindex++;
1749                                                 
1750                                                         glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1751                                                         if (!recalc)
1752                                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1753                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1754                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1755                                                         vindex++;
1756                                                 
1757                                                         glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1758                                                         if (!recalc)
1759                                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1760                                                         TexCoord(vertexarray[(indexarray[vindex])],enabled );
1761                                                         glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1762                                                         vindex++;
1763                                                 }
1764                                         }
1765                                         glEnd();        
1766                                         break;
1767                                 }
1768                         default:
1769                                 {
1770                                 }
1771
1772                         } // switch
1773                 } // for each vertexarray
1774         }
1775 #endif
1776 }
1777
1778
1779
1780 void RAS_OpenGLRasterizer::SetProjectionMatrix(MT_CmMatrix4x4 &mat)
1781 {
1782         glMatrixMode(GL_PROJECTION);
1783         double* matrix = &mat(0,0);
1784         glLoadMatrixd(matrix);
1785 }
1786
1787
1788 void RAS_OpenGLRasterizer::SetProjectionMatrix(const MT_Matrix4x4 & mat)
1789 {
1790         glMatrixMode(GL_PROJECTION);
1791         double matrix[16];
1792         /* Get into argument. Looks a bit dodgy, but it's ok. */
1793         mat.getValue(matrix);
1794         /* Internally, MT_Matrix4x4 uses doubles (MT_Scalar). */
1795         glLoadMatrixd(matrix);  
1796 }
1797
1798 MT_Matrix4x4 RAS_OpenGLRasterizer::GetFrustumMatrix(
1799         float left,
1800         float right,
1801         float bottom,
1802         float top,
1803         float frustnear,
1804         float frustfar,
1805         bool
1806 ){
1807         MT_Matrix4x4 result;
1808         double mat[16];
1809
1810         // correction for stereo
1811         if(m_stereomode != RAS_STEREO_NOSTEREO)
1812         {
1813                         float near_div_focallength;
1814                         // next 2 params should be specified on command line and in Blender publisher
1815                         if (!m_setfocallength)
1816                                 m_focallength = 1.5 * right;  // derived from example
1817                         if (!m_seteyesep)
1818                                 m_eyeseparation = 0.18 * right;  // just a guess...
1819
1820                         near_div_focallength = frustnear / m_focallength;
1821                         switch(m_curreye)
1822                         {
1823                                 case RAS_STEREO_LEFTEYE:
1824                                                 left += 0.5 * m_eyeseparation * near_div_focallength;
1825                                                 right += 0.5 * m_eyeseparation * near_div_focallength;
1826                                                 break;
1827                                 case RAS_STEREO_RIGHTEYE:
1828                                                 left -= 0.5 * m_eyeseparation * near_div_focallength;
1829                                                 right -= 0.5 * m_eyeseparation * near_div_focallength;
1830                                                 break;
1831                         }
1832                         // leave bottom, top, bottom and top untouched
1833         }
1834         
1835         glMatrixMode(GL_PROJECTION);
1836         glLoadIdentity();
1837         glFrustum(left, right, bottom, top, frustnear, frustfar);
1838                 
1839         glGetDoublev(GL_PROJECTION_MATRIX, mat);
1840         result.setValue(mat);
1841
1842         return result;
1843 }
1844
1845
1846 // next arguments probably contain redundant info, for later...
1847 void RAS_OpenGLRasterizer::SetViewMatrix(const MT_Matrix4x4 &mat, const MT_Vector3& campos,
1848                 const MT_Point3 &, const MT_Quaternion &camOrientQuat)
1849 {
1850         MT_Matrix4x4 viewMat = mat;
1851
1852         // correction for stereo
1853         if(m_stereomode != RAS_STEREO_NOSTEREO)
1854         {
1855                 MT_Matrix3x3 camOrientMat3x3(camOrientQuat);
1856                 MT_Vector3 unitViewDir(0.0, -1.0, 0.0);  // minus y direction, Blender convention
1857                 MT_Vector3 unitViewupVec(0.0, 0.0, 1.0);
1858                 MT_Vector3 viewDir, viewupVec;
1859                 MT_Vector3 eyeline;
1860
1861                 // actual viewDir
1862                 viewDir = camOrientMat3x3 * unitViewDir;  // this is the moto convention, vector on right hand side
1863                 // actual viewup vec
1864                 viewupVec = camOrientMat3x3 * unitViewupVec;
1865
1866                 // vector between eyes
1867                 eyeline = viewDir.cross(viewupVec);
1868
1869                 switch(m_curreye)
1870                 {
1871                         case RAS_STEREO_LEFTEYE:
1872                                 {
1873                                 // translate to left by half the eye distance
1874                                 MT_Transform transform;
1875                                 transform.setIdentity();
1876                                 transform.translate(-(eyeline * m_eyeseparation / 2.0));
1877                                 viewMat *= transform;
1878                                 }
1879                                 break;
1880                         case RAS_STEREO_RIGHTEYE:
1881                                 {
1882                                 // translate to right by half the eye distance
1883                                 MT_Transform transform;
1884                                 transform.setIdentity();
1885                                 transform.translate(eyeline * m_eyeseparation / 2.0);
1886                                 viewMat *= transform;
1887                                 }
1888                                 break;
1889                 }
1890         }
1891
1892         // convert row major matrix 'viewMat' to column major for OpenGL
1893         MT_Scalar cammat[16];
1894         viewMat.getValue(cammat);
1895         MT_CmMatrix4x4 viewCmmat = cammat;
1896
1897         glMatrixMode(GL_MODELVIEW);
1898         m_viewmatrix = viewCmmat;
1899         glLoadMatrixd(&m_viewmatrix(0,0));
1900         m_campos = campos;
1901 }
1902
1903
1904 const MT_Point3& RAS_OpenGLRasterizer::GetCameraPosition()
1905 {
1906         return m_campos;
1907 }
1908
1909
1910
1911 void RAS_OpenGLRasterizer::LoadViewMatrix()
1912 {
1913         glLoadMatrixd(&m_viewmatrix(0,0));
1914 }
1915
1916
1917
1918 void RAS_OpenGLRasterizer::EnableTextures(bool enable)
1919 {
1920 }
1921
1922
1923
1924 void RAS_OpenGLRasterizer::SetCullFace(bool enable)
1925 {
1926         if (enable)
1927                 glEnable(GL_CULL_FACE);
1928         else
1929                 glDisable(GL_CULL_FACE);
1930 }
1931
1932 void RAS_OpenGLRasterizer::SetLines(bool enable)
1933 {
1934         if (enable)
1935                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
1936         else
1937                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1938 }
1939
1940 void RAS_OpenGLRasterizer::SetSpecularity(float specX,
1941                                                                                   float specY,
1942                                                                                   float specZ,
1943                                                                                   float specval)
1944 {
1945         GLfloat mat_specular[] = {specX, specY, specZ, specval};
1946         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
1947 }
1948
1949
1950
1951 void RAS_OpenGLRasterizer::SetShinyness(float shiny)
1952 {
1953         GLfloat mat_shininess[] = {     shiny };
1954         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
1955 }
1956
1957
1958
1959 void RAS_OpenGLRasterizer::SetDiffuse(float difX,float difY,float difZ,float diffuse)
1960 {
1961         GLfloat mat_diffuse [] = {difX, difY,difZ, diffuse};
1962         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
1963 }
1964
1965 void RAS_OpenGLRasterizer::SetEmissive(float eX, float eY, float eZ, float e)
1966 {
1967         GLfloat mat_emit [] = {eX,eY,eZ,e};
1968         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emit);
1969 }
1970
1971
1972 double RAS_OpenGLRasterizer::GetTime()
1973 {
1974         return m_time;
1975 }
1976
1977 void RAS_OpenGLRasterizer::SetPolygonOffset(float mult, float add)
1978 {
1979         glPolygonOffset(mult, add);
1980         GLint mode = GL_POLYGON_OFFSET_FILL;
1981         if (m_drawingmode < KX_SHADED)
1982                 mode = GL_POLYGON_OFFSET_LINE;
1983         if (mult != 0.0f || add != 0.0f)
1984                 glEnable(mode);
1985         else
1986                 glDisable(mode);
1987 }
1988
1989 void RAS_OpenGLRasterizer::EnableMotionBlur(float motionblurvalue)
1990 {
1991         m_motionblur = 1;
1992         m_motionblurvalue = motionblurvalue;
1993 }
1994
1995 void RAS_OpenGLRasterizer::DisableMotionBlur()
1996 {
1997         m_motionblur = 0;
1998         m_motionblurvalue = -1.0;
1999 }