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