-
Notifications
You must be signed in to change notification settings - Fork 0
/
Light.cpp
194 lines (175 loc) · 6.36 KB
/
Light.cpp
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
#include "Light.h"
Light::Light(int _number, ptree& _config) {
id = _number;
switch (_number) {
case 0:
name = GL_LIGHT0;
break;
case 1:
name = GL_LIGHT1;
break;
case 2:
name = GL_LIGHT2;
break;
case 3:
name = GL_LIGHT3;
break;
case 4:
name = GL_LIGHT4;
break;
case 5:
name = GL_LIGHT5;
break;
case 6:
name = GL_LIGHT6;
break;
case 7:
name = GL_LIGHT7;
break;
}
try {
string _name = lexical_cast<string>(_number);
loadConfig(_name, _config);
} catch (bad_lexical_cast exception) {
cerr << "Light " << id << "::constructor -> Lexical Cast." << endl;
exit(1);
}
}
Light::~Light(void) {
}
void Light::switchOn(void) {
glEnable(name);
enabled = true;
}
void Light::switchOff(void) {
glDisable(name);
enabled = false;
}
void Light::setAmbient(GLfloat _r, GLfloat _g, GLfloat _b, GLfloat _a) {
ambient[0] = _r;
ambient[1] = _g;
ambient[2] = _b;
ambient[3] = _a;
glLightfv(name, GL_AMBIENT, ambient);
}
void Light::setDiffuse(GLfloat _r, GLfloat _g, GLfloat _b, GLfloat _a) {
diffuse[0] = _r;
diffuse[1] = _g;
diffuse[2] = _b;
diffuse[3] = _a;
glLightfv(name, GL_DIFFUSE, diffuse);
}
void Light::setSpecular(GLfloat _r, GLfloat _g, GLfloat _b, GLfloat _a) {
specular[0] = _r;
specular[1] = _g;
specular[2] = _b;
specular[3] = _a;
glLightfv(name, GL_SPECULAR, specular);
}
void Light::setPosition(GLfloat _x, GLfloat _y, GLfloat _z, GLfloat _w) {
position[0] = _x;
position[1] = _y;
position[2] = _z;
position[3] = _w;
glLightfv(name, GL_POSITION, position);
}
void Light::rePosition(void) {
glLightfv(name, GL_POSITION, position);
}
void Light::setSpotDirection(GLfloat _x, GLfloat _y, GLfloat _z) {
spotDirection[0] = _x;
spotDirection[1] = _y;
spotDirection[2] = _z;
glLightfv(name, GL_SPOT_DIRECTION, spotDirection);
}
void Light::reSpotDirection(void) {
glLightfv(name, GL_SPOT_DIRECTION, spotDirection);
}
void Light::setSpotExponent(GLfloat _exponent) {
spotExponent = _exponent;
glLightf(name, GL_SPOT_EXPONENT, spotExponent);
}
void Light::setSpotCutOff(GLfloat _cutOff) {
spotCutOff = _cutOff;
glLightf(name, GL_SPOT_CUTOFF, spotCutOff);
}
void Light::setAttenuation(GLfloat _constant, GLfloat _linear, GLfloat _quadratic) {
attenuation[0] = _constant;
attenuation[1] = _linear;
attenuation[2] = _quadratic;
glLightf(name, GL_CONSTANT_ATTENUATION, attenuation[0]);
glLightf(name, GL_LINEAR_ATTENUATION, attenuation[1]);
glLightf(name, GL_QUADRATIC_ATTENUATION, attenuation[2]);
}
bool Light::getState(void) {
return enabled;
}
void Light::loadConfig(const string& _name, ptree& _config) {
string path = "config.lights.light-" + _name + ".";
try {
// Set up variables of light.
dynamic = _config.get<bool>(path + "dynamic");
enabled = _config.get<bool>(path + "enabled");
if (enabled == true) {
switchOn();
} else {
switchOff();
}
setAmbient(
_config.get<float>(path + "ambient.r"),
_config.get<float>(path + "ambient.g"),
_config.get<float>(path + "ambient.b"),
_config.get<float>(path + "ambient.a")
);
setDiffuse(
_config.get<float>(path + "diffuse.r"),
_config.get<float>(path + "diffuse.g"),
_config.get<float>(path + "diffuse.b"),
_config.get<float>(path + "diffuse.a")
);
setSpecular(
_config.get<float>(path + "specular.r"),
_config.get<float>(path + "specular.g"),
_config.get<float>(path + "specular.b"),
_config.get<float>(path + "specular.a")
);
setPosition(
_config.get<float>(path + "position.x"),
_config.get<float>(path + "position.y"),
_config.get<float>(path + "position.z"),
_config.get<float>(path + "position.w")
);
setSpotDirection(
_config.get<float>(path + "spot-direction.x"),
_config.get<float>(path + "spot-direction.y"),
_config.get<float>(path + "spot-direction.z")
);
setSpotExponent(_config.get<float>(path + "spot-exponent"));
setSpotCutOff(_config.get<float>(path + "spot-cutoff"));
setAttenuation(
_config.get<float>(path + "attenuation.constant"),
_config.get<float>(path + "attenuation.linear"),
_config.get<float>(path + "attenuation.quadratic")
);
} catch (ptree_bad_path exception) {
cerr << "Light " + _name + "::loadConfig -> PTree Bad Path." << endl << exception.what() << endl;
exit(1);
} catch (ptree_bad_data exception) {
cerr << "Light " + _name + "::loadConfig -> PTree Bad Data." << endl << exception.what() << endl;
exit(1);
}
}
ostream & operator<<(ostream& os, Light& light) {
os << "Light " << light.id << endl;
os << "- ambient(" << light.ambient[0] << ", " << light.ambient[1] << ", " << light.ambient[2] << ", " << light.ambient[3] << ")" << endl;
os << "- diffuse(" << light.diffuse[0] << ", " << light.diffuse[1] << ", " << light.diffuse[2] << ", " << light.diffuse[3] << ")" << endl;
os << "- specular(" << light.specular[0] << ", " << light.specular[1] << ", " << light.specular[2] << ", " << light.specular[3] << ")" << endl;
os << "- position(" << light.position[0] << ", " << light.position[1] << ", " << light.position[2] << ", " << light.position[3] << ")" << endl;
os << "- spotDirection(" << light.spotDirection[0] << ", " << light.spotDirection[1] << ", " << light.spotDirection[2] << ", " << light.spotDirection[3] << ")" << endl;
os << "- spotExponent(" << light.spotExponent << ")" << endl;
os << "- spotCutOff(" << light.spotCutOff << ")" << endl;
os << "- attenuation(" << light.attenuation[0] << ", " << light.attenuation[1] << ", " << light.attenuation[2] << ")" << endl;
os << "- state(" << light.enabled << ")" << endl;
os << endl;
return os;
}