code cleanup:
[blender.git] / source / gameengine / Ketsji / KX_Light.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/Ketsji/KX_Light.cpp
29  *  \ingroup ketsji
30  */
31
32 #if defined(WIN32) && !defined(FREE_WINDOWS)
33 #pragma warning (disable : 4786)
34 #endif
35
36 #include <stdio.h>
37
38 #include "GL/glew.h"
39
40 #include "KX_Light.h"
41 #include "KX_Camera.h"
42 #include "RAS_IRasterizer.h"
43 #include "RAS_IRenderTools.h"
44
45 #include "KX_PyMath.h"
46
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_lamp_types.h"
50 #include "GPU_material.h"
51  
52 KX_LightObject::KX_LightObject(void* sgReplicationInfo,SG_Callbacks callbacks,
53                                                            class RAS_IRenderTools* rendertools,
54                                                            const RAS_LightObject&       lightobj,
55                                                            bool glsl)
56         : KX_GameObject(sgReplicationInfo,callbacks),
57           m_rendertools(rendertools)
58 {
59         m_lightobj = lightobj;
60         m_lightobj.m_scene = sgReplicationInfo;
61         m_lightobj.m_light = this;
62         m_rendertools->AddLight(&m_lightobj);
63         m_glsl = glsl;
64         m_blenderscene = ((KX_Scene*)sgReplicationInfo)->GetBlenderScene();
65 };
66
67
68 KX_LightObject::~KX_LightObject()
69 {
70         GPULamp *lamp;
71         Lamp *la = (Lamp*)GetBlenderObject()->data;
72
73         if ((lamp = GetGPULamp())) {
74                 float obmat[4][4] = {{0}};
75                 GPU_lamp_update(lamp, 0, 0, obmat);
76                 GPU_lamp_update_distance(lamp, la->dist, la->att1, la->att2);
77                 GPU_lamp_update_spot(lamp, la->spotsize, la->spotblend);
78         }
79
80         m_rendertools->RemoveLight(&m_lightobj);
81 }
82
83
84 CValue*         KX_LightObject::GetReplica()
85 {
86
87         KX_LightObject* replica = new KX_LightObject(*this);
88
89         replica->ProcessReplica();
90         
91         replica->m_lightobj.m_light = replica;
92         m_rendertools->AddLight(&replica->m_lightobj);
93
94         return replica;
95 }
96
97 bool KX_LightObject::ApplyLight(KX_Scene *kxscene, int oblayer, int slot)
98 {
99         KX_Scene* lightscene = (KX_Scene*)m_lightobj.m_scene;
100         float vec[4];
101         int scenelayer = ~0;
102
103         if (kxscene && kxscene->GetBlenderScene())
104                 scenelayer = kxscene->GetBlenderScene()->lay;
105         
106         /* only use lights in the same layer as the object */
107         if (!(m_lightobj.m_layer & oblayer))
108                 return false;
109         /* only use lights in the same scene, and in a visible layer */
110         if (kxscene != lightscene || !(m_lightobj.m_layer & scenelayer))
111                 return false;
112
113         // lights don't get their openGL matrix updated, do it now
114         if (GetSGNode()->IsDirty())
115                 GetOpenGLMatrix();
116
117         MT_CmMatrix4x4& worldmatrix= *GetOpenGLMatrixPtr();
118
119         vec[0] = worldmatrix(0,3);
120         vec[1] = worldmatrix(1,3);
121         vec[2] = worldmatrix(2,3);
122         vec[3] = 1.0f;
123
124         if (m_lightobj.m_type==RAS_LightObject::LIGHT_SUN) {
125                 
126                 vec[0] = worldmatrix(0,2);
127                 vec[1] = worldmatrix(1,2);
128                 vec[2] = worldmatrix(2,2);
129                 //vec[0]= base->object->obmat[2][0];
130                 //vec[1]= base->object->obmat[2][1];
131                 //vec[2]= base->object->obmat[2][2];
132                 vec[3]= 0.0;
133                 glLightfv((GLenum)(GL_LIGHT0+slot), GL_POSITION, vec); 
134         }
135         else {
136                 //vec[3]= 1.0;
137                 glLightfv((GLenum)(GL_LIGHT0+slot), GL_POSITION, vec); 
138                 glLightf((GLenum)(GL_LIGHT0+slot), GL_CONSTANT_ATTENUATION, 1.0);
139                 glLightf((GLenum)(GL_LIGHT0+slot), GL_LINEAR_ATTENUATION, m_lightobj.m_att1/m_lightobj.m_distance);
140                 // without this next line it looks backward compatible.
141                 //attennuation still is acceptable 
142                 glLightf((GLenum)(GL_LIGHT0+slot), GL_QUADRATIC_ATTENUATION, m_lightobj.m_att2/(m_lightobj.m_distance*m_lightobj.m_distance)); 
143                 
144                 if (m_lightobj.m_type==RAS_LightObject::LIGHT_SPOT) {
145                         vec[0] = -worldmatrix(0,2);
146                         vec[1] = -worldmatrix(1,2);
147                         vec[2] = -worldmatrix(2,2);
148                         //vec[0]= -base->object->obmat[2][0];
149                         //vec[1]= -base->object->obmat[2][1];
150                         //vec[2]= -base->object->obmat[2][2];
151                         glLightfv((GLenum)(GL_LIGHT0+slot), GL_SPOT_DIRECTION, vec);
152                         glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_CUTOFF, m_lightobj.m_spotsize / 2.0f);
153                         glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_EXPONENT, 128.0f * m_lightobj.m_spotblend);
154                 }
155                 else {
156                         glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_CUTOFF, 180.0);
157                 }
158         }
159         
160         if (m_lightobj.m_nodiffuse) {
161                 vec[0] = vec[1] = vec[2] = vec[3] = 0.0;
162         }
163         else {
164                 vec[0]= m_lightobj.m_energy*m_lightobj.m_red;
165                 vec[1]= m_lightobj.m_energy*m_lightobj.m_green;
166                 vec[2]= m_lightobj.m_energy*m_lightobj.m_blue;
167                 vec[3]= 1.0;
168         }
169
170         glLightfv((GLenum)(GL_LIGHT0+slot), GL_DIFFUSE, vec);
171         if (m_lightobj.m_nospecular)
172         {
173                 vec[0] = vec[1] = vec[2] = vec[3] = 0.0;
174         }
175         else if (m_lightobj.m_nodiffuse) {
176                 vec[0]= m_lightobj.m_energy*m_lightobj.m_red;
177                 vec[1]= m_lightobj.m_energy*m_lightobj.m_green;
178                 vec[2]= m_lightobj.m_energy*m_lightobj.m_blue;
179                 vec[3]= 1.0;
180         }
181
182         glLightfv((GLenum)(GL_LIGHT0+slot), GL_SPECULAR, vec);
183         glEnable((GLenum)(GL_LIGHT0+slot));
184
185         return true;
186 }
187
188 GPULamp *KX_LightObject::GetGPULamp()
189 {
190         if (m_glsl)
191                 return GPU_lamp_from_blender(m_blenderscene, GetBlenderObject(), GetBlenderGroupObject());
192         else
193                 return NULL;
194 }
195
196 void KX_LightObject::Update()
197 {
198         GPULamp *lamp;
199
200         if ((lamp = GetGPULamp()) != NULL && GetSGNode()) {
201                 float obmat[4][4];
202                 // lights don't get their openGL matrix updated, do it now
203                 if (GetSGNode()->IsDirty())
204                         GetOpenGLMatrix();
205                 double *dobmat = GetOpenGLMatrixPtr()->getPointer();
206
207                 for (int i=0; i<4; i++)
208                         for (int j=0; j<4; j++, dobmat++)
209                                 obmat[i][j] = (float)*dobmat;
210
211                 GPU_lamp_update(lamp, m_lightobj.m_layer, 0, obmat);
212                 GPU_lamp_update_colors(lamp, m_lightobj.m_red, m_lightobj.m_green, 
213                         m_lightobj.m_blue, m_lightobj.m_energy);
214                 GPU_lamp_update_distance(lamp, m_lightobj.m_distance, m_lightobj.m_att1, m_lightobj.m_att2);
215                 GPU_lamp_update_spot(lamp, m_lightobj.m_spotsize, m_lightobj.m_spotblend);
216         }
217 }
218
219 bool KX_LightObject::HasShadowBuffer()
220 {
221         GPULamp *lamp;
222
223         if ((lamp = GetGPULamp()))
224                 return GPU_lamp_has_shadow_buffer(lamp);
225         else
226                 return false;
227 }
228
229 int KX_LightObject::GetShadowLayer()
230 {
231         GPULamp *lamp;
232
233         if ((lamp = GetGPULamp()))
234                 return GPU_lamp_shadow_layer(lamp);
235         else
236                 return 0;
237 }
238
239 void KX_LightObject::BindShadowBuffer(RAS_IRasterizer *ras, KX_Camera *cam, MT_Transform& camtrans)
240 {
241         GPULamp *lamp;
242         float viewmat[4][4], winmat[4][4];
243         int winsize;
244
245         /* bind framebuffer */
246         lamp = GetGPULamp();
247         GPU_lamp_shadow_buffer_bind(lamp, viewmat, &winsize, winmat);
248
249         /* setup camera transformation */
250         MT_Matrix4x4 modelviewmat((float*)viewmat);
251         MT_Matrix4x4 projectionmat((float*)winmat);
252
253         MT_Transform trans = MT_Transform((float*)viewmat);
254         camtrans.invert(trans);
255
256         cam->SetModelviewMatrix(modelviewmat);
257         cam->SetProjectionMatrix(projectionmat);
258         
259         cam->NodeSetLocalPosition(camtrans.getOrigin());
260         cam->NodeSetLocalOrientation(camtrans.getBasis());
261         cam->NodeUpdateGS(0);
262
263         /* setup rasterizer transformations */
264         /* SetViewMatrix may use stereomode which we temporarily disable here */
265         RAS_IRasterizer::StereoMode stereomode = ras->GetStereoMode();
266         ras->SetStereoMode(RAS_IRasterizer::RAS_STEREO_NOSTEREO);
267         ras->SetProjectionMatrix(projectionmat);
268         ras->SetViewMatrix(modelviewmat, cam->NodeGetWorldOrientation(), cam->NodeGetWorldPosition(), cam->GetCameraData()->m_perspective);
269         ras->SetStereoMode(stereomode);
270 }
271
272 void KX_LightObject::UnbindShadowBuffer(RAS_IRasterizer *ras)
273 {
274         GPULamp *lamp = GetGPULamp();
275         GPU_lamp_shadow_buffer_unbind(lamp);
276 }
277
278 struct Image *KX_LightObject::GetTextureImage(short texslot)
279 {
280         Lamp *la = (Lamp*)GetBlenderObject()->data;
281
282         if (texslot >= MAX_MTEX || texslot < 0)
283         {
284                 printf("KX_LightObject::GetTextureImage(): texslot exceeds slot bounds (0-%d)\n", MAX_MTEX-1);
285                 return NULL;
286         }
287         
288         if (la->mtex[texslot])
289                 return la->mtex[texslot]->tex->ima;
290
291         return NULL;
292 }
293
294 #ifdef WITH_PYTHON
295 /* ------------------------------------------------------------------------- */
296 /* Python Integration Hooks                                                                      */
297 /* ------------------------------------------------------------------------- */
298
299 PyTypeObject KX_LightObject::Type = {
300         PyVarObject_HEAD_INIT(NULL, 0)
301         "KX_LightObject",
302         sizeof(PyObjectPlus_Proxy),
303         0,
304         py_base_dealloc,
305         0,
306         0,
307         0,
308         0,
309         py_base_repr,
310         0,
311         &KX_GameObject::Sequence,
312         &KX_GameObject::Mapping,
313         0,0,0,
314         NULL,
315         NULL,
316         0,
317         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
318         0,0,0,0,0,0,0,
319         Methods,
320         0,
321         0,
322         &KX_GameObject::Type,
323         0,0,0,0,0,0,
324         py_base_new
325 };
326
327 PyMethodDef KX_LightObject::Methods[] = {
328         {NULL,NULL} //Sentinel
329 };
330
331 PyAttributeDef KX_LightObject::Attributes[] = {
332         KX_PYATTRIBUTE_INT_RW("layer", 1, 20, true, KX_LightObject, m_lightobj.m_layer),
333         KX_PYATTRIBUTE_FLOAT_RW("energy", 0, 10, KX_LightObject, m_lightobj.m_energy),
334         KX_PYATTRIBUTE_FLOAT_RW("distance", 0.01, 5000, KX_LightObject, m_lightobj.m_distance),
335         KX_PYATTRIBUTE_RW_FUNCTION("color", KX_LightObject, pyattr_get_color, pyattr_set_color),
336         KX_PYATTRIBUTE_FLOAT_RW("lin_attenuation", 0, 1, KX_LightObject, m_lightobj.m_att1),
337         KX_PYATTRIBUTE_FLOAT_RW("quad_attenuation", 0, 1, KX_LightObject, m_lightobj.m_att2),
338         KX_PYATTRIBUTE_FLOAT_RW("spotsize", 1, 180, KX_LightObject, m_lightobj.m_spotsize),
339         KX_PYATTRIBUTE_FLOAT_RW("spotblend", 0, 1, KX_LightObject, m_lightobj.m_spotblend),
340         KX_PYATTRIBUTE_RO_FUNCTION("SPOT", KX_LightObject, pyattr_get_typeconst),
341         KX_PYATTRIBUTE_RO_FUNCTION("SUN", KX_LightObject, pyattr_get_typeconst),
342         KX_PYATTRIBUTE_RO_FUNCTION("NORMAL", KX_LightObject, pyattr_get_typeconst),
343         KX_PYATTRIBUTE_RW_FUNCTION("type", KX_LightObject, pyattr_get_type, pyattr_set_type),
344         { NULL }        //Sentinel
345 };
346
347 PyObject* KX_LightObject::pyattr_get_color(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
348 {
349         KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
350         return Py_BuildValue("[fff]", self->m_lightobj.m_red, self->m_lightobj.m_green, self->m_lightobj.m_blue);
351 }
352
353 int KX_LightObject::pyattr_set_color(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
354 {
355         KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
356
357         MT_Vector3 color;
358         if (PyVecTo(value, color))
359         {
360                 self->m_lightobj.m_red = color[0];
361                 self->m_lightobj.m_green = color[1];
362                 self->m_lightobj.m_blue = color[2];
363                 return PY_SET_ATTR_SUCCESS;
364         }
365         return PY_SET_ATTR_FAIL;
366 }
367
368 PyObject* KX_LightObject::pyattr_get_typeconst(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
369 {
370         PyObject* retvalue;
371
372         const char* type = attrdef->m_name;
373
374         if (!strcmp(type, "SPOT")) {
375                 retvalue = PyLong_FromSsize_t(RAS_LightObject::LIGHT_SPOT);
376         } else if (!strcmp(type, "SUN")) {
377                 retvalue = PyLong_FromSsize_t(RAS_LightObject::LIGHT_SUN);
378         } else if (!strcmp(type, "NORMAL")) {
379                 retvalue = PyLong_FromSsize_t(RAS_LightObject::LIGHT_NORMAL);
380         }
381         else {
382                 /* should never happen */
383                 PyErr_SetString(PyExc_TypeError, "light.type: internal error, invalid light type");
384                 retvalue = NULL;
385         }
386
387         return retvalue;
388 }
389
390 PyObject* KX_LightObject::pyattr_get_type(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef)
391 {
392         KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
393         return PyLong_FromSsize_t(self->m_lightobj.m_type);
394 }
395
396 int KX_LightObject::pyattr_set_type(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject* value)
397 {
398         KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
399         int val = PyLong_AsSsize_t(value);
400         if ((val==-1 && PyErr_Occurred()) || val<0 || val>2) {
401                 PyErr_SetString(PyExc_ValueError, "light.type= val: KX_LightObject, expected an int between 0 and 2");
402                 return PY_SET_ATTR_FAIL;
403         }
404         
405         switch(val) {
406                 case 0:
407                         self->m_lightobj.m_type = self->m_lightobj.LIGHT_SPOT;
408                         break;
409                 case 1:
410                         self->m_lightobj.m_type = self->m_lightobj.LIGHT_SUN;
411                         break;
412                 case 2:
413                         self->m_lightobj.m_type = self->m_lightobj.LIGHT_NORMAL;
414                         break;
415         }
416
417         return PY_SET_ATTR_SUCCESS;
418 }
419 #endif // WITH_PYTHON