-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLight.cpp
243 lines (197 loc) · 5.51 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
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
// @Begin License@
// This file is part of Coldest.
//
// Coldest is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Coldest 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Coldest. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2008-2012 Ben Nemec
// @End License@
#include "Light.h"
long int Light::infinity = 10000;
Light::Light()
{
}
int Light::Add()
{
vector<GLfloat> temp;
for (int i = 0; i < 4; ++i)
temp.push_back(1);
posdir.push_back(Vector3());
diffuse.push_back(temp);
ambient.push_back(temp);
specular.push_back(temp);
view.push_back(GraphicMatrix());
proj.push_back(GraphicMatrix());
type.push_back(0);
return 0; // Why does this function return a value?
}
void Light::Clear()
{
posdir.clear();
diffuse.clear();
ambient.clear();
specular.clear();
view.clear();
proj.clear();
type.clear();
}
/* Note that SetPos and SetDir are mutually exclusive. If you use one
then you shouldn't use the other or the type of your light will
change.
*/
void Light::SetPos(int num, Vector3 position)
{
if (!isvalid(num)) return;
posdir[num] = position;
type[num] = 1;
}
void Light::SetDir(int num, Vector3 direction)
{
if (!isvalid(num)) return;
posdir[num] = direction;
posdir[num].normalize();
type[num] = 0;
}
/* This works for either type because returning a point very far away
on the vector posdir approximates a directional light position,
which is useful for shadowing.
*/
Vector3 Light::GetPos(int num)
{
if (!isvalid(num)) return Vector3();
if (type[num] == 1) return posdir[num];
return (posdir[num] * infinity);
}
// Returns 0 vector for positional lights, returns posdir for directional ones
Vector3 Light::GetDir(int num)
{
if (!isvalid(num)) return Vector3();
if (type[num] == 1) return Vector3();
return posdir[num];
}
void Light::SetDiffuse(int num, GLfloat values[4])
{
if (!isvalid(num)) return;
for (int i = 0; i < 4; ++i)
diffuse[num][i] = values[i];
}
void Light::SetAmbient(int num, GLfloat values[4])
{
if (!isvalid(num)) return;
for (int i = 0; i < 4; ++i)
ambient[num][i] = values[i];
}
void Light::SetSpecular(int num, GLfloat values[4])
{
if (!isvalid(num)) return;
for (int i = 0; i < 4; ++i)
specular[num][i] = values[i];
}
// Note: As you can see, only two lights are currently supported.
void Light::Place()
{
GLfloat v[4];
// Place the lights
for (int i = 0; i < NumLights(); ++i)
{
v[0] = posdir[i].x;
v[1] = posdir[i].y;
v[2] = posdir[i].z;
v[3] = type[i];
switch(i)
{
case 0:
glLightfv(GL_LIGHT0, GL_POSITION, v);
glLightfv(GL_LIGHT0, GL_DIFFUSE, &diffuse[i][0]);
glLightfv(GL_LIGHT0, GL_AMBIENT, &ambient[i][0]);
glLightfv(GL_LIGHT0, GL_SPECULAR, &specular[i][0]);
break;
case 1:
glLightfv(GL_LIGHT1, GL_POSITION, v);
glLightfv(GL_LIGHT1, GL_DIFFUSE, &diffuse[i][0]);
glLightfv(GL_LIGHT1, GL_AMBIENT, &ambient[i][0]);
glLightfv(GL_LIGHT1, GL_SPECULAR, &specular[i][0]);
break;
}
}
}
GraphicMatrix Light::GetView(int num, Vector3 lookat)
{
if (!isvalid(num)) return GraphicMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
{
glLoadIdentity();
Vector3 up(0, 1, 0);
Vector3 p = GetPos(num);
p += lookat;
// Not allowed to look straight down the up vector
if (p.x == lookat.x && p.z == lookat.z)
up = Vector3(0, 0, 1);
gluLookAt(p.x, p.y, p.z, lookat.x, lookat.y, lookat.z, up.x, up.y, up.z);
glGetFloatv(GL_MODELVIEW_MATRIX, view[num]);
}
glPopMatrix();
return view[num];
}
GraphicMatrix Light::GetProj(int num, float viewsize)
{
if (!isvalid(num)) return GraphicMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
{
glLoadIdentity();
glOrtho(-viewsize, viewsize, -viewsize, viewsize, infinity - 5000, infinity + 5000);
glGetFloatv(GL_MODELVIEW_MATRIX, proj[num]);
}
glPopMatrix();
return proj[num];
}
// These really only need to be calculated once, but for now this works
Vector3 Light::GetRots(int num)
{
if (!isvalid(num)) return Vector3();
Vector3 rots;
Vector3 dir = GetDir(num);
Vector3 start(0, 0, -1); // Initial view direction
dir *= -1; // Dir is where it's coming from, we need where it's going to
dir.y = 0;
dir.normalize();
rots.y = acos(start.dot(dir)) * 180.f / 3.14159265;
if (start.cross(dir).y >= 0)
rots.y *= -1;
dir = GetDir(num);
dir *= -1;
GraphicMatrix rotm;
rotm.rotatey(rots.y);
start.transform(rotm);
rots.x = acos(start.dot(dir)) * 180.f / 3.14159265;
/*rots.print(); // Debugging
start = Vector3(0, 0, -1);
rotm.identity();
rotm.rotatex(rots.x);
rotm.rotatey(rots.y);
start.transform(rotm);
start.print();
dir.print();
logout << endl;*/
return rots;
}
int Light::NumLights()
{
return posdir.size();
}
bool Light::isvalid(int num)
{
return (num >= 0 || num < NumLights());
}