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