This repository has been archived by the owner on Jan 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlight_system.cpp
executable file
·251 lines (205 loc) · 9.28 KB
/
light_system.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
242
243
244
245
246
247
248
249
250
251
/**
* SPG Assignment #2 - 2013
*
* Author: Stamate Cosmin, 342C4
*
* Description: Light ensemble system, controlling all the light sources and
* the main light source, the sun.
*/
#include "light_system.h"
// Sun constructor. Loads the texture and initializes the vectors.
Sun::Sun(Camera* camera)
:Movable(SUN_POSITION, glm::vec3(), glm::vec3(), glm::vec3()) {
this->camera = camera;
this->texture = lab::loadTextureBMP("resources\\sun.bmp");
}
// Deconstructor.
Sun::~Sun() {
glDeleteBuffers(1, &(this->texture));
}
// Render the sun. The sun is composed of a plane with a texture applied to it.
// For realism, it is always facing the the camera.
// The sun is also placed always at a fixed position, relative to the camera,
// so it always stays away enough from the camera.
void Sun::render(unsigned int shader, glm::mat4 model_matrix) {
// Update the sun's position.
glm::vec3 sun_position = this->position +
glm::vec3(this->camera->position.x, 0, 0);
// Compute the sun vectors such that it faces the camera.
this->forward = glm::normalize(this->camera->position - sun_position);
this->right = glm::normalize(glm::cross(glm::vec3(0, 1, 0), this->forward));
this->up = glm::normalize(glm::cross(this->forward, this->right));
// Build the rotation matrix based on previous calculated vectors.
glm::mat4 rotation_matrix;
rotation_matrix[0] = glm::vec4(this->right, 0);
rotation_matrix[1] = glm::vec4(this->up, 0);
rotation_matrix[2] = glm::vec4(this->forward, 0);
// Send information to the shader.
glUniform1i(glGetUniformLocation(shader, "draw_sun"), true);
glUniform3f(glGetUniformLocation(shader, "sun_position"),
sun_position.x, sun_position.y, sun_position.z);
glUniform4f(glGetUniformLocation(shader, "sun_color"),
SUN_COLOR.r, SUN_COLOR.g, SUN_COLOR.b, SUN_COLOR.a);
glUniform1f(glGetUniformLocation(shader, "sun_size"), SUN_SIZE);
// We're no longer drawing fog for the sun plane, to ensure realism.
glUniform1i(glGetUniformLocation(shader, "fog_switch"), false);
// Send texture data to the shader.
glActiveTexture(GL_TEXTURE0 + 4);
glBindTexture(GL_TEXTURE_2D, this->texture);
glUniform1i(glGetUniformLocation(shader, "sun_texture"), 4);
// Render the basic plane.
RawModelFactory::renderModel(RAW_MODEL_PLANE,
(RawModelMaterial*)&SUN_MATERIAL,
sun_position,
glm::vec3(SUN_SIZE, SUN_SIZE, 1),
model_matrix, rotation_matrix, shader);
// Notify the shader we're no longer rendering the sun plane.
glUniform1i(glGetUniformLocation(shader, "draw_sun"), false);
}
// Instantiate a simple light, with its variables.
Light::Light(unsigned int type, glm::vec3 position, RawModelMaterial* material,
float size) {
this->position = position;
this->type = type;
this->material = material;
this->size = glm::vec3(size, size, size);
}
Light::~Light() {}
// Set a light's type.
void Light::setType(unsigned int type) { this->type = type; }
// Set a light's position.
void Light::move(glm::vec3 movement) {
this->position = this->position + movement;
}
// Get a light's position.
glm::vec3 Light::getPosition() { return this->position; }
// Render a simple model to give a hint as what the light is.
void Light::render(unsigned int shader, glm::vec3 offset,
glm::mat4 model_matrix) {
// If it is a point light, draw a sphere.
if (this->type == LIGHT_OMNI) {
RawModelFactory::renderModel(RAW_MODEL_SPHERE, material,
this->position + offset, this->size, model_matrix, glm::mat4(),
shader);
} // If it is a spotlight, draw a cone.
else {
RawModelFactory::renderModel(RAW_MODEL_CONE, material,
this->position + offset, this->size, model_matrix, glm::mat4(),
shader);
}
}
// Assign variables, initialize the simple random number generator from C++.
LightSystem::LightSystem(unsigned int type, Camera* camera)
: Movable(glm::vec3(0, 0, 0), camera->forward, camera->right, camera->up) {
this->type = type;
this->setRelativePosition(camera->position);
this->light_count = 0;
this->fog = true;
// Initialize random seed.
srand((unsigned int)time(NULL));
this->sun = new Sun(camera);
}
// Deconstructor.
LightSystem::~LightSystem() {}
// Adds a new light to the system.
void LightSystem::addLight() {
// Add a light only if there's still enough space.
if (this->light_count < LIGHT_MAXIMUM_COUNT) {
// Compute the random position.
glm::vec3 position = this->position + glm::vec3(
(rand() % 100) / 100.0f *
LIGHT_MAXIMUM_RADIUS_2 - LIGHT_MAXIMUM_RADIUS,
(rand() % 100) / 100.0f *
LIGHT_MAXIMUM_HEIGHT_2 - LIGHT_MAXIMUM_HEIGHT,
(rand() % 100) / 100.0f *
LIGHT_MAXIMUM_RADIUS_2 - LIGHT_MAXIMUM_RADIUS);
// Compute the basic color.
glm::vec4 color = glm::vec4(
LIGHT_MINIMUM_COLOR.r + (rand() % 100) / 100.0f * LIGHT_RANGE_COLOR.r,
LIGHT_MINIMUM_COLOR.g + (rand() % 100) / 100.0f * LIGHT_RANGE_COLOR.g,
LIGHT_MINIMUM_COLOR.b + (rand() % 100) / 100.0f * LIGHT_RANGE_COLOR.b,
LIGHT_MINIMUM_COLOR.a + (rand() % 100) / 100.0f * LIGHT_RANGE_COLOR.a);
// Compute the size.
float size = LIGHT_MINIMUM_SIZE +
(rand() % 100) / 100.0f * LIGHT_MAXIMUM_SIZE;
// Compute the spotlight angles.
float inner_angle = LIGHT_MINIMUM_SPOT_ANGLE +
(rand() % 100) / 100.0f * LIGHT_RANGE_SPOT_ANGLE;
float outer_angle = inner_angle + LIGHT_FADE_SPOT_ANGLE;
// Assign the material, based on the color.
RawModelMaterial* material = new RawModelMaterial(LIGHT_SHININESS,
color * 1.2f, color, color, color * 1.4f);
// Instantiate the light objec.
Light* new_light = new Light(this->type, position, material, size);
// Assign the light variables, for later use.
this->lights[this->light_count] = new_light;
this->light_colors[this->light_count] = color;
this->light_sizes[this->light_count] = size;
this->light_inner_angles[this->light_count] = glm::cos(inner_angle);
this->light_outer_angles[this->light_count] = glm::cos(outer_angle);
this->light_count++;
}
}
// Switch the lighting system from point to spotlight and vice-versa.
void LightSystem::switchType() {
if (this->type == LIGHT_OMNI) this->type = LIGHT_SPOT;
else this->type = LIGHT_OMNI;
for (int i = 0; i < this->light_count; i++) {
this->lights[i]->setType(type);
}
}
// Update the system's relative position.
void LightSystem::setRelativePosition(glm::vec3 position) {
this->relative_position = position;
}
// Move the light system on the intended path. The system is actually rendered
// based on the relative position.
void LightSystem::move(float time, float angle_y) {
glm::vec3 movement = Movable::move(time, glm::vec2(0, 0));
// Move all the lights.
for (int i = 0; i < this->light_count; i++) {
this->lights[i]->move(movement);
}
this->rotateY(angle_y);
}
// Switch the fog on and off.
void LightSystem::switchFog() { this->fog = !this->fog; }
// Render the light system.
void LightSystem::render(unsigned int shader, glm::mat4 model_matrix) {
glm::vec3 offset = this->relative_position;
// First render the sun's model and light.
this->sun->render(shader, model_matrix);
// Turn the fog on or off and send fog variables.
glUniform1i(glGetUniformLocation(shader, "fog_switch"), this->fog);
glUniform1f(glGetUniformLocation(shader, "fog_start"), FOG_START_RADIUS);
glUniform1f(glGetUniformLocation(shader, "fog_end"), FOG_END_RADIUS);
glUniform4f(glGetUniformLocation(shader, "fog_color"),
FOG_COLOR.x, FOG_COLOR.y, FOG_COLOR.z, FOG_COLOR.a);
// Send ambiental light color.
glUniform4f(glGetUniformLocation(shader, "ambiental_light"),
LIGHT_AMBIENTAL.x, LIGHT_AMBIENTAL.y, LIGHT_AMBIENTAL.z,
LIGHT_AMBIENTAL.a);
// Send global light information.
glUniform1i(glGetUniformLocation(shader, "light_type"), this->type);
glUniform3f(glGetUniformLocation(shader, "spotlight_direction"),
LIGHT_SPOT_DIRECTION.x, LIGHT_SPOT_DIRECTION.y,
LIGHT_SPOT_DIRECTION.z);
// Render all the individual light models.
for (int i = 0; i < this->light_count; i++) {
this->lights[i]->render(shader, offset, model_matrix);
this->light_positions[i] = offset + this->lights[i]->getPosition();
}
// Send light sources information to the shader.
glUniform1i(glGetUniformLocation(shader, "light_count"), this->light_count);
glUniform1i(glGetUniformLocation(shader, "light_type"), this->type);
glUniform3fv(glGetUniformLocation(shader, "light_positions"),
this->light_count, (GLfloat*)this->light_positions);
glUniform4fv(glGetUniformLocation(shader, "light_colors"),
this->light_count, (GLfloat*)this->light_colors);
glUniform1fv(glGetUniformLocation(shader, "light_inner_angles"),
this->light_count, (GLfloat*)this->light_inner_angles);
glUniform1fv(glGetUniformLocation(shader, "light_outer_angles"),
this->light_count, (GLfloat*)this->light_outer_angles);
glUniform1fv(glGetUniformLocation(shader, "light_sizes"),
this->light_count, (GLfloat*)this->light_sizes);
}