add missing files after merging
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 /** \file gameengine/Ketsji/KX_Light.cpp
30  *  \ingroup ketsji
31  */
32
33
34 #if defined(WIN32) && !defined(FREE_WINDOWS)
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, 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 NULL;
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, 0, 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         /* SetViewMatrix may use stereomode which we temporarily disable here */
258         RAS_IRasterizer::StereoMode stereomode = ras->GetStereoMode();
259         ras->SetStereoMode(RAS_IRasterizer::RAS_STEREO_NOSTEREO);
260         ras->SetProjectionMatrix(projectionmat);
261         ras->SetViewMatrix(modelviewmat, cam->NodeGetWorldOrientation(), cam->NodeGetWorldPosition(), cam->GetCameraData()->m_perspective);
262         ras->SetStereoMode(stereomode);
263 }
264
265 void KX_LightObject::UnbindShadowBuffer(RAS_IRasterizer *ras)
266 {
267         GPULamp *lamp = GetGPULamp();
268         GPU_lamp_shadow_buffer_unbind(lamp);
269 }
270
271 #ifdef WITH_PYTHON
272 /* ------------------------------------------------------------------------- */
273 /* Python Integration Hooks                                                                      */
274 /* ------------------------------------------------------------------------- */
275
276 PyTypeObject KX_LightObject::Type = {
277         PyVarObject_HEAD_INIT(NULL, 0)
278         "KX_LightObject",
279         sizeof(PyObjectPlus_Proxy),
280         0,
281         py_base_dealloc,
282         0,
283         0,
284         0,
285         0,
286         py_base_repr,
287         0,
288         &KX_GameObject::Sequence,
289         &KX_GameObject::Mapping,
290         0,0,0,
291         NULL,
292         NULL,
293         0,
294         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
295         0,0,0,0,0,0,0,
296         Methods,
297         0,
298         0,
299         &KX_GameObject::Type,
300         0,0,0,0,0,0,
301         py_base_new
302 };
303
304 PyMethodDef KX_LightObject::Methods[] = {
305         {NULL,NULL} //Sentinel
306 };
307
308 PyAttributeDef KX_LightObject::Attributes[] = {
309         KX_PYATTRIBUTE_INT_RW("layer", 1, 20, true, KX_LightObject, m_lightobj.m_layer),
310         KX_PYATTRIBUTE_FLOAT_RW("energy", 0, 10, KX_LightObject, m_lightobj.m_energy),
311         KX_PYATTRIBUTE_FLOAT_RW("distance", 0.01, 5000, KX_LightObject, m_lightobj.m_distance),
312         KX_PYATTRIBUTE_RW_FUNCTION("color", KX_LightObject, pyattr_get_color, pyattr_set_color),
313         KX_PYATTRIBUTE_FLOAT_RW("lin_attenuation", 0, 1, KX_LightObject, m_lightobj.m_att1),
314         KX_PYATTRIBUTE_FLOAT_RW("quad_attenuation", 0, 1, KX_LightObject, m_lightobj.m_att2),
315         KX_PYATTRIBUTE_FLOAT_RW("spotsize", 1, 180, KX_LightObject, m_lightobj.m_spotsize),
316         KX_PYATTRIBUTE_FLOAT_RW("spotblend", 0, 1, KX_LightObject, m_lightobj.m_spotblend),
317         KX_PYATTRIBUTE_RO_FUNCTION("SPOT", KX_LightObject, pyattr_get_typeconst),
318         KX_PYATTRIBUTE_RO_FUNCTION("SUN", KX_LightObject, pyattr_get_typeconst),
319         KX_PYATTRIBUTE_RO_FUNCTION("NORMAL", KX_LightObject, pyattr_get_typeconst),
320         KX_PYATTRIBUTE_RW_FUNCTION("type", KX_LightObject, pyattr_get_type, pyattr_set_type),
321         { NULL }        //Sentinel
322 };
323
324 PyObject* KX_LightObject::pyattr_get_color(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
325 {
326         KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
327         return Py_BuildValue("[fff]", self->m_lightobj.m_red, self->m_lightobj.m_green, self->m_lightobj.m_blue);
328 }
329
330 int KX_LightObject::pyattr_set_color(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
331 {
332         KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
333
334         MT_Vector3 color;
335         if (PyVecTo(value, color))
336         {
337                 self->m_lightobj.m_red = color[0];
338                 self->m_lightobj.m_green = color[1];
339                 self->m_lightobj.m_blue = color[2];
340                 return PY_SET_ATTR_SUCCESS;
341         }
342         return PY_SET_ATTR_FAIL;
343 }
344
345 PyObject* KX_LightObject::pyattr_get_typeconst(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
346 {
347         PyObject* retvalue;
348
349         const char* type = attrdef->m_name;
350
351         if(!strcmp(type, "SPOT")) {
352                 retvalue = PyLong_FromSsize_t(RAS_LightObject::LIGHT_SPOT);
353         } else if (!strcmp(type, "SUN")) {
354                 retvalue = PyLong_FromSsize_t(RAS_LightObject::LIGHT_SUN);
355         } else if (!strcmp(type, "NORMAL")) {
356                 retvalue = PyLong_FromSsize_t(RAS_LightObject::LIGHT_NORMAL);
357         }
358         else {
359                 /* should never happen */
360                 PyErr_SetString(PyExc_TypeError, "light.type: internal error, invalid light type");
361                 retvalue = NULL;
362         }
363
364         return retvalue;
365 }
366
367 PyObject* KX_LightObject::pyattr_get_type(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef)
368 {
369         KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
370         return PyLong_FromSsize_t(self->m_lightobj.m_type);
371 }
372
373 int KX_LightObject::pyattr_set_type(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject* value)
374 {
375         KX_LightObject* self = static_cast<KX_LightObject*>(self_v);
376         int val = PyLong_AsSsize_t(value);
377         if((val==-1 && PyErr_Occurred()) || val<0 || val>2) {
378                 PyErr_SetString(PyExc_ValueError, "light.type= val: KX_LightObject, expected an int between 0 and 2");
379                 return PY_SET_ATTR_FAIL;
380         }
381         
382         switch(val) {
383                 case 0:
384                         self->m_lightobj.m_type = self->m_lightobj.LIGHT_SPOT;
385                         break;
386                 case 1:
387                         self->m_lightobj.m_type = self->m_lightobj.LIGHT_SUN;
388                         break;
389                 case 2:
390                         self->m_lightobj.m_type = self->m_lightobj.LIGHT_NORMAL;
391                         break;
392         }
393
394         return PY_SET_ATTR_SUCCESS;
395 }
396 #endif // WITH_PYTHON