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