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
|
/*
render/lightenvironment.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#include "render/lightenvironment.h"
#include "render/gl.h"
namespace render
{
LightEnvironment::LightEnvironment()
{
env_base_id = GL_LIGHT0;
env_attenuation = 0.0005f;
env_ambient_intensity = 0.1f;
env_diffuse_intensity = 0.75f;
env_specular_intensity = 0.75f;
}
LightEnvironment::~LightEnvironment()
{
clear();
}
void LightEnvironment::clear()
{
for (Container::iterator it = env_container.begin(); it != env_container.end(); ++it) {
Light *light = (*it);
delete light;
(*it) = 0;
}
env_container.clear();
}
void LightEnvironment::add(Light *light)
{
env_container.push_back(light);
}
void LightEnvironment::enable()
{
int gl_light_id = env_base_id;
for (Container::iterator it = env_container.begin(); it != env_container.end(); ++it) {
//Light *light = (*it);
gl::enable(gl_light_id);
gl_light_id++;
}
}
void LightEnvironment::disable()
{
int gl_light_id = env_base_id;
for (Container::iterator it = env_container.begin(); it != env_container.end(); ++it) {
//Light *light = (*it);
gl::disable(gl_light_id);
gl_light_id++;
}
}
void LightEnvironment::draw(const math::Vector3f translate)
{
GLfloat gl_light_location[4];
GLfloat gl_ambient_light[4];
GLfloat gl_diffuse_light[4];
GLfloat gl_specular_light[4];
int gl_light_id = env_base_id;
for (Container::iterator it = env_container.begin(); it != env_container.end(); ++it) {
Light *light = (*it);
for (size_t i = 0; i < 3; i++) {
gl_light_location[i] = light->location()[i] + translate[i];
gl_ambient_light[i] = light->color()[i] * env_ambient_intensity;
gl_diffuse_light[i] = light->color()[i] * env_diffuse_intensity;
gl_specular_light[i] = light->color()[i] * env_specular_intensity;
}
gl_light_location[3] = 1.0f;
gl_ambient_light[3] = 1.0f;
gl_diffuse_light[3] = 1.0f;
gl_specular_light[3] = 1.0f;
gl::light(gl_light_id, GL_POSITION, gl_light_location);
gl::light(gl_light_id, GL_AMBIENT, gl_ambient_light);
gl::light(gl_light_id, GL_DIFFUSE, gl_diffuse_light);
gl::light(gl_light_id, GL_SPECULAR, gl_specular_light);
// attenuation
gl::light(gl_light_id, GL_CONSTANT_ATTENUATION, light->attenuation()[0]);
gl::light(gl_light_id, GL_LINEAR_ATTENUATION, light->attenuation()[1]);
gl::light(gl_light_id, GL_QUADRATIC_ATTENUATION, light->attenuation()[2]);
gl_light_id++;
}
}
void LightEnvironment::draw()
{
draw(math::Vector3f());
}
} // namespace render
|