-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindow.cpp
366 lines (307 loc) · 10.6 KB
/
Window.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
364
365
366
#include "Window.h"
#include "Model.h"
#include "UI/GUI.h"
#include "main.h"
// Game Manager
GameManager* Window::game = nullptr;
// Window input
//glm::vec2 Window::move_input{0,0};
// Window Properties
int Window::width;
int Window::height;
const char* Window::windowTitle = "GLFW Starter Project";
// Objects to Render
Player* Window::player;
FBO* Window::postprocessing;
FBO* Window::bloom;
// Camera Matrices
// Projection matrix:
glm::mat4 Window::projection;
constexpr float camera_angle = glm::radians(45.0f);
constexpr float camera_dist = 30;
// View Matrix:
glm::vec3 Window::eyePos(0, 0, 20); // Camera position.
glm::vec3 Window::lookAtPoint(0, 0, 0); // The point we are looking at.
glm::vec3 Window::upVector(0, 1, 0); // The up direction of the camera.
glm::mat4 Window::view = glm::lookAt(Window::eyePos, Window::lookAtPoint, Window::upVector);
// Shader Program ID
GLuint Window::worldShaderProgram;
GLuint Window::waterShaderProgram;
GLuint Window::leafShaderProgram;
GLuint Window::modelShaderProgram;
GLuint Window::animationShaderProgram;
GLuint Window::debugShaderProgram;
GLuint Window::domeShaderProgram;
GLuint Window::shadowShaderProgram;
GLuint Window::blurShaderProgram;
GLuint Window::finalShaderProgram;
GLuint Window::particleShaderProgram;
// Shader to Program
std::map<ModelEnum, GLuint> Window::modelShader;
bool Window::initializeProgram() {
// Create a shader program with a vertex shader and a fragment shader.
worldShaderProgram = LoadShaders("shaders/world.vert", "shaders/world.frag");
waterShaderProgram = LoadShaders("shaders/water.vert", "shaders/water.frag");
leafShaderProgram = LoadShaders("shaders/leaves.vert", "shaders/world.frag");
modelShaderProgram = LoadShaders("shaders/model.vert", "shaders/model.frag");
animationShaderProgram = LoadShaders("shaders/animation.vert", "shaders/animation.frag");
shadowShaderProgram = LoadShaders("shaders/shadows.vert", "shaders/shadows.frag", "shaders/shadows.geom");
blurShaderProgram = LoadShaders("shaders/blur.vert", "shaders/blur.frag");
finalShaderProgram = LoadShaders("shaders/final.vert", "shaders/final.frag");
particleShaderProgram = LoadShaders("shaders/particles.vert", "shaders/particles.frag");
debugShaderProgram = LoadShaders("shaders/shader.vert", "shaders/shader.frag");
domeShaderProgram = LoadShaders("shaders/dome.vert", "shaders/dome.frag");
// Check the shader program.
if (!worldShaderProgram)
{
std::cerr << "Failed to initialize shader program" << std::endl;
return false;
}
// Check the shader program.
if (!modelShaderProgram)
{
std::cerr << "Failed to initialize shader program" << std::endl;
return false;
}
if (!animationShaderProgram)
{
std::cerr << "Failed to initialize shader program" << std::endl;
return false;
}
if (!shadowShaderProgram)
{
std::cerr << "Failed to initialize shader program" << std::endl;
return false;
}
if (!blurShaderProgram) {
std::cerr << "Failed to initialize shader program" << std::endl;
return false;
}
if (!finalShaderProgram) {
std::cerr << "Failed to initialize shader program" << std::endl;
return false;
}
if (!particleShaderProgram) {
std::cerr << "Failed to initialize shader program" << std::endl;
return false;
}
if (!leafShaderProgram) {
std::cerr << "Failed to initialize shader program" << std::endl;
return false;
}
return true;
}
bool Window::initializeObjects()
{
// Use later, in case many objects need many shaders
modelShader = {
{ CHAR_BUMBUS, animationShaderProgram },
{ CHAR_POGO, animationShaderProgram },
{ CHAR_SWAINKY, animationShaderProgram },
{ CHAR_GILMAN, animationShaderProgram }, // TODO rename
{ CHAR_NPC, animationShaderProgram },
{ VEG_CABBAGE, animationShaderProgram },
{ VEG_CARROT, animationShaderProgram },
{ VEG_CORN, animationShaderProgram },
{ VEG_RADISH, animationShaderProgram },
{ VEG_TOMATO, animationShaderProgram },
{ VEG_GOLDEN_EGGPLANT, animationShaderProgram },
{ WATERING_CAN, animationShaderProgram },
{ POISON, animationShaderProgram },
{ SHOVEL, animationShaderProgram },
{ HOE, animationShaderProgram },
{ NET, animationShaderProgram},
{ GLUE, animationShaderProgram},
{ FERTILIZER, animationShaderProgram},
{ GLUE_ON_GROUND,animationShaderProgram},
{ OATS, animationShaderProgram },
{ SOJU, animationShaderProgram },
{ WORLD_PLOT_RED, animationShaderProgram },
{ WORLD_PLOT_BLUE, animationShaderProgram },
{ WORLD_PLOT_GREEN, animationShaderProgram },
{ WORLD_PLOT_YELLOW, animationShaderProgram },
{ WORLD_SEED_CABBAGE, animationShaderProgram },
{ WORLD_SEED_CARROT, animationShaderProgram },
{ WORLD_SEED_CORN, animationShaderProgram },
{ WORLD_SEED_RADISH, animationShaderProgram },
{ WORLD_SEED_TOMATO, animationShaderProgram },
{ WORLD_FLAG_CABBAGE, animationShaderProgram },
{ WORLD_FLAG_CARROT, animationShaderProgram },
{ WORLD_FLAG_CORN, animationShaderProgram },
{ WORLD_FLAG_RADISH, animationShaderProgram },
{ WORLD_FLAG_TOMATO, animationShaderProgram },
{ WORLD_FLAG_POISON, animationShaderProgram },
{INDICATOR_WATER,animationShaderProgram},
{INDICATOR_FERTILIZER,animationShaderProgram},
{ DEBUG_CIRCLE, debugShaderProgram },
{ DEBUG_SQUARE, debugShaderProgram },
{ WORLD_MAP, worldShaderProgram },
{ WORLD_DOME, domeShaderProgram },
{ WORLD_WATER, waterShaderProgram },
{ WORLD_LEAVES, leafShaderProgram },
{ WORLD_GRASS, leafShaderProgram },
{ WORLD_PODIUM, worldShaderProgram },
{ PARTICLE_GLOW, particleShaderProgram },
{ PARTICLE_DUST, particleShaderProgram },
{ PARTICLE_FIREFLIES, particleShaderProgram },
{ PARTICLE_FIREFLIES_PURPLE, particleShaderProgram },
{ PARTICLE_FIREFLIES_ORANGE, particleShaderProgram },
{ PARTICLE_FIREFLIES_BLUE, particleShaderProgram },
{ PARTICLE_FIREFLIES_RED, particleShaderProgram },
{ PARTICLE_FIREFLIES_PINK, particleShaderProgram },
{ PARTICLE_FIREFLIES_GREEN, particleShaderProgram },
{ PARTICLE_FIREFLIES_WHITE, particleShaderProgram },
{BALLOON_YELLOW, animationShaderProgram },
{BALLOON_RED, animationShaderProgram },
{BALLOON_BLUE, animationShaderProgram },
{BALLOON_PURPLE, animationShaderProgram },
{BALLOON_PINK, animationShaderProgram },
{BALLOON_GREEN, animationShaderProgram },
};
return true;
}
void Window::cleanUp()
{
// Deallcoate the objects.
// delete player;
// Delete the shader program.
glDeleteProgram(worldShaderProgram);
glDeleteProgram(modelShaderProgram);
glDeleteProgram(animationShaderProgram);
glDeleteProgram(shadowShaderProgram);
}
GLFWwindow* Window::createWindow(int width, int height)
{
// Initialize GLFW.
if (!glfwInit())
{
std::cerr << "Failed to initialize GLFW" << std::endl;
return NULL;
}
// 4x antialiasing.
glfwWindowHint(GLFW_SAMPLES, 4);
#ifdef __APPLE__
// Apple implements its own version of OpenGL and requires special treatments
// to make it uses modern OpenGL.
// Ensure that minimum OpenGL version is 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Enable forward compatibility and allow a modern OpenGL context
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// Create the GLFW window.
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
//TODO: change it back to fullscreen
GLFWwindow* window = glfwCreateWindow(width, height, windowTitle, monitor, NULL);
//GLFWwindow* window = glfwCreateWindow(width, height, windowTitle, NULL, NULL);
// Check if the window could not be created.
if (!window)
{
std::cerr << "Failed to open GLFW window." << std::endl;
glfwTerminate();
return NULL;
}
// Make the context of the window.
glfwMakeContextCurrent(window);
#ifndef __APPLE__
// On Windows and Linux, we need GLEW to provide modern OpenGL functionality.
// Initialize GLEW.
if (glewInit())
{
std::cerr << "Failed to initialize GLEW" << std::endl;
return NULL;
}
#endif
// Set swap interval to 1.
glfwSwapInterval(0);
// Call the resize callback to make sure things get drawn immediately.
Window::resizeCallback(window, width, height);
return window;
}
void Window::resizeCallback(GLFWwindow* window, int width, int height)
{
#ifdef __APPLE__
// In case your Mac has a retina display.
glfwGetFramebufferSize(window, &width, &height);
#endif
Window::width = width;
Window::height = height;
// Set the viewport size.
glViewport(0, 0, width, height);
// Set the projection matrix.
if (height != 0) {
Window::projection = glm::perspective(glm::radians(60.0),
double(width) / (double)height, 1.0, 1000.0);
}
GUI::updateDisplayRatio(width, height);
if (Window::bloom != nullptr) {
Window::bloom->resize(width, height);
}
}
void Window::logicCallback()
{
// Perform any necessary updates here
GameManager::UpdateFixedDeltaTime();
game->FixedUpdate();
}
float Lerp(const float a, const float b, const float f) //TODO move to a more global scope
{
return a + f * (b - a);
}
void Window::displayCallback(GLFWwindow* window)
{
// Gets events, including input such as keyboard and mouse or window resizing
glfwPollEvents();
// Swap buffers.
glfwSwapBuffers(window);
}
void Window::cursorCallback(GLFWwindow* window, double xpos, double ypos)
{
if ((glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)) {
std::cout << "RIGHT_CLICK" << std::endl;
}
if ((glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)) {
std::cout << "LEFT_CLICK" << std::endl;
}
}
glm::vec3 Window::GetEyePos()
{
return eyePos;
}
void Window::renderDepthMap() {
// glUseProgram(modelShaderProgram);
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_2D_ARRAY, FBO::dm);
// glUniform1i(glGetUniformLocation(modelShaderProgram, "map"), 0);
unsigned int quadVAO = 0;
unsigned int quadVBO;
if (quadVAO == 0)
{
float quadVertices[] = {
// positions // texture Coords
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
};
// setup plane VAO
glGenVertexArrays(1, &quadVAO);
glGenBuffers(1, &quadVBO);
glBindVertexArray(quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
}
glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
// glUseProgram(0);
}