-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
288 lines (219 loc) · 8.32 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
#include "gl_objects/mesh.hpp"
#include "player.hpp"
#include "gl_objects/shader.hpp"
#include "gl_objects/texture.hpp"
#include "chunks/chunk_manager.hpp"
#include "chunks/chunk_dealer.hpp"
#include "cube_map.hpp"
#include "utils/gl_includes.hpp"
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
#include "utils/debug.hpp"
std::shared_ptr<CubeMap> g_cubeMap{};
// Window parameters
GLFWwindow *g_window{};
// GPU objects
GLuint g_program{}; // A GPU program contains at least a vertex shader and a fragment shader
Player g_player{};
ChunkManager *g_chunkManager{};
ChunkDealer *g_chunkDealer{};
glm::mat4 g_viewMatrix;
glm::mat4 g_projMatrix;
int g_tool = 1;
// Executed each time the window is resized. Adjust the aspect ratio and the rendering viewport to the current window.
void window_size_callback(GLFWwindow *window, int width, int height) {
g_player.m_camera.set_aspect_ratio(static_cast<float>(width) / static_cast<float>(height));
glViewport(0, 0, (GLint)width, (GLint)height); // Dimension of the rendering region in the window
g_player.m_camera.set_screen_center(glm::vec2(width / 2, height / 2));
g_projMatrix = g_player.m_camera.compute_projection_matrix();
}
bool shiftPressed = false;
// Executed each time a key is entered.
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
g_player.update_input_keys(key, action);
if (action == GLFW_PRESS) {
if (key == GLFW_KEY_Z) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
if (key == GLFW_KEY_F) {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if ((key == GLFW_KEY_Q)) {
glfwSetWindowShouldClose(window, true); // Closes the application if the escape key is pressed
}
if (key == GLFW_KEY_LEFT_SHIFT)
shiftPressed = true;
if (key == GLFW_KEY_R) {
// g_chunkManager->reloadChunks();
}
if (key == GLFW_KEY_T) {
g_chunkManager->saveChunks();
}
if (key == GLFW_KEY_LEFT) {
int size = BlockPalette::block_descs.size();
if (--g_tool <= 0) g_tool = size - 1;
}
if (key == GLFW_KEY_RIGHT) {
int size = BlockPalette::block_descs.size();
if (++g_tool >= size) g_tool = 1;
}
}
if (action == GLFW_RELEASE) {
if (key == GLFW_KEY_LEFT_SHIFT) shiftPressed = false;
}
}
void cursor_pos_callback(GLFWwindow *window, double xpos, double ypos) {
g_player.m_camera.update_input_mouse_pos(window, glm::vec2(xpos, ypos));
}
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) {
g_player.m_camera.update_input_mouse_button(button, action);
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
glm::ivec3 block_pos{};
glm::ivec3 normal{};
glm::vec3 dir = glm::normalize(g_player.m_camera.get_target() - g_player.m_camera.get_position());
if (g_chunkManager->raycast(g_player.m_camera.get_position(), dir, 15, block_pos, normal)) {
std::cout << "Remove block !! at {" << block_pos.x << ", " << block_pos.y << ", " << block_pos.z << "} ? :(\n";
g_chunkManager->setBlock(block_pos, 0, true);
} else {
std::cout << "No block ? :(\n";
}
}
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
glm::ivec3 block_pos{};
glm::ivec3 normal{};
glm::vec3 dir = glm::normalize(g_player.m_camera.get_target() - g_player.m_camera.get_position());
if (g_chunkManager->raycast(g_player.m_camera.get_position(), dir, 15, block_pos, normal)) {
std::cout << "Set block !! at {" << block_pos.x << ", " << block_pos.y << ", " << block_pos.z << "} ? Block ID: " << g_tool << " :(\n";
g_chunkManager->setBlock(block_pos + normal, g_tool, true);
} else {
std::cout << "No block ? :(\n";
}
}
}
void initGLFW() {
glfwSetErrorCallback(error_callback);
// Initialize GLFW, the library responsible for window management
if (!glfwInit()) {
std::cerr << "ERROR: Failed to init GLFW" << std::endl;
std::exit(EXIT_FAILURE);
}
// Before creating the window, set some option flags
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
// Create the window
g_window = glfwCreateWindow(
1024, 768,
"Minecraft clone attemps #93180289301", nullptr, nullptr);
if (!g_window) {
std::cerr << "ERROR: Failed to open window" << std::endl;
glfwTerminate();
std::exit(EXIT_FAILURE);
}
// Load the OpenGL context in the GLFW window using GLAD OpenGL wrangler
glfwMakeContextCurrent(g_window);
glfwSetWindowSizeCallback(g_window, window_size_callback);
glfwSetKeyCallback(g_window, key_callback);
glfwSetMouseButtonCallback(g_window, mouse_button_callback);
glfwSetCursorPosCallback(g_window, cursor_pos_callback);
}
void initOpenGL() {
// Load extensions for modern OpenGL
if (!gladLoadGL()) {
std::cerr << "ERROR: Failed to initialize OpenGL context" << std::endl;
glfwTerminate();
std::exit(EXIT_FAILURE);
}
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(glDebugOutput, nullptr);
glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_ERROR, GL_DEBUG_SEVERITY_HIGH, 0, nullptr, GL_TRUE);
// glfwSwapInterval(0);
}
void initScene() {
g_cubeMap = std::make_shared<CubeMap>();
Chunk::init_chunks();
g_chunkManager = new ChunkManager();
g_chunkDealer = new ChunkDealer(100, g_chunkManager);
g_chunkManager->chunk_dealer = g_chunkDealer;
// Init shader
g_program = glCreateProgram();
loadShader(g_program, GL_VERTEX_SHADER, "../resources/vertexShader.glsl");
loadShader(g_program, GL_FRAGMENT_SHADER, "../resources/fragmentShader.glsl");
glLinkProgram(g_program);
// Init camera
int width, height;
glfwGetWindowSize(g_window, &width, &height);
g_player.m_camera.init(width, height);
}
float last_time = 0;
int nb_frames = 0;
void init() {
initGLFW();
initOpenGL();
initScene();
last_time = glfwGetTime();
BlockPalette::bind_texture(g_program);
glUseProgram(g_program);
setUniform(g_program, "u_texture", 0);
setUniform(g_program, "u_chunkSize", Chunk::chunk_size);
g_projMatrix = g_player.m_camera.compute_projection_matrix();
}
void render() {
nb_frames++;
float time_now = glfwGetTime();
if (time_now - last_time > 1) {
float fps = nb_frames / (time_now - last_time);
std::stringstream ss;
ss << "Minecraft clone attemps #93180289301 - " << fps << " FPS";
glfwSetWindowTitle(g_window, ss.str().c_str());
nb_frames = 0;
last_time = time_now;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
g_viewMatrix = g_player.m_camera.compute_view_matrix();
// Render stars
g_cubeMap->render(g_projMatrix, g_viewMatrix);
// Render the rest
glUseProgram(g_program);
setUniform(g_program, "u_viewProjMat", g_projMatrix * g_viewMatrix);
g_chunkManager->renderAll(g_program, g_player.m_camera);
}
float last_physic_time = 0;
void update(const float currentTimeInSec) {
float delta_time = currentTimeInSec - last_physic_time;
last_physic_time = currentTimeInSec;
g_player.update(delta_time);
glm::vec3 cam_pos = g_player.m_camera.get_position();
g_chunkManager->updateQueue(cam_pos);
g_chunkManager->unloadUselessChunks();
}
void clear() {
g_chunkManager->destroy();
delete g_chunkManager;
delete g_chunkDealer;
glDeleteProgram(g_program);
glfwDestroyWindow(g_window);
glfwTerminate();
}
int main(int argc, char **argv) {
init();
while (!glfwWindowShouldClose(g_window)) {
update(static_cast<float>(glfwGetTime()));
render();
glfwSwapBuffers(g_window);
glfwPollEvents();
}
clear();
return EXIT_SUCCESS;
}