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