File: Light.cpp

package info (click to toggle)
sofa-framework 1.0~beta4-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 88,688 kB
  • ctags: 27,205
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 67
file content (345 lines) | stat: -rw-r--r-- 8,664 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library is free software; you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as published by    *
* the Free Software Foundation; either version 2.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/
//
// C++ Implementation: Light
//
// Description:
//
//
// Author: The SOFA team </www.sofa-framework.org>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include <sofa/component/visualmodel/Light.h>
#include <sofa/component/visualmodel/LightManager.h>
#include <sofa/core/ObjectFactory.h>

namespace sofa
{

namespace component
{

namespace visualmodel
{

SOFA_DECL_CLASS(Light)

SOFA_DECL_CLASS(DirectionalLight)
//Register DirectionalLight in the Object Factory
int DirectionalLightClass = core::RegisterObject("Directional Light")
.add< DirectionalLight >()
;

SOFA_DECL_CLASS(PositionalLight)
//Register PositionalLight in the Object Factory
int PositionalLightClass = core::RegisterObject("Positional Light")
.add< PositionalLight >()
;

SOFA_DECL_CLASS(SpotLight)
//Register SpotLight in the Object Factory
int SpotLightClass = core::RegisterObject("Spot Light")
.add< SpotLight >()
;

Light::Light()
: color(initData(&color, (Vector3) Vector3(1,1,1), "color", "Set the color of the light"))
, zNear(initData(&zNear, (float) 4.0, "zNear", "Set minimum distance for view field"))
, zFar(initData(&zFar, (float) 50.0, "zFar", "Set minimum distance for view field"))

{
	lightID = 0;
}

Light::~Light()
{
}

void Light::setID(const GLint& id)
{
	lightID = id;
}

void Light::init()
{
	sofa::core::objectmodel::BaseContext* context = this->getContext();
	LightManager* lm = context->core::objectmodel::BaseContext::get<LightManager>();

	lm->putLight(this);

}

void Light::initVisual()
{
	//Init Light part
	glLightf(GL_LIGHT0+lightID, GL_SPOT_CUTOFF, 180.0);
	GLfloat c[4] = { (GLfloat) color.getValue()[0], (GLfloat)color.getValue()[1], (GLfloat)color.getValue()[2], 1.0 };
	glLightfv(GL_LIGHT0+lightID, GL_AMBIENT, c);
	glLightfv(GL_LIGHT0+lightID, GL_DIFFUSE, c);
	glLightfv(GL_LIGHT0+lightID, GL_SPECULAR, c);
	glLightf(GL_LIGHT0+lightID, GL_LINEAR_ATTENUATION, 0.0);

	//init Shadow part
	computeShadowMapSize();
	//Shadow part
	//Shadow texture init
	shadowFBO.init(shadowTexWidth, shadowTexHeight);

}

void Light::reinit()
{

	initVisual();

}

void Light::drawLight()
{

}

void Light::preDrawShadow()
{
	computeShadowMapSize();

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();

	shadowFBO.start();
}

void Light::postDrawShadow()
{
	//Unbind fbo
	shadowFBO.stop();

	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();
}

void Light::computeShadowMapSize()
{
	// Current viewport
	GLint		viewport[4];
	glGetIntegerv(GL_VIEWPORT, viewport);
	GLint windowWidth = viewport[2];
	GLint windowHeight = viewport[3];

	//Get the size of the shadow map
	if (windowWidth >= 1024 && windowHeight >= 1024) {
		shadowTexWidth = shadowTexHeight = 1024;
	}
	else if (windowWidth >= 512 && windowHeight >= 512) {
		shadowTexWidth = shadowTexHeight = 512;
	}
	else if (windowWidth >= 256 && windowHeight >= 256) {
		shadowTexWidth = shadowTexHeight = 256;
	}
	else {
		shadowTexWidth = shadowTexHeight = 128;
	}
}


GLuint Light::getShadowMapSize()
{
	return shadowTexWidth;
}


DirectionalLight::DirectionalLight():
	direction(initData(&direction, (Vector3) Vector3(0,0,-1), "direction", "Set the direction of the light"))
{

}

DirectionalLight::~DirectionalLight()
{

}

void DirectionalLight::initVisual()
{
	Light::initVisual();

}

void DirectionalLight::reinit()
{
	initVisual();
}

void DirectionalLight::drawLight()
{
	Light::drawLight();
	GLfloat dir[4];

	dir[0]=(GLfloat)(direction.getValue()[0]);
	dir[1]=(GLfloat)(direction.getValue()[1]);
	dir[2]=(GLfloat)(direction.getValue()[2]);
	dir[3]=0.0; // directional

	glLightfv(GL_LIGHT0+lightID, GL_POSITION, dir);
}

PositionalLight::PositionalLight():
	position(initData(&position, (Vector3) Vector3(-0.7,0.3,0.0), "position", "Set the position of the light")),
	attenuation(initData(&attenuation, (float) 0.0, "attenuation", "Set the attenuation of the light"))
{

}

PositionalLight::~PositionalLight()
{

}

void PositionalLight::initVisual()
{
	Light::initVisual();

}

void PositionalLight::reinit()
{
	initVisual();

}

void PositionalLight::drawLight()
{
	Light::drawLight();

	GLfloat pos[4];
	pos[0]=(GLfloat)(position.getValue()[0]);
	pos[1]=(GLfloat)(position.getValue()[1]);
	pos[2]=(GLfloat)(position.getValue()[2]);
	pos[3]=1.0; // positional
	glLightfv(GL_LIGHT0+lightID, GL_POSITION, pos);

	glLightf(GL_LIGHT0+lightID, GL_LINEAR_ATTENUATION, attenuation.getValue());

}


SpotLight::SpotLight():
	direction(initData(&direction, (Vector3) Vector3(0,0,-1), "direction", "Set the direction of the light")),
	cutoff(initData(&cutoff, (float) 30.0, "cutoff", "Set the angle (cutoff) of the spot")),
	exponent(initData(&exponent, (float) 20.0, "exponent", "Set the exponent of the spot"))
{

}

SpotLight::~SpotLight()
{

}

void SpotLight::initVisual()
{
	PositionalLight::initVisual();

}

void SpotLight::reinit()
{
	initVisual();

}

void SpotLight::drawLight()
{
	PositionalLight::drawLight();

	GLfloat dir[]={(GLfloat)(direction.getValue()[0]), (GLfloat)(direction.getValue()[1]), (GLfloat)(direction.getValue()[2])};
	glLightf(GL_LIGHT0+lightID, GL_SPOT_CUTOFF, cutoff.getValue());
	glLightfv(GL_LIGHT0+lightID, GL_SPOT_DIRECTION, dir);
	glLightf(GL_LIGHT0+lightID, GL_SPOT_EXPONENT, exponent.getValue());

}

void SpotLight::preDrawShadow()
{
	Light::preDrawShadow();

	//float d = 4.0 * tan(cutoff.getValue()*3.14159/180);

	//Projection matrix
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	//glFrustum(-d, d, -d, d, ZNEAR, ZFAR);
	gluPerspective(2*cutoff.getValue(),1.0, zNear.getValue(), zFar.getValue());

	//Modelview matrix
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
//	gluLookAt(position.getValue()[0], position.getValue()[1], position.getValue()[2],
//			position.getValue()[0] + direction.getValue()[0],
//			position.getValue()[1] + direction.getValue()[1],
//			position.getValue()[2] + direction.getValue()[2],
//			0,1,0);
	gluLookAt(position.getValue()[0], position.getValue()[1], position.getValue()[2],0 ,0 ,0, direction.getValue()[0], direction.getValue()[1], direction.getValue()[2]);

	//Save the two matrices
	glGetFloatv(GL_PROJECTION_MATRIX, lightMatProj);
	glGetFloatv(GL_MODELVIEW_MATRIX, lightMatModelview);

	//glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, shadowFBO);

	glViewport(0, 0, shadowTexWidth, shadowTexHeight);
	glClear(GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);
}

GLuint SpotLight::getShadowTexture()
{
	//return debugVisualShadowTexture;
	//return shadowTexture;
	return shadowFBO.getDepthTexture();
}

GLfloat* SpotLight::getProjectionMatrix()
{
	return lightMatProj;
}

GLfloat* SpotLight::getModelviewMatrix()
{
	return lightMatModelview;
}

}

} //namespace component

} //namespace sofa