-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
363 lines (292 loc) · 11.3 KB
/
main.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#define STB_IMAGE_IMPLEMENTATION
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <vector>
#include "CommonValues.h"
#include "common/Mesh.h"
#include "common/Shader.h"
#include "common/GLWindow.h"
#include "common/Camera.h"
#include "common/Texture.h"
#include "common/DirectionalLight.h"
#include "common/PointLight.h"
#include "common/SpotLight.h"
#include "common/Material.h"
#include "common/Model.h"
#include <assimp/Importer.hpp>
const float toRadians = 3.14159265f / 180.0f;
std::vector<Mesh *> meshList;
std::vector<Shader> shaderList;
GLWindow mainWindow;
Camera camera;
Material shinyMaterial;
Material dullMaterial;
Model enterprise;
GLfloat deltaTime = 0.0f;
GLfloat lastTime = 0.0f;
Texture brinkTexture;
Texture dirtyTexture;
Texture plainTexture;
Texture brick2Texture;
Texture roadTexture;
DirectionalLight mainLight;
PointLight pointLights[MAX_POINT_LIGHTS];
SpotLight spotLights[MAX_SPOT_LIGHTS];
void FramebufferResize(GLFWwindow *window, float height, float width);
// Vertex Shader
static const char *vShader = "shaders/shader.vert";
// Fragment Shader
static const char *fShader = "shaders/shader.frag";
void calcAverageNormals(unsigned int *indices,
unsigned int indiceCount,
GLfloat *vertices,
unsigned int verticesCount,
unsigned int vLength,
unsigned int normalOffset)
{
for (size_t i = 0; i < indiceCount; i += 3)
{
unsigned int in0 = indices[i] * vLength;
unsigned int in1 = indices[i + 1] * vLength;
unsigned int in2 = indices[i + 2] * vLength;
glm::vec3 v1(vertices[in1] - vertices[in0],
vertices[in1 + 1] - vertices[in0 + 1],
vertices[in1 + 2] - vertices[in0 + 2]);
glm::vec3 v2(vertices[in2] - vertices[in0],
vertices[in2 + 1] - vertices[in0 + 1],
vertices[in2 + 2] - vertices[in0 + 2]);
glm::vec3 normal = glm::cross(v1, v2);
normal = glm::normalize(normal);
in0 += normalOffset;
in1 += normalOffset;
in2 += normalOffset;
vertices[in0] += normal.x;
vertices[in0 + 1] += normal.y;
vertices[in0 + 2] += normal.z;
vertices[in1] += normal.x;
vertices[in1 + 1] += normal.y;
vertices[in1 + 2] += normal.z;
vertices[in2] += normal.x;
vertices[in2 + 1] += normal.y;
vertices[in2 + 2] += normal.z;
}
for (size_t i = 0; i < verticesCount / vLength; i++)
{
unsigned int nOffset = i * vLength + normalOffset;
glm::vec3 vec(vertices[nOffset], vertices[nOffset + 1], vertices[nOffset + 2]);
vec = glm::normalize(vec);
vertices[nOffset] = vec.x;
vertices[nOffset + 1] = vec.y;
vertices[nOffset + 2] = vec.z;
}
}
void CreateObjects()
{
unsigned int indices[] = {
0, 3, 1, // Side
1, 3, 2, // Side
2, 3, 0, // Front
0, 1, 2}; // base
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, // Bottom Left
0.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Bottom Back (Middle)
1.0f, -1.0f, -0.6f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Bottom Right
0.0f, 1.0f, 0.0f, 1.0f, 2.0f, 0.0f, 0.0f, 0.0f}; // Top
GLfloat floorVertices[] = {
-10.0f, 0.0f, -10.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, // back left
10.0f, 0.0f, -10.0f, 10.0f, 0.0f, 0.0f, -1.0f, 0.0f, // back right
-10.0f, 0.0f, 10.0f, 0.0f, 10.0f, 0.0f, -1.0f, 0.0f, // front left
10.0f, 0.0f, 10.0f, 10.0f, 10.0f, 0.0f, -1.0f, 0.0f, // front right
};
unsigned int floorIndices[] = {
0, 2, 1, // tri 1
1, 2, 3 // tri 2
};
calcAverageNormals(indices, 12, vertices, 32, 8, 5);
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);
}
// Create a cube
void CreateCube()
{
unsigned int indices[] = {
0, 1, 2, // Front - tstartUpop triangle
1, 2, 3, // Front
4, 5, 6, // Back - top triangle
5, 6, 7, // Back
1, 5, 3, // Right Side
3, 5, 7, // Right Side Back
0, 4, 6, // Left
0, 6, 2, // Left
0, 4, 5, // startUp
0, 1, 5, // Top
2, 6, 7, // Bottom
2, 3, 7 // Bottom
};
GLfloat vertices[] = {
// x y z u v nx ny nz
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, // 0 Left Top Front
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, // 1 Right Top Front
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // 2 Left Bottom Front
1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // 3 Right Bottom Front
-1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, // 4 Left Top Back
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, // 5 Right Top Back
-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // 6 Left Bottom Back
1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f}; // 7 Right Bottom Back*/
Mesh *cube1 = new Mesh();
cube1->CreateMesh(vertices, indices, 64, 36);
meshList.push_back(cube1);
}
void CreateShaders()
{
Shader *shader1 = new Shader();
shader1->CreateFromFiles(vShader, fShader);
shaderList.push_back(*shader1);
}
int main()
{
mainWindow = GLWindow(1366, 768);
mainWindow.Initialise();
CreateObjects();
CreateCube();
CreateShaders();
camera = Camera(glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f),
-90.0f,
0.0f,
5.0f,
0.5f);
brinkTexture = Texture((char *)"textures/brick.png");
brinkTexture.LoadTextureA();
dirtyTexture = Texture((char *)"textures/dirt.png");
dirtyTexture.LoadTextureA();
plainTexture = Texture((char *)"textures/plain.png");
plainTexture.LoadTextureA();
roadTexture = Texture((char *)"textures/road.jpg");
roadTexture.LoadTexture();
shinyMaterial = Material(4.0f, 256);
dullMaterial = Material(0.3f, 4);
enterprise = Model();
enterprise.LoadModel("models/enterprise1701d.obj");
brick2Texture = Texture((char *)"textures/wall.jpg");
brick2Texture.LoadTexture();
dirtyTexture.UseTexture();
mainLight = DirectionalLight(1.0f, 1.0f, 1.0f, // colour and
0.3f, 0.6f, // 0.3, 0.4 Ambient intensity and Intensity of light for diffuse
0.0f, 0.0f, -1.0f); // Position of light
unsigned int pointLightCount = 0;
pointLights[0] = PointLight(0.0f, 0.0f, 1.0f,
0.3f, 0.1f,
4.0f, 0.0f, 0.0f,
0.3f, 0.2f, 0.1f);
pointLightCount++;
pointLights[1] = PointLight(0.0f, 1.0f, 0.0f,
0.3f, 0.1f,
-4.0f, 2.0f, 0.0f,
0.3f, 0.1f, 0.1f);
pointLightCount++;
unsigned int spotLightCount = 0;
spotLights[0] = SpotLight(1.0f, 1.0f, 1.0f,
0.0f, 2.0f,
1.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
20.0f);
spotLightCount++;
spotLights[1] = SpotLight(1.0f, 1.0f, 1.0f,
0.0f, 1.0f,
0.0f, -1.5f, 0.0f,
-100.0f, -1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
20.0f);
spotLightCount++;
GLuint uniformProjection = 0, uniformModel = 0, uniformView = 0;
glm::mat4 projection = glm::perspective(45.0f, (GLfloat)mainWindow.getBufferWidth() / (GLfloat)mainWindow.getBufferHeight(), 0.1f, 100.0f);
// uncomment this call to draw in wireframe polygons.
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// main loop until window closed
while (!mainWindow.getShouldClose())
{
GLfloat currentTime = glfwGetTime(); // SDL_GetPErformanceCounter() <- Need to convert to seconds
deltaTime = currentTime - lastTime; // (currentTime-lastTime)*1000 / SDL_GetPerformanceFrequency() converts it to seconds
lastTime = currentTime;
//clear the windowglm::value_ptr(projection)
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera.keyControl(mainWindow.getsKeys(), deltaTime);
camera.mouseControl(mainWindow.getXChange(), mainWindow.getYChange());
shaderList[0].UseShader();
glm::vec3 lowerLight = camera.getCameraPosition();
lowerLight.y -= 0.3f; // More realistic position of flash light (or say a game)
//spotLights[0].SetFlash(lowerLight, camera.getCameraDirection());
shaderList[0].SetDirectionalLight(&mainLight);
shaderList[0].SetPointLights(pointLights, pointLightCount);
shaderList[0].SetSpotLights(spotLights, spotLightCount);
glUniformMatrix4fv(shaderList[0].GetProjectionLocation(), 1, GL_FALSE, glm::value_ptr(projection));
//float projectionVal[16] = {0.0f};
//glGetUniformfv(shaderList[0].GetShaderID(), shaderList[0].GetProjectionLocation(), projectionVal);
glUniformMatrix4fv(shaderList[0].GetViewLocation(), 1, GL_FALSE, glm::value_ptr(camera.calculateViewMatrix()));
//float viewVal[16] = {0.0f};
//glGetUniformfv(shaderList[0].GetShaderID(), shaderList[0].GetViewLocation(), viewVal);
glUniform3f(shaderList[0].GetEyePositionLocation(), camera.getCameraPosition().x, camera.getCameraPosition().y, camera.getCameraPosition().z);
// Triangle 1
glm::mat4 model(1.0f);
model = glm::translate(model, glm::vec3(0.0f, 0.0f, -2.5f));
glUniformMatrix4fv(uniformModel = shaderList[0].GetModelLocation(), 1, GL_FALSE, glm::value_ptr(model));
//float modelVal[16] = {0.0f};
//glGetUniformfv(shaderList[0].GetShaderID(), shaderList[0].GetModelLocation(), modelVal);
brinkTexture.UseTexture();
shinyMaterial.UseMaterial(shaderList[0].GetSpecularIntensityLocation(), shaderList[0].GetShininess());
meshList[0]->RenderMesh();
// Triangle 2
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(4.0f, 0.0f, -2.5f));
//model = glm::scale(model, glm::vec3(0.4f, 0.4f, 1.0f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
dirtyTexture.UseTexture();
dullMaterial.UseMaterial(shaderList[0].GetSpecularIntensityLocation(), shaderList[0].GetShininess());
meshList[1]->RenderMesh();
//Floor
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.0f, -2.0f, -2.5f));
//model = glm::scale(model, glm::vec3(0.4f, 0.4f, 1.0f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
roadTexture.UseTexture();
shinyMaterial.UseMaterial(shaderList[0].GetSpecularIntensityLocation(), shaderList[0].GetShininess());
meshList[2]->RenderMesh();
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(-20.0f, -2.0f, 0.0f));
model = glm::scale(model, glm::vec3(0.5f, 0.5f, 0.5f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
shinyMaterial.UseMaterial(shaderList[0].GetSpecularIntensityLocation(), shaderList[0].GetShininess());
enterprise.RenderModel();
// Cube
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(-4.0f, 0.0f, -3.5f));
model = glm::rotate(model, 60 * toRadians, glm::vec3(0.0f, 1.0f, 0.0f));
model = scale(model, glm::vec3(0.4f, 0.4f, 1.0f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE, glm::value_ptr(model));
shinyMaterial.UseMaterial(shaderList[0].GetSpecularIntensityLocation(), shaderList[0].GetShininess());
brick2Texture.UseTexture();
meshList[3]->RenderMesh();
glUseProgram(0);
mainWindow.swapBuffers();
// get and handle user input events
glfwPollEvents();
}
return 0;
}