-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
657 lines (547 loc) · 21.5 KB
/
Copy pathmain.cpp
File metadata and controls
657 lines (547 loc) · 21.5 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#define STB_IMAGE_IMPLEMENTATION
#define STB_PERLIN_IMPLEMENTATION
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <nlohmann/json.hpp>
#include "stb_image.h"
#include "stb_perlin.h"
#include "shader_s.hpp"
#include "block_manager.hpp"
#include "world.hpp"
using json = nlohmann::json;
// ========================
// === Global Variables ===
// ========================
unsigned int windowWidth = 1600;
unsigned int windowHeight = 900;
const char* windowTitle = "Cubeblock";
// Camera Settings
float fov = 75.0f;
float deltaTime = 0.0f;
float lastFrame = 0.0f;
// Physics Variables
bool isGravityMode = false; // Start in spectator mode (toggle with 'G')
float playerVerticalVelocity = 0.0f;
bool isGrounded = false;
// Physics Constants
const float GRAVITY = 35.0f; // Stronger than earth gravity feels better in games
const float JUMP_FORCE = 10.0f;
const float PLAYER_HEIGHT = 1.8f;
const float PLAYER_WIDTH = 0.6f;
const float cameraSpeed = 0.05f;
glm::vec3 cameraPos = glm::vec3(0.0f, 10.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
bool firstMouse = true;
float yaw = -90.0f;
float pitch = 0.0f;
float lastX = windowWidth / 2.0;
float lastY = windowHeight / 2.0;
float lastModifyTime = 0.0f;
float lastAutoSaveTime = 0.0f;
unsigned int highlightVAO = 0, highlightVBO = 0;
unsigned int chVAO, chVBO;
// Global Managers
BlockManager globalBlockManager;
World world;
// =======================
// === Shader Sources ===
// =======================
// Simple Shader for target block highlight
const char* highlightVert = R"(
#version 460 core
layout (location = 0) in vec3 aPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(aPos, 1.0);
}
)";
const char* highlightFrag = R"(
#version 460 core
out vec4 FragColor;
void main() {
FragColor = vec4(1.0, 1.0, 1.0, 0.4); // White with 40% opacity
}
)";
// UI Vertex Shader
const char* crosshairVert = R"(
#version 460 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec2 aTexCoord;
out vec2 TexCoord;
uniform float scale;
uniform float aspectRatio;
void main() {
// Position: x scaled by aspect ratio to maintain square shape
gl_Position = vec4(aPos.x * scale / aspectRatio, aPos.y * scale, 0.0, 1.0);
TexCoord = aTexCoord;
}
)";
// UI Fragment Shader
const char* crosshairFrag = R"(
#version 460 core
out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D uiTexture;
void main() {
FragColor = texture(uiTexture, TexCoord);
}
)";
// ========================
// === Helper Functions ===
// ========================
// Load crosshair texture
unsigned int loadTexture(const char* path) {
unsigned int textureID;
glGenTextures(1, &textureID);
int width, height, nrComponents;
unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);
if (data) {
GLenum format = (nrComponents == 4) ? GL_RGBA : GL_RGB;
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_image_free(data);
}
else {
std::cout << "Texture failed to load at path: " << path << std::endl;
stbi_image_free(data);
}
return textureID;
}
struct RaycastResult {
bool hit;
glm::ivec3 blockPos;
glm::vec3 worldPos;
glm::ivec3 normal; // Stores which face we hit
};
RaycastResult raycast(glm::vec3 start, glm::vec3 direction, float range) {
glm::vec3 dir = glm::normalize(direction);
glm::ivec3 mapPos = glm::floor(start);
glm::vec3 deltaDist = glm::abs(1.0f / dir);
glm::ivec3 step;
glm::vec3 sideDist;
// Track the last axis we moved on to calculate normal
int lastAxis = -1; // 0=x, 1=y, 2=z
if (dir.x < 0) { step.x = -1; sideDist.x = (start.x - mapPos.x) * deltaDist.x; }
else { step.x = 1; sideDist.x = (mapPos.x + 1.0f - start.x) * deltaDist.x; }
if (dir.y < 0) { step.y = -1; sideDist.y = (start.y - mapPos.y) * deltaDist.y; }
else { step.y = 1; sideDist.y = (mapPos.y + 1.0f - start.y) * deltaDist.y; }
if (dir.z < 0) { step.z = -1; sideDist.z = (start.z - mapPos.z) * deltaDist.z; }
else { step.z = 1; sideDist.z = (mapPos.z + 1.0f - start.z) * deltaDist.z; }
float dist = 0.0f;
while (dist < range) {
if (sideDist.x < sideDist.y) {
if (sideDist.x < sideDist.z) {
mapPos.x += step.x;
dist = sideDist.x;
sideDist.x += deltaDist.x;
lastAxis = 0;
}
else {
mapPos.z += step.z;
dist = sideDist.z;
sideDist.z += deltaDist.z;
lastAxis = 2;
}
}
else {
if (sideDist.y < sideDist.z) {
mapPos.y += step.y;
dist = sideDist.y;
sideDist.y += deltaDist.y;
lastAxis = 1;
}
else {
mapPos.z += step.z;
dist = sideDist.z;
sideDist.z += deltaDist.z;
lastAxis = 2;
}
}
BlockID b = world.getBlock(mapPos.x, mapPos.y, mapPos.z);
if (b != BLOCK_AIR) {
// Calculate Normal based on the last step taken
glm::ivec3 normal(0);
if (lastAxis == 0) normal.x = -step.x;
if (lastAxis == 1) normal.y = -step.y;
if (lastAxis == 2) normal.z = -step.z;
return { true, mapPos, start + dir * dist, normal };
}
}
return { false, glm::ivec3(0), glm::vec3(0), glm::ivec3(0) };
}
// ========================
// === Input Callbacks ===
// ========================
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
windowWidth = width;
windowHeight = height;
}
void mouse_callback(GLFWwindow* window, double xposIn, double yposIn) {
float xpos = static_cast<float>(xposIn);
float ypos = static_cast<float>(yposIn);
if (firstMouse) {
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
float sensitivity = 0.1f;
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if (pitch > 89.0f) pitch = 89.0f;
if (pitch < -89.0f) pitch = -89.0f;
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
fov -= (float)yoffset;
if (fov < 1.0f) fov = 1.0f;
if (fov > 90.0f) fov = 90.0f;
}
bool checkCollision(glm::vec3 pos) {
// Define Player Bounding Box
// Eyes are at pos.y. Feet are at pos.y - PLAYER_HEIGHT
float minX = pos.x - PLAYER_WIDTH / 2.0f;
float maxX = pos.x + PLAYER_WIDTH / 2.0f;
float minY = pos.y - PLAYER_HEIGHT;
float maxY = pos.y + 0.1f; // Small buffer above head
float minZ = pos.z - PLAYER_WIDTH / 2.0f;
float maxZ = pos.z + PLAYER_WIDTH / 2.0f;
// Check every integer block coordinate inside this box
// We floor/ceil to get the range of blocks we are touching
int startX = (int)floor(minX);
int endX = (int)floor(maxX);
int startY = (int)floor(minY);
int endY = (int)floor(maxY);
int startZ = (int)floor(minZ);
int endZ = (int)floor(maxZ);
for (int y = startY; y <= endY; y++) {
for (int x = startX; x <= endX; x++) {
for (int z = startZ; z <= endZ; z++) {
if (world.getBlock(x, y, z) != BLOCK_AIR) {
return true; // Collision detected
}
}
}
}
return false;
}
bool gKeyPressed = false;
void processInput(GLFWwindow* window) {
// === Mode Toggling ===
if (glfwGetKey(window, GLFW_KEY_G) == GLFW_PRESS) {
if (!gKeyPressed) {
isGravityMode = !isGravityMode;
gKeyPressed = true;
std::cout << "Gravity Mode: " << (isGravityMode ? "ON" : "OFF") << std::endl;
// Reset velocity when switching
playerVerticalVelocity = 0.0f;
}
}
else {
gKeyPressed = false;
}
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_I) == GLFW_PRESS) world.isInfinite = !world.isInfinite;
// === Calculate Intended Movement Direction ===
float speed = 5.0f * deltaTime;
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) speed *= 2.5f;
glm::vec3 moveDir(0.0f);
// Flatten cameraFront for movement so we don't fly into the ground when looking down
glm::vec3 flatFront = glm::normalize(glm::vec3(cameraFront.x, 0.0f, cameraFront.z));
glm::vec3 flatRight = glm::normalize(glm::cross(flatFront, cameraUp));
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) moveDir += flatFront;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) moveDir -= flatFront;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) moveDir -= flatRight;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) moveDir += flatRight;
if (glm::length(moveDir) > 0.0f) moveDir = glm::normalize(moveDir) * speed;
// === Apply Movement based on Mode ===
// Spectator Mode
if (!isGravityMode) {
cameraPos += moveDir;
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) cameraPos += cameraUp * speed;
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) cameraPos -= cameraUp * speed;
}
// Gravity Mode
else {
// Horizontal Collision (X axis)
if (moveDir.x != 0.0f) {
if (!checkCollision(cameraPos + glm::vec3(moveDir.x, 0.0f, 0.0f))) {
cameraPos.x += moveDir.x;
}
}
// Horizontal Collision (Z axis)
if (moveDir.z != 0.0f) {
if (!checkCollision(cameraPos + glm::vec3(0.0f, 0.0f, moveDir.z))) {
cameraPos.z += moveDir.z;
}
}
// Gravity and Jumping
playerVerticalVelocity -= GRAVITY * deltaTime;
// Jump Input
if (isGrounded && glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
playerVerticalVelocity = JUMP_FORCE;
isGrounded = false;
}
// Vertical Collision (Y axis)
float verticalMove = playerVerticalVelocity * deltaTime;
if (checkCollision(cameraPos + glm::vec3(0.0f, verticalMove, 0.0f))) {
// If moving down (falling) and hit something -> Landed
if (verticalMove < 0.0f) {
isGrounded = true;
playerVerticalVelocity = 0.0f;
}
// If moving up (jump) and hit head -> Bonk
else if (verticalMove > 0.0f) {
playerVerticalVelocity = 0.0f;
}
}
else {
// No collision, apply movement
cameraPos.y += verticalMove;
isGrounded = false; // We are in the air
}
}
// Mouse Logic
float currentTime = (float)glfwGetTime();
if (currentTime - lastModifyTime > 0.2f) {
bool leftClick = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS;
bool rightClick = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS;
if (leftClick || rightClick) {
RaycastResult ray = raycast(cameraPos, cameraFront, 8.0f);
if (ray.hit) {
if (rightClick) {
glm::ivec3 p = ray.blockPos + ray.normal;
// Prevent placing block inside player's head/feet
glm::vec3 playerBox = cameraPos; // Simple check against camera pos
if (glm::distance(glm::vec3(p.x + 0.5f, p.y + 0.5f, p.z + 0.5f), cameraPos) > 1.5f) {
world.setBlock(p.x, p.y, p.z, BLOCK_STONE);
}
}
else if (leftClick) {
world.setBlock(ray.blockPos.x, ray.blockPos.y, ray.blockPos.z, BLOCK_AIR);
}
lastModifyTime = currentTime;
}
}
}
}
// =====================
// === Main Function ===
// =====================
int main()
{
// Initialise GLFW
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, windowTitle, NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window!" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glEnable(GL_DEPTH_TEST);
// ============================
// === Shader Compilation ===
// ============================
// Build Highlight Shader
unsigned int hVert = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(hVert, 1, &highlightVert, NULL);
glCompileShader(hVert);
unsigned int hFrag = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(hFrag, 1, &highlightFrag, NULL);
glCompileShader(hFrag);
unsigned int highlightProg = glCreateProgram();
glAttachShader(highlightProg, hVert);
glAttachShader(highlightProg, hFrag);
glLinkProgram(highlightProg);
// Build crosshair shader
unsigned int chVert = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(chVert, 1, &crosshairVert, NULL);
glCompileShader(chVert);
unsigned int chFrag = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(chFrag, 1, &crosshairFrag, NULL);
glCompileShader(chFrag);
unsigned int crosshairProg = glCreateProgram();
glAttachShader(crosshairProg, chVert);
glAttachShader(crosshairProg, chFrag);
glLinkProgram(crosshairProg);
Shader ourShader("shaders/vertex.glsl", "shaders/fragment.glsl");
// ============================
// === Mesh Generation ===
// ============================
// Build Highlight Cube Mesh
float cubeVertices[] = {
// Back face
-0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f,
// Front face
-0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f,
// Left face
-0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f,
// Right face
0.5f, 0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f,
// Bottom face
-0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f,
// Top face
-0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f
};
glGenVertexArrays(1, &highlightVAO);
glGenBuffers(1, &highlightVBO);
glBindVertexArray(highlightVAO);
glBindBuffer(GL_ARRAY_BUFFER, highlightVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Create Quad Mesh (Centered at 0,0)
float chVertices[] = {
// Pos // Tex
-0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 1.0f, 1.0f
};
glGenVertexArrays(1, &chVAO);
glGenBuffers(1, &chVBO);
glBindVertexArray(chVAO);
glBindBuffer(GL_ARRAY_BUFFER, chVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(chVertices), chVertices, GL_STATIC_DRAW);
// Attrib 0: Position (2 floats)
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Attrib 1: TexCoord (2 floats)
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
glEnableVertexAttribArray(1);
// ============================
// === Initialization ===
// ============================
// Load textures from json file
globalBlockManager.loadBlocks("blocks.json");
// Load Texture
unsigned int crosshairTexture = loadTexture("textures/crosshair.png"); // Make sure this file exists!
// Configure Shader
ourShader.use();
ourShader.setInt("textureArray", 0);
ourShader.setVec3("light.direction", -0.2f, -1.0f, -0.3f);
ourShader.setVec3("light.ambient", 0.2f, 0.2f, 0.2f);
ourShader.setVec3("light.diffuse", 0.8f, 0.8f, 0.8f);
// World Settings
world.isInfinite = true;
// ============================
// === Render Loop ===
// ============================
while (!glfwWindowShouldClose(window))
{
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
glClearColor(0.5f, 0.81f, 0.92f, 1.0f); // Sky Blue Color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
glm::mat4 projection = glm::perspective(glm::radians(fov), (float)windowWidth / (float)windowHeight, 0.1f, 100.0f);
// --- UPDATE WORLD ---
world.update(cameraPos);
// --- RENDER WORLD ---
ourShader.use();
ourShader.setMat4("view", view);
ourShader.setMat4("projection", projection);
ourShader.setVec3("viewPos", cameraPos);
// === FOG SETTINGS ===
ourShader.setVec3("fogColor", 0.53f, 0.81f, 0.92f);
// Calculate dynamic density based on Render Distance
// Formula: density = 2.0 / MaxDistance.
// This ensures visibility drops to ~1.8% at the edge of the world.
float maxDist = (float)(world.renderDistance * CHUNK_SIZE);
float density = 2.4f / maxDist; // 2.4 makes it slightly thicker to hide corners
ourShader.setFloat("fogDensity", density);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, globalBlockManager.textureArrayID);
world.render(ourShader);
// Auto-save
if (currentFrame - lastAutoSaveTime > 60.0f) {
std::cout << "Auto-saving..." << std::endl;
world.saveAllChunks();
lastAutoSaveTime = currentFrame;
}
// Raycast
RaycastResult ray = raycast(cameraPos, cameraFront, 8.0f);
if (ray.hit) {
// Enable Blending for transparency
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(highlightProg);
// Set View/Proj
glUniformMatrix4fv(glGetUniformLocation(highlightProg, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(highlightProg, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
// Position the cube at the hit block
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(
ray.blockPos.x + 0.5f,
ray.blockPos.y + 0.5f,
ray.blockPos.z + 0.5f
));
model = glm::scale(model, glm::vec3(1.01f));
glUniformMatrix4fv(glGetUniformLocation(highlightProg, "model"), 1, GL_FALSE, glm::value_ptr(model));
glBindVertexArray(highlightVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glDisable(GL_BLEND);
}
// Draw crosshair
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glUseProgram(crosshairProg);
glUniform1f(glGetUniformLocation(crosshairProg, "scale"), 0.05f);
glUniform1f(glGetUniformLocation(crosshairProg, "aspectRatio"), (float)windowWidth / (float)windowHeight);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, crosshairTexture);
glBindVertexArray(chVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glEnable(GL_DEPTH_TEST);
world.saveAllChunks();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}