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