- assigning vectors in the BGE wasn't updating the vector from the callback
[blender.git] / source / gameengine / Ketsji / KX_Light.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL 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.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #ifdef WIN32
34
35 #pragma warning (disable : 4786)
36 #endif
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 "GPU_material.h"
50  
51 KX_LightObject::KX_LightObject(void* sgReplicationInfo,SG_Callbacks callbacks,
52                                                            class RAS_IRenderTools* rendertools,
53                                                            const RAS_LightObject&       lightobj,
54                                                            bool glsl)
55         : KX_GameObject(sgReplicationInfo,callbacks),
56           m_rendertools(rendertools)
57 {
58         m_lightobj = lightobj;
59         m_lightobj.m_scene = sgReplicationInfo;
60         m_lightobj.m_light = this;
61         m_rendertools->AddLight(&m_lightobj);
62         m_glsl = glsl;
63         m_blenderscene = ((KX_Scene*)sgReplicationInfo)->GetBlenderScene();
64 };
65
66
67 KX_LightObject::~KX_LightObject()
68 {
69         GPULamp *lamp;
70
71         if((lamp = GetGPULamp())) {
72                 float obmat[4][4] = {{0}};
73                 GPU_lamp_update(lamp, 0, obmat);
74         }
75
76         m_rendertools->RemoveLight(&m_lightobj);
77 }
78
79
80 CValue*         KX_LightObject::GetReplica()
81 {
82
83         KX_LightObject* replica = new KX_LightObject(*this);
84
85         replica->ProcessReplica();
86         
87         replica->m_lightobj.m_light = replica;
88         m_rendertools->AddLight(&replica->m_lightobj);
89
90         return replica;
91 }
92
93 bool KX_LightObject::ApplyLight(KX_Scene *kxscene, int oblayer, int slot)
94 {
95         KX_Scene* lightscene = (KX_Scene*)m_lightobj.m_scene;
96         float vec[4];
97         int scenelayer = ~0;
98
99         if(kxscene && kxscene->GetBlenderScene())
100                 scenelayer = kxscene->GetBlenderScene()->lay;
101         
102         /* only use lights in the same layer as the object */
103         if(!(m_lightobj.m_layer & oblayer))
104                 return false;
105         /* only use lights in the same scene, and in a visible layer */
106         if(kxscene != lightscene || !(m_lightobj.m_layer & scenelayer))
107                 return false;
108
109         // lights don't get their openGL matrix updated, do it now
110         if(GetSGNode()->IsDirty())
111                 GetOpenGLMatrix();
112
113         MT_CmMatrix4x4& worldmatrix= *GetOpenGLMatrixPtr();
114
115         vec[0] = worldmatrix(0,3);
116         vec[1] = worldmatrix(1,3);
117         vec[2] = worldmatrix(2,3);
118         vec[3] = 1.0f;
119
120         if(m_lightobj.m_type==RAS_LightObject::LIGHT_SUN) {
121                 
122                 vec[0] = worldmatrix(0,2);
123                 vec[1] = worldmatrix(1,2);
124                 vec[2] = worldmatrix(2,2);
125                 //vec[0]= base->object->obmat[2][0];
126                 //vec[1]= base->object->obmat[2][1];
127                 //vec[2]= base->object->obmat[2][2];
128                 vec[3]= 0.0;
129                 glLightfv((GLenum)(GL_LIGHT0+slot), GL_POSITION, vec); 
130         }
131         else {
132                 //vec[3]= 1.0;
133                 glLightfv((GLenum)(GL_LIGHT0+slot), GL_POSITION, vec); 
134                 glLightf((GLenum)(GL_LIGHT0+slot), GL_CONSTANT_ATTENUATION, 1.0);
135                 glLightf((GLenum)(GL_LIGHT0+slot), GL_LINEAR_ATTENUATION, m_lightobj.m_att1/m_lightobj.m_distance);
136                 // without this next line it looks backward compatible.
137                 //attennuation still is acceptable 
138                 glLightf((GLenum)(GL_LIGHT0+slot), GL_QUADRATIC_ATTENUATION, m_lightobj.m_att2/(m_lightobj.m_distance*m_lightobj.m_distance)); 
139                 
140                 if(m_lightobj.m_type==RAS_LightObject::LIGHT_SPOT) {
141                         vec[0] = -worldmatrix(0,2);
142                         vec[1] = -worldmatrix(1,2);
143                         vec[2] = -worldmatrix(2,2);
144                         //vec[0]= -base->object->obmat[2][0];
145                         //vec[1]= -base->object->obmat[2][1];
146                         //vec[2]= -base->object->obmat[2][2];
147                         glLightfv((GLenum)(GL_LIGHT0+slot), GL_SPOT_DIRECTION, vec);
148                         glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_CUTOFF, m_lightobj.m_spotsize/2.0);
149                         glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_EXPONENT, 128.0*m_lightobj.m_spotblend);
150                 }
151                 else
152                         glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_CUTOFF, 180.0);
153         }
154         
155         if (m_lightobj.m_nodiffuse) {
156                 vec[0] = vec[1] = vec[2] = vec[3] = 0.0;
157         }
158         else {
159                 vec[0]= m_lightobj.m_energy*m_lightobj.m_red;
160                 vec[1]= m_lightobj.m_energy*m_lightobj.m_green;
161                 vec[2]= m_lightobj.m_energy*m_lightobj.m_blue;
162                 vec[3]= 1.0;
163         }
164
165         glLightfv((GLenum)(GL_LIGHT0+slot), GL_DIFFUSE, vec);
166         if(m_lightobj.m_nospecular)
167         {
168                 vec[0] = vec[1] = vec[2] = vec[3] = 0.0;
169         }
170         else if (m_lightobj.m_nodiffuse) {
171                 vec[0]= m_lightobj.m_energy*m_lightobj.m_red;
172                 vec[1]= m_lightobj.m_energy*m_lightobj.m_green;
173                 vec[2]= m_lightobj.m_energy*m_lightobj.m_blue;
174                 vec[3]= 1.0;
175         }
176
177         glLightfv((GLenum)(GL_LIGHT0+slot), GL_SPECULAR, vec);
178         glEnable((GLenum)(GL_LIGHT0+slot));
179
180         return true;
181 }
182
183 GPULamp *KX_LightObject::GetGPULamp()
184 {
185         if(m_glsl)
186                 return GPU_lamp_from_blender(m_blenderscene, GetBlenderObject(), GetBlenderGroupObject());
187         else
188                 return false;
189 }
190
191 void KX_LightObject::Update()
192 {
193         GPULamp *lamp;
194
195         if((lamp = GetGPULamp()) != NULL && GetSGNode()) {
196                 float obmat[4][4];
197                 // lights don't get their openGL matrix updated, do it now
198                 if (GetSGNode()->IsDirty())
199                         GetOpenGLMatrix();
200                 double *dobmat = GetOpenGLMatrixPtr()->getPointer();
201
202                 for(int i=0; i<4; i++)
203                         for(int j=0; j<4; j++, dobmat++)
204                                 obmat[i][j] = (float)*dobmat;
205
206                 GPU_lamp_update(lamp, m_lightobj.m_layer, obmat);
207                 GPU_lamp_update_colors(lamp, m_lightobj.m_red, m_lightobj.m_green, 
208                         m_lightobj.m_blue, m_lightobj.m_energy);
209         }
210 }
211
212 bool KX_LightObject::HasShadowBuffer()
213 {
214         GPULamp *lamp;
215
216         if((lamp = GetGPULamp()))
217                 return GPU_lamp_has_shadow_buffer(lamp);
218         else
219                 return false;
220 }
221
222 int KX_LightObject::GetShadowLayer()
223 {
224         GPULamp *lamp;
225
226         if((lamp = GetGPULamp()))
227                 return GPU_lamp_shadow_layer(lamp);
228         else
229                 return 0;
230 }
231
232 void KX_LightObject::BindShadowBuffer(RAS_IRasterizer *ras, KX_Camera *cam, MT_Transform& camtrans)
233 {
234         GPULamp *lamp;
235         float viewmat[4][4], winmat[4][4];
236         int winsize;
237
238         /* bind framebuffer */
239         lamp = GetGPULamp();
240         GPU_lamp_shadow_buffer_bind(lamp, viewmat, &winsize, winmat);
241
242         /* setup camera transformation */
243         MT_Matrix4x4 modelviewmat((float*)viewmat);
244         MT_Matrix4x4 projectionmat((float*)winmat);
245
246         MT_Transform trans = MT_Transform((float*)viewmat);
247         camtrans.invert(trans);
248
249         cam->SetModelviewMatrix(modelviewmat);
250         cam->SetProjectionMatrix(projectionmat);
251         
252         cam->NodeSetLocalPosition(camtrans.getOrigin());
253         cam->NodeSetLocalOrientation(camtrans.getBasis());
254         cam->NodeUpdateGS(0);
255
256         /* setup rasterizer transformations */
257         ras->SetProjectionMatrix(projectionmat);
258         ras->SetViewMatrix(modelviewmat, cam->NodeGetWorldOrientation(), cam->NodeGetWorldPosition(), cam->GetCameraData()->m_perspective);
259 }
260
261 void KX_LightObject::UnbindShadowBuffer(RAS_IRasterizer *ras)
262 {
263         GPULamp *lamp = GetGPULamp();
264         GPU_lamp_shadow_buffer_unbind(lamp);
265 }
266
267 /* ------------------------------------------------------------------------- */
268 /* Python Integration Hooks                                                                      */
269 /* ------------------------------------------------------------------------- */
270
271 PyTypeObject KX_LightObject::Type = {
272         PyVarObject_HEAD_INIT(NULL, 0)
273         "KX_LightObject",
274         sizeof(PyObjectPlus_Proxy),
275         0,
276         py_base_dealloc,
277         0,
278         0,
279         0,
280         0,
281         py_base_repr,
282         0,
283         &KX_GameObject::Sequence,
284         &KX_GameObject::Mapping,
285         0,0,0,
286         NULL,
287         NULL,
288         0,
289         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
290         0,0,0,0,0,0,0,
291         Methods,
292         0,
293         0,
294         &KX_GameObject::Type,
295         0,0,0,0,0,0,
296         py_base_new
297 };
298
299 PyMethodDef KX_LightObject::Methods[] = {
300         {NULL,NULL} //Sentinel
301 };
302
303 PyAttributeDef KX_LightObject::Attributes[] = {
304         KX_PYATTRIBUTE_INT_RW("layer", 1, 20, true, KX_LightObject, m_lightobj.m_layer),
305         KX_PYATTRIBUTE_FLOAT_RW("energy", 0, 10, KX_LightObject, m_lightobj.m_energy),
306         KX_PYATTRIBUTE_FLOAT_RW("distance", 0.01, 5000, KX_LightObject, m_lightobj.m_distance),
307         KX_PYATTRIBUTE_RW_FUNCTION("color", KX_LightObject, pyattr_get_color, pyattr_set_color),
308         KX_PYATTRIBUTE_RW_FUNCTION("colour", KX_LightObject, pyattr_get_color, pyattr_set_color),
309         KX_PYATTRIBUTE_FLOAT_RW("lin_attenuation", 0, 1, KX_LightObject, m_lightobj.m_att1),
310         KX_PYATTRIBUTE_FLOAT_RW("quad_attenuation", 0, 1, KX_LightObject, m_lightobj.m_att2),
311         KX_PYATTRIBUTE_FLOAT_RW("spotsize", 1, 180, KX_LightObject, m_lightobj.m_spotsize),
312         KX_PYATTRIBUTE_FLOAT_RW("spotblend", 0, 1, KX_LightObject, m_lightobj.m_spotblend),
313         KX_PYATTRIBUTE_RO_FUNCTION("SPOT", KX_LightObject, pyattr_get_typeconst),
314         KX_PYATTRIBUTE_RO_FUNCTION("SUN", KX_LightObject, pyattr_get_typeconst),
315         KX_PYATTRIBUTE_RO_FUNCTION("NORMAL", KX_LightObject, pyattr_get_typeconst),
316         KX_PYATTRIBUTE_RW_FUNCTION("type", KX_LightObject, pyattr_get_type, pyattr_set_type),
317         { NULL }        //Sentinel
318 };
319
320 PyObject* KX_LightObject::pyattr_get_color(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
321 {
322         KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
323         return Py_BuildValue("[fff]", self->m_lightobj.m_red, self->m_lightobj.m_green, self->m_lightobj.m_blue);
324 }
325
326 int KX_LightObject::pyattr_set_color(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
327 {
328         KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
329
330         MT_Vector3 color;
331         if (PyVecTo(value, color))
332         {
333                 self->m_lightobj.m_red = color[0];
334                 self->m_lightobj.m_green = color[1];
335                 self->m_lightobj.m_blue = color[2];
336                 return PY_SET_ATTR_SUCCESS;
337         }
338         return PY_SET_ATTR_FAIL;
339 }
340
341 PyObject* KX_LightObject::pyattr_get_typeconst(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
342 {
343         PyObject* retvalue;
344
345         const char* type = attrdef->m_name;
346
347         if(strcmp(type, "SPOT")) {
348                 retvalue = PyLong_FromSsize_t(RAS_LightObject::LIGHT_SPOT);
349         } else if (strcmp(type, "SUN")) {
350                 retvalue = PyLong_FromSsize_t(RAS_LightObject::LIGHT_SUN);
351         } else if (strcmp(type, "NORMAL")) {
352                 retvalue = PyLong_FromSsize_t(RAS_LightObject::LIGHT_NORMAL);
353         }
354
355         return retvalue;
356 }
357
358 PyObject* KX_LightObject::pyattr_get_type(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef)
359 {
360         KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
361         return PyLong_FromSsize_t(self->m_lightobj.m_type);
362 }
363
364 int KX_LightObject::pyattr_set_type(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject* value)
365 {
366         KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
367         int val = PyLong_AsSsize_t(value);
368         if((val==-1 && PyErr_Occurred()) || val<0 || val>2) {
369                 PyErr_SetString(PyExc_ValueError, "light.type= val: KX_LightObject, expected an int between 0 and 2");
370                 return PY_SET_ATTR_FAIL;
371         }
372         
373         switch(val) {
374                 case 0:
375                         self->m_lightobj.m_type = self->m_lightobj.LIGHT_SPOT;
376                         break;
377                 case 1:
378                         self->m_lightobj.m_type = self->m_lightobj.LIGHT_SUN;
379                         break;
380                 case 2:
381                         self->m_lightobj.m_type = self->m_lightobj.LIGHT_NORMAL;
382                         break;
383         }
384
385         return PY_SET_ATTR_SUCCESS;
386 }