big warning hunt commit
[blender.git] / source / gameengine / BlenderRoutines / KX_BlenderRenderTools.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 "KX_BlenderRenderTools.h"
33
34 #ifdef WIN32
35 // OpenGL gl.h needs 'windows.h' on windows platforms 
36 #include <windows.h>
37 #endif //WIN32
38 #ifdef __APPLE__
39 #include <OpenGL/gl.h>
40 #else
41 #include <GL/gl.h>
42 #endif
43
44 #include "RAS_IRenderTools.h"
45 #include "RAS_IRasterizer.h"
46 #include "RAS_LightObject.h"
47 #include "RAS_ICanvas.h"
48 #include "RAS_GLExtensionManager.h"
49
50
51 // next two includes/dependencies come from the shadow feature
52 // it needs the gameobject and the sumo physics scene for a raycast
53 #include "KX_GameObject.h"
54
55 #include "KX_BlenderPolyMaterial.h"
56 #include "KX_PolygonMaterial.h"
57 #include "Value.h"
58
59 #include "KX_BlenderGL.h" // for text printing
60 #include "STR_String.h"
61 #include "RAS_BucketManager.h" // for polymaterial (needed for textprinting)
62
63 #include "SM_Scene.h"
64 #include "SumoPhysicsEnvironment.h"
65 #include "KX_SumoPhysicsController.h"
66 #include "KX_Scene.h"
67
68 KX_BlenderRenderTools::KX_BlenderRenderTools()
69 {
70         glGetIntegerv(GL_MAX_LIGHTS, (GLint*) &m_numgllights);
71         if (m_numgllights < 8)
72                 m_numgllights = 8;
73 }
74
75 /**
76 ProcessLighting performs lighting on objects. the layer is a bitfield that contains layer information.
77 There are 20 'official' layers in blender.
78 A light is applied on an object only when they are in the same layer.
79 OpenGL has a maximum of 8 lights (simultaneous), so 20 * 8 lights are possible in a scene.
80 */
81
82 int     KX_BlenderRenderTools::ProcessLighting(int layer) 
83 {
84         
85         int result = false;
86
87         if (layer < 0)
88         {
89                 DisableOpenGLLights();
90                 result = false;
91         } else
92         {
93                 if (m_clientobject)
94                 {
95
96                         
97                         if (applyLights(layer))
98                         {
99                                 EnableOpenGLLights();
100                                 result = true;
101                         } else
102                         {
103                                 DisableOpenGLLights();
104                                 result = false;
105                         }
106
107                         
108                 }
109         }
110         return result;
111         
112         
113 }
114
115
116 void KX_BlenderRenderTools::BeginFrame(RAS_IRasterizer* rasty)
117 {
118         m_clientobject = NULL;
119         m_lastblenderobject = NULL;
120         m_lastblenderlights = false;
121         m_lastlayer = -1;
122         m_lastlighting = false;
123         m_modified = true;
124         DisableOpenGLLights();
125
126
127 }
128         
129
130 void KX_BlenderRenderTools::applyTransform(RAS_IRasterizer* rasty,double* oglmatrix,int objectdrawmode )
131 {
132         /* FIXME:
133         blender: intern/moto/include/MT_Vector3.inl:42: MT_Vector3 operator/(const
134         MT_Vector3&, double): Assertion `!MT_fuzzyZero(s)' failed. 
135         
136         Program received signal SIGABRT, Aborted. 
137         [Switching to Thread 16384 (LWP 1519)] 
138         0x40477571 in kill () from /lib/libc.so.6 
139         (gdb) bt 
140         #7  0x08334368 in MT_Vector3::normalized() const () 
141         #8  0x0833e6ec in KX_BlenderRenderTools::applyTransform(RAS_IRasterizer*, double*, int) () 
142         */
143
144         if (objectdrawmode & RAS_IPolyMaterial::BILLBOARD_SCREENALIGNED ||
145                 objectdrawmode & RAS_IPolyMaterial::BILLBOARD_AXISALIGNED)
146         {
147                 // rotate the billboard/halo
148                 //page 360/361 3D Game Engine Design, David Eberly for a discussion
149                 // on screen aligned and axis aligned billboards
150                 // assumed is that the preprocessor transformed all billboard polygons
151                 // so that their normal points into the positive x direction (1.0 , 0.0 , 0.0)
152                 // when new parenting for objects is done, this rotation
153                 // will be moved into the object
154                 
155                 MT_Point3 objpos (oglmatrix[12],oglmatrix[13],oglmatrix[14]);
156                 MT_Point3 campos = rasty->GetCameraPosition();
157                 MT_Vector3 dir = (campos - objpos).safe_normalized();
158                 MT_Vector3 up(0,0,1.0);
159
160                 KX_GameObject* gameobj = (KX_GameObject*) this->m_clientobject;
161                 // get scaling of halo object
162                 MT_Vector3  size = gameobj->GetSGNode()->GetLocalScale();
163                 
164                 bool screenaligned = (objectdrawmode & RAS_IPolyMaterial::BILLBOARD_SCREENALIGNED)!=0;//false; //either screen or axisaligned
165                 if (screenaligned)
166                 {
167                         up = (up - up.dot(dir) * dir).safe_normalized();
168                 } else
169                 {
170                         dir = (dir - up.dot(dir)*up).safe_normalized();
171                 }
172
173                 MT_Vector3 left = dir.normalized();
174                 dir = (left.cross(up)).normalized();
175
176                 // we have calculated the row vectors, now we keep
177                 // local scaling into account:
178
179                 left *= size[0];
180                 dir  *= size[1];
181                 up   *= size[2];
182                 double maat[16]={
183                         left[0], left[1],left[2], 0,
184                                 dir[0], dir[1],dir[2],0,
185                                 up[0],up[1],up[2],0,
186                                 0,0,0,1};
187                         glTranslated(objpos[0],objpos[1],objpos[2]);
188                         glMultMatrixd(maat);
189                         
190         } else
191         {
192                 if (objectdrawmode & RAS_IPolyMaterial::SHADOW)
193                 {
194                         // shadow must be cast to the ground, physics system needed here!
195                         MT_Point3 frompoint(oglmatrix[12],oglmatrix[13],oglmatrix[14]);
196                         KX_GameObject *gameobj = (KX_GameObject*) this->m_clientobject;
197                         MT_Vector3 direction = MT_Vector3(0,0,-1);
198
199                         
200                         direction.normalize();
201                         direction *= 100000;
202
203                         MT_Point3 topoint = frompoint + direction;
204                         MT_Point3 resultpoint;
205                         MT_Vector3 resultnormal;
206
207                         //todo:
208                         //use physics abstraction
209                         KX_Scene* kxscene = (KX_Scene*) m_auxilaryClientInfo;
210                         SumoPhysicsEnvironment *spe = dynamic_cast<SumoPhysicsEnvironment *>( kxscene->GetPhysicsEnvironment());
211                         SM_Scene *scene = spe->GetSumoScene();
212                         KX_SumoPhysicsController *spc = dynamic_cast<KX_SumoPhysicsController *>( gameobj->GetPhysicsController());
213                         KX_GameObject *parent = gameobj->GetParent();
214                         if (!spc && parent)
215                                 spc = dynamic_cast<KX_SumoPhysicsController *>(parent->GetPhysicsController());
216                         if (parent)
217                                 parent->Release();
218                         SM_Object *thisObj = spc?spc->GetSumoObject():NULL;
219                         
220                         if (scene->rayTest(thisObj, frompoint, topoint, resultpoint, resultnormal))
221                         {
222                                 MT_Vector3 left(oglmatrix[0],oglmatrix[1],oglmatrix[2]);
223                                 MT_Vector3 dir = -(left.cross(resultnormal)).safe_normalized();
224                                 left = (dir.cross(resultnormal)).safe_normalized();
225                                 // for the up vector, we take the 'resultnormal' returned by the physics
226                                 
227                                 double maat[16]={
228                                                 left[0],        left[1],        left[2], 0,
229                                                  dir[0],         dir[1],         dir[2], 0,
230                                         resultnormal[0],resultnormal[1],resultnormal[2], 0,
231                                                       0,              0,              0, 1};
232                                 glTranslated(resultpoint[0],resultpoint[1],resultpoint[2]);
233                                 //glMultMatrixd(oglmatrix);
234                                 glMultMatrixd(maat);
235                         } else
236                         {
237                                 glMultMatrixd(oglmatrix);
238                         }
239                 } else
240                 {
241
242                         // 'normal' object
243                         glMultMatrixd(oglmatrix);
244                 }
245         }
246 }
247
248
249 /**
250 Render Text renders text into a (series of) polygon, using a texture font,
251 Each character consists of one polygon (one quad or two triangles)
252 */
253 void    KX_BlenderRenderTools::RenderText(int mode,RAS_IPolyMaterial* polymat,float v1[3],float v2[3],float v3[3],float v4[3])
254 {
255                 
256         STR_String mytext = ((CValue*)m_clientobject)->GetPropertyText("Text");
257         
258         KX_PolygonMaterial* blenderpoly = static_cast<KX_PolygonMaterial*>(polymat);
259         struct TFace* tface = blenderpoly->GetTFace();
260         
261         BL_RenderText( mode,mytext,mytext.Length(),tface,v1,v2,v3,v4);
262         
263 }
264
265
266
267 KX_BlenderRenderTools::~KX_BlenderRenderTools()
268 {
269 };
270         
271         
272 void    KX_BlenderRenderTools::EndFrame(RAS_IRasterizer* rasty)
273 {
274 }
275         
276
277         
278 void KX_BlenderRenderTools::DisableOpenGLLights()
279 {
280         glDisable(GL_LIGHTING);
281         glDisable(GL_COLOR_MATERIAL);
282 }
283
284         
285 void KX_BlenderRenderTools::EnableOpenGLLights()
286 {
287         glEnable(GL_LIGHTING);
288         
289         glEnable(GL_COLOR_MATERIAL);
290         glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE);
291         glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, false);
292         if (bgl::QueryExtension(bgl::_GL_EXT_separate_specular_color) || bgl::QueryVersion(1, 2))
293                 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
294
295 }
296         
297
298 /**
299  * Rendering text using 2D bitmap functionality.  
300  */
301 void KX_BlenderRenderTools::RenderText2D(RAS_TEXT_RENDER_MODE mode,
302                                                                                  const char* text,
303                                                                                  int xco,
304                                                                                  int yco,                                                                        
305                                                                                  int width,
306                                                                                  int height)
307 {
308         switch (mode) {
309         case RAS_IRenderTools::RAS_TEXT_PADDED: {
310                 STR_String tmpstr(text);
311                 BL_print_gamedebug_line_padded(tmpstr.Ptr(),xco,yco,width,height);
312                 break;
313         }
314         default: {
315                 STR_String tmpstr(text);
316                 BL_print_gamedebug_line(tmpstr.Ptr(),xco,yco,width,height);
317         }
318         }
319 }
320
321         
322
323 void KX_BlenderRenderTools::PushMatrix()
324 {
325         glPushMatrix();
326 }
327
328 void KX_BlenderRenderTools::PopMatrix()
329 {
330         glPopMatrix();
331 }
332
333
334
335 int     KX_BlenderRenderTools::applyLights(int objectlayer)
336 {
337 // taken from blender source, incompatibility between Blender Object / GameObject       
338         
339         unsigned int count;
340         float vec[4];
341                 
342         vec[3]= 1.0;
343         
344         for(count=0; count<m_numgllights; count++)
345                 glDisable((GLenum)(GL_LIGHT0+count));
346         
347         //std::vector<struct    RAS_LightObject*> m_lights;
348         std::vector<struct      RAS_LightObject*>::iterator lit = m_lights.begin();
349         
350         glPushMatrix();
351         glLoadMatrixf(m_viewmat);
352         for (lit = m_lights.begin(), count = 0; !(lit==m_lights.end()) && count < m_numgllights; ++lit)
353         {
354                 RAS_LightObject* lightdata = (*lit);
355                 if (lightdata->m_layer & objectlayer)
356                 {
357                         vec[0] = (*(lightdata->m_worldmatrix))(0,3);
358                         vec[1] = (*(lightdata->m_worldmatrix))(1,3);
359                         vec[2] = (*(lightdata->m_worldmatrix))(2,3);
360                         vec[3] = 1;
361
362                         if(lightdata->m_type==RAS_LightObject::LIGHT_SUN) {
363                                 
364                                 vec[0] = (*(lightdata->m_worldmatrix))(0,2);
365                                 vec[1] = (*(lightdata->m_worldmatrix))(1,2);
366                                 vec[2] = (*(lightdata->m_worldmatrix))(2,2);
367                                 //vec[0]= base->object->obmat[2][0];
368                                 //vec[1]= base->object->obmat[2][1];
369                                 //vec[2]= base->object->obmat[2][2];
370                                 vec[3]= 0.0;
371                                 glLightfv((GLenum)(GL_LIGHT0+count), GL_POSITION, vec); 
372                         }
373                         else {
374                                 //vec[3]= 1.0;
375                                 glLightfv((GLenum)(GL_LIGHT0+count), GL_POSITION, vec); 
376                                 glLightf((GLenum)(GL_LIGHT0+count), GL_CONSTANT_ATTENUATION, 1.0);
377                                 glLightf((GLenum)(GL_LIGHT0+count), GL_LINEAR_ATTENUATION, lightdata->m_att1/lightdata->m_distance);
378                                 // without this next line it looks backward compatible.
379                                 //attennuation still is acceptable 
380                                 glLightf((GLenum)(GL_LIGHT0+count), GL_QUADRATIC_ATTENUATION, lightdata->m_att2/(lightdata->m_distance*lightdata->m_distance)); 
381                                 
382                                 if(lightdata->m_type==RAS_LightObject::LIGHT_SPOT) {
383                                         vec[0] = -(*(lightdata->m_worldmatrix))(0,2);
384                                         vec[1] = -(*(lightdata->m_worldmatrix))(1,2);
385                                         vec[2] = -(*(lightdata->m_worldmatrix))(2,2);
386                                         //vec[0]= -base->object->obmat[2][0];
387                                         //vec[1]= -base->object->obmat[2][1];
388                                         //vec[2]= -base->object->obmat[2][2];
389                                         glLightfv((GLenum)(GL_LIGHT0+count), GL_SPOT_DIRECTION, vec);
390                                         glLightf((GLenum)(GL_LIGHT0+count), GL_SPOT_CUTOFF, lightdata->m_spotsize/2.0);
391                                         glLightf((GLenum)(GL_LIGHT0+count), GL_SPOT_EXPONENT, 128.0*lightdata->m_spotblend);
392                                 }
393                                 else glLightf((GLenum)(GL_LIGHT0+count), GL_SPOT_CUTOFF, 180.0);
394                         }
395                         
396                         if (lightdata->m_nodiffuse)
397                         {
398                                 vec[0] = vec[1] = vec[2] = vec[3] = 0.0;
399                         } else {
400                                 vec[0]= lightdata->m_energy*lightdata->m_red;
401                                 vec[1]= lightdata->m_energy*lightdata->m_green;
402                                 vec[2]= lightdata->m_energy*lightdata->m_blue;
403                                 vec[3]= 1.0;
404                         }
405                         glLightfv((GLenum)(GL_LIGHT0+count), GL_DIFFUSE, vec);
406                         if (lightdata->m_nospecular)
407                         {
408                                 vec[0] = vec[1] = vec[2] = vec[3] = 0.0;
409                         } else if (lightdata->m_nodiffuse) {
410                                 vec[0]= lightdata->m_energy*lightdata->m_red;
411                                 vec[1]= lightdata->m_energy*lightdata->m_green;
412                                 vec[2]= lightdata->m_energy*lightdata->m_blue;
413                                 vec[3]= 1.0;
414                         }
415                         glLightfv((GLenum)(GL_LIGHT0+count), GL_SPECULAR, vec);
416                         glEnable((GLenum)(GL_LIGHT0+count));
417
418                         count++;
419                         
420                 }
421         }
422         glPopMatrix();
423
424         return count;
425
426 }
427
428
429
430 RAS_IPolyMaterial* KX_BlenderRenderTools::CreateBlenderPolyMaterial(
431                 const STR_String &texname,
432                 bool ba,const STR_String& matname,int tile,int tilexrep,int tileyrep,int mode,bool transparant,bool zsort, int lightlayer
433                 ,bool bIsTriangle,void* clientobject,void* tface)
434 {
435         assert(!"Deprecated");
436 /*      return new KX_BlenderPolyMaterial(
437
438                 texname,
439                 ba,matname,tile,tilexrep,tileyrep,mode,transparant,zsort, lightlayer
440                 ,bIsTriangle,clientobject,(struct TFace*)tface);*/
441         return NULL;
442 }
443
444 unsigned int KX_BlenderRenderTools::m_numgllights;