-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
221 lines (182 loc) · 6.93 KB
/
main.cpp
File metadata and controls
221 lines (182 loc) · 6.93 KB
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
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
#include <vector>
// Include GLEW
#include <GL/glew.h>
// Include GLFW
#include <GLFW/glfw3.h>
GLFWwindow* window;
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;
using namespace std;
#include <shader.hpp>
#include <controls.hpp>
#include <texture.hpp>
#include <objloader.hpp>
#include <gameobj.hpp>
#include <lights.hpp>
#define MAX_LIGHTS 9
int main(void)
{
// GLFW and GLEW initialization
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
getchar();
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(1980, 1080, "Project CL-3", NULL, NULL);
if (window == NULL) {
fprintf(stderr, "Failed to open GLFW window.\n");
getchar();
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
getchar();
glfwTerminate();
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
glfwPollEvents();
glfwSetCursorPos(window, 1980 / 2, 1080 / 2);
glClearColor(0.3f, 0.2f, 0.4f, 0.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glFrontFace(GL_CW);
printf("\n=== INITIALIZING ===\n");
LightManager lightManager;
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders("VertexShader.vertexshader", "FragmentShader.fragmentshader");
// emissive shader only for lights
GLuint emissiveProgramID = LoadShaders("VertexShader.vertexshader", "emissive.fragmentshader");
GLuint Emissive_MatrixID = glGetUniformLocation(emissiveProgramID, "MVP");
// normal mapping shader
GLuint normalShaderID = LoadShaders("NormalMapping.vertexshader", "NormalMapping.fragmentshader");
GLuint NM_MatrixID = glGetUniformLocation(normalShaderID, "MVP");
GLuint NM_ModelMatrixID = glGetUniformLocation(normalShaderID, "M");
GLuint NM_ViewPosID = glGetUniformLocation(normalShaderID, "ViewPos_worldspace");
GLuint NM_DiffuseID = glGetUniformLocation(normalShaderID, "diffuseMap");
GLuint NM_normalID = glGetUniformLocation(normalShaderID, "normalMap");
GLuint NM_LightPosIDs[MAX_LIGHTS];
GLuint NM_LightColorIDs[MAX_LIGHTS];
// Get Uniform Handles
GLuint MatrixID = glGetUniformLocation(programID, "MVP");
GLuint ModelMatrixID = glGetUniformLocation(programID, "M");
GLuint ViewMatrixID = glGetUniformLocation(programID, "V");
GLuint TextureID = glGetUniformLocation(programID, "myTextureSampler");
GLuint ViewPosID = glGetUniformLocation(programID, "ViewPos_worldspace");
vector<bool> keyPressed(MAX_LIGHTS, false); // for light toggling
GLuint LightPosIDs[MAX_LIGHTS];
GLuint LightColorIDs[MAX_LIGHTS];
for (int i = 0; i < MAX_LIGHTS; i++) {
std::string posName = "LightPositions[" + std::to_string(i) + "]";
std::string colName = "LightColors[" + std::to_string(i) + "]";
LightPosIDs[i] = glGetUniformLocation(programID, posName.c_str());
LightColorIDs[i] = glGetUniformLocation(programID, colName.c_str());
NM_LightPosIDs[i] = glGetUniformLocation(normalShaderID, posName.c_str());
NM_LightColorIDs[i] = glGetUniformLocation(normalShaderID, colName.c_str());
}
printf("CREATING OBJECTS\n");
vector<GameObject*> sceneObjects = loadScene("classroom.obj");
lightManager.initFromScene(sceneObjects);
printf("ENTERING RENDER LOOP\n");
do {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
computeMatricesFromInputs();
mat4 ProjectionMatrix = getProjectionMatrix();
mat4 ViewMatrix = getViewMatrix();
// SET THE UNIFORMS THAT ARE CONSTANT FOR ALL OBJECTS!
glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, &ViewMatrix[0][0]);
setLights(lightManager, window, keyPressed);
// light data
for (int lightIdx = 0; lightIdx < MAX_LIGHTS; lightIdx++) {
vec3 pos = lightManager.getLightPosition(lightIdx);
glUniform3f(LightPosIDs[lightIdx], pos.x, pos.y, pos.z);
if (lightManager.lightStates[lightIdx] == true) {
glUniform3f(LightColorIDs[lightIdx], 0.3f, 0.3f, 0.28f); // ON
}
else {
glUniform3f(LightColorIDs[lightIdx], 0.0f, 0.0f, 0.0f); // OFF
}
}
// Set camera position - get it from controls.hpp
vec3 cameraPos = getCameraPosition();
glUniform3f(ViewPosID, cameraPos.x, cameraPos.y, cameraPos.z);
// Draw all the objects
for (size_t i = 0; i < sceneObjects.size(); i++) {
bool isGlowing = (sceneObjects[i]->lightID != -1 && lightManager.lightStates[sceneObjects[i]->lightID - 1]) ? true : false;
if(isGlowing) {
// draw with emissive shader
glUseProgram(emissiveProgramID);
mat4 ModelMatrix = sceneObjects[i]->modelMatrix;
mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
glUniformMatrix4fv(Emissive_MatrixID, 1, GL_FALSE, &MVP[0][0]);
glBindVertexArray(sceneObjects[i]->VAO);
glDrawArrays(GL_TRIANGLES, 0, sceneObjects[i]->vertexCount);
}
else if(sceneObjects[i]->isWall) {
// 1. ACTIVATE SHADER
glUseProgram(normalShaderID);
// 2. BIND TEXTURES
// Bind Diffuse to Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, sceneObjects[i]->textureID);
glUniform1i(NM_DiffuseID, 0);
// Bind Normal Map to Unit 1
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, sceneObjects[i]->normalMapID);
glUniform1i(NM_normalID, 1);
// 3. SEND MATRICES & CAMERA
// Calculate MVP for this specific object
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * sceneObjects[i]->modelMatrix;
glUniformMatrix4fv(NM_MatrixID, 1, GL_FALSE, &MVP[0][0]);
glUniformMatrix4fv(NM_ModelMatrixID, 1, GL_FALSE, &sceneObjects[i]->modelMatrix[0][0]);
glUniform3f(NM_ViewPosID, cameraPos.x, cameraPos.y, cameraPos.z);
// 4. SEND LIGHTS (Crucial!)
// We must re-send light data to this shader
for (int k = 0; k < MAX_LIGHTS; k++) {
// Position
vec3 pos = lightManager.getLightPosition(k);
glUniform3f(NM_LightPosIDs[k], pos.x, pos.y, pos.z);
// Color
if (lightManager.lightStates[k] == true) {
glUniform3f(NM_LightColorIDs[k], 0.3f, 0.3f, 0.28f); // Light is ON
}
else {
glUniform3f(NM_LightColorIDs[k], 0.0f, 0.0f, 0.0f); // Light is OFF
}
}
// 5. DRAW
glBindVertexArray(sceneObjects[i]->VAO);
glDrawArrays(GL_TRIANGLES, 0, sceneObjects[i]->vertexCount);
}
else {
glUseProgram(programID);
sceneObjects[i]->Draw(ProjectionMatrix, ViewMatrix,
MatrixID, ModelMatrixID, TextureID);
}
}
glfwSwapBuffers(window);
glfwPollEvents();
} while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
printf("\nEXITING\n");
for (size_t i = 0; i < sceneObjects.size(); i++) {
delete sceneObjects[i];
}
sceneObjects.clear();
glDeleteProgram(programID);
glfwTerminate();
return 0;
}