cleanup: spelling, comments, alignment
[blender.git] / source / gameengine / Rasterizer / RAS_OpenGLRasterizer / RAS_OpenGLRasterizer.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp
29  *  \ingroup bgerastogl
30  */
31
32  
33 #include <math.h>
34 #include <stdlib.h>
35  
36 #include "RAS_OpenGLRasterizer.h"
37
38 #include "glew-mx.h"
39
40 #include "RAS_ICanvas.h"
41 #include "RAS_Rect.h"
42 #include "RAS_TexVert.h"
43 #include "RAS_MeshObject.h"
44 #include "RAS_Polygon.h"
45 #include "RAS_ILightObject.h"
46 #include "MT_CmMatrix4x4.h"
47
48 #include "RAS_OpenGLLight.h"
49
50 #include "RAS_StorageVA.h"
51 #include "RAS_StorageVBO.h"
52
53 #include "GPU_draw.h"
54 #include "GPU_extensions.h"
55 #include "GPU_material.h"
56 #include "GPU_shader.h"
57
58 extern "C"{
59         #include "BLF_api.h"
60         #include "BKE_DerivedMesh.h"
61 }
62
63
64 // XXX Clean these up <<<
65 #include "EXP_Value.h"
66 #include "KX_Scene.h"
67 #include "KX_RayCast.h"
68 #include "KX_GameObject.h"
69 // >>>
70
71 #ifndef M_PI
72 #define M_PI            3.14159265358979323846
73 #endif
74
75 /**
76  *  32x32 bit masks for vinterlace stereo mode
77  */
78 static GLuint left_eye_vinterlace_mask[32];
79 static GLuint right_eye_vinterlace_mask[32];
80
81 /**
82  *  32x32 bit masks for hinterlace stereo mode.
83  *  Left eye = &hinterlace_mask[0]
84  *  Right eye = &hinterlace_mask[1]
85  */
86 static GLuint hinterlace_mask[33];
87
88 RAS_OpenGLRasterizer::RAS_OpenGLRasterizer(RAS_ICanvas* canvas, RAS_STORAGE_TYPE storage)
89         :RAS_IRasterizer(canvas),
90         m_2DCanvas(canvas),
91         m_fogenabled(false),
92         m_time(0.0f),
93         m_campos(0.0f, 0.0f, 0.0f),
94         m_camortho(false),
95         m_stereomode(RAS_STEREO_NOSTEREO),
96         m_curreye(RAS_STEREO_LEFTEYE),
97         m_eyeseparation(0.0f),
98         m_focallength(0.0f),
99         m_setfocallength(false),
100         m_noOfScanlines(32),
101         m_motionblur(0),
102         m_motionblurvalue(-1.0f),
103         m_usingoverrideshader(false),
104         m_clientobject(NULL),
105         m_auxilaryClientInfo(NULL),
106         m_drawingmode(KX_TEXTURED),
107         m_texco_num(0),
108         m_attrib_num(0),
109         //m_last_alphablend(GPU_BLEND_SOLID),
110         m_last_frontface(true),
111         m_materialCachingInfo(0),
112         m_storage_type(storage)
113 {
114         m_viewmatrix.setIdentity();
115         m_viewinvmatrix.setIdentity();
116         
117         for (int i = 0; i < 32; i++)
118         {
119                 left_eye_vinterlace_mask[i] = 0x55555555;
120                 right_eye_vinterlace_mask[i] = 0xAAAAAAAA;
121                 hinterlace_mask[i] = (i&1)*0xFFFFFFFF;
122         }
123         hinterlace_mask[32] = 0;
124
125         m_prevafvalue = GPU_get_anisotropic();
126
127         if (m_storage_type == RAS_VBO /*|| m_storage_type == RAS_AUTO_STORAGE && GLEW_ARB_vertex_buffer_object*/) {
128                 m_storage = new RAS_StorageVBO(&m_texco_num, m_texco, &m_attrib_num, m_attrib, m_attrib_layer);
129         }
130         else if ((m_storage_type == RAS_VA) || (m_storage_type == RAS_AUTO_STORAGE)) {
131                 m_storage = new RAS_StorageVA(&m_texco_num, m_texco, &m_attrib_num, m_attrib, m_attrib_layer);
132         }
133         else {
134                 printf("Unknown rasterizer storage type, falling back to vertex arrays\n");
135                 m_storage = new RAS_StorageVA(&m_texco_num, m_texco, &m_attrib_num, m_attrib, m_attrib_layer);
136         }
137
138         glGetIntegerv(GL_MAX_LIGHTS, (GLint *) &m_numgllights);
139         if (m_numgllights < 8)
140                 m_numgllights = 8;
141
142         PrintHardwareInfo();
143 }
144
145
146
147 RAS_OpenGLRasterizer::~RAS_OpenGLRasterizer()
148 {
149         // Restore the previous AF value
150         GPU_set_anisotropic(m_prevafvalue);
151
152         if (m_storage)
153                 delete m_storage;
154 }
155
156 bool RAS_OpenGLRasterizer::Init()
157 {
158         bool storage_init;
159         GPU_state_init();
160
161
162         m_ambr = 0.0f;
163         m_ambg = 0.0f;
164         m_ambb = 0.0f;
165
166         glDisable(GL_BLEND);
167         glDisable(GL_ALPHA_TEST);
168         //m_last_alphablend = GPU_BLEND_SOLID;
169         GPU_set_material_alpha_blend(GPU_BLEND_SOLID);
170
171         glFrontFace(GL_CCW);
172         m_last_frontface = true;
173
174         m_redback = 0.4375f;
175         m_greenback = 0.4375f;
176         m_blueback = 0.4375f;
177         m_alphaback = 0.0f;
178
179         glClearColor(m_redback,m_greenback,m_blueback,m_alphaback);
180         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
181         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
182
183
184         glShadeModel(GL_SMOOTH);
185
186         storage_init = m_storage->Init();
187
188         return true && storage_init;
189 }
190
191
192 void RAS_OpenGLRasterizer::SetAmbientColor(float color[3])
193 {
194         m_ambr = color[0];
195         m_ambg = color[1];
196         m_ambb = color[2];
197 }
198
199 void RAS_OpenGLRasterizer::SetAmbient(float factor)
200 {
201         float ambient[] = {m_ambr * factor, m_ambg * factor, m_ambb * factor, 1.0f};
202         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
203 }
204
205 void RAS_OpenGLRasterizer::SetBackColor(float color[3])
206 {
207         m_redback = color[0];
208         m_greenback = color[1];
209         m_blueback = color[2];
210         m_alphaback = 1.0f;
211 }
212
213 void RAS_OpenGLRasterizer::SetFog(short type, float start, float dist, float intensity, float color[3])
214 {
215         float params[4] = {color[0], color[1], color[2], 1.0f};
216         glFogi(GL_FOG_MODE, GL_LINEAR);
217         glFogf(GL_FOG_DENSITY, intensity / 10.0f);
218         glFogf(GL_FOG_START, start);
219         glFogf(GL_FOG_END, start + dist);
220         glFogfv(GL_FOG_COLOR, params);
221 }
222
223 void RAS_OpenGLRasterizer::EnableFog(bool enable)
224 {
225         m_fogenabled = enable;
226 }
227
228 void RAS_OpenGLRasterizer::DisplayFog()
229 {
230         if ((m_drawingmode >= KX_SOLID) && m_fogenabled) {
231                 glEnable(GL_FOG);
232         }
233         else {
234                 glDisable(GL_FOG);
235         }
236 }
237
238 bool RAS_OpenGLRasterizer::SetMaterial(const RAS_IPolyMaterial& mat)
239 {
240         return mat.Activate(this, m_materialCachingInfo);
241 }
242
243
244
245 void RAS_OpenGLRasterizer::Exit()
246 {
247         m_storage->Exit();
248
249         glEnable(GL_CULL_FACE);
250         glEnable(GL_DEPTH_TEST);
251         glClearDepth(1.0f);
252         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
253         glClearColor(m_redback, m_greenback, m_blueback, m_alphaback);
254         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
255         glDepthMask (GL_TRUE);
256         glDepthFunc(GL_LEQUAL);
257         glBlendFunc(GL_ONE, GL_ZERO);
258         
259         glDisable(GL_POLYGON_STIPPLE);
260         
261         glDisable(GL_LIGHTING);
262         if (GLEW_EXT_separate_specular_color || GLEW_VERSION_1_2)
263                 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
264         
265         EndFrame();
266 }
267
268 bool RAS_OpenGLRasterizer::BeginFrame(double time)
269 {
270         m_time = time;
271
272         // Blender camera routine destroys the settings
273         if (m_drawingmode < KX_SOLID)
274         {
275                 glDisable(GL_CULL_FACE);
276                 glDisable(GL_DEPTH_TEST);
277         }
278         else
279         {
280                 glEnable(GL_DEPTH_TEST);
281                 glEnable(GL_CULL_FACE);
282         }
283
284         glDisable(GL_BLEND);
285         glDisable(GL_ALPHA_TEST);
286         //m_last_alphablend = GPU_BLEND_SOLID;
287         GPU_set_material_alpha_blend(GPU_BLEND_SOLID);
288
289         glFrontFace(GL_CCW);
290         m_last_frontface = true;
291
292         glShadeModel(GL_SMOOTH);
293
294         glEnable(GL_MULTISAMPLE_ARB);
295
296         m_2DCanvas->BeginFrame();
297
298         // Render Tools
299         m_clientobject = NULL;
300         m_lastlightlayer = -1;
301         m_lastauxinfo = NULL;
302         m_lastlighting = true; /* force disable in DisableOpenGLLights() */
303         DisableOpenGLLights();
304         
305         return true;
306 }
307
308
309
310 void RAS_OpenGLRasterizer::SetDrawingMode(int drawingmode)
311 {
312         m_drawingmode = drawingmode;
313
314         if (m_drawingmode == KX_WIREFRAME)
315                 glDisable(GL_CULL_FACE);
316
317         m_storage->SetDrawingMode(drawingmode);
318 }
319
320 int RAS_OpenGLRasterizer::GetDrawingMode()
321 {
322         return m_drawingmode;
323 }
324
325
326 void RAS_OpenGLRasterizer::SetDepthMask(DepthMask depthmask)
327 {
328         glDepthMask(depthmask == KX_DEPTHMASK_DISABLED ? GL_FALSE : GL_TRUE);
329 }
330
331
332 void RAS_OpenGLRasterizer::ClearColorBuffer()
333 {
334         m_2DCanvas->ClearColor(m_redback,m_greenback,m_blueback,m_alphaback);
335         m_2DCanvas->ClearBuffer(RAS_ICanvas::COLOR_BUFFER);
336 }
337
338
339 void RAS_OpenGLRasterizer::ClearDepthBuffer()
340 {
341         m_2DCanvas->ClearBuffer(RAS_ICanvas::DEPTH_BUFFER);
342 }
343
344
345 void RAS_OpenGLRasterizer::ClearCachingInfo(void)
346 {
347         m_materialCachingInfo = 0;
348 }
349
350 void RAS_OpenGLRasterizer::FlushDebugShapes(SCA_IScene *scene)
351 {
352         std::vector<OglDebugShape> &debugShapes = m_debugShapes[scene];
353         if (debugShapes.empty())
354                 return;
355
356         // DrawDebugLines
357         GLboolean light, tex;
358
359         light= glIsEnabled(GL_LIGHTING);
360         tex= glIsEnabled(GL_TEXTURE_2D);
361
362         if (light) glDisable(GL_LIGHTING);
363         if (tex) glDisable(GL_TEXTURE_2D);
364
365         // draw lines
366         glBegin(GL_LINES);
367         for (unsigned int i = 0; i < debugShapes.size(); i++) {
368                 if (debugShapes[i].m_type != OglDebugShape::LINE)
369                         continue;
370                 glColor4f(debugShapes[i].m_color[0], debugShapes[i].m_color[1], debugShapes[i].m_color[2], 1.0f);
371                 const MT_Scalar *fromPtr = &debugShapes[i].m_pos.x();
372                 const MT_Scalar *toPtr= &debugShapes[i].m_param.x();
373                 glVertex3fv(fromPtr);
374                 glVertex3fv(toPtr);
375         }
376         glEnd();
377
378         // draw circles
379         for (unsigned int i = 0; i < debugShapes.size(); i++) {
380                 if (debugShapes[i].m_type != OglDebugShape::CIRCLE)
381                         continue;
382                 glBegin(GL_LINE_LOOP);
383                 glColor4f(debugShapes[i].m_color[0], debugShapes[i].m_color[1], debugShapes[i].m_color[2], 1.0f);
384
385                 static const MT_Vector3 worldUp(0.0f, 0.0f, 1.0f);
386                 MT_Vector3 norm = debugShapes[i].m_param;
387                 MT_Matrix3x3 tr;
388                 if (norm.fuzzyZero() || norm == worldUp)
389                 {
390                         tr.setIdentity();
391                 }
392                 else
393                 {
394                         MT_Vector3 xaxis, yaxis;
395                         xaxis = MT_cross(norm, worldUp);
396                         yaxis = MT_cross(xaxis, norm);
397                         tr.setValue(xaxis.x(), xaxis.y(), xaxis.z(),
398                                 yaxis.x(), yaxis.y(), yaxis.z(),
399                                 norm.x(), norm.y(), norm.z());
400                 }
401                 MT_Scalar rad = debugShapes[i].m_param2.x();
402                 int n = (int)debugShapes[i].m_param2.y();
403                 for (int j = 0; j<n; j++)
404                 {
405                         MT_Scalar theta = j*(float)M_PI*2/n;
406                         MT_Vector3 pos(cosf(theta) * rad, sinf(theta) * rad, 0.0f);
407                         pos = pos*tr;
408                         pos += debugShapes[i].m_pos;
409                         const MT_Scalar* posPtr = &pos.x();
410                         glVertex3fv(posPtr);
411                 }
412                 glEnd();
413         }
414
415         if (light) glEnable(GL_LIGHTING);
416         if (tex) glEnable(GL_TEXTURE_2D);
417
418         debugShapes.clear();
419 }
420
421 void RAS_OpenGLRasterizer::EndFrame()
422 {
423         glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
424
425         glDisable(GL_MULTISAMPLE_ARB);
426
427         m_2DCanvas->EndFrame();
428 }
429
430 void RAS_OpenGLRasterizer::SetRenderArea()
431 {
432         RAS_Rect area;
433         // only above/below stereo method needs viewport adjustment
434         switch (m_stereomode)
435         {
436                 case RAS_STEREO_ABOVEBELOW:
437                         switch (m_curreye) {
438                                 case RAS_STEREO_LEFTEYE:
439                                         // upper half of window
440                                         area.SetLeft(0);
441                                         area.SetBottom(m_2DCanvas->GetHeight() -
442                                                 int(m_2DCanvas->GetHeight() - m_noOfScanlines) / 2);
443         
444                                         area.SetRight(int(m_2DCanvas->GetWidth()));
445                                         area.SetTop(int(m_2DCanvas->GetHeight()));
446                                         m_2DCanvas->SetDisplayArea(&area);
447                                         break;
448                                 case RAS_STEREO_RIGHTEYE:
449                                         // lower half of window
450                                         area.SetLeft(0);
451                                         area.SetBottom(0);
452                                         area.SetRight(int(m_2DCanvas->GetWidth()));
453                                         area.SetTop(int(m_2DCanvas->GetHeight() - m_noOfScanlines) / 2);
454                                         m_2DCanvas->SetDisplayArea(&area);
455                                         break;
456                         }
457                         break;
458                 case RAS_STEREO_3DTVTOPBOTTOM:
459                         switch (m_curreye) {
460                                 case RAS_STEREO_LEFTEYE:
461                                         // upper half of window
462                                         area.SetLeft(0);
463                                         area.SetBottom(m_2DCanvas->GetHeight() -
464                                                 m_2DCanvas->GetHeight() / 2);
465         
466                                         area.SetRight(m_2DCanvas->GetWidth());
467                                         area.SetTop(m_2DCanvas->GetHeight());
468                                         m_2DCanvas->SetDisplayArea(&area);
469                                         break;
470                                 case RAS_STEREO_RIGHTEYE:
471                                         // lower half of window
472                                         area.SetLeft(0);
473                                         area.SetBottom(0);
474                                         area.SetRight(m_2DCanvas->GetWidth());
475                                         area.SetTop(m_2DCanvas->GetHeight() / 2);
476                                         m_2DCanvas->SetDisplayArea(&area);
477                                         break;
478                         }
479                         break;
480                 case RAS_STEREO_SIDEBYSIDE:
481                         switch (m_curreye)
482                         {
483                                 case RAS_STEREO_LEFTEYE:
484                                         // Left half of window
485                                         area.SetLeft(0);
486                                         area.SetBottom(0);
487                                         area.SetRight(m_2DCanvas->GetWidth()/2);
488                                         area.SetTop(m_2DCanvas->GetHeight());
489                                         m_2DCanvas->SetDisplayArea(&area);
490                                         break;
491                                 case RAS_STEREO_RIGHTEYE:
492                                         // Right half of window
493                                         area.SetLeft(m_2DCanvas->GetWidth()/2);
494                                         area.SetBottom(0);
495                                         area.SetRight(m_2DCanvas->GetWidth());
496                                         area.SetTop(m_2DCanvas->GetHeight());
497                                         m_2DCanvas->SetDisplayArea(&area);
498                                         break;
499                         }
500                         break;
501                 default:
502                         // every available pixel
503                         area.SetLeft(0);
504                         area.SetBottom(0);
505                         area.SetRight(int(m_2DCanvas->GetWidth()));
506                         area.SetTop(int(m_2DCanvas->GetHeight()));
507                         m_2DCanvas->SetDisplayArea(&area);
508                         break;
509         }
510 }
511         
512 void RAS_OpenGLRasterizer::SetStereoMode(const StereoMode stereomode)
513 {
514         m_stereomode = stereomode;
515 }
516
517 RAS_IRasterizer::StereoMode RAS_OpenGLRasterizer::GetStereoMode()
518 {
519         return m_stereomode;
520 }
521
522 bool RAS_OpenGLRasterizer::Stereo()
523 {
524         if (m_stereomode > RAS_STEREO_NOSTEREO) // > 0
525                 return true;
526         else
527                 return false;
528 }
529
530 bool RAS_OpenGLRasterizer::InterlacedStereo()
531 {
532         return m_stereomode == RAS_STEREO_VINTERLACE || m_stereomode == RAS_STEREO_INTERLACED;
533 }
534
535 void RAS_OpenGLRasterizer::SetEye(const StereoEye eye)
536 {
537         m_curreye = eye;
538         switch (m_stereomode)
539         {
540                 case RAS_STEREO_QUADBUFFERED:
541                         glDrawBuffer(m_curreye == RAS_STEREO_LEFTEYE ? GL_BACK_LEFT : GL_BACK_RIGHT);
542                         break;
543                 case RAS_STEREO_ANAGLYPH:
544                         if (m_curreye == RAS_STEREO_LEFTEYE) {
545                                 glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
546                         }
547                         else {
548                                 //glAccum(GL_LOAD, 1.0f);
549                                 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_FALSE);
550                                 ClearDepthBuffer();
551                         }
552                         break;
553                 case RAS_STEREO_VINTERLACE:
554                 {
555                         // OpenGL stippling is deprecated, it is no longer possible to affect all shaders
556                         // this way, offscreen rendering and then compositing may be the better solution
557                         glEnable(GL_POLYGON_STIPPLE);
558                         glPolygonStipple((const GLubyte*) ((m_curreye == RAS_STEREO_LEFTEYE) ? left_eye_vinterlace_mask : right_eye_vinterlace_mask));
559                         if (m_curreye == RAS_STEREO_RIGHTEYE)
560                                 ClearDepthBuffer();
561                         break;
562                 }
563                 case RAS_STEREO_INTERLACED:
564                 {
565                         glEnable(GL_POLYGON_STIPPLE);
566                         glPolygonStipple((const GLubyte*) &hinterlace_mask[m_curreye == RAS_STEREO_LEFTEYE?0:1]);
567                         if (m_curreye == RAS_STEREO_RIGHTEYE)
568                                 ClearDepthBuffer();
569                         break;
570                 }
571                 default:
572                         break;
573         }
574 }
575
576 RAS_IRasterizer::StereoEye RAS_OpenGLRasterizer::GetEye()
577 {
578         return m_curreye;
579 }
580
581
582 void RAS_OpenGLRasterizer::SetEyeSeparation(const float eyeseparation)
583 {
584         m_eyeseparation = eyeseparation;
585 }
586
587 float RAS_OpenGLRasterizer::GetEyeSeparation()
588 {
589         return m_eyeseparation;
590 }
591
592 void RAS_OpenGLRasterizer::SetFocalLength(const float focallength)
593 {
594         m_focallength = focallength;
595         m_setfocallength = true;
596 }
597
598 float RAS_OpenGLRasterizer::GetFocalLength()
599 {
600         return m_focallength;
601 }
602
603
604 void RAS_OpenGLRasterizer::SwapBuffers()
605 {
606         m_2DCanvas->SwapBuffers();
607 }
608
609
610
611 const MT_Matrix4x4& RAS_OpenGLRasterizer::GetViewMatrix() const
612 {
613         return m_viewmatrix;
614 }
615
616 const MT_Matrix4x4& RAS_OpenGLRasterizer::GetViewInvMatrix() const
617 {
618         return m_viewinvmatrix;
619 }
620
621 void RAS_OpenGLRasterizer::IndexPrimitives_3DText(RAS_MeshSlot& ms,
622                                                                         class RAS_IPolyMaterial* polymat)
623
624         bool obcolor = ms.m_bObjectColor;
625         MT_Vector4& rgba = ms.m_RGBAcolor;
626         RAS_MeshSlot::iterator it;
627
628         const STR_String& mytext = ((CValue*)m_clientobject)->GetPropertyText("Text");
629
630         // handle object color
631         if (obcolor) {
632                 glDisableClientState(GL_COLOR_ARRAY);
633                 glColor4d(rgba[0], rgba[1], rgba[2], rgba[3]);
634         }
635         else
636                 glEnableClientState(GL_COLOR_ARRAY);
637
638         for (ms.begin(it); !ms.end(it); ms.next(it)) {
639                 RAS_TexVert *vertex;
640                 size_t i, j, numvert;
641                 
642                 numvert = it.array->m_type;
643
644                 if (it.array->m_type == RAS_DisplayArray::LINE) {
645                         // line drawing, no text
646                         glBegin(GL_LINES);
647
648                         for (i=0; i<it.totindex; i+=2)
649                         {
650                                 vertex = &it.vertex[it.index[i]];
651                                 glVertex3fv(vertex->getXYZ());
652
653                                 vertex = &it.vertex[it.index[i+1]];
654                                 glVertex3fv(vertex->getXYZ());
655                         }
656
657                         glEnd();
658                 }
659                 else {
660                         // triangle and quad text drawing
661                         for (i=0; i<it.totindex; i+=numvert)
662                         {
663                                 float  v[4][3];
664                                 const float  *v_ptr[4] = {NULL};
665                                 const float *uv_ptr[4] = {NULL};
666                                 int glattrib, unit;
667
668                                 for (j=0; j<numvert; j++) {
669                                         vertex = &it.vertex[it.index[i+j]];
670
671                                         v[j][0] = vertex->getXYZ()[0];
672                                         v[j][1] = vertex->getXYZ()[1];
673                                         v[j][2] = vertex->getXYZ()[2];
674                                         v_ptr[j] = v[j];
675
676                                         uv_ptr[j] = vertex->getUV(0);
677                                 }
678
679                                 // find the right opengl attribute
680                                 glattrib = -1;
681                                 if (GLEW_ARB_vertex_program)
682                                         for (unit=0; unit<m_attrib_num; unit++)
683                                                 if (m_attrib[unit] == RAS_TEXCO_UV)
684                                                         glattrib = unit;
685
686                                 GPU_render_text(
687                                         polymat->GetMTexPoly(), polymat->GetDrawingMode(), mytext, mytext.Length(), polymat->GetMCol(),
688                                         v_ptr, uv_ptr, glattrib);
689
690                                 ClearCachingInfo();
691                         }
692                 }
693         }
694
695         glDisableClientState(GL_COLOR_ARRAY);
696 }
697
698 void RAS_OpenGLRasterizer::SetTexCoordNum(int num)
699 {
700         m_texco_num = num;
701         if (m_texco_num > RAS_MAX_TEXCO)
702                 m_texco_num = RAS_MAX_TEXCO;
703 }
704
705 void RAS_OpenGLRasterizer::SetAttribNum(int num)
706 {
707         m_attrib_num = num;
708         if (m_attrib_num > RAS_MAX_ATTRIB)
709                 m_attrib_num = RAS_MAX_ATTRIB;
710 }
711
712 void RAS_OpenGLRasterizer::SetTexCoord(TexCoGen coords, int unit)
713 {
714         // this changes from material to material
715         if (unit < RAS_MAX_TEXCO)
716                 m_texco[unit] = coords;
717 }
718
719 void RAS_OpenGLRasterizer::SetAttrib(TexCoGen coords, int unit, int layer)
720 {
721         // this changes from material to material
722         if (unit < RAS_MAX_ATTRIB) {
723                 m_attrib[unit] = coords;
724                 m_attrib_layer[unit] = layer;
725         }
726 }
727
728 void RAS_OpenGLRasterizer::IndexPrimitives(RAS_MeshSlot& ms)
729 {
730         if (ms.m_pDerivedMesh)
731                 DrawDerivedMesh(ms);
732         else
733                 m_storage->IndexPrimitives(ms);
734 }
735
736 // Code for hooking into Blender's mesh drawing for derived meshes.
737 // If/when we use more of Blender's drawing code, we may be able to
738 // clean this up
739 static bool current_wireframe;
740 static RAS_MaterialBucket *current_bucket;
741 static RAS_IPolyMaterial *current_polymat;
742 static RAS_MeshSlot *current_ms;
743 static RAS_MeshObject *current_mesh;
744 static int current_blmat_nr;
745 static GPUVertexAttribs current_gpu_attribs;
746 static Image *current_image;
747 static int CheckMaterialDM(int matnr, void *attribs)
748 {
749         // only draw the current material
750         if (matnr != current_blmat_nr)
751                 return 0;
752         GPUVertexAttribs *gattribs = (GPUVertexAttribs *)attribs;
753         if (gattribs)
754                 memcpy(gattribs, &current_gpu_attribs, sizeof(GPUVertexAttribs));
755         return 1;
756 }
757
758 static DMDrawOption CheckTexDM(MTexPoly *mtexpoly, const bool has_mcol, int matnr)
759 {
760
761         // index is the original face index, retrieve the polygon
762         if (matnr == current_blmat_nr &&
763                 (mtexpoly == NULL || mtexpoly->tpage == current_image)) {
764                 // must handle color.
765                 if (current_wireframe)
766                         return DM_DRAW_OPTION_NO_MCOL;
767                 if (current_ms->m_bObjectColor) {
768                         MT_Vector4& rgba = current_ms->m_RGBAcolor;
769                         glColor4d(rgba[0], rgba[1], rgba[2], rgba[3]);
770                         // don't use mcol
771                         return DM_DRAW_OPTION_NO_MCOL;
772                 }
773                 if (!has_mcol) {
774                         // we have to set the color from the material
775                         unsigned char rgba[4];
776                         current_polymat->GetMaterialRGBAColor(rgba);
777                         glColor4ubv((const GLubyte *)rgba);
778                         return DM_DRAW_OPTION_NORMAL;
779                 }
780                 return DM_DRAW_OPTION_NORMAL;
781         }
782         return DM_DRAW_OPTION_SKIP;
783 }
784
785 void RAS_OpenGLRasterizer::DrawDerivedMesh(class RAS_MeshSlot &ms)
786 {
787         // mesh data is in derived mesh
788         current_bucket = ms.m_bucket;
789         current_polymat = current_bucket->GetPolyMaterial();
790         current_ms = &ms;
791         current_mesh = ms.m_mesh;
792         current_wireframe = m_drawingmode <= RAS_IRasterizer::KX_WIREFRAME;
793         // MCol *mcol = (MCol*)ms.m_pDerivedMesh->getFaceDataArray(ms.m_pDerivedMesh, CD_MCOL); /* UNUSED */
794
795         // handle two-side
796         if (current_polymat->GetDrawingMode() & RAS_IRasterizer::KX_BACKCULL)
797                 this->SetCullFace(true);
798         else
799                 this->SetCullFace(false);
800
801         if (current_polymat->GetFlag() & RAS_BLENDERGLSL) {
802                 // GetMaterialIndex return the original mface material index,
803                 // increment by 1 to match what derived mesh is doing
804                 current_blmat_nr = current_polymat->GetMaterialIndex()+1;
805                 // For GLSL we need to retrieve the GPU material attribute
806                 Material* blmat = current_polymat->GetBlenderMaterial();
807                 Scene* blscene = current_polymat->GetBlenderScene();
808                 if (!current_wireframe && blscene && blmat)
809                         GPU_material_vertex_attributes(GPU_material_from_blender(blscene, blmat, false), &current_gpu_attribs);
810                 else
811                         memset(&current_gpu_attribs, 0, sizeof(current_gpu_attribs));
812                 // DM draw can mess up blending mode, restore at the end
813                 int current_blend_mode = GPU_get_material_alpha_blend();
814                 ms.m_pDerivedMesh->drawFacesGLSL(ms.m_pDerivedMesh, CheckMaterialDM);
815                 GPU_set_material_alpha_blend(current_blend_mode);
816         } else {
817                 //ms.m_pDerivedMesh->drawMappedFacesTex(ms.m_pDerivedMesh, CheckTexfaceDM, mcol);
818                 current_blmat_nr = current_polymat->GetMaterialIndex();
819                 current_image = current_polymat->GetBlenderImage();
820                 ms.m_pDerivedMesh->drawFacesTex(ms.m_pDerivedMesh, CheckTexDM, NULL, NULL, DM_DRAW_USE_ACTIVE_UV);
821         }
822 }
823
824 void RAS_OpenGLRasterizer::SetProjectionMatrix(MT_CmMatrix4x4 &mat)
825 {
826         glMatrixMode(GL_PROJECTION);
827         float* matrix = &mat(0, 0);
828         glLoadMatrixf(matrix);
829
830         m_camortho = (mat(3, 3) != 0.0f);
831 }
832
833 void RAS_OpenGLRasterizer::SetProjectionMatrix(const MT_Matrix4x4 & mat)
834 {
835         glMatrixMode(GL_PROJECTION);
836         float matrix[16];
837         /* Get into argument. Looks a bit dodgy, but it's ok. */
838         mat.getValue(matrix);
839         glLoadMatrixf(matrix);
840
841         m_camortho = (mat[3][3] != 0.0f);
842 }
843
844 MT_Matrix4x4 RAS_OpenGLRasterizer::GetFrustumMatrix(
845         float left,
846         float right,
847         float bottom,
848         float top,
849         float frustnear,
850         float frustfar,
851         float focallength,
852         bool 
853 ) {
854         MT_Matrix4x4 result;
855         float mat[16];
856
857         // correction for stereo
858         if (Stereo())
859         {
860                         float near_div_focallength;
861                         float offset;
862
863                         // if Rasterizer.setFocalLength is not called we use the camera focallength
864                         if (!m_setfocallength)
865                                 // if focallength is null we use a value known to be reasonable
866                                 m_focallength = (focallength == 0.f) ? m_eyeseparation * 30.0f
867                                         : focallength;
868
869                         near_div_focallength = frustnear / m_focallength;
870                         offset = 0.5f * m_eyeseparation * near_div_focallength;
871                         switch (m_curreye) {
872                                 case RAS_STEREO_LEFTEYE:
873                                                 left += offset;
874                                                 right += offset;
875                                                 break;
876                                 case RAS_STEREO_RIGHTEYE:
877                                                 left -= offset;
878                                                 right -= offset;
879                                                 break;
880                         }
881                         // leave bottom and top untouched
882                         if (m_stereomode == RAS_STEREO_3DTVTOPBOTTOM) {
883                                 // restore the vertical frustum because the 3DTV will
884                                 // expand the top and bottom part to the full size of the screen
885                                 bottom *= 2.0f;
886                                 top *= 2.0f;
887                         }
888         }
889         
890         glMatrixMode(GL_PROJECTION);
891         glLoadIdentity();
892         glFrustum(left, right, bottom, top, frustnear, frustfar);
893                 
894         glGetFloatv(GL_PROJECTION_MATRIX, mat);
895         result.setValue(mat);
896
897         return result;
898 }
899
900 MT_Matrix4x4 RAS_OpenGLRasterizer::GetOrthoMatrix(
901         float left,
902         float right,
903         float bottom,
904         float top,
905         float frustnear,
906         float frustfar
907 ) {
908         MT_Matrix4x4 result;
909         float mat[16];
910
911         // stereo is meaningless for orthographic, disable it
912         glMatrixMode(GL_PROJECTION);
913         glLoadIdentity();
914         glOrtho(left, right, bottom, top, frustnear, frustfar);
915                 
916         glGetFloatv(GL_PROJECTION_MATRIX, mat);
917         result.setValue(mat);
918
919         return result;
920 }
921
922
923 // next arguments probably contain redundant info, for later...
924 void RAS_OpenGLRasterizer::SetViewMatrix(const MT_Matrix4x4 &mat, 
925                                                                                  const MT_Matrix3x3 & camOrientMat3x3,
926                                                                                  const MT_Point3 & pos,
927                                                                                  bool perspective)
928 {
929         m_viewmatrix = mat;
930
931         // correction for stereo
932         if (Stereo() && perspective)
933         {
934                 MT_Vector3 unitViewDir(0.0f, -1.0f, 0.0f);  // minus y direction, Blender convention
935                 MT_Vector3 unitViewupVec(0.0f, 0.0f, 1.0f);
936                 MT_Vector3 viewDir, viewupVec;
937                 MT_Vector3 eyeline;
938
939                 // actual viewDir
940                 viewDir = camOrientMat3x3 * unitViewDir;  // this is the moto convention, vector on right hand side
941                 // actual viewup vec
942                 viewupVec = camOrientMat3x3 * unitViewupVec;
943
944                 // vector between eyes
945                 eyeline = viewDir.cross(viewupVec);
946
947                 switch (m_curreye) {
948                         case RAS_STEREO_LEFTEYE:
949                                 {
950                                 // translate to left by half the eye distance
951                                 MT_Transform transform;
952                                 transform.setIdentity();
953                                 transform.translate(-(eyeline * m_eyeseparation / 2.0f));
954                                 m_viewmatrix *= transform;
955                                 }
956                                 break;
957                         case RAS_STEREO_RIGHTEYE:
958                                 {
959                                 // translate to right by half the eye distance
960                                 MT_Transform transform;
961                                 transform.setIdentity();
962                                 transform.translate(eyeline * m_eyeseparation / 2.0f);
963                                 m_viewmatrix *= transform;
964                                 }
965                                 break;
966                 }
967         }
968
969         m_viewinvmatrix = m_viewmatrix;
970         m_viewinvmatrix.invert();
971
972         // note: getValue gives back column major as needed by OpenGL
973         MT_Scalar glviewmat[16];
974         m_viewmatrix.getValue(glviewmat);
975
976         glMatrixMode(GL_MODELVIEW);
977         glLoadMatrixf(glviewmat);
978         m_campos = pos;
979 }
980
981
982 const MT_Point3& RAS_OpenGLRasterizer::GetCameraPosition()
983 {
984         return m_campos;
985 }
986
987 bool RAS_OpenGLRasterizer::GetCameraOrtho()
988 {
989         return m_camortho;
990 }
991
992 void RAS_OpenGLRasterizer::SetCullFace(bool enable)
993 {
994         if (enable)
995                 glEnable(GL_CULL_FACE);
996         else
997                 glDisable(GL_CULL_FACE);
998 }
999
1000 void RAS_OpenGLRasterizer::SetLines(bool enable)
1001 {
1002         if (enable)
1003                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
1004         else
1005                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1006 }
1007
1008 void RAS_OpenGLRasterizer::SetSpecularity(float specX,
1009                                                                                   float specY,
1010                                                                                   float specZ,
1011                                                                                   float specval)
1012 {
1013         GLfloat mat_specular[] = {specX, specY, specZ, specval};
1014         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
1015 }
1016
1017
1018
1019 void RAS_OpenGLRasterizer::SetShinyness(float shiny)
1020 {
1021         GLfloat mat_shininess[] = {     shiny };
1022         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
1023 }
1024
1025
1026
1027 void RAS_OpenGLRasterizer::SetDiffuse(float difX,float difY,float difZ,float diffuse)
1028 {
1029         GLfloat mat_diffuse [] = {difX, difY,difZ, diffuse};
1030         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
1031 }
1032
1033 void RAS_OpenGLRasterizer::SetEmissive(float eX, float eY, float eZ, float e)
1034 {
1035         GLfloat mat_emit [] = {eX,eY,eZ,e};
1036         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emit);
1037 }
1038
1039
1040 double RAS_OpenGLRasterizer::GetTime()
1041 {
1042         return m_time;
1043 }
1044
1045 void RAS_OpenGLRasterizer::SetPolygonOffset(float mult, float add)
1046 {
1047         glPolygonOffset(mult, add);
1048         GLint mode = GL_POLYGON_OFFSET_FILL;
1049         if (m_drawingmode < KX_SHADED)
1050                 mode = GL_POLYGON_OFFSET_LINE;
1051         if (mult != 0.0f || add != 0.0f)
1052                 glEnable(mode);
1053         else
1054                 glDisable(mode);
1055 }
1056
1057 void RAS_OpenGLRasterizer::EnableMotionBlur(float motionblurvalue)
1058 {
1059         /* don't just set m_motionblur to 1, but check if it is 0 so
1060          * we don't reset a motion blur that is already enabled */
1061         if (m_motionblur == 0)
1062                 m_motionblur = 1;
1063         m_motionblurvalue = motionblurvalue;
1064 }
1065
1066 void RAS_OpenGLRasterizer::DisableMotionBlur()
1067 {
1068         m_motionblur = 0;
1069         m_motionblurvalue = -1.0f;
1070 }
1071
1072 void RAS_OpenGLRasterizer::SetAlphaBlend(int alphablend)
1073 {
1074         /* Variance shadow maps don't handle alpha well, best to not allow it for now  */
1075         if (m_drawingmode == KX_SHADOW && m_usingoverrideshader)
1076                 GPU_set_material_alpha_blend(GPU_BLEND_SOLID);
1077         else
1078                 GPU_set_material_alpha_blend(alphablend);
1079 /*
1080         if (alphablend == m_last_alphablend)
1081                 return;
1082
1083         if (alphablend == GPU_BLEND_SOLID) {
1084                 glDisable(GL_BLEND);
1085                 glDisable(GL_ALPHA_TEST);
1086                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1087         }
1088         else if (alphablend == GPU_BLEND_ADD) {
1089                 glBlendFunc(GL_ONE, GL_ONE);
1090                 glEnable(GL_BLEND);
1091                 glDisable(GL_ALPHA_TEST);
1092         }
1093         else if (alphablend == GPU_BLEND_ALPHA) {
1094                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1095                 glEnable(GL_BLEND);
1096                 glEnable(GL_ALPHA_TEST);
1097                 glAlphaFunc(GL_GREATER, 0.0f);
1098         }
1099         else if (alphablend == GPU_BLEND_CLIP) {
1100                 glDisable(GL_BLEND); 
1101                 glEnable(GL_ALPHA_TEST);
1102                 glAlphaFunc(GL_GREATER, 0.5f);
1103         }
1104
1105         m_last_alphablend = alphablend;
1106 */
1107 }
1108
1109 void RAS_OpenGLRasterizer::SetFrontFace(bool ccw)
1110 {
1111         if (m_last_frontface == ccw)
1112                 return;
1113
1114         if (ccw)
1115                 glFrontFace(GL_CCW);
1116         else
1117                 glFrontFace(GL_CW);
1118         
1119         m_last_frontface = ccw;
1120 }
1121
1122 void RAS_OpenGLRasterizer::SetAnisotropicFiltering(short level)
1123 {
1124         GPU_set_anisotropic((float)level);
1125 }
1126
1127 short RAS_OpenGLRasterizer::GetAnisotropicFiltering()
1128 {
1129         return (short)GPU_get_anisotropic();
1130 }
1131
1132 void RAS_OpenGLRasterizer::SetMipmapping(MipmapOption val)
1133 {
1134         if (val == RAS_IRasterizer::RAS_MIPMAP_LINEAR)
1135         {
1136                 GPU_set_linear_mipmap(1);
1137                 GPU_set_mipmap(1);
1138         }
1139         else if (val == RAS_IRasterizer::RAS_MIPMAP_NEAREST)
1140         {
1141                 GPU_set_linear_mipmap(0);
1142                 GPU_set_mipmap(1);
1143         }
1144         else
1145         {
1146                 GPU_set_linear_mipmap(0);
1147                 GPU_set_mipmap(0);
1148         }
1149 }
1150
1151 RAS_IRasterizer::MipmapOption RAS_OpenGLRasterizer::GetMipmapping()
1152 {
1153         if (GPU_get_mipmap()) {
1154                 if (GPU_get_linear_mipmap()) {
1155                         return RAS_IRasterizer::RAS_MIPMAP_LINEAR;
1156                 }
1157                 else {
1158                         return RAS_IRasterizer::RAS_MIPMAP_NEAREST;
1159                 }
1160         }
1161         else {
1162                 return RAS_IRasterizer::RAS_MIPMAP_NONE;
1163         }
1164 }
1165
1166 void RAS_OpenGLRasterizer::SetUsingOverrideShader(bool val)
1167 {
1168         m_usingoverrideshader = val;
1169 }
1170
1171 bool RAS_OpenGLRasterizer::GetUsingOverrideShader()
1172 {
1173         return m_usingoverrideshader;
1174 }
1175
1176 /**
1177  * Render Tools
1178  */
1179
1180 /* ProcessLighting performs lighting on objects. the layer is a bitfield that
1181  * contains layer information. There are 20 'official' layers in blender. A
1182  * light is applied on an object only when they are in the same layer. OpenGL
1183  * has a maximum of 8 lights (simultaneous), so 20 * 8 lights are possible in
1184  * a scene. */
1185
1186 void RAS_OpenGLRasterizer::ProcessLighting(bool uselights, const MT_Transform& viewmat)
1187 {
1188         bool enable = false;
1189         int layer= -1;
1190
1191         /* find the layer */
1192         if (uselights) {
1193                 if (m_clientobject)
1194                         layer = static_cast<KX_GameObject*>(m_clientobject)->GetLayer();
1195         }
1196
1197         /* avoid state switching */
1198         if (m_lastlightlayer == layer && m_lastauxinfo == m_auxilaryClientInfo)
1199                 return;
1200
1201         m_lastlightlayer = layer;
1202         m_lastauxinfo = m_auxilaryClientInfo;
1203
1204         /* enable/disable lights as needed */
1205         if (layer >= 0) {
1206                 //enable = ApplyLights(layer, viewmat);
1207                 // taken from blender source, incompatibility between Blender Object / GameObject
1208                 KX_Scene* kxscene = (KX_Scene*)m_auxilaryClientInfo;
1209                 float glviewmat[16];
1210                 unsigned int count;
1211                 std::vector<RAS_OpenGLLight*>::iterator lit = m_lights.begin();
1212
1213                 for (count=0; count<m_numgllights; count++)
1214                         glDisable((GLenum)(GL_LIGHT0+count));
1215
1216                 viewmat.getValue(glviewmat);
1217
1218                 glPushMatrix();
1219                 glLoadMatrixf(glviewmat);
1220                 for (lit = m_lights.begin(), count = 0; !(lit==m_lights.end()) && count < m_numgllights; ++lit)
1221                 {
1222                         RAS_OpenGLLight* light = (*lit);
1223
1224                         if (light->ApplyFixedFunctionLighting(kxscene, layer, count))
1225                                 count++;
1226                 }
1227                 glPopMatrix();
1228
1229                 enable = count > 0;
1230         }
1231
1232         if (enable)
1233                 EnableOpenGLLights();
1234         else
1235                 DisableOpenGLLights();
1236 }
1237
1238 void RAS_OpenGLRasterizer::EnableOpenGLLights()
1239 {
1240         if (m_lastlighting == true)
1241                 return;
1242
1243         glEnable(GL_LIGHTING);
1244         glEnable(GL_COLOR_MATERIAL);
1245
1246         glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
1247         glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
1248         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, (GetCameraOrtho())? GL_FALSE: GL_TRUE);
1249         if (GLEW_EXT_separate_specular_color || GLEW_VERSION_1_2)
1250                 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1251
1252         m_lastlighting = true;
1253 }
1254
1255 void RAS_OpenGLRasterizer::DisableOpenGLLights()
1256 {
1257         if (m_lastlighting == false)
1258                 return;
1259
1260         glDisable(GL_LIGHTING);
1261         glDisable(GL_COLOR_MATERIAL);
1262
1263         m_lastlighting = false;
1264 }
1265
1266 RAS_ILightObject *RAS_OpenGLRasterizer::CreateLight()
1267 {
1268         return new RAS_OpenGLLight(this);
1269 }
1270
1271 void RAS_OpenGLRasterizer::AddLight(RAS_ILightObject* lightobject)
1272 {
1273         RAS_OpenGLLight* gllight = dynamic_cast<RAS_OpenGLLight*>(lightobject);
1274         assert(gllight);
1275         m_lights.push_back(gllight);
1276 }
1277
1278 void RAS_OpenGLRasterizer::RemoveLight(RAS_ILightObject* lightobject)
1279 {
1280         RAS_OpenGLLight* gllight = dynamic_cast<RAS_OpenGLLight*>(lightobject);
1281         assert(gllight);
1282
1283         std::vector<RAS_OpenGLLight*>::iterator lit =
1284                 std::find(m_lights.begin(),m_lights.end(),gllight);
1285
1286         if (!(lit==m_lights.end()))
1287                 m_lights.erase(lit);
1288 }
1289
1290 bool RAS_OpenGLRasterizer::RayHit(struct KX_ClientObjectInfo *client, KX_RayCast *result, float *oglmatrix)
1291 {
1292         if (result->m_hitMesh) {
1293
1294                 RAS_Polygon* poly = result->m_hitMesh->GetPolygon(result->m_hitPolygon);
1295                 if (!poly->IsVisible())
1296                         return false;
1297
1298                 MT_Vector3 resultnormal(result->m_hitNormal);
1299                 MT_Vector3 left(oglmatrix[0],oglmatrix[1],oglmatrix[2]);
1300                 MT_Vector3 dir = -(left.cross(resultnormal)).safe_normalized();
1301                 left = (dir.cross(resultnormal)).safe_normalized();
1302                 // for the up vector, we take the 'resultnormal' returned by the physics
1303
1304                 float maat[16] = {left[0],         left[1],         left[2],         0,
1305                                        dir[0],          dir[1],          dir[2],          0,
1306                                   resultnormal[0], resultnormal[1], resultnormal[2], 0,
1307                                   0,               0,               0,               1};
1308
1309                 glTranslatef(oglmatrix[12],oglmatrix[13],oglmatrix[14]);
1310                 //glMultMatrixd(oglmatrix);
1311                 glMultMatrixf(maat);
1312                 return true;
1313         }
1314         else {
1315                 return false;
1316         }
1317 }
1318
1319 void RAS_OpenGLRasterizer::applyTransform(float* oglmatrix,int objectdrawmode )
1320 {
1321         /* FIXME:
1322         blender: intern/moto/include/MT_Vector3.inl:42: MT_Vector3 operator/(const
1323         MT_Vector3&, double): Assertion `!MT_fuzzyZero(s)' failed.
1324
1325         Program received signal SIGABRT, Aborted.
1326         [Switching to Thread 16384 (LWP 1519)]
1327         0x40477571 in kill () from /lib/libc.so.6
1328         (gdb) bt
1329         #7  0x08334368 in MT_Vector3::normalized() const ()
1330         #8  0x0833e6ec in RAS_OpenGLRasterizer::applyTransform(RAS_IRasterizer*, double*, int) ()
1331         */
1332
1333         if (objectdrawmode & RAS_IPolyMaterial::BILLBOARD_SCREENALIGNED ||
1334                 objectdrawmode & RAS_IPolyMaterial::BILLBOARD_AXISALIGNED)
1335         {
1336                 // rotate the billboard/halo
1337                 //page 360/361 3D Game Engine Design, David Eberly for a discussion
1338                 // on screen aligned and axis aligned billboards
1339                 // assumed is that the preprocessor transformed all billboard polygons
1340                 // so that their normal points into the positive x direction (1.0f, 0.0f, 0.0f)
1341                 // when new parenting for objects is done, this rotation
1342                 // will be moved into the object
1343
1344                 MT_Point3 objpos (oglmatrix[12],oglmatrix[13],oglmatrix[14]);
1345                 MT_Point3 campos = GetCameraPosition();
1346                 MT_Vector3 dir = (campos - objpos).safe_normalized();
1347                 MT_Vector3 up(0,0,1.0f);
1348
1349                 KX_GameObject* gameobj = (KX_GameObject*)m_clientobject;
1350                 // get scaling of halo object
1351                 MT_Vector3  size = gameobj->GetSGNode()->GetWorldScaling();
1352
1353                 bool screenaligned = (objectdrawmode & RAS_IPolyMaterial::BILLBOARD_SCREENALIGNED)!=0;//false; //either screen or axisaligned
1354                 if (screenaligned)
1355                 {
1356                         up = (up - up.dot(dir) * dir).safe_normalized();
1357                 } else
1358                 {
1359                         dir = (dir - up.dot(dir)*up).safe_normalized();
1360                 }
1361
1362                 MT_Vector3 left = dir.normalized();
1363                 dir = (up.cross(left)).normalized();
1364
1365                 // we have calculated the row vectors, now we keep
1366                 // local scaling into account:
1367
1368                 left *= size[0];
1369                 dir  *= size[1];
1370                 up   *= size[2];
1371
1372                 float maat[16] = {left[0], left[1], left[2], 0,
1373                                    dir[0],  dir[1],  dir[2],  0,
1374                                    up[0],   up[1],   up[2],   0,
1375                                    0,       0,       0,       1};
1376
1377                 glTranslatef(objpos[0],objpos[1],objpos[2]);
1378                 glMultMatrixf(maat);
1379
1380         }
1381         else {
1382                 if (objectdrawmode & RAS_IPolyMaterial::SHADOW)
1383                 {
1384                         // shadow must be cast to the ground, physics system needed here!
1385                         MT_Point3 frompoint(oglmatrix[12],oglmatrix[13],oglmatrix[14]);
1386                         KX_GameObject *gameobj = (KX_GameObject*)m_clientobject;
1387                         MT_Vector3 direction = MT_Vector3(0,0,-1);
1388
1389                         direction.normalize();
1390                         direction *= 100000;
1391
1392                         MT_Point3 topoint = frompoint + direction;
1393
1394                         KX_Scene* kxscene = (KX_Scene*) m_auxilaryClientInfo;
1395                         PHY_IPhysicsEnvironment* physics_environment = kxscene->GetPhysicsEnvironment();
1396                         PHY_IPhysicsController* physics_controller = gameobj->GetPhysicsController();
1397
1398                         KX_GameObject *parent = gameobj->GetParent();
1399                         if (!physics_controller && parent)
1400                                 physics_controller = parent->GetPhysicsController();
1401
1402                         KX_RayCast::Callback<RAS_OpenGLRasterizer, float> callback(this, physics_controller, oglmatrix);
1403                         if (!KX_RayCast::RayTest(physics_environment, frompoint, topoint, callback))
1404                         {
1405                                 // couldn't find something to cast the shadow on...
1406                                 glMultMatrixf(oglmatrix);
1407                         }
1408                         else
1409                         { // we found the "ground", but the cast matrix doesn't take
1410                           // scaling in consideration, so we must apply the object scale
1411                                 MT_Vector3  size = gameobj->GetSGNode()->GetLocalScale();
1412                                 glScalef(size[0], size[1], size[2]);
1413                         }
1414                 } else
1415                 {
1416
1417                         // 'normal' object
1418                         glMultMatrixf(oglmatrix);
1419                 }
1420         }
1421 }
1422
1423 static void DisableForText()
1424 {
1425         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); /* needed for texture fonts otherwise they render as wireframe */
1426
1427         glDisable(GL_BLEND);
1428         glDisable(GL_ALPHA_TEST);
1429
1430         glDisable(GL_LIGHTING);
1431         glDisable(GL_COLOR_MATERIAL);
1432
1433         if (GLEW_ARB_multitexture) {
1434                 for (int i=0; i<RAS_MAX_TEXCO; i++) {
1435                         glActiveTextureARB(GL_TEXTURE0_ARB+i);
1436
1437                         if (GLEW_ARB_texture_cube_map) {
1438                                 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1439                                 glDisable(GL_TEXTURE_GEN_S);
1440                                 glDisable(GL_TEXTURE_GEN_T);
1441                                 glDisable(GL_TEXTURE_GEN_Q);
1442                                 glDisable(GL_TEXTURE_GEN_R);
1443                         }
1444                         glDisable(GL_TEXTURE_2D);
1445                 }
1446
1447                 glActiveTextureARB(GL_TEXTURE0_ARB);
1448         }
1449         else {
1450                 if (GLEW_ARB_texture_cube_map)
1451                         glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1452
1453                 glDisable(GL_TEXTURE_2D);
1454         }
1455 }
1456
1457 void RAS_OpenGLRasterizer::RenderBox2D(int xco,
1458                         int yco,
1459                         int width,
1460                         int height,
1461                         float percentage)
1462 {
1463         /* This is a rather important line :( The gl-mode hasn't been left
1464          * behind quite as neatly as we'd have wanted to. I don't know
1465          * what cause it, though :/ .*/
1466         glDisable(GL_DEPTH_TEST);
1467
1468         glMatrixMode(GL_PROJECTION);
1469         glPushMatrix();
1470         glLoadIdentity();
1471
1472         glOrtho(0, width, 0, height, -100, 100);
1473
1474         glMatrixMode(GL_MODELVIEW);
1475         glPushMatrix();
1476         glLoadIdentity();
1477
1478         yco = height - yco;
1479         int barsize = 50;
1480
1481         /* draw in black first */
1482         glColor3ub(0, 0, 0);
1483         glBegin(GL_QUADS);
1484         glVertex2f(xco + 1 + 1 + barsize * percentage, yco - 1 + 10);
1485         glVertex2f(xco + 1, yco - 1 + 10);
1486         glVertex2f(xco + 1, yco - 1);
1487         glVertex2f(xco + 1 + 1 + barsize * percentage, yco - 1);
1488         glEnd();
1489
1490         glColor3ub(255, 255, 255);
1491         glBegin(GL_QUADS);
1492         glVertex2f(xco + 1 + barsize * percentage, yco + 10);
1493         glVertex2f(xco, yco + 10);
1494         glVertex2f(xco, yco);
1495         glVertex2f(xco + 1 + barsize * percentage, yco);
1496         glEnd();
1497
1498         glMatrixMode(GL_PROJECTION);
1499         glPopMatrix();
1500         glMatrixMode(GL_MODELVIEW);
1501         glPopMatrix();
1502         glEnable(GL_DEPTH_TEST);
1503 }
1504
1505 void RAS_OpenGLRasterizer::RenderText3D(
1506         int fontid, const char *text, int size, int dpi,
1507         const float color[4], const float mat[16], float aspect)
1508 {
1509         /* gl prepping */
1510         DisableForText();
1511
1512         /* the actual drawing */
1513         glColor4fv(color);
1514
1515         /* multiply the text matrix by the object matrix */
1516         BLF_enable(fontid, BLF_MATRIX|BLF_ASPECT);
1517         BLF_matrix(fontid, mat);
1518
1519         /* aspect is the inverse scale that allows you to increase
1520          * your resolution without sizing the final text size
1521          * the bigger the size, the smaller the aspect */
1522         BLF_aspect(fontid, aspect, aspect, aspect);
1523
1524         BLF_size(fontid, size, dpi);
1525         BLF_position(fontid, 0, 0, 0);
1526         BLF_draw(fontid, text, 65535);
1527
1528         BLF_disable(fontid, BLF_MATRIX|BLF_ASPECT);
1529 }
1530
1531 void RAS_OpenGLRasterizer::RenderText2D(
1532         RAS_TEXT_RENDER_MODE mode,
1533         const char* text,
1534         int xco, int yco,
1535         int width, int height)
1536 {
1537         /* This is a rather important line :( The gl-mode hasn't been left
1538          * behind quite as neatly as we'd have wanted to. I don't know
1539          * what cause it, though :/ .*/
1540         DisableForText();
1541         glDisable(GL_DEPTH_TEST);
1542
1543         glMatrixMode(GL_PROJECTION);
1544         glPushMatrix();
1545         glLoadIdentity();
1546
1547         glOrtho(0, width, 0, height, -100, 100);
1548
1549         glMatrixMode(GL_MODELVIEW);
1550         glPushMatrix();
1551         glLoadIdentity();
1552
1553         if (mode == RAS_TEXT_PADDED) {
1554                 /* draw in black first */
1555                 glColor3ub(0, 0, 0);
1556                 BLF_size(blf_mono_font, 11, 72);
1557                 BLF_position(blf_mono_font, (float)xco+1, (float)(height-yco-1), 0.0f);
1558                 BLF_draw(blf_mono_font, text, 65535); /* XXX, use real len */
1559         }
1560
1561         /* the actual drawing */
1562         glColor3ub(255, 255, 255);
1563         BLF_size(blf_mono_font, 11, 72);
1564         BLF_position(blf_mono_font, (float)xco, (float)(height-yco), 0.0f);
1565         BLF_draw(blf_mono_font, text, 65535); /* XXX, use real len */
1566
1567         glMatrixMode(GL_PROJECTION);
1568         glPopMatrix();
1569         glMatrixMode(GL_MODELVIEW);
1570         glPopMatrix();
1571         glEnable(GL_DEPTH_TEST);
1572 }
1573
1574 void RAS_OpenGLRasterizer::PushMatrix()
1575 {
1576         glPushMatrix();
1577 }
1578
1579 void RAS_OpenGLRasterizer::PopMatrix()
1580 {
1581         glPopMatrix();
1582 }
1583
1584 void RAS_OpenGLRasterizer::MotionBlur()
1585 {
1586         int state = GetMotionBlurState();
1587         float motionblurvalue;
1588         if (state)
1589         {
1590                 motionblurvalue = GetMotionBlurValue();
1591                 if (state==1)
1592                 {
1593                         // bugfix:load color buffer into accum buffer for the first time(state=1)
1594                         glAccum(GL_LOAD, 1.0f);
1595                         SetMotionBlurState(2);
1596                 }
1597                 else if (motionblurvalue >= 0.0f && motionblurvalue <= 1.0f) {
1598                         glAccum(GL_MULT, motionblurvalue);
1599                         glAccum(GL_ACCUM, 1-motionblurvalue);
1600                         glAccum(GL_RETURN, 1.0f);
1601                         glFlush();
1602                 }
1603         }
1604 }
1605
1606 void RAS_OpenGLRasterizer::SetClientObject(void* obj)
1607 {
1608         if (m_clientobject != obj)
1609         {
1610                 bool ccw = (obj == NULL || !((KX_GameObject*)obj)->IsNegativeScaling());
1611                 SetFrontFace(ccw);
1612
1613                 m_clientobject = obj;
1614         }
1615 }
1616
1617 void RAS_OpenGLRasterizer::SetAuxilaryClientInfo(void* inf)
1618 {
1619         m_auxilaryClientInfo = inf;
1620 }
1621
1622 void RAS_OpenGLRasterizer::PrintHardwareInfo()
1623 {
1624         #define pprint(x) std::cout << x << std::endl;
1625
1626         pprint("GL_VENDOR: " << glGetString(GL_VENDOR));
1627         pprint("GL_RENDERER: " << glGetString(GL_RENDERER));
1628         pprint("GL_VERSION:  " << glGetString(GL_VERSION));
1629         bool support=0;
1630         pprint("Supported Extensions...");
1631         pprint(" GL_ARB_shader_objects supported?       "<< (GLEW_ARB_shader_objects?"yes.":"no."));
1632
1633         support= GLEW_ARB_vertex_shader;
1634         pprint(" GL_ARB_vertex_shader supported?        "<< (support?"yes.":"no."));
1635         if (support) {
1636                 pprint(" ----------Details----------");
1637                 int max=0;
1638                 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB, (GLint*)&max);
1639                 pprint("  Max uniform components." << max);
1640
1641                 glGetIntegerv(GL_MAX_VARYING_FLOATS_ARB, (GLint*)&max);
1642                 pprint("  Max varying floats." << max);
1643
1644                 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS_ARB, (GLint*)&max);
1645                 pprint("  Max vertex texture units." << max);
1646
1647                 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS_ARB, (GLint*)&max);
1648                 pprint("  Max combined texture units." << max);
1649                 pprint("");
1650         }
1651
1652         support=GLEW_ARB_fragment_shader;
1653         pprint(" GL_ARB_fragment_shader supported?      "<< (support?"yes.":"no."));
1654         if (support) {
1655                 pprint(" ----------Details----------");
1656                 int max=0;
1657                 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS_ARB, (GLint*)&max);
1658                 pprint("  Max uniform components." << max);
1659                 pprint("");
1660         }
1661
1662         support = GLEW_ARB_texture_cube_map;
1663         pprint(" GL_ARB_texture_cube_map supported?     "<< (support?"yes.":"no."));
1664         if (support) {
1665                 pprint(" ----------Details----------");
1666                 int size=0;
1667                 glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB, (GLint*)&size);
1668                 pprint("  Max cubemap size." << size);
1669                 pprint("");
1670         }
1671
1672         support = GLEW_ARB_multitexture;
1673         pprint(" GL_ARB_multitexture supported?         "<< (support?"yes.":"no."));
1674         if (support) {
1675                 pprint(" ----------Details----------");
1676                 int units=0;
1677                 glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, (GLint*)&units);
1678                 pprint("  Max texture units available.  " << units);
1679                 pprint("");
1680         }
1681
1682         pprint(" GL_ARB_texture_env_combine supported?  "<< (GLEW_ARB_texture_env_combine?"yes.":"no."));
1683
1684         pprint(" GL_ARB_texture_non_power_of_two supported  " << (GPU_full_non_power_of_two_support()?"yes.":"no."));
1685 }
1686