328 lines
11 KiB
C++
328 lines
11 KiB
C++
#include <iostream>
|
|
|
|
#include <glad/glad.h> // Must be included before GLFW
|
|
#include <GLFW/glfw3.h>
|
|
#include <glm/glm.hpp>
|
|
|
|
#include "Shader.h"
|
|
#include "Camera.h"
|
|
#include "Object3D.h"
|
|
#include "World.h"
|
|
|
|
// Window dimensions
|
|
unsigned int SCR_WIDTH = 1920;
|
|
unsigned int SCR_HEIGHT = 1080;
|
|
|
|
// Jump
|
|
const float JUMP_COOLDOWN = 0.0f;
|
|
const float JUMP_POWER = 10.0f; // Initial Jump Speed
|
|
float last_jump = 0.0f;
|
|
|
|
// Camera
|
|
const float CAMERA_SPEED = 1.0f;
|
|
const float MAX_CAMERA_SPEED = 2.0f;
|
|
const float LOOK_SENSITIVITY = 0.1f;
|
|
|
|
const float Z_FAR = 1000.0f;
|
|
const float Z_NEAR = 0.1f;
|
|
bool WIREFRAME_MODE = !true;
|
|
const float FOV = 110.0f;
|
|
|
|
const int VIEW_DISTANCE = 4; // In Chunks
|
|
|
|
bool W_pressed, S_pressed, A_pressed, D_pressed = false;
|
|
|
|
glm::mat4 projection = glm::mat4(1.0f);
|
|
Camera camera = Camera(CAMERA_SPEED, glm::vec3(0.0f, 1.0f, 3.0f));
|
|
|
|
// Callback function for when the window is resized
|
|
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
|
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
|
{
|
|
// make sure the viewport matches the new window dimensions; note that width and
|
|
// height will be significantly larger than specified on retina displays.
|
|
glViewport(0, 0, width, height);
|
|
SCR_WIDTH = width;
|
|
SCR_HEIGHT = height;
|
|
|
|
// Perspective needs to be recalculated on window size change
|
|
projection = glm::perspective(glm::radians(FOV), (float)SCR_WIDTH / (float)SCR_HEIGHT, Z_NEAR, Z_FAR);
|
|
}
|
|
|
|
float yaw, pitch = 0;
|
|
bool first_mouse = true;
|
|
bool mouse_lock = true;
|
|
float last_x = 400, last_y = 300;
|
|
|
|
// Mouse Look
|
|
void mouse_callback(GLFWwindow * window, double xpos, double ypos)
|
|
{
|
|
float x_offset = xpos - last_x;
|
|
float y_offset = ypos - last_y;
|
|
last_x = xpos;
|
|
last_y = ypos;
|
|
|
|
if (first_mouse)
|
|
{
|
|
first_mouse = false;
|
|
return;
|
|
}
|
|
|
|
x_offset *= LOOK_SENSITIVITY;
|
|
y_offset *= LOOK_SENSITIVITY;
|
|
|
|
yaw += x_offset;
|
|
pitch += y_offset;
|
|
|
|
if (pitch > 89.0f)
|
|
pitch = 89.0f;
|
|
if (pitch < -89.0f)
|
|
pitch = -89.0f;
|
|
|
|
glm::vec3 direction;
|
|
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
|
direction.y = -sin(glm::radians(pitch));
|
|
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
|
camera.target = glm::normalize(direction);
|
|
}
|
|
|
|
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
|
|
void processInput(GLFWwindow *window)
|
|
{
|
|
float current_time = glfwGetTime();
|
|
|
|
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
|
// if (mouse_lock) {
|
|
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
|
// mouse_lock = false;
|
|
// } else {
|
|
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
// mouse_lock = true;
|
|
// }
|
|
glfwSetWindowShouldClose(window, true);
|
|
}
|
|
|
|
// ---- Movement ----
|
|
// Forward / Backward
|
|
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
|
{
|
|
camera.forward_velocity = camera.speed * glm::vec3(camera.target.x, 0.0f, camera.target.z);
|
|
W_pressed = true;
|
|
}
|
|
|
|
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
|
{
|
|
camera.forward_velocity = -camera.speed * glm::vec3(camera.target.x, 0.0f, camera.target.z);
|
|
S_pressed = true;
|
|
}
|
|
|
|
|
|
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_RELEASE && S_pressed)
|
|
{
|
|
camera.forward_velocity = glm::vec3(0.0f);
|
|
S_pressed = false;
|
|
}
|
|
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_RELEASE && W_pressed)
|
|
{
|
|
camera.forward_velocity = glm::vec3(0.0f);
|
|
W_pressed = false;
|
|
}
|
|
|
|
|
|
// Horizontal
|
|
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
|
{
|
|
camera.horizontal_velocity = -glm::normalize(glm::cross(glm::vec3(camera.target.x, 0.0f, camera.target.z), camera.worldUp)) * camera.speed;
|
|
A_pressed = true;
|
|
}
|
|
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
|
{
|
|
camera.horizontal_velocity = glm::normalize(glm::cross(glm::vec3(camera.target.x, 0.0f, camera.target.z), camera.worldUp)) * camera.speed;
|
|
D_pressed = true;
|
|
}
|
|
|
|
|
|
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_RELEASE && A_pressed)
|
|
{
|
|
camera.horizontal_velocity = glm::vec3(0.0f);
|
|
A_pressed = false;
|
|
}
|
|
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_RELEASE && D_pressed)
|
|
{
|
|
camera.horizontal_velocity = glm::vec3(0.0f);
|
|
D_pressed = false;
|
|
}
|
|
|
|
|
|
// Jump
|
|
if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
|
// camera.velocity -= glm::normalize(glm::cross(camera.target, glm::normalize(glm::cross(camera.target, camera.worldUp)))) * camera.speed * 0.5f;
|
|
{
|
|
if (current_time - last_jump > JUMP_COOLDOWN && camera.position.y == 0)
|
|
{
|
|
camera.vertical_velocity.y = JUMP_POWER;
|
|
last_jump = current_time;
|
|
}
|
|
}
|
|
|
|
// Sprint
|
|
if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
|
camera.speed = CAMERA_SPEED * 7.5;
|
|
if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_RELEASE)
|
|
camera.speed = CAMERA_SPEED * 20;
|
|
}
|
|
|
|
int main() {
|
|
// 1. --- Initialize GLFW ---
|
|
if (!glfwInit()) {
|
|
std::cerr << "Failed to initialize GLFW" << std::endl;
|
|
return -1;
|
|
}
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
// 2. --- Create a Window ---
|
|
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Voxel Engine 0.4.0", NULL, NULL);
|
|
if (window == NULL) {
|
|
std::cerr << "Failed to create GLFW window" << std::endl;
|
|
glfwTerminate();
|
|
return -1;
|
|
}
|
|
glfwMakeContextCurrent(window);
|
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
|
|
|
// 3. --- Initialize GLAD ---
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
std::cerr << "Failed to initialize GLAD" << std::endl;
|
|
return -1;
|
|
}
|
|
|
|
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
|
|
|
|
// 4. Shader Setup
|
|
Shader shader("shaders/cube_vert.glsl", "shaders/cube_frag.glsl");
|
|
|
|
// 5. --- Set up Vertex Data and Buffers ---
|
|
|
|
// Load Cube OBJ
|
|
World world;
|
|
// Object3D cube = Object3D("objs/cube.obj");
|
|
|
|
// std::cout << "World created with " << world.chunks.size() << " chunks" << std::endl;
|
|
// Mesh generation now happens automatically when first rendering
|
|
|
|
if (WIREFRAME_MODE)
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
|
|
|
// Setup mouse look
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
glfwSetCursorPosCallback(window, mouse_callback);
|
|
|
|
// Setup projection matrix
|
|
projection = glm::perspective(glm::radians(FOV), (float)SCR_WIDTH / (float)SCR_HEIGHT, Z_NEAR, Z_FAR); // This doesn't need to be recalculated every frame
|
|
|
|
glm::mat4 view;
|
|
double start_time = glfwGetTime();
|
|
double move_time = glfwGetTime();
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
bool debug_printed = false;
|
|
|
|
// 6. --- The Render Loop ---
|
|
while (!glfwWindowShouldClose(window)) {
|
|
// Input (e.g., close window on ESC)
|
|
processInput(window);
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Clear screen
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Z-Buffer
|
|
|
|
view = camera.getView();
|
|
|
|
shader.use();
|
|
|
|
if (!debug_printed) {
|
|
std::cout << "\n=== RENDER DEBUG ===" << std::endl;
|
|
std::cout << "Camera pos: (" << camera.position.x << ", " << camera.position.y << ", " << camera.position.z << ")" << std::endl;
|
|
std::cout << "Camera target: (" << camera.target.x << ", " << camera.target.y << ", " << camera.target.z << ")" << std::endl;
|
|
// std::cout << "Number of chunks to draw: " << world.chunks.size() << std::endl;
|
|
}
|
|
|
|
// Generate chunks around the camera
|
|
glm::ivec3 camera_chunk = glm::ivec3(camera.position) / CHUNK_SIZE;
|
|
|
|
// ---- PASS 1: Draw solid grey cubes ----
|
|
glEnable(GL_POLYGON_OFFSET_FILL);
|
|
glPolygonOffset(1.0, 1.0); // Push solid faces "back"
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
shader.setVec3("u_Color", glm::vec3(0.5, 0.5, 0.5));
|
|
|
|
for (int y = -VIEW_DISTANCE; y < VIEW_DISTANCE; y++) {
|
|
for (int z = -VIEW_DISTANCE; z < VIEW_DISTANCE; z++) {
|
|
for (int x = -VIEW_DISTANCE; x < VIEW_DISTANCE; x++) {
|
|
glm::ivec3 chunk_offset = glm::ivec3(x, y, z);
|
|
glm::ivec3 chunk_pos = camera_chunk + chunk_offset;
|
|
|
|
glm::mat4 model = glm::mat4(1.0f);
|
|
model = glm::translate(model, glm::vec3(chunk_pos * CHUNK_SIZE));
|
|
glm::mat4 mvp = projection * view * model;
|
|
shader.setMat4("u_mvp", mvp);
|
|
|
|
Chunk* chunk = world.get_or_create_chunk(chunk_pos);
|
|
|
|
Mesh* chunk_mesh = chunk->getMesh();
|
|
if (chunk_mesh)
|
|
chunk_mesh->draw();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!debug_printed) {
|
|
debug_printed = true;
|
|
}
|
|
|
|
glDisable(GL_POLYGON_OFFSET_FILL);
|
|
|
|
// ---- PASS 2: Draw wireframe on top ----
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Just the lines
|
|
shader.setVec3("u_Color", glm::vec3(1.0, 1.0, 1.0));
|
|
|
|
for (int y = -VIEW_DISTANCE; y < VIEW_DISTANCE; y++) {
|
|
for (int z = -VIEW_DISTANCE; z < VIEW_DISTANCE; z++) {
|
|
for (int x = -VIEW_DISTANCE; x < VIEW_DISTANCE; x++) {
|
|
glm::ivec3 chunk_offset = glm::ivec3(x, y, z);
|
|
glm::ivec3 chunk_pos = camera_chunk + chunk_offset;
|
|
|
|
glm::mat4 model = glm::mat4(1.0f);
|
|
model = glm::translate(model, glm::vec3(chunk_pos * CHUNK_SIZE));
|
|
glm::mat4 mvp = projection * view * model;
|
|
shader.setMat4("u_mvp", mvp);
|
|
|
|
Chunk* chunk = world.get_or_create_chunk(chunk_pos);
|
|
|
|
Mesh* chunk_mesh = chunk->getMesh();
|
|
if (chunk_mesh)
|
|
chunk_mesh->draw();
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- Reset polygon mode for next frame (good practice) ---
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
|
|
double end_time = glfwGetTime();
|
|
float time_since_last_move = end_time - move_time;
|
|
if (time_since_last_move >= 0.017)
|
|
{
|
|
camera.move(time_since_last_move);
|
|
move_time = end_time;
|
|
}
|
|
|
|
// Swap buffers and poll for events
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
}
|
|
|
|
glfwTerminate(); // Clean up GLFW
|
|
return 0;
|
|
} |