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