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