diff --git a/CMakeLists.txt b/CMakeLists.txt index 61c70f8..e25983f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ add_executable(VoxelEngine src/Object3D.h src/Object3D.cpp src/Camera.h + src/World.h ) # --- Configure GLAD (from your src/ and include/ folders) --- diff --git a/build.ps1 b/build.ps1 new file mode 100644 index 0000000..3a462cd --- /dev/null +++ b/build.ps1 @@ -0,0 +1,11 @@ +cmake -S . -B build +cd build +cmake --build . + +cd .. +cp vendor/lib-vc2022/glfw3.dll build/Debug +cp -r shaders/ build/Debug +cp -r objs/ build/Debug + +cd build/Debug +.\VoxelEngine.exe diff --git a/src/World.h b/src/World.h new file mode 100644 index 0000000..dfb4279 --- /dev/null +++ b/src/World.h @@ -0,0 +1,30 @@ +#ifndef WORLD_H +#define CAMERA_H + +#include + +#include + +// Hash function for glm::ivec3 to use with unordered_set +namespace std { + template <> + struct hash { + size_t operator()(const glm::ivec3& v) const { + // Combine hash values of x, y, z components + size_t h1 = hash()(v.x); + size_t h2 = hash()(v.y); + size_t h3 = hash()(v.z); + + // Use a simple hash combination algorithm + return h1 ^ (h2 << 1) ^ (h3 << 2); + } + }; +} + +class World +{ + public: + std::unordered_set voxels; +}; + +#endif WORLD_H diff --git a/src/main.cpp b/src/main.cpp index 7621046..3dbed2a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,16 +1,18 @@ +#include +#include + #include // Must be included before GLFW #include -#include - #include #include "Shader.h" #include "Camera.h" #include "Object3D.h" +#include "World.h" // Window dimensions -unsigned int SCR_WIDTH = 800; -unsigned int SCR_HEIGHT = 600; +unsigned int SCR_WIDTH = 800 * 1.5; +unsigned int SCR_HEIGHT = 600 * 1.5; // Jump const float JUMP_COOLDOWN = 0.0f; @@ -169,6 +171,22 @@ void processInput(GLFWwindow *window) camera.speed = CAMERA_SPEED * 20; } +// Draw a cube at some x, y, z +void draw_cube(glm::ivec3 coords, Shader& shader, unsigned int VAO, size_t indexCount, const glm::mat4& view, const glm::mat4& projection) { + // Create model matrix for positioning the cube + glm::mat4 model = glm::mat4(1.0f); + model = glm::translate(model, glm::vec3(coords)); + + // Calculate MVP matrix + glm::mat4 mvp = projection * view * model; + + // Set the uniform and draw + shader.use(); + shader.setMat4("u_mvp", mvp); + glBindVertexArray(VAO); + glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, nullptr); +} + int main() { // 1. --- Initialize GLFW --- if (!glfwInit()) { @@ -201,8 +219,18 @@ int main() { // 5. --- Set up Vertex Data and Buffers --- // Load Cube OBJ + World world; Object3D cube = Object3D("objs/cube.obj"); + // Place cubes in the world + for (int z = 0; z <= 32; z++) { + for (int x = 0; x <= 32; x++) { + world.voxels.insert(glm::ivec3(x, -1, z)); + } + } + + world.voxels.insert(glm::ivec3(7, 0, 13)); + unsigned int VBO, VAO, EBO; glGenVertexArrays(1, &VAO); // 1. Create Vertex Array Object (VAO) glGenBuffers(1, &VBO); // 2. Create Vertex Buffer Object (VBO) @@ -256,11 +284,10 @@ int main() { view = camera.getView(); - // Draw the triangle - shader.use(); - shader.setMat4("u_mvp", projection * view); - glBindVertexArray(VAO); // Bind the VAO (our triangle's "recipe") - glDrawElements(GL_TRIANGLES, cube.EBO_buffer.size(), GL_UNSIGNED_INT, nullptr); // Draw it! + // Draw the World + for (glm::ivec3 voxel_pos : world.voxels) { + draw_cube(voxel_pos, shader, VAO, cube.EBO_buffer.size(), view, projection); + } double end_time = glfwGetTime(); float time_since_last_move = end_time - move_time;