From c2e3d555bbb83cda1303ac2b443a7e8849c90a4b Mon Sep 17 00:00:00 2001 From: Tunacan Date: Tue, 31 Aug 2021 16:26:46 +0300 Subject: [PATCH] i forgor what i did --- .gitignore | 3 ++- WindowsMakefile | 31 +++++++++++++++++++++++++++++++ src/camera.h | 27 ++++++++++++++------------- src/main.cpp | 24 ++++++++++++++++++++---- src/texture.h | 6 +++--- src/world.cpp | 21 ++++++++++----------- src/world.h | 4 +++- 7 files changed, 83 insertions(+), 33 deletions(-) create mode 100644 WindowsMakefile diff --git a/.gitignore b/.gitignore index d4d3848..b83c81e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ out/ .vscode/ -voksel \ No newline at end of file +voksel +voksel.exe \ No newline at end of file diff --git a/WindowsMakefile b/WindowsMakefile new file mode 100644 index 0000000..495c518 --- /dev/null +++ b/WindowsMakefile @@ -0,0 +1,31 @@ +CC = x86_64-w64-mingw32-gcc +CPP = x86_64-w64-mingw32-g++ +STRIP = x86_64-w64-mingw32-strip +CFLAGS = -O2 -g -Wstrict-aliasing -Iinclude +LDFLAGS = -static -lglfw3 -lopengl32 -lgdi32 -luser32 -lkernel32 -lssp -lnoise + +OBJ = out/glad.o out/stb_image.o out/world.o out/main.o +BIN = voksel.exe + +.PHONY: all clean + +all: dirs $(BIN) + +dirs: + mkdir -p out + +run: all + ./$(BIN) + +$(BIN): $(OBJ) + $(CPP) -o $(BIN) $^ $(LDFLAGS) + $(STRIP) $(BIN) + +out/%.o: src/%.c + $(CC) -o $@ -c $< $(CFLAGS) + +out/%.o: src/%.cpp + $(CPP) -o $@ -c $< $(CFLAGS) + +clean: + rm -rf $(BIN) $(OBJ) diff --git a/src/camera.h b/src/camera.h index 7cc7190..db1f127 100644 --- a/src/camera.h +++ b/src/camera.h @@ -17,11 +17,11 @@ enum CameraMovement { }; // Default camera values -const float YAW = -90.0f; -const float PITCH = 0.0f; -const float SPEED = 2.5f; -const float SENSITIVITY = 0.1f; -const float ZOOM = 90.0f; +const f32 YAW = -90.0f; +const f32 PITCH = 0.0f; +const f32 SPEED = 2.5f; +const f32 SENSITIVITY = 0.1f; +const f32 ZOOM = 90.0f; // An abstract camera class that processes input and calculates the corresponding Euler Angles, Vectors and Matrices for use in OpenGL @@ -35,16 +35,17 @@ public: glm::vec3 right; glm::vec3 worldUp; // euler Angles - float yaw; - float pitch; + f32 yaw; + f32 pitch; // camera options - float movementSpeed; - float mouseSensitivity; - float zoom; + f32 movementSpeed; + f32 mouseSensitivity; + f32 zoom; + u32 renderDistance = 4; // constructor with vectors explicit Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), - float yaw = YAW, float pitch = PITCH) : position(position), worldUp(up), yaw(yaw), pitch(pitch), + f32 yaw = YAW, f32 pitch = PITCH) : position(position), worldUp(up), yaw(yaw), pitch(pitch), front(glm::vec3(0.0f, 0.0f, -1.0f)), movementSpeed(SPEED), mouseSensitivity(SENSITIVITY), zoom(ZOOM) { updateCameraVectors(); @@ -58,7 +59,7 @@ public: // processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems) void processKeyboard(CameraMovement direction, double deltaTime) { - float velocity = movementSpeed * deltaTime; + f32 velocity = movementSpeed * deltaTime; if (direction == FORWARD) position += front * velocity; if (direction == BACKWARD) @@ -70,7 +71,7 @@ public: } // processes input received from a mouse input system. Expects the offset value in both the x and y direction. - void processMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true) + void processMouseMovement(f32 xoffset, f32 yoffset, GLboolean constrainPitch = true) { xoffset *= mouseSensitivity; yoffset *= mouseSensitivity; diff --git a/src/main.cpp b/src/main.cpp index 92fdd3d..c764a24 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,7 @@ u32 windowWidth = 800; u32 windowHeight = 600; // camera -Camera camera(glm::vec3(0.0f, 2.0f, -3.0f)); +Camera camera(glm::vec3(0.0f, 2.0f, 0.0f)); f32 lastX = windowWidth / 2.0f; f32 lastY = windowHeight / 2.0f; bool firstMouse = true; @@ -59,7 +59,7 @@ int main(int argc, char *argv[]) { glEnable(GL_CULL_FACE); stbi_set_flip_vertically_on_load(true); - Shader shader("../res/shaders/chunk.vs.glsl", "../res/shaders/chunk.fs.glsl"); + Shader shader("res/shaders/chunk.vs.glsl", "res/shaders/chunk.fs.glsl"); TextureAtlas textureAtlas("res/textureatlas.png", GL_RGBA, 16); @@ -89,7 +89,7 @@ int main(int argc, char *argv[]) { glm::mat4 view = camera.getViewMatrix(); shader.setMat4("view", view); - world->render(&shader, &textureAtlas); + world->render(&camera, &shader, &textureAtlas); { i64 timeTaken = 0; @@ -99,9 +99,25 @@ int main(int argc, char *argv[]) { auto p = c->first; timeTaken += c->second->buildMesh(world, &textureAtlas, p.x, p.y, p.z); totalTimeTaken += timeTaken; + world->chunksWithMeshes[p] = c->second; world->chunksToBuild.erase(p); } else break; - } while (timeTaken < 1500); + } while (timeTaken < 2000); + } + + if (world->chunksWithMeshes.size() > 0) { + auto c = world->chunksWithMeshes.begin(); + auto p = c->first; + if (p.x < floor(camera.position.x / 3.2f) - camera.renderDistance || + p.y < floor(camera.position.y / 3.2f) - camera.renderDistance || + p.z < floor(camera.position.z / 3.2f) - camera.renderDistance || + p.x > floor(camera.position.x / 3.2f) + camera.renderDistance || + p.y > floor(camera.position.y / 3.2f) + camera.renderDistance || + p.z > floor(camera.position.z / 3.2f) + camera.renderDistance) + { + if (c->second->mesh != nullptr) delete c->second->mesh; + world->chunksWithMeshes.erase(p); + } } glfwSwapBuffers(window); diff --git a/src/texture.h b/src/texture.h index e6ea78f..d4c98e1 100644 --- a/src/texture.h +++ b/src/texture.h @@ -11,7 +11,7 @@ public: i32 height; i32 nrChannels; - Texture(char *texturePath, int format) { + Texture(const char *texturePath, int format) { glGenTextures(1, &id); glBindTexture(GL_TEXTURE_2D, id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); @@ -27,7 +27,7 @@ public: stbi_image_free(data); } - void bind(Shader *shader, char *name, u32 unit) { + void bind(Shader *shader, const char *name, u32 unit) { glActiveTexture(GL_TEXTURE0 + unit); glBindTexture(GL_TEXTURE_2D, id); shader->setInt(name, unit); @@ -38,7 +38,7 @@ class TextureAtlas : public Texture { public: f32 stride; - TextureAtlas(char *texturePath, int format, u32 tileSize) : Texture(texturePath, format) { + TextureAtlas(const char *texturePath, int format, u32 tileSize) : Texture(texturePath, format) { stride = static_cast(tileSize) / static_cast(width); } diff --git a/src/world.cpp b/src/world.cpp index 0bffa89..5c8e2e1 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -5,13 +5,8 @@ #include #include -World::World() { - -} - -Chunk::Chunk() { - -} +World::World() {} +Chunk::Chunk() {} // returns the time it took in microseconds i64 Chunk::buildMesh(World *world, TextureAtlas *textureAtlas, i32 chunkX, i32 chunkY, i32 chunkZ) { @@ -102,6 +97,10 @@ i64 Chunk::buildMesh(World *world, TextureAtlas *textureAtlas, i32 chunkX, i32 c uv.push_back(sideTexCoordsInverse[i]); } } + delete[] topTexCoords; + delete[] sideTexCoords; + delete[] sideTexCoordsInverse; + delete[] bottomTexCoords; } } } @@ -222,12 +221,12 @@ void World::setBlock(u32 block, i32 x, i32 y, i32 z, TextureAtlas *textureAtlas) } } -void World::render(Shader *shader, TextureAtlas *textureAtlas) { +void World::render(Camera *camera, Shader *shader, TextureAtlas *textureAtlas) { shader->use(); textureAtlas->bind(shader, "a_texture", 0); - for (i32 x = -8; x < 9; x++) - for (i32 y = -4; y < 5; y++) - for (i32 z = -8; z < 9; z++) { + for (i32 x = floor(camera->position.x / 3.2f) - camera->renderDistance; x < floor(camera->position.x / 3.2f) + camera->renderDistance; x++) + for (i32 y = floor(camera->position.y / 3.2f) - camera->renderDistance; y < floor(camera->position.y / 3.2f) + camera->renderDistance; y++) + for (i32 z = floor(camera->position.z / 3.2f) - camera->renderDistance; z < floor(camera->position.z / 3.2f) + camera->renderDistance; z++) { glm::mat4 model = glm::mat4(1.0f); model = glm::translate(model, glm::vec3(CHUNK_SIZE * 0.2f * x, CHUNK_SIZE * 0.2f * y, CHUNK_SIZE * 0.2f * z)); model = glm::scale(model, glm::vec3(0.1f, 0.1f, 0.1f)); diff --git a/src/world.h b/src/world.h index 2ec1263..ed49e46 100644 --- a/src/world.h +++ b/src/world.h @@ -7,6 +7,7 @@ #include "mesh.h" #include "shader.h" #include "texture.h" +#include "camera.h" #include "util.h" #define CHUNK_SIZE 16 @@ -28,6 +29,7 @@ public: noise::module::Perlin noise; std::unordered_map chunks; std::unordered_map chunksToBuild; + std::unordered_map chunksWithMeshes; World(); @@ -37,5 +39,5 @@ public: inline u32 getBlockForMesh(Chunk *chunk, i32 chunkX, i32 chunkY, i32 chunkZ, i32 offsetX, i32 offsetY, i32 offsetZ); void setBlock(u32 block, i32 x, i32 y, i32 z, TextureAtlas *textureAtlas); - void render(Shader *shader, TextureAtlas *textureAtlas); + void render(Camera *camera, Shader *shader, TextureAtlas *textureAtlas); };