-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIluminación.cpp
313 lines (240 loc) · 8.16 KB
/
Iluminación.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
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
/*
Semestre 2022-2
Pr�ctica 8: Iluminaci�n 1
*/
//para cargar imagen
#define STB_IMAGE_IMPLEMENTATION
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <vector>
#include <math.h>
#include <glew.h>
#include <glfw3.h>
#include <glm.hpp>
#include <gtc\matrix_transform.hpp>
#include <gtc\type_ptr.hpp>
//para probar el importer
//#include<assimp/Importer.hpp>
#include "Window.h"
#include "Mesh.h"
#include "Shader_light.h"
#include "Camera.h"
#include "Texture.h"
#include "Sphere.h"
#include"Model.h"
#include "Skybox.h"
//para iluminaci�n
#include "CommonValues.h"
#include "DirectionalLight.h"
#include "PointLight.h"
#include "SpotLight.h"
#include "Material.h"
const float toRadians = 3.14159265f / 180.0f;
Window mainWindow;
std::vector<Mesh*> meshList;
std::vector<Shader> shaderList;
Camera camera;
Texture brickTexture;
Texture dirtTexture;
Texture plainTexture;
Texture pisoTexture;
Model Kitt_M;
Model Llanta_M;
Model Camino_M;
Model Blackhawk_M;
Model Dado_M;
Skybox skybox;
//materiales
Material Material_brillante;
Material Material_opaco;
//Sphere cabeza = Sphere(0.5, 20, 20);
GLfloat deltaTime = 0.0f;
GLfloat lastTime = 0.0f;
static double limitFPS = 1.0 / 60.0;
// luz direccional
DirectionalLight mainLight;
//para declarar varias luces de tipo pointlight
PointLight pointLights[MAX_POINT_LIGHTS];
SpotLight spotLights[MAX_SPOT_LIGHTS];
// Vertex Shader
static const char* vShader = "shaders/shader_light.vert";
// Fragment Shader
static const char* fShader = "shaders/shader_light.frag";
void CreateObjects()
{
unsigned int indices[] = {
0, 3, 1,
1, 3, 2,
2, 3, 0,
0, 1, 2
};
GLfloat vertices[] = {
// x y z u v nx ny nz
-1.0f, -1.0f, -0.6f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, -1.0f, 1.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f,
1.0f, -1.0f, -0.6f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f
};
unsigned int floorIndices[] = {
0, 2, 1,
1, 2, 3
};
GLfloat floorVertices[] = {
-10.0f, 0.0f, -10.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f,
10.0f, 0.0f, -10.0f, 10.0f, 0.0f, 0.0f, -1.0f, 0.0f,
-10.0f, 0.0f, 10.0f, 0.0f, 10.0f, 0.0f, -1.0f, 0.0f,
10.0f, 0.0f, 10.0f, 10.0f, 10.0f, 0.0f, -1.0f, 0.0f
};
Mesh *obj1 = new Mesh();
obj1->CreateMesh(vertices, indices, 32, 12);
meshList.push_back(obj1);
Mesh *obj2 = new Mesh();
obj2->CreateMesh(vertices, indices, 32, 12);
meshList.push_back(obj2);
Mesh *obj3 = new Mesh();
obj3->CreateMesh(floorVertices, floorIndices, 32, 6);
meshList.push_back(obj3);
}
void CreateShaders()
{
Shader *shader1 = new Shader();
shader1->CreateFromFiles(vShader, fShader);
shaderList.push_back(*shader1);
}
int main()
{
mainWindow = Window(1366, 768); // 1280, 1024 or 1024, 768
mainWindow.Initialise();
CreateObjects();
CreateShaders();
camera = Camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), -60.0f, 0.0f, 5.0f, 0.5f);
brickTexture = Texture("Textures/brick.png");
brickTexture.LoadTextureA();
dirtTexture = Texture("Textures/dirt.png");
dirtTexture.LoadTextureA();
plainTexture = Texture("Textures/plain.png");
plainTexture.LoadTextureA();
pisoTexture = Texture("Textures/piso.tga");
pisoTexture.LoadTextureA();
Kitt_M = Model();
Kitt_M.LoadModel("Models/kitt_optimizado.obj");
Llanta_M = Model();
Llanta_M.LoadModel("Models/k_rueda.3ds");
Blackhawk_M = Model();
Blackhawk_M.LoadModel("Models/uh60.obj");
Camino_M = Model();
Camino_M.LoadModel("Models/railroad track.obj");
std::vector<std::string> skyboxFaces;
skyboxFaces.push_back("Textures/Skybox/cupertin-lake_rt.tga");
skyboxFaces.push_back("Textures/Skybox/cupertin-lake_lf.tga");
skyboxFaces.push_back("Textures/Skybox/cupertin-lake_dn.tga");
skyboxFaces.push_back("Textures/Skybox/cupertin-lake_up.tga");
skyboxFaces.push_back("Textures/Skybox/cupertin-lake_bk.tga");
skyboxFaces.push_back("Textures/Skybox/cupertin-lake_ft.tga");
skybox = Skybox(skyboxFaces);
Material_brillante = Material(4.0f, 256);
Material_opaco = Material(0.3f, 4);
//luz direccional, s�lo 1 y siempre debe de existir
mainLight = DirectionalLight(1.0f, 1.0f, 1.0f,
0.3f, 0.3f,
0.0f, 0.0f, -1.0f);
//contador de luces puntuales
unsigned int pointLightCount = 0;
//Declaraci�n de primer luz puntual
pointLights[0] = PointLight(1.0f, 0.0f, 0.0f,
0.0f, 1.0f,
2.0f, 1.5f, 1.5f,
0.3f, 0.2f, 0.1f);
pointLightCount++;
unsigned int spotLightCount = 0;
//linterna
spotLights[0] = SpotLight(1.0f, 1.0f, 1.0f,
0.0f, 2.0f,
0.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
5.0f);
spotLightCount++;
//luz fija
spotLights[1] = SpotLight(0.0f, 0.0f, 1.0f,
1.0f, 2.0f,
5.0f, 10.0f, 0.0f,
0.0f, -5.0f, 0.0f,
1.0f, 0.0f, 0.0f,
15.0f);
spotLightCount++;
//luz de helic�ptero
//luz de faro
GLuint uniformProjection = 0, uniformModel = 0, uniformView = 0, uniformEyePosition = 0,
uniformSpecularIntensity = 0, uniformShininess = 0;
GLuint uniformColor = 0;
glm::mat4 projection = glm::perspective(45.0f, (GLfloat)mainWindow.getBufferWidth() / mainWindow.getBufferHeight(), 0.1f, 1000.0f);
////Loop mientras no se cierra la ventana
while (!mainWindow.getShouldClose())
{
GLfloat now = glfwGetTime();
deltaTime = now - lastTime;
deltaTime += (now - lastTime) / limitFPS;
lastTime = now;
//Recibir eventos del usuario
glfwPollEvents();
camera.keyControl(mainWindow.getsKeys(), deltaTime);
camera.mouseControl(mainWindow.getXChange(), mainWindow.getYChange());
// Clear the window
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
skybox.DrawSkybox(camera.calculateViewMatrix(), projection);
shaderList[0].UseShader();
uniformModel = shaderList[0].GetModelLocation();
uniformProjection = shaderList[0].GetProjectionLocation();
uniformView = shaderList[0].GetViewLocation();
uniformEyePosition = shaderList[0].GetEyePositionLocation();
uniformColor = shaderList[0].getColorLocation();
//informaci�n en el shader de intensidad especular y brillo
uniformSpecularIntensity = shaderList[0].GetSpecularIntensityLocation();
uniformShininess = shaderList[0].GetShininessLocation();
glUniformMatrix4fv(uniformProjection, 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(uniformView, 1, GL_FALSE, glm::value_ptr(camera.calculateViewMatrix()));
glUniform3f(uniformEyePosition, camera.getCameraPosition().x, camera.getCameraPosition().y, camera.getCameraPosition().z);
// luz ligada a la c�mara de tipo flash
glm::vec3 lowerLight = camera.getCameraPosition();
lowerLight.y -= 0.3f;
spotLights[0].SetFlash(lowerLight, camera.getCameraDirection());
//informaci�n al shader de fuentes de iluminaci�n
shaderList[0].SetDirectionalLight(&mainLight);
shaderList[0].SetPointLights(pointLights, pointLightCount);
shaderList[0].SetSpotLights(spotLights, spotLightCount);
glm::mat4 model(1.0);
glm::mat4 modelaux(1.0);
glm::vec3 color = glm::vec3(1.0f, 1.0f, 1.0f);
model = glm::mat4(1.0);
model = glm::translate(model, glm::vec3(0.0f, -2.0f, 0.0f));
model = glm::scale(model, glm::vec3(30.0f, 1.0f, 30.0f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
glUniform3fv(uniformColor, 1, glm::value_ptr(color));
pisoTexture.UseTexture();
Material_opaco.UseMaterial(uniformSpecularIntensity, uniformShininess);
meshList[2]->RenderMesh();
model = glm::mat4(1.0);
model = glm::translate(model, glm::vec3(0.0f, 0.5f, -1.5f));
modelaux = model;
model = glm::scale(model, glm::vec3(0.5f, 0.5f, 0.5f));
model = glm::rotate(model, -90 * toRadians, glm::vec3(0.0f, 1.0f, 0.0f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
Kitt_M.RenderModel();
model = glm::mat4(1.0);
model = glm::translate(model, glm::vec3(0.0f, 3.0f, -1.0));
model = glm::scale(model, glm::vec3(0.3f, 0.3f, 0.3f));
model = glm::rotate(model, -90 * toRadians, glm::vec3(1.0f, 0.0f, 0.0f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
Blackhawk_M.RenderModel();
model = glm::mat4(1.0);
model = glm::translate(model, glm::vec3(0.0f, -1.53f, 0.0f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
Camino_M.RenderModel();
glUseProgram(0);
mainWindow.swapBuffers();
}
return 0;
}