prepare code for a more dynamic world
This commit is contained in:
parent
5f1bf88374
commit
54a01e3a05
@ -19,6 +19,7 @@ add_executable(VoxelEngine
|
|||||||
src/Object3D.h
|
src/Object3D.h
|
||||||
src/Object3D.cpp
|
src/Object3D.cpp
|
||||||
src/Camera.h
|
src/Camera.h
|
||||||
|
src/World.h
|
||||||
)
|
)
|
||||||
|
|
||||||
# --- Configure GLAD (from your src/ and include/ folders) ---
|
# --- Configure GLAD (from your src/ and include/ folders) ---
|
||||||
|
|||||||
11
build.ps1
Normal file
11
build.ps1
Normal file
@ -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
|
||||||
30
src/World.h
Normal file
30
src/World.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#ifndef WORLD_H
|
||||||
|
#define CAMERA_H
|
||||||
|
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
// Hash function for glm::ivec3 to use with unordered_set
|
||||||
|
namespace std {
|
||||||
|
template <>
|
||||||
|
struct hash<glm::ivec3> {
|
||||||
|
size_t operator()(const glm::ivec3& v) const {
|
||||||
|
// Combine hash values of x, y, z components
|
||||||
|
size_t h1 = hash<int>()(v.x);
|
||||||
|
size_t h2 = hash<int>()(v.y);
|
||||||
|
size_t h3 = hash<int>()(v.z);
|
||||||
|
|
||||||
|
// Use a simple hash combination algorithm
|
||||||
|
return h1 ^ (h2 << 1) ^ (h3 << 2);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
class World
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::unordered_set<glm::ivec3> voxels;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif WORLD_H
|
||||||
45
src/main.cpp
45
src/main.cpp
@ -1,16 +1,18 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
#include <glad/glad.h> // Must be included before GLFW
|
#include <glad/glad.h> // Must be included before GLFW
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
#include "Shader.h"
|
#include "Shader.h"
|
||||||
#include "Camera.h"
|
#include "Camera.h"
|
||||||
#include "Object3D.h"
|
#include "Object3D.h"
|
||||||
|
#include "World.h"
|
||||||
|
|
||||||
// Window dimensions
|
// Window dimensions
|
||||||
unsigned int SCR_WIDTH = 800;
|
unsigned int SCR_WIDTH = 800 * 1.5;
|
||||||
unsigned int SCR_HEIGHT = 600;
|
unsigned int SCR_HEIGHT = 600 * 1.5;
|
||||||
|
|
||||||
// Jump
|
// Jump
|
||||||
const float JUMP_COOLDOWN = 0.0f;
|
const float JUMP_COOLDOWN = 0.0f;
|
||||||
@ -169,6 +171,22 @@ void processInput(GLFWwindow *window)
|
|||||||
camera.speed = CAMERA_SPEED * 20;
|
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() {
|
int main() {
|
||||||
// 1. --- Initialize GLFW ---
|
// 1. --- Initialize GLFW ---
|
||||||
if (!glfwInit()) {
|
if (!glfwInit()) {
|
||||||
@ -201,8 +219,18 @@ int main() {
|
|||||||
// 5. --- Set up Vertex Data and Buffers ---
|
// 5. --- Set up Vertex Data and Buffers ---
|
||||||
|
|
||||||
// Load Cube OBJ
|
// Load Cube OBJ
|
||||||
|
World world;
|
||||||
Object3D cube = Object3D("objs/cube.obj");
|
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;
|
unsigned int VBO, VAO, EBO;
|
||||||
glGenVertexArrays(1, &VAO); // 1. Create Vertex Array Object (VAO)
|
glGenVertexArrays(1, &VAO); // 1. Create Vertex Array Object (VAO)
|
||||||
glGenBuffers(1, &VBO); // 2. Create Vertex Buffer Object (VBO)
|
glGenBuffers(1, &VBO); // 2. Create Vertex Buffer Object (VBO)
|
||||||
@ -256,11 +284,10 @@ int main() {
|
|||||||
|
|
||||||
view = camera.getView();
|
view = camera.getView();
|
||||||
|
|
||||||
// Draw the triangle
|
// Draw the World
|
||||||
shader.use();
|
for (glm::ivec3 voxel_pos : world.voxels) {
|
||||||
shader.setMat4("u_mvp", projection * view);
|
draw_cube(voxel_pos, shader, VAO, cube.EBO_buffer.size(), view, projection);
|
||||||
glBindVertexArray(VAO); // Bind the VAO (our triangle's "recipe")
|
}
|
||||||
glDrawElements(GL_TRIANGLES, cube.EBO_buffer.size(), GL_UNSIGNED_INT, nullptr); // Draw it!
|
|
||||||
|
|
||||||
double end_time = glfwGetTime();
|
double end_time = glfwGetTime();
|
||||||
float time_since_last_move = end_time - move_time;
|
float time_since_last_move = end_time - move_time;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user