This repository was archived by the owner on Dec 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
334 lines (255 loc) · 12 KB
/
Copy pathmain.cpp
File metadata and controls
334 lines (255 loc) · 12 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
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
#include <string>
#include "Logger.h"
#include "PathProvider.h"
#include "ThreadPool.h"
#include "EventBus.h"
#include "ServiceLocator.h"
#include "GlResourceDeleter.h"
#include "WindowController.h"
#include "Camera.h"
#include "Shader.h"
#include "ScreenshotCreator.h"
#include "StateController.h"
#include "MainMenuState.h"
#include "GameState.h"
#include "World.h"
#include "BlocksIncluder.h"
#include "RayCastHit.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include "FontsLoader.h"
#include "TextureManager.h"
#include "WireFrameCube.h"
#include "SkySettings.h"
#include "f3InfoScreen.h"
#include <GL/glext.h>
#include "GLSettingsController.h"
#ifdef _WIN32
#include "PCInputController.h"
#endif
std::unique_ptr<IInputController> inputController =
#ifdef _WIN32
std::make_unique<PCInputController>();
#else
nullptr;
#endif
#include "CommandsIncluder.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
#include "ScreenQuad.h"
#include "BlockCache.h"
#include "ShaderTypes.h"
#include "WorldSeed.h"
#include "WorldGenerationTypes.h"
WindowContext ctx;
void setupSceneShader(Shader& shader,
const Camera& camera,
const glm::mat4& projection,
const glm::mat4& model,
World& world,
const SkyLightInfo& skyLightInfo);
float deltaTime = 0.0f;
float lastFrame = 0.0f;
std::optional<RaycastHit> raycastHit;
int shadowUpdateFrameCounter = 0;
const int shadowUpdateInterval = 10;
// Later create an entity class - and then Player will be an entity and will have a camera and choosed block controls
Camera camera;
void init() {
setlocale(LC_ALL, "Russian");
Logger::getInstance().Log("Application started", LogLevel::Info, LogOutput::Both, LogWriteMode::Overwrite);
ThreadPool::getInstance(); // Initialize ThreadPool
EventBus::getInstance(); // Initialize EventBus
FileHandler::getInstance(); // Initialize FileHandler
PathProvider::getInstance(); // Initialize PathProvider
}
int main() {
init();
WindowController windowController;
windowController.init();
//windowController.toggleFullscreen();
windowController.bindCameraControls(&camera);
windowController.setWindowTitle("MineOx 0.2");
GLFWwindow* window = windowController.getWindow();
FontsLoader fontsLoader(windowController.getWidth(), windowController.getHeight());
f3InfoScreen f3InfoScreen;
ScreenshotCreator screenshotCreator;
StateController stateController;
auto& textureManager = TextureManager::getInstance();
auto& fileHandler = FileHandler::getInstance();
auto& pathProvider = PathProvider::getInstance();
auto& openGLSettingsController = GLSettingsController::getInstance();
auto& blockCacheController = BlockCache::getInstance();
fileHandler.providePathToDataFolder(pathProvider.getDataPath());
fileHandler.init();
if (!window) {
Logger::getInstance().Log("Failed to create GLFW window", LogLevel::Critical, LogOutput::Both);
return -1;
}
openGLSettingsController.initializeOpenGLSettings(); // Initialize OpenGL settings
blockCacheController.loadAll();
stateController.changeState(std::make_unique<MainMenuState>());
std::string worldName = "New world";
WorldSeed seed = WorldSeed::random();
Logger::getInstance().Log("Using world seed: " + seed.toString(), LogLevel::Warning, LogOutput::Both, LogWriteMode::Append);
camera = Camera();
World world(seed, worldName, GenerationType::Noise); // Later, get seed and world name from user input
ScreenQuad quad;
ServiceLocator::ProvideWorld(&world);
f3InfoScreen.provideWorldSeed(world.getSeed());
// Create directories if not exist
std::vector<fs::path> dirs = {
pathProvider.getRootPath(),
pathProvider.getConfigPath(),
pathProvider.getLogsPath(),
pathProvider.getScreenshotsPath(),
pathProvider.getWorldsPath(),
pathProvider.getWorldChunksPath(worldName)
};
fileHandler.ensureDirectoriesExist(dirs);
//world.initWorld(camera.Position);
Shader shader = Shader::fromPaths(pathProvider.getShaderPath(ShaderType::Main));
Shader wireFrameCubeShader = Shader::fromPaths(pathProvider.getShaderPath(ShaderType::Wireframe));
Shader fontsShader = Shader::fromPaths(pathProvider.getShaderPath(ShaderType::Font));
Shader depthShader = Shader::fromPaths(pathProvider.getShaderPath(ShaderType::Depth));
Shader chatShader = Shader::fromPaths(pathProvider.getShaderPath(ShaderType::Chat));
Shader postProcessShader = Shader::fromPaths(pathProvider.getShaderPath(ShaderType::PostProcess));
float symbolSize = 32.0f; // Size of the font symbols
fontsLoader.loadFont((PathProvider::getInstance().getFontsPath() / "arial.ttf").string());
fontsLoader.loadCharacters(symbolSize);
world.getShadowController().init();
WireFrameCube wireFrameCube;
wireFrameCube.init();
ChatController chatController = ChatController(chatShader);
chatController.init();
chatController.setvisible(true);
ServiceLocator::ProvideChatController(&chatController);
ServiceLocator::ProvideCamera(&camera);
glfwSetWindowUserPointer(window, &chatController);
windowController.registerCharCallback();
ctx.camera = &camera;
ctx.chat = &chatController;
glfwSetWindowUserPointer(window, &ctx);
glm::mat4 projection = glm::perspective(
glm::radians(camera.FOV),
(float)windowController.getWidth() / (float)windowController.getHeight(),
0.1f,
1000.0f
);
glm::mat4 model = glm::mat4(1.0f);
textureManager.bindTextures();
unsigned int fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// создаём цветовую текстуру
unsigned int texColorBuffer;
glGenTextures(1, &texColorBuffer);
glBindTexture(GL_TEXTURE_2D, texColorBuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, windowController.getWidth(), windowController.getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);
// рендербуфер для depth + stencil
unsigned int rbo;
glGenRenderbuffers(1, &rbo);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, windowController.getWidth(), windowController.getHeight());
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "FBO не собран!" << std::endl;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
while (!glfwWindowShouldClose(window)) {
if(window) {
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
shadowUpdateFrameCounter++;
world.getTimeOfDayController().update(deltaTime);
auto skyLightInfo = world.getTimeOfDayController().getSkyLightInfo();
//stateController.handleInput(window);
//stateController.update(deltaTime);
if (shadowUpdateFrameCounter >= shadowUpdateInterval) {
shadowUpdateFrameCounter = 0;
world.getShadowController().update(
skyLightInfo.lightDirection,
camera.Position,
world.getViewDistance()
);
}
world.getShadowController().renderShadows(world.getChunkController().getLoadedChunks(), depthShader);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// It will change viewport size, so we need to set it again
glViewport(0, 0, windowController.getWidth(), windowController.getHeight());
raycastHit = world.raycast(camera.Position, camera.Front, 5.0f);
f3InfoScreen.update(deltaTime, camera, world, raycastHit);
inputController->processKeyInput(&camera, deltaTime, raycastHit, camera.choosedBlock, window, chatController);
world.update(PlayerPos(glm::ivec3(camera.Position.x, camera.Position.y, camera.Position.z)));
setupSceneShader(shader, camera, projection, model, world, skyLightInfo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glViewport(0, 0, windowController.getWidth(), windowController.getHeight());
glClearColor(skyLightInfo.skyColor.r, skyLightInfo.skyColor.g, skyLightInfo.skyColor.b, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// здесь обычный рендер:
world.render(shader, skyLightInfo.lightDirection, skyLightInfo.lightColor);
if (raycastHit) {
wireFrameCube.render(glm::vec3(raycastHit->blockPos), camera, projection, wireFrameCubeShader);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
//stateController.render();
glClear(GL_COLOR_BUFFER_BIT);
openGLSettingsController.disableCullDepth();
postProcessShader.use();
postProcessShader.setInt("screenTexture", 2);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, texColorBuffer);
quad.render();
openGLSettingsController.enableCullDepth();
openGLSettingsController.disableCullDepth();
f3InfoScreen.render(fontsLoader, fontsShader, static_cast<float>(windowController.getHeight()));
openGLSettingsController.enableCullDepth();
openGLSettingsController.disableCullDepth();
chatController.render(windowController.getWidth(), windowController.getHeight(), symbolSize, fontsLoader, fontsShader);
openGLSettingsController.enableCullDepth();
if(inputController->getShouldTakeScreenshot()) {
screenshotCreator.doTheScreenshotAndSave(window);
Logger::getInstance().Log("Screenshot taken", LogLevel::Info, LogOutput::Both, LogWriteMode::Append);
inputController->setShouldTakeScreenshot(false);
}
if (chatController.getVisibility()) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
} else {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
}
GLResourceDeleter::getInstance().processDeletes();
glfwSwapBuffers(window);
glfwPollEvents();
}
}
Logger::getInstance().Log("Application shutdown", LogLevel::Warning, LogOutput::Both, LogWriteMode::Append);
windowController.shutdown();
return 0;
}
void setupSceneShader(Shader& shader,
const Camera& camera,
const glm::mat4& projection,
const glm::mat4& model,
World& world,
const SkyLightInfo& skyLightInfo) {
shader.use();
shader.setMat4("view", camera.GetViewMatrix());
shader.setMat4("projection", projection);
shader.setMat4("model", model);
shader.setMat4("lightSpaceMatrix", world.getShadowController().getSunLightSpaceMatrix());
shader.setInt("atlasTexture", 0);
shader.setInt("shadowMap", 1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, TextureManager::getInstance().getAtlasID());
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, world.getShadowController().getSunShadowMap());
shader.setFloat("shadowMapSize", 8192.0f);
shader.setVec3("lightColor", skyLightInfo.lightColor);
shader.setVec3("lightDir", skyLightInfo.lightDirection);
shader.setFloat("lightIntensity", skyLightInfo.lightIntensity);
}