improved deactivation, exposed more tweakable parameters to python,
[blender.git] / source / gameengine / Rasterizer / RAS_OpenGLRasterizer / RAS_OpenGLRasterizer.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31  
32 #include <math.h>
33  
34 #include "RAS_OpenGLRasterizer.h"
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #ifdef WIN32
41 #include <windows.h>
42 #endif // WIN32
43 #ifdef __APPLE__
44 #include <OpenGL/gl.h>
45 #include <OpenGL/glu.h>
46 #else
47 #include <GL/gl.h>
48 #include <GL/glu.h>
49 #endif
50
51 #include "RAS_Rect.h"
52 #include "RAS_TexVert.h"
53 #include "MT_CmMatrix4x4.h"
54 #include "RAS_IRenderTools.h" // rendering text
55
56 #include "RAS_GLExtensionManager.h"
57
58 /**
59  *  32x32 bit masks for vinterlace stereo mode
60  */
61 static GLuint left_eye_vinterlace_mask[32];
62 static GLuint right_eye_vinterlace_mask[32];
63
64 /**
65  *  32x32 bit masks for hinterlace stereo mode.
66  *  Left eye = &hinterlace_mask[0]
67  *  Right eye = &hinterlace_mask[1]
68  */
69 static GLuint hinterlace_mask[33];
70
71 RAS_OpenGLRasterizer::RAS_OpenGLRasterizer(RAS_ICanvas* canvas)
72         :RAS_IRasterizer(canvas),
73         m_2DCanvas(canvas),
74         m_fogenabled(false),
75         m_time(0.0),
76         m_stereomode(RAS_STEREO_NOSTEREO),
77         m_curreye(RAS_STEREO_LEFTEYE),
78         m_eyeseparation(0.0),
79         m_seteyesep(false),
80         m_focallength(0.0),
81         m_setfocallength(false),
82         m_noOfScanlines(32),
83         m_materialCachingInfo(0)
84 {
85         m_viewmatrix.Identity();
86         
87         for (int i = 0; i < 32; i++)
88         {
89                 left_eye_vinterlace_mask[i] = 0x55555555;
90                 right_eye_vinterlace_mask[i] = 0xAAAAAAAA;
91                 hinterlace_mask[i] = (i&1)*0xFFFFFFFF;
92         }
93         hinterlace_mask[32] = 0;
94 }
95
96
97
98 RAS_OpenGLRasterizer::~RAS_OpenGLRasterizer()
99 {
100 }
101
102
103
104 static void Myinit_gl_stuff(void)       
105 {
106         float mat_specular[] = { 0.5, 0.5, 0.5, 1.0 };
107         float mat_shininess[] = { 35.0 };
108 /*      float one= 1.0; */
109         int a, x, y;
110         GLubyte pat[32*32];
111         const GLubyte *patc= pat;
112                 
113         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_specular);
114         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
115         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
116
117
118 #if defined(__FreeBSD) || defined(__linux__)
119         glDisable(GL_DITHER);   /* op sgi/sun hardware && 12 bits */
120 #endif
121         
122         /* no local viewer, looks ugly in ortho mode */
123         /* glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, &one); */
124         
125         glDepthFunc(GL_LEQUAL);
126         /* scaling matrices */
127         glEnable(GL_NORMALIZE);
128
129         glShadeModel(GL_FLAT);
130
131         glDisable(GL_ALPHA_TEST);
132         glDisable(GL_BLEND);
133         glDisable(GL_DEPTH_TEST);
134         glDisable(GL_FOG);
135         glDisable(GL_LIGHTING);
136         glDisable(GL_LOGIC_OP);
137         glDisable(GL_STENCIL_TEST);
138         glDisable(GL_TEXTURE_1D);
139         glDisable(GL_TEXTURE_2D);
140
141         glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
142         glPixelTransferi(GL_RED_SCALE, 1);
143         glPixelTransferi(GL_RED_BIAS, 0);
144         glPixelTransferi(GL_GREEN_SCALE, 1);
145         glPixelTransferi(GL_GREEN_BIAS, 0);
146         glPixelTransferi(GL_BLUE_SCALE, 1);
147         glPixelTransferi(GL_BLUE_BIAS, 0);
148         glPixelTransferi(GL_ALPHA_SCALE, 1);
149         glPixelTransferi(GL_ALPHA_BIAS, 0);
150
151         a = 0;
152         for(x=0; x<32; x++)
153         {
154                 for(y=0; y<4; y++)
155                 {
156                         if( (x) & 1) pat[a++]= 0x88;
157                         else pat[a++]= 0x22;
158                 }
159         }
160         
161         glPolygonStipple(patc);
162         
163         glFrontFace(GL_CCW);
164         glCullFace(GL_BACK);
165         glEnable(GL_CULL_FACE);
166 }
167
168
169
170 bool RAS_OpenGLRasterizer::Init()
171 {
172
173         Myinit_gl_stuff();
174
175         m_redback = 0.4375;
176         m_greenback = 0.4375;
177         m_blueback = 0.4375;
178         m_alphaback = 0.0;
179         
180         glClearColor(m_redback,m_greenback,m_blueback,m_alphaback);
181         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
182         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
183
184         glShadeModel(GL_SMOOTH);
185
186         return true;
187 }
188
189
190
191 void RAS_OpenGLRasterizer::SetBackColor(float red,
192                                                                                 float green,
193                                                                                 float blue,
194                                                                                 float alpha)
195 {
196         m_redback = red;
197         m_greenback = green;
198         m_blueback = blue;
199         m_alphaback = alpha;
200 }
201
202
203
204 void RAS_OpenGLRasterizer::SetFogColor(float r,
205                                                                            float g,
206                                                                            float b)
207 {
208         m_fogr = r;
209         m_fogg = g;
210         m_fogb = b;
211         m_fogenabled = true;
212 }
213
214
215
216 void RAS_OpenGLRasterizer::SetFogStart(float start)
217 {
218         m_fogstart = start;
219         m_fogenabled = true;
220 }
221
222
223
224 void RAS_OpenGLRasterizer::SetFogEnd(float fogend)
225 {
226         m_fogdist = fogend;
227         m_fogenabled = true;
228 }
229
230
231
232 void RAS_OpenGLRasterizer::SetFog(float start,
233                                                                   float dist,
234                                                                   float r,
235                                                                   float g,
236                                                                   float b)
237 {
238         m_fogstart = start;
239         m_fogdist = dist;
240         m_fogr = r;
241         m_fogg = g;
242         m_fogb = b;
243         m_fogenabled = true;
244 }
245
246
247
248 void RAS_OpenGLRasterizer::DisableFog()
249 {
250         m_fogenabled = false;
251 }
252
253
254
255 void RAS_OpenGLRasterizer::DisplayFog()
256 {
257         if ((m_drawingmode >= KX_SOLID) && m_fogenabled)
258         {
259                 float params[5];
260                 glFogi(GL_FOG_MODE, GL_LINEAR);
261                 glFogf(GL_FOG_DENSITY, 0.1f);
262                 glFogf(GL_FOG_START, m_fogstart);
263                 glFogf(GL_FOG_END, m_fogstart + m_fogdist);
264                 params[0]= m_fogr;
265                 params[1]= m_fogg;
266                 params[2]= m_fogb;
267                 params[3]= 0.0;
268                 glFogfv(GL_FOG_COLOR, params); 
269                 glEnable(GL_FOG);
270         } 
271         else
272         {
273                 glDisable(GL_FOG);
274         }
275 }
276
277
278
279 bool RAS_OpenGLRasterizer::SetMaterial(const RAS_IPolyMaterial& mat)
280 {
281         return mat.Activate(this, m_materialCachingInfo);
282 }
283
284
285
286 void RAS_OpenGLRasterizer::Exit()
287 {
288
289         glEnable(GL_CULL_FACE);
290         glEnable(GL_DEPTH_TEST);
291         glClearDepth(1.0); 
292         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
293         glClearColor(m_redback, m_greenback, m_blueback, m_alphaback);
294         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
295         glDepthMask (GL_TRUE);
296         glDepthFunc(GL_LEQUAL);
297         glBlendFunc(GL_ONE, GL_ZERO);
298         
299         glDisable(GL_POLYGON_STIPPLE);
300         
301         glDisable(GL_LIGHTING);
302         if (bgl::QueryExtension(bgl::_GL_EXT_separate_specular_color) || bgl::QueryVersion(1, 2))
303                 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
304         
305         EndFrame();
306 }
307
308 bool RAS_OpenGLRasterizer::InterlacedStereo() const
309 {
310         return m_stereomode == RAS_STEREO_VINTERLACE || m_stereomode == RAS_STEREO_INTERLACED;
311 }
312
313 bool RAS_OpenGLRasterizer::BeginFrame(int drawingmode, double time)
314 {
315         m_time = time;
316         m_drawingmode = drawingmode;
317         
318         if (!InterlacedStereo() || m_curreye == RAS_STEREO_LEFTEYE)
319         {
320                 m_2DCanvas->ClearColor(m_redback,m_greenback,m_blueback,m_alphaback);
321                 m_2DCanvas->ClearBuffer(RAS_ICanvas::COLOR_BUFFER);
322         }
323
324         // Blender camera routine destroys the settings
325         if (m_drawingmode < KX_SOLID)
326         {
327                 glDisable (GL_CULL_FACE);
328                 glDisable (GL_DEPTH_TEST);
329         }
330         else
331         {
332                 glEnable(GL_DEPTH_TEST);
333                 glEnable (GL_CULL_FACE);
334         }
335
336         glShadeModel(GL_SMOOTH);
337
338         m_2DCanvas->BeginFrame();
339         
340         return true;
341 }
342
343
344
345 void RAS_OpenGLRasterizer::SetDrawingMode(int drawingmode)
346 {
347         m_drawingmode = drawingmode;
348
349         switch (m_drawingmode)
350         {
351         case KX_BOUNDINGBOX:
352                 {
353                 }
354         case KX_WIREFRAME:
355                 {
356                         glDisable (GL_CULL_FACE);
357                         break;
358                 }
359         case KX_TEXTURED:
360                 {
361                 }
362         case KX_SHADED:
363                 {
364                 }
365         case KX_SOLID:
366                 {
367                 }
368         default:
369                 {
370                 }
371         }
372 }
373
374
375
376 int RAS_OpenGLRasterizer::GetDrawingMode()
377 {
378         return m_drawingmode;
379 }
380
381
382
383 void RAS_OpenGLRasterizer::SetDepthMask(DepthMask depthmask)
384 {
385         glDepthMask(depthmask == KX_DEPTHMASK_DISABLED ? GL_FALSE : GL_TRUE);
386 }
387
388
389
390 void RAS_OpenGLRasterizer::ClearDepthBuffer()
391 {
392         m_2DCanvas->ClearBuffer(RAS_ICanvas::DEPTH_BUFFER);
393 }
394
395
396 void RAS_OpenGLRasterizer::ClearCachingInfo(void)
397 {
398         m_materialCachingInfo = 0;
399 }
400
401
402 void RAS_OpenGLRasterizer::EndFrame()
403 {
404         glDisable(GL_LIGHTING);
405         glDisable(GL_TEXTURE);
406
407         //DrawDebugLines
408         glBegin(GL_LINES);
409         for (unsigned int i=0;i<m_debugLines.size();i++)
410         {
411                 
412
413                 glColor4f(m_debugLines[i].m_color[0],m_debugLines[i].m_color[1],m_debugLines[i].m_color[2],1.f);
414                 const MT_Scalar* fromPtr = &m_debugLines[i].m_from.x();
415                 const MT_Scalar* toPtr= &m_debugLines[i].m_to.x();
416
417                 glVertex3dv(fromPtr);
418                 glVertex3dv(toPtr);
419         }
420         glEnd();
421
422         m_debugLines.clear();
423
424         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
425         m_2DCanvas->EndFrame();
426 }       
427
428 void RAS_OpenGLRasterizer::SetRenderArea()
429 {
430         // only above/below stereo method needs viewport adjustment
431         switch (m_stereomode)
432         {
433                 case RAS_STEREO_ABOVEBELOW:
434                         switch(m_curreye)
435                         {
436                                 case RAS_STEREO_LEFTEYE:
437                                         // upper half of window
438                                         m_2DCanvas->GetDisplayArea().SetLeft(0);
439                                         m_2DCanvas->GetDisplayArea().SetBottom(m_2DCanvas->GetHeight() -
440                                                 int(m_2DCanvas->GetHeight() - m_noOfScanlines) / 2);
441         
442                                         m_2DCanvas->GetDisplayArea().SetRight(int(m_2DCanvas->GetWidth()));
443                                         m_2DCanvas->GetDisplayArea().SetTop(int(m_2DCanvas->GetHeight()));
444                                         break;
445                                 case RAS_STEREO_RIGHTEYE:
446                                         // lower half of window
447                                         m_2DCanvas->GetDisplayArea().SetLeft(0);
448                                         m_2DCanvas->GetDisplayArea().SetBottom(0);
449                                         m_2DCanvas->GetDisplayArea().SetRight(int(m_2DCanvas->GetWidth()));
450                                         m_2DCanvas->GetDisplayArea().SetTop(int(m_2DCanvas->GetHeight() - m_noOfScanlines) / 2);
451                                         break;
452                         }
453                         break;
454                 case RAS_STEREO_SIDEBYSIDE:
455                         switch (m_curreye)
456                         {
457                                 case RAS_STEREO_LEFTEYE:
458                                         // Left half of window
459                                         m_2DCanvas->GetDisplayArea().SetLeft(0);
460                                         m_2DCanvas->GetDisplayArea().SetBottom(0);
461                                         m_2DCanvas->GetDisplayArea().SetRight(m_2DCanvas->GetWidth()/2);
462                                         m_2DCanvas->GetDisplayArea().SetTop(m_2DCanvas->GetHeight());
463                                         break;
464                                 case RAS_STEREO_RIGHTEYE:
465                                         // Right half of window
466                                         m_2DCanvas->GetDisplayArea().SetLeft(m_2DCanvas->GetWidth()/2);
467                                         m_2DCanvas->GetDisplayArea().SetBottom(0);
468                                         m_2DCanvas->GetDisplayArea().SetRight(m_2DCanvas->GetWidth());
469                                         m_2DCanvas->GetDisplayArea().SetTop(m_2DCanvas->GetHeight());
470                                         break;
471                         }
472                         break;
473                 default:
474                         // every available pixel
475                         m_2DCanvas->GetDisplayArea().SetLeft(0);
476                         m_2DCanvas->GetDisplayArea().SetBottom(0);
477                         m_2DCanvas->GetDisplayArea().SetRight(int(m_2DCanvas->GetWidth()));
478                         m_2DCanvas->GetDisplayArea().SetTop(int(m_2DCanvas->GetHeight()));
479                         break;
480         }
481 }
482
483         
484 void RAS_OpenGLRasterizer::SetStereoMode(const StereoMode stereomode)
485 {
486         m_stereomode = stereomode;
487 }
488
489
490
491 bool RAS_OpenGLRasterizer::Stereo()
492 {
493         if(m_stereomode == RAS_STEREO_NOSTEREO)
494                 return false;
495         else
496                 return true;
497 }
498
499
500 void RAS_OpenGLRasterizer::SetEye(StereoEye eye)
501 {
502         m_curreye = eye;
503         switch (m_stereomode)
504         {
505                 case RAS_STEREO_QUADBUFFERED:
506                         glDrawBuffer(m_curreye == RAS_STEREO_LEFTEYE ? GL_BACK_LEFT : GL_BACK_RIGHT);
507                         break;
508                 case RAS_STEREO_ANAGLYPH:
509                         if (m_curreye == RAS_STEREO_LEFTEYE)
510                         {
511                                 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_FALSE);
512                         } else {
513                                 //glAccum(GL_LOAD, 1.0);
514                                 glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
515                                 ClearDepthBuffer();
516                         }
517                         break;
518                 case RAS_STEREO_VINTERLACE:
519                 {
520                         glEnable(GL_POLYGON_STIPPLE);
521                         glPolygonStipple((const GLubyte*) ((m_curreye == RAS_STEREO_LEFTEYE) ? left_eye_vinterlace_mask : right_eye_vinterlace_mask));
522                         if (m_curreye == RAS_STEREO_RIGHTEYE)
523                                 ClearDepthBuffer();
524                         break;
525                 }
526                 case RAS_STEREO_INTERLACED:
527                 {
528                         glEnable(GL_POLYGON_STIPPLE);
529                         glPolygonStipple((const GLubyte*) &hinterlace_mask[m_curreye == RAS_STEREO_LEFTEYE?0:1]);
530                         if (m_curreye == RAS_STEREO_RIGHTEYE)
531                                 ClearDepthBuffer();
532                         break;
533                 }
534                 default:
535                         break;
536         }
537 }
538
539 RAS_IRasterizer::StereoEye RAS_OpenGLRasterizer::GetEye()
540 {
541         return m_curreye;
542 }
543
544
545 void RAS_OpenGLRasterizer::SetEyeSeparation(float eyeseparation)
546 {
547         m_eyeseparation = eyeseparation;
548         m_seteyesep = true;
549 }
550
551 float RAS_OpenGLRasterizer::GetEyeSeparation()
552 {
553         return m_eyeseparation;
554 }
555
556 void RAS_OpenGLRasterizer::SetFocalLength(float focallength)
557 {
558         m_focallength = focallength;
559         m_setfocallength = true;
560 }
561
562 float RAS_OpenGLRasterizer::GetFocalLength()
563 {
564         return m_focallength;
565 }
566
567
568 void RAS_OpenGLRasterizer::SwapBuffers()
569 {
570         m_2DCanvas->SwapBuffers();
571 }
572
573
574
575 void RAS_OpenGLRasterizer::IndexPrimitives(const vecVertexArray & vertexarrays,
576                                                                         const vecIndexArrays & indexarrays,
577                                                                         int mode,
578                                                                         class RAS_IPolyMaterial* polymat,
579                                                                         class RAS_IRenderTools* rendertools,
580                                                                         bool useObjectColor,
581                                                                         const MT_Vector4& rgbacolor
582                                                                         )
583
584         GLenum drawmode;
585         switch (mode)
586         {
587         case 0:
588                 drawmode = GL_TRIANGLES;
589                 break;
590         case 1:
591                 drawmode = GL_LINES;
592                 break;
593         case 2:
594                 drawmode = GL_QUADS;
595                 break;
596         default:
597                 drawmode = GL_LINES;
598                 break;
599         }
600         
601         const RAS_TexVert* vertexarray ;
602         unsigned int numindices,vt;
603
604         for (vt=0;vt<vertexarrays.size();vt++)
605         {
606                 vertexarray = &((*vertexarrays[vt]) [0]);
607                 const KX_IndexArray & indexarray = (*indexarrays[vt]);
608                 numindices = indexarray.size();
609                 
610                 if (!numindices)
611                         break;
612                 
613                 int vindex=0;
614                 switch (mode)
615                 {
616                 case 1:
617                         {
618                                 glBegin(GL_LINES);
619                                 vindex=0;
620                                 for (unsigned int i=0;i<numindices;i+=2)
621                                 {
622                                         glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
623                                         glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
624                                 }
625                                 glEnd();
626                         }
627                         break;
628                 case 2:
629                         {
630                                 glBegin(GL_QUADS);
631                                 vindex=0;
632                                 if (useObjectColor)
633                                 {
634                                         for (unsigned int i=0;i<numindices;i+=4)
635                                         {
636
637                                                 glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
638
639                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
640                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
641                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
642                                                 vindex++;
643                                                 
644                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
645                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
646                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
647                                                 vindex++;
648                                                 
649                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
650                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
651                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
652                                                 vindex++;
653                                                 
654                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
655                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
656                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
657                                                 vindex++;
658                                         }
659                                 }
660                                 else
661                                 {
662                                         for (unsigned int i=0;i<numindices;i+=4)
663                                         {
664                                                 // This looks curiously endian unsafe to me.
665                                                 // However it depends on the way the colors are packed into 
666                                                 // the m_rgba field of RAS_TexVert
667
668                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
669                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
670                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
671                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
672                                                 vindex++;
673                                                 
674                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
675                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
676                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
677                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
678                                                 vindex++;
679                                                 
680                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
681                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
682                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
683                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
684                                                 vindex++;
685                                                 
686                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
687                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
688                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
689                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
690                                                 vindex++;
691                                         }
692                                 }
693                                 glEnd();        
694                                 break;
695                         }
696                 case 0:
697                         {
698                                 glBegin(GL_TRIANGLES);
699                                 vindex=0;
700                                 if (useObjectColor)
701                                 {
702                                         for (unsigned int i=0;i<numindices;i+=3)
703                                         {
704
705                                                 glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
706
707                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
708                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
709                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
710                                                 vindex++;
711                                                 
712                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
713                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
714                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
715                                                 vindex++;
716                                                 
717                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
718                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
719                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
720                                                 vindex++;
721                                         }
722                                 }
723                                 else 
724                                 {
725                                         for (unsigned int i=0;i<numindices;i+=3)
726                                         {
727
728                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
729                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
730                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
731                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
732                                                 vindex++;
733                                                 
734                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
735                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
736                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
737                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
738                                                 vindex++;
739                                                 
740                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
741                                                 glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
742                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
743                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
744                                                 vindex++;
745                                         }
746                                 }
747                                 glEnd();        
748                                 break;
749                         }
750                 default:
751                         {
752                         }
753                         
754                 } // switch
755         } // for each vertexarray
756
757 }
758
759 void RAS_OpenGLRasterizer::IndexPrimitives_Ex(const vecVertexArray & vertexarrays,
760                                                                         const vecIndexArrays & indexarrays,
761                                                                         int mode,
762                                                                         class RAS_IPolyMaterial* polymat,
763                                                                         class RAS_IRenderTools* rendertools,
764                                                                         bool useObjectColor,
765                                                                         const MT_Vector4& rgbacolor
766                                                                         )
767
768         bool    recalc;
769         GLenum drawmode;
770         switch (mode)
771         {
772         case 0:
773                 drawmode = GL_TRIANGLES;
774                 break;
775         case 1:
776                 drawmode = GL_LINES;
777                 break;
778         case 2:
779                 drawmode = GL_QUADS;
780                 break;
781         default:
782                 drawmode = GL_LINES;
783                 break;
784         }
785         
786         const RAS_TexVert* vertexarray ;
787         unsigned int numindices,vt;
788
789         for (vt=0;vt<vertexarrays.size();vt++)
790         {
791                 vertexarray = &((*vertexarrays[vt]) [0]);
792                 const KX_IndexArray & indexarray = (*indexarrays[vt]);
793                 numindices = indexarray.size();
794                 
795                 if (!numindices)
796                         continue;
797                 
798                 int vindex=0;
799                 switch (mode)
800                 {
801                 case 1:
802                         {
803                                 glBegin(GL_LINES);
804                                 vindex=0;
805                                 for (unsigned int i=0;i<numindices;i+=2)
806                                 {
807                                         glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
808                                         glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
809                                 }
810                                 glEnd();
811                         }
812                         break;
813                 case 2:
814                         {
815                                 glBegin(GL_QUADS);
816                                 vindex=0;
817                                 if (useObjectColor)
818                                 {
819                                         for (unsigned int i=0;i<numindices;i+=4)
820                                         {
821                                                 MT_Point3 mv1, mv2, mv3, mv4, fnor;
822                                                 /* Calc a new face normal */
823
824                                                 if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL)
825                                                         recalc= true;
826                                                 else
827                                                         recalc=false;
828
829                                                 if (recalc){
830                                                         mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ());
831                                                         mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ());
832                                                         mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
833                                                         mv4 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
834                                                         
835                                                         fnor = (((mv2-mv1).cross(mv3-mv2))+((mv4-mv3).cross(mv1-mv4))).safe_normalized();
836
837                                                         glNormal3f(fnor[0], fnor[1], fnor[2]);
838                                                 }
839
840                                                 glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
841
842                                                 if (!recalc)
843                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
844                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
845                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
846                                                 vindex++;
847                                                 
848                                                 if (!recalc)
849                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
850                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
851                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
852                                                 vindex++;
853                                                 
854                                                 if (!recalc)
855                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());                                             
856                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
857                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
858                                                 vindex++;
859                                                 
860                                                 if (!recalc)
861                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
862                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
863                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
864                                                 vindex++;
865                                         }
866                                 }
867                                 else
868                                 {
869                                         for (unsigned int i=0;i<numindices;i+=4)
870                                         {
871                                                 // This looks curiously endian unsafe to me.
872                                                 // However it depends on the way the colors are packed into 
873                                                 // the m_rgba field of RAS_TexVert
874                                                 MT_Point3 mv1, mv2, mv3, mv4, fnor;
875                                                 /* Calc a new face normal */
876
877                                                 if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL)
878                                                         recalc= true;
879                                                 else
880                                                         recalc=false;
881
882
883                                                 if (recalc){
884                                                         mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ());
885                                                         mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ());
886                                                         mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
887                                                         mv4 = MT_Point3(vertexarray[(indexarray[vindex+3])].getLocalXYZ());
888                                                         
889                                                         fnor = (((mv2-mv1).cross(mv3-mv2))+((mv4-mv3).cross(mv1-mv4))).safe_normalized();
890
891                                                         glNormal3f(fnor[0], fnor[1], fnor[2]);
892                                                 }
893
894                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
895                                                 if (!recalc)
896                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
897                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
898                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
899                                                 vindex++;
900                                                 
901                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
902                                                 if (!recalc)
903                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
904                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
905                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
906                                                 vindex++;
907                                                 
908                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
909                                                 if (!recalc)
910                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
911                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
912                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
913                                                 vindex++;
914                                                 
915                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
916                                                 if (!recalc)
917                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
918                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
919                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
920                                                 vindex++;
921                                         }
922                                 }
923                                 glEnd();        
924                                 break;
925                         }
926                 case 0:
927                         {
928                                 glBegin(GL_TRIANGLES);
929                                 vindex=0;
930                                 if (useObjectColor)
931                                 {
932                                         for (unsigned int i=0;i<numindices;i+=3)
933                                         {
934                                                 MT_Point3 mv1, mv2, mv3, fnor;
935                                                 /* Calc a new face normal */
936
937                                                 if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL)
938                                                         recalc= true;
939                                                 else
940                                                         recalc=false;
941
942                                                 if (recalc){
943                                                         mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ());
944                                                         mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ());
945                                                         mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
946                                                         
947                                                         fnor = ((mv2-mv1).cross(mv3-mv2)).safe_normalized();
948                                                         glNormal3f(fnor[0], fnor[1], fnor[2]);
949                                                 }
950
951                                                 glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
952
953                                                 if (!recalc)
954                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
955                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
956                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
957                                                 vindex++;
958                                                 
959                                                 if (!recalc)
960                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
961                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
962                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
963                                                 vindex++;
964                                                 
965                                                 if (!recalc)
966                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
967                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
968                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
969                                                 vindex++;
970                                         }
971                                 }
972                                 else 
973                                 {
974                                         for (unsigned int i=0;i<numindices;i+=3)
975                                         {
976                                                 MT_Point3 mv1, mv2, mv3, fnor;
977                                                 /* Calc a new face normal */
978
979                                                 if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL)
980                                                         recalc= true;
981                                                 else
982                                                         recalc=false;
983
984
985                                                 if (recalc){
986                                                         mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ());
987                                                         mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ());
988                                                         mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ());
989                                                         
990                                                         fnor = ((mv2-mv1).cross(mv3-mv2)).safe_normalized();
991                                                         glNormal3f(fnor[0], fnor[1], fnor[2]);
992                                                 }
993
994                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
995                                                 if (!recalc)
996                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
997                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
998                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
999                                                 vindex++;
1000                                                 
1001                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1002                                                 if (!recalc)
1003                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1004                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
1005                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1006                                                 vindex++;
1007                                                 
1008                                                 glColor4ubv((const GLubyte *)(vertexarray[(indexarray[vindex])].getRGBA()));
1009                                                 if (!recalc)
1010                                                         glNormal3fv(vertexarray[(indexarray[vindex])].getNormal());
1011                                                 glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1());
1012                                                 glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ());
1013                                                 vindex++;
1014                                         }
1015                                 }
1016                                 glEnd();        
1017                                 break;
1018                         }
1019                 default:
1020                         {
1021                         }
1022                         
1023                 } // switch
1024         } // for each vertexarray
1025
1026 }
1027
1028
1029
1030 void RAS_OpenGLRasterizer::IndexPrimitives_3DText(const vecVertexArray & vertexarrays,
1031                                                                         const vecIndexArrays & indexarrays,
1032                                                                         int mode,
1033                                                                         class RAS_IPolyMaterial* polymat,
1034                                                                         class RAS_IRenderTools* rendertools,
1035                                                                         bool useObjectColor,
1036                                                                         const MT_Vector4& rgbacolor
1037                                                                         )
1038
1039         GLenum drawmode;
1040         switch (mode)
1041         {
1042         case 0:
1043                 drawmode = GL_TRIANGLES;
1044                 break;
1045         case 1:
1046                 drawmode = GL_LINES;
1047                 break;
1048         case 2:
1049                 drawmode = GL_QUADS;
1050                 break;
1051         default:
1052                 drawmode = GL_LINES;
1053                 break;
1054         }
1055         
1056         const RAS_TexVert* vertexarray ;
1057         
1058         unsigned int numindices, vt;
1059         
1060         if (useObjectColor)
1061         {
1062                 glDisableClientState(GL_COLOR_ARRAY);
1063                 glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]);
1064         }
1065         else
1066         {
1067                 glEnableClientState(GL_COLOR_ARRAY);
1068         }
1069         
1070         for (vt=0;vt<vertexarrays.size();vt++)
1071         {
1072                 vertexarray = &((*vertexarrays[vt]) [0]);
1073                 const KX_IndexArray & indexarray = (*indexarrays[vt]);
1074                 numindices = indexarray.size();
1075                 
1076                 if (!numindices)
1077                         break;
1078                 
1079                 int vindex=0;
1080                 switch (mode)
1081                 {
1082                 case 1:
1083                         {
1084                                 glBegin(GL_LINES);
1085                                 vindex=0;
1086                                 for (unsigned int i=0;i<numindices;i+=2)
1087                                 {
1088                                         glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
1089                                         glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ());
1090                                 }
1091                                 glEnd();
1092                         }
1093                         break;
1094                 case 2:
1095                         {
1096                                 vindex=0;
1097                                 for (unsigned int i=0;i<numindices;i+=4)
1098                                 {
1099                                         float v1[3],v2[3],v3[3],v4[3];
1100                                         
1101                                         v1[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0];
1102                                         v1[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1];
1103                                         v1[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2];
1104                                         vindex++;
1105                                         
1106                                         v2[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0];
1107                                         v2[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1];
1108                                         v2[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2];
1109                                         vindex++;
1110                                         
1111                                         v3[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0];
1112                                         v3[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1];
1113                                         v3[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2];
1114                                         vindex++;
1115                                         
1116                                         v4[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0];
1117                                         v4[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1];
1118                                         v4[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2];
1119                                         
1120                                         vindex++;
1121                                         
1122                                         rendertools->RenderText(polymat->GetDrawingMode(),polymat,v1,v2,v3,v4);
1123                                         ClearCachingInfo();
1124                                 }
1125                                 break;
1126                         }
1127                 case 0:
1128                         {
1129                                 glBegin(GL_TRIANGLES);
1130                                 vindex=0;
1131                                 for (unsigned int i=0;i<numindices;i+=3)
1132                                 {
1133                                         float v1[3],v2[3],v3[3];
1134                                         
1135                                         v1[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0];
1136                                         v1[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1];
1137                                         v1[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2];
1138                                         vindex++;
1139                                         
1140                                         v2[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0];
1141                                         v2[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1];
1142                                         v2[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2];
1143                                         vindex++;
1144                                         
1145                                         v3[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0];
1146                                         v3[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1];
1147                                         v3[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2];
1148                                         vindex++;
1149                                         
1150                                         rendertools->RenderText(polymat->GetDrawingMode(),polymat,v1,v2,v3,NULL);               
1151                                         ClearCachingInfo();
1152                                 }
1153                                 glEnd();        
1154                                 break;
1155                         }
1156                 default:
1157                         {
1158                         }
1159                 }       //switch
1160         }       //for each vertexarray
1161 }
1162
1163
1164
1165 void RAS_OpenGLRasterizer::SetProjectionMatrix(MT_CmMatrix4x4 &mat)
1166 {
1167         glMatrixMode(GL_PROJECTION);
1168         double* matrix = &mat(0,0);
1169         glLoadMatrixd(matrix);
1170 }
1171
1172
1173 void RAS_OpenGLRasterizer::SetProjectionMatrix(const MT_Matrix4x4 & mat)
1174 {
1175         glMatrixMode(GL_PROJECTION);
1176         double matrix[16];
1177         /* Get into argument. Looks a bit dodgy, but it's ok. */
1178         mat.getValue(matrix);
1179         /* Internally, MT_Matrix4x4 uses doubles (MT_Scalar). */
1180         glLoadMatrixd(matrix);  
1181 }
1182
1183 MT_Matrix4x4 RAS_OpenGLRasterizer::GetFrustumMatrix(
1184         float left,
1185         float right,
1186         float bottom,
1187         float top,
1188         float frustnear,
1189         float frustfar,
1190         bool
1191 ){
1192         MT_Matrix4x4 result;
1193         double mat[16];
1194
1195         // correction for stereo
1196         if(m_stereomode != RAS_STEREO_NOSTEREO)
1197         {
1198                         float near_div_focallength;
1199                         // next 2 params should be specified on command line and in Blender publisher
1200                         if (!m_setfocallength)
1201                                 m_focallength = 1.5 * right;  // derived from example
1202                         if (!m_seteyesep)
1203                                 m_eyeseparation = 0.18 * right;  // just a guess...
1204
1205                         near_div_focallength = frustnear / m_focallength;
1206                         switch(m_curreye)
1207                         {
1208                                 case RAS_STEREO_LEFTEYE:
1209                                                 left += 0.5 * m_eyeseparation * near_div_focallength;
1210                                                 right += 0.5 * m_eyeseparation * near_div_focallength;
1211                                                 break;
1212                                 case RAS_STEREO_RIGHTEYE:
1213                                                 left -= 0.5 * m_eyeseparation * near_div_focallength;
1214                                                 right -= 0.5 * m_eyeseparation * near_div_focallength;
1215                                                 break;
1216                         }
1217                         // leave bottom, top, bottom and top untouched
1218         }
1219         
1220         glMatrixMode(GL_PROJECTION);
1221         glLoadIdentity();
1222         glFrustum(left, right, bottom, top, frustnear, frustfar);
1223                 
1224         glGetDoublev(GL_PROJECTION_MATRIX, mat);
1225         result.setValue(mat);
1226
1227         return result;
1228 }
1229
1230
1231 // next arguments probably contain redundant info, for later...
1232 void RAS_OpenGLRasterizer::SetViewMatrix(const MT_Matrix4x4 &mat, const MT_Vector3& campos,
1233                 const MT_Point3 &, const MT_Quaternion &camOrientQuat)
1234 {
1235         MT_Matrix4x4 viewMat = mat;
1236
1237         // correction for stereo
1238         if(m_stereomode != RAS_STEREO_NOSTEREO)
1239         {
1240                 MT_Matrix3x3 camOrientMat3x3(camOrientQuat);
1241                 MT_Vector3 unitViewDir(0.0, -1.0, 0.0);  // minus y direction, Blender convention
1242                 MT_Vector3 unitViewupVec(0.0, 0.0, 1.0);
1243                 MT_Vector3 viewDir, viewupVec;
1244                 MT_Vector3 eyeline;
1245
1246                 // actual viewDir
1247                 viewDir = camOrientMat3x3 * unitViewDir;  // this is the moto convention, vector on right hand side
1248                 // actual viewup vec
1249                 viewupVec = camOrientMat3x3 * unitViewupVec;
1250
1251                 // vector between eyes
1252                 eyeline = viewDir.cross(viewupVec);
1253
1254                 switch(m_curreye)
1255                 {
1256                         case RAS_STEREO_LEFTEYE:
1257                                 {
1258                                 // translate to left by half the eye distance
1259                                 MT_Transform transform;
1260                                 transform.setIdentity();
1261                                 transform.translate(-(eyeline * m_eyeseparation / 2.0));
1262                                 viewMat *= transform;
1263                                 }
1264                                 break;
1265                         case RAS_STEREO_RIGHTEYE:
1266                                 {
1267                                 // translate to right by half the eye distance
1268                                 MT_Transform transform;
1269                                 transform.setIdentity();
1270                                 transform.translate(eyeline * m_eyeseparation / 2.0);
1271                                 viewMat *= transform;
1272                                 }
1273                                 break;
1274                 }
1275         }
1276
1277         // convert row major matrix 'viewMat' to column major for OpenGL
1278         MT_Scalar cammat[16];
1279         viewMat.getValue(cammat);
1280         MT_CmMatrix4x4 viewCmmat = cammat;
1281
1282         glMatrixMode(GL_MODELVIEW);
1283         m_viewmatrix = viewCmmat;
1284         glLoadMatrixd(&m_viewmatrix(0,0));
1285         m_campos = campos;
1286 }
1287
1288
1289 const MT_Point3& RAS_OpenGLRasterizer::GetCameraPosition()
1290 {
1291         return m_campos;
1292 }
1293
1294
1295
1296 void RAS_OpenGLRasterizer::LoadViewMatrix()
1297 {
1298         glLoadMatrixd(&m_viewmatrix(0,0));
1299 }
1300
1301
1302
1303 void RAS_OpenGLRasterizer::EnableTextures(bool enable)
1304 {
1305 }
1306
1307
1308
1309 void RAS_OpenGLRasterizer::SetCullFace(bool enable)
1310 {
1311         if (enable)
1312                 glEnable(GL_CULL_FACE);
1313         else
1314                 glDisable(GL_CULL_FACE);
1315 }
1316
1317 void RAS_OpenGLRasterizer::SetLines(bool enable)
1318 {
1319         if (enable)
1320                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
1321         else
1322                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1323 }
1324
1325 void RAS_OpenGLRasterizer::SetSpecularity(float specX,
1326                                                                                   float specY,
1327                                                                                   float specZ,
1328                                                                                   float specval)
1329 {
1330         GLfloat mat_specular[] = {specX, specY, specZ, specval};
1331         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
1332 }
1333
1334
1335
1336 void RAS_OpenGLRasterizer::SetShinyness(float shiny)
1337 {
1338         GLfloat mat_shininess[] = {     shiny };
1339         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
1340 }
1341
1342
1343
1344 void RAS_OpenGLRasterizer::SetDiffuse(float difX,float difY,float difZ,float diffuse)
1345 {
1346         GLfloat mat_diffuse [] = {difX, difY,difZ, diffuse};
1347         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
1348 }
1349
1350 double RAS_OpenGLRasterizer::GetTime()
1351 {
1352         return m_time;
1353 }
1354
1355 void RAS_OpenGLRasterizer::SetPolygonOffset(float mult, float add)
1356 {
1357         glPolygonOffset(mult, add);
1358         GLint mode = GL_POLYGON_OFFSET_FILL;
1359         if (m_drawingmode < KX_SHADED)
1360                 mode = GL_POLYGON_OFFSET_LINE;
1361         if (mult != 0.0f || add != 0.0f)
1362                 glEnable(mode);
1363         else
1364                 glDisable(mode);
1365 }