Merge branch 'master' into blender2.8
[blender.git] / source / gameengine / Rasterizer / RAS_OpenGLRasterizer / RAS_OpenGLLight.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): Mitchell Stokes
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include "GPU_glew.h"
29
30 #include <stdio.h>
31
32
33 #include "RAS_OpenGLLight.h"
34 #include "RAS_OpenGLRasterizer.h"
35 #include "RAS_ICanvas.h"
36
37 #include "MT_CmMatrix4x4.h"
38
39 #include "KX_Camera.h"
40 #include "KX_Light.h"
41 #include "KX_Scene.h"
42
43 #include "DNA_lamp_types.h"
44 #include "DNA_scene_types.h"
45
46 #include "GPU_lamp.h"
47 #include "GPU_material.h"
48
49 RAS_OpenGLLight::RAS_OpenGLLight(RAS_OpenGLRasterizer *ras)
50     :m_rasterizer(ras)
51 {
52 }
53
54 RAS_OpenGLLight::~RAS_OpenGLLight()
55 {
56         GPULamp *lamp;
57         KX_LightObject* kxlight = (KX_LightObject*)m_light;
58         Lamp *la = (Lamp*)kxlight->GetBlenderObject()->data;
59
60         if ((lamp = GetGPULamp())) {
61                 float obmat[4][4] = {{0}};
62                 GPU_lamp_update(lamp, 0, 0, obmat);
63                 GPU_lamp_update_distance(lamp, la->dist, la->att1, la->att2, la->coeff_const, la->coeff_lin, la->coeff_quad);
64                 GPU_lamp_update_spot(lamp, la->spotsize, la->spotblend);
65         }
66 }
67
68 bool RAS_OpenGLLight::ApplyFixedFunctionLighting(KX_Scene *kxscene, int oblayer, int slot)
69 {
70         KX_Scene* lightscene = (KX_Scene*)m_scene;
71         KX_LightObject* kxlight = (KX_LightObject*)m_light;
72         float vec[4];
73         int scenelayer = ~0;
74
75         if (kxscene && kxscene->GetBlenderScene())
76                 scenelayer = kxscene->GetBlenderScene()->lay;
77
78         /* only use lights in the same layer as the object */
79         if (!(m_layer & oblayer))
80                 return false;
81         /* only use lights in the same scene, and in a visible layer */
82         if (kxscene != lightscene || !(m_layer & scenelayer))
83                 return false;
84
85         // lights don't get their openGL matrix updated, do it now
86         if (kxlight->GetSGNode()->IsDirty())
87                 kxlight->GetOpenGLMatrix();
88
89         MT_CmMatrix4x4& worldmatrix= *kxlight->GetOpenGLMatrixPtr();
90
91         vec[0] = worldmatrix(0,3);
92         vec[1] = worldmatrix(1,3);
93         vec[2] = worldmatrix(2,3);
94         vec[3] = 1.0f;
95
96         if (m_type==RAS_ILightObject::LIGHT_SUN) {
97
98                 vec[0] = worldmatrix(0,2);
99                 vec[1] = worldmatrix(1,2);
100                 vec[2] = worldmatrix(2,2);
101                 //vec[0] = base->object->obmat[2][0];
102                 //vec[1] = base->object->obmat[2][1];
103                 //vec[2] = base->object->obmat[2][2];
104                 vec[3] = 0.0f;
105                 glLightfv((GLenum)(GL_LIGHT0+slot), GL_POSITION, vec);
106         }
107         else {
108                 //vec[3] = 1.0f;
109                 glLightfv((GLenum)(GL_LIGHT0+slot), GL_POSITION, vec);
110                 glLightf((GLenum)(GL_LIGHT0+slot), GL_CONSTANT_ATTENUATION, 1.0f);
111                 glLightf((GLenum)(GL_LIGHT0+slot), GL_LINEAR_ATTENUATION, m_att1/m_distance);
112                 // without this next line it looks backward compatible.
113                 //attennuation still is acceptable
114                 glLightf((GLenum)(GL_LIGHT0+slot), GL_QUADRATIC_ATTENUATION, m_att2/(m_distance*m_distance));
115
116                 if (m_type==RAS_ILightObject::LIGHT_SPOT) {
117                         vec[0] = -worldmatrix(0,2);
118                         vec[1] = -worldmatrix(1,2);
119                         vec[2] = -worldmatrix(2,2);
120                         //vec[0] = -base->object->obmat[2][0];
121                         //vec[1] = -base->object->obmat[2][1];
122                         //vec[2] = -base->object->obmat[2][2];
123                         glLightfv((GLenum)(GL_LIGHT0+slot), GL_SPOT_DIRECTION, vec);
124                         glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_CUTOFF, m_spotsize / 2.0f);
125                         glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_EXPONENT, 128.0f * m_spotblend);
126                 }
127                 else {
128                         glLightf((GLenum)(GL_LIGHT0+slot), GL_SPOT_CUTOFF, 180.0f);
129                 }
130         }
131
132         if (m_nodiffuse) {
133                 vec[0] = vec[1] = vec[2] = vec[3] = 0.0f;
134         }
135         else {
136                 vec[0] = m_energy*m_color[0];
137                 vec[1] = m_energy*m_color[1];
138                 vec[2] = m_energy*m_color[2];
139                 vec[3] = 1.0f;
140         }
141
142         glLightfv((GLenum)(GL_LIGHT0+slot), GL_DIFFUSE, vec);
143         if (m_nospecular)
144         {
145                 vec[0] = vec[1] = vec[2] = vec[3] = 0.0f;
146         }
147         else if (m_nodiffuse) {
148                 vec[0] = m_energy*m_color[0];
149                 vec[1] = m_energy*m_color[1];
150                 vec[2] = m_energy*m_color[2];
151                 vec[3] = 1.0f;
152         }
153
154         glLightfv((GLenum)(GL_LIGHT0+slot), GL_SPECULAR, vec);
155         glEnable((GLenum)(GL_LIGHT0+slot));
156
157         return true;
158 }
159
160 GPULamp *RAS_OpenGLLight::GetGPULamp()
161 {
162         KX_LightObject* kxlight = (KX_LightObject*)m_light;
163
164         if (m_glsl)
165                 return GPU_lamp_from_blender(kxlight->GetScene()->GetBlenderScene(), kxlight->GetBlenderObject(), kxlight->GetBlenderGroupObject());
166         else
167                 return NULL;
168 }
169
170
171 bool RAS_OpenGLLight::HasShadowBuffer()
172 {
173         GPULamp *lamp;
174
175         if ((lamp = GetGPULamp()))
176                 return GPU_lamp_has_shadow_buffer(lamp);
177         else
178                 return false;
179 }
180
181 int RAS_OpenGLLight::GetShadowBindCode()
182 {
183         GPULamp *lamp;
184         
185         if ((lamp = GetGPULamp()))
186                 return GPU_lamp_shadow_bind_code(lamp);
187         return -1;
188 }
189
190 MT_Matrix4x4 RAS_OpenGLLight::GetShadowMatrix()
191 {
192         GPULamp *lamp;
193
194         if ((lamp = GetGPULamp()))
195                 return MT_Matrix4x4(GPU_lamp_dynpersmat(lamp));
196         MT_Matrix4x4 mat;
197         mat.setIdentity();
198         return mat;
199 }
200
201 int RAS_OpenGLLight::GetShadowLayer()
202 {
203         GPULamp *lamp;
204
205         if ((lamp = GetGPULamp()))
206                 return GPU_lamp_shadow_layer(lamp);
207         else
208                 return 0;
209 }
210
211 void RAS_OpenGLLight::BindShadowBuffer(RAS_ICanvas *canvas, KX_Camera *cam, MT_Transform& camtrans)
212 {
213         GPULamp *lamp;
214         float viewmat[4][4], winmat[4][4];
215         int winsize;
216
217         /* bind framebuffer */
218         lamp = GetGPULamp();
219         GPU_lamp_shadow_buffer_bind(lamp, viewmat, &winsize, winmat);
220
221         if (GPU_lamp_shadow_buffer_type(lamp) == LA_SHADMAP_VARIANCE)
222                 m_rasterizer->SetUsingOverrideShader(true);
223
224         /* GPU_lamp_shadow_buffer_bind() changes the viewport, so update the canvas */
225         canvas->UpdateViewPort(0, 0, winsize, winsize);
226
227         /* setup camera transformation */
228         MT_Matrix4x4 modelviewmat((float*)viewmat);
229         MT_Matrix4x4 projectionmat((float*)winmat);
230
231         MT_Transform trans = MT_Transform((float*)viewmat);
232         camtrans.invert(trans);
233
234         cam->SetModelviewMatrix(modelviewmat);
235         cam->SetProjectionMatrix(projectionmat);
236
237         cam->NodeSetLocalPosition(camtrans.getOrigin());
238         cam->NodeSetLocalOrientation(camtrans.getBasis());
239         cam->NodeUpdateGS(0);
240
241         /* setup rasterizer transformations */
242         /* SetViewMatrix may use stereomode which we temporarily disable here */
243         RAS_IRasterizer::StereoMode stereomode = m_rasterizer->GetStereoMode();
244         m_rasterizer->SetStereoMode(RAS_IRasterizer::RAS_STEREO_NOSTEREO);
245         m_rasterizer->SetProjectionMatrix(projectionmat);
246         m_rasterizer->SetViewMatrix(modelviewmat, cam->NodeGetWorldOrientation(), cam->NodeGetWorldPosition(), cam->NodeGetLocalScaling(), cam->GetCameraData()->m_perspective);
247         m_rasterizer->SetStereoMode(stereomode);
248 }
249
250 void RAS_OpenGLLight::UnbindShadowBuffer()
251 {
252         GPULamp *lamp = GetGPULamp();
253         GPU_lamp_shadow_buffer_unbind(lamp);
254
255         if (GPU_lamp_shadow_buffer_type(lamp) == LA_SHADMAP_VARIANCE)
256                 m_rasterizer->SetUsingOverrideShader(false);
257 }
258
259 Image *RAS_OpenGLLight::GetTextureImage(short texslot)
260 {
261         KX_LightObject* kxlight = (KX_LightObject*)m_light;
262         Lamp *la = (Lamp*)kxlight->GetBlenderObject()->data;
263
264         if (texslot >= MAX_MTEX || texslot < 0)
265         {
266                 printf("KX_LightObject::GetTextureImage(): texslot exceeds slot bounds (0-%d)\n", MAX_MTEX-1);
267                 return NULL;
268         }
269
270         if (la->mtex[texslot])
271                 return la->mtex[texslot]->tex->ima;
272
273         return NULL;
274 }
275
276 void RAS_OpenGLLight::Update()
277 {
278         GPULamp *lamp;
279         KX_LightObject* kxlight = (KX_LightObject*)m_light;
280
281         if ((lamp = GetGPULamp()) != NULL && kxlight->GetSGNode()) {
282                 float obmat[4][4];
283                 // lights don't get their openGL matrix updated, do it now
284                 if (kxlight->GetSGNode()->IsDirty())
285                         kxlight->GetOpenGLMatrix();
286                 float *dobmat = kxlight->GetOpenGLMatrixPtr()->getPointer();
287
288                 for (int i=0; i<4; i++)
289                         for (int j=0; j<4; j++, dobmat++)
290                                 obmat[i][j] = (float)*dobmat;
291                 int hide = kxlight->GetVisible() ? 0 : 1;
292                 GPU_lamp_update(lamp, m_layer, hide, obmat);
293                 GPU_lamp_update_colors(lamp, m_color[0], m_color[1],
294                         m_color[2], m_energy);
295                 GPU_lamp_update_distance(lamp, m_distance, m_att1, m_att2, m_coeff_const, m_coeff_lin, m_coeff_quad);
296                 GPU_lamp_update_spot(lamp, m_spotsize, m_spotblend);
297         }
298 }
299
300