diff options
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/World/BlockSide.hpp | 66 | ||||
| -rw-r--r-- | src/World/BlockType.hpp | 11 | ||||
| -rw-r--r-- | src/World/Chunk.cpp | 72 | ||||
| -rw-r--r-- | src/World/Chunk.hpp | 30 | ||||
| -rw-r--r-- | src/World/Generator.cpp | 26 | ||||
| -rw-r--r-- | src/World/Generator.hpp | 15 | ||||
| -rw-r--r-- | src/main.cpp | 41 |
8 files changed, 230 insertions, 33 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 25abc3c..99d9c81 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ if (LINUX) find_package(OpenGL REQUIRED) endif (LINUX) -add_executable(meowcraft src/main.cpp src/Window.cpp src/Window.hpp src/Mesh.cpp src/Mesh.hpp src/Math/Vector.hpp src/Math/Math.hpp src/Binder.cpp src/Binder.hpp src/Shader/Shader.cpp src/Shader/Shader.hpp src/Shader/ShaderProgram.cpp src/Shader/ShaderProgram.hpp src/Math/Matrix.hpp src/Math/MVP.cpp src/Math/MVP.hpp src/Camera.cpp src/Camera.hpp src/Math/Rotation.hpp src/Shader/Uniform.cpp src/Shader/Uniform.hpp src/Mouse.cpp src/Mouse.hpp src/Math/Trig.hpp src/Texture.cpp src/Texture.hpp src/Assets.cpp src/Assets.hpp src/Image/RawImage.cpp src/Image/RawImage.hpp src/Image/PPMParser.cpp src/Image/PPMParser.hpp) +add_executable(meowcraft src/main.cpp src/Window.cpp src/Window.hpp src/Mesh.cpp src/Mesh.hpp src/Math/Vector.hpp src/Math/Math.hpp src/Binder.cpp src/Binder.hpp src/Shader/Shader.cpp src/Shader/Shader.hpp src/Shader/ShaderProgram.cpp src/Shader/ShaderProgram.hpp src/Math/Matrix.hpp src/Math/MVP.cpp src/Math/MVP.hpp src/Camera.cpp src/Camera.hpp src/Math/Rotation.hpp src/Shader/Uniform.cpp src/Shader/Uniform.hpp src/Mouse.cpp src/Mouse.hpp src/Math/Trig.hpp src/Texture.cpp src/Texture.hpp src/Assets.cpp src/Assets.hpp src/Image/RawImage.cpp src/Image/RawImage.hpp src/Image/PPMParser.cpp src/Image/PPMParser.hpp src/World/Chunk.cpp src/World/Chunk.hpp src/World/BlockType.hpp src/World/Generator.cpp src/World/Generator.hpp src/World/BlockSide.hpp) target_link_libraries(meowcraft glfw GLEW::GLEW) if (LINUX) diff --git a/src/World/BlockSide.hpp b/src/World/BlockSide.hpp new file mode 100644 index 0000000..45efb11 --- /dev/null +++ b/src/World/BlockSide.hpp @@ -0,0 +1,66 @@ +#pragma once + +#include <cstdint> +#include <vector> +#include <array> +#include "../Math/Vector.hpp" + +namespace MC { + +class BlockSide { +public: + enum Value : uint8_t { + Front, + Back, + + Top, + Bottom, + + Left, + Right, + }; + + BlockSide() = default; + BlockSide(Value side) : m_side(side) {} + + operator Value() const { return m_side; } + + std::array<Vector<3>, 4> face() { + switch (m_side) { + case Front: + return {{ + {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 1.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 1.0f}, + }}; + case Back: + return {{ + {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, + }}; + case Top: + return {{ + {0.0f, 1.0f, 1.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, + }}; + case Bottom: + return {{ + {0.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f}, + }}; + case Left: + return {{ + {0.0f, 0.0f, 0.0f},{0.0f, 0.0f, 1.0f},{0.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 0.0f}, + }}; + case Right: + return {{ + {1.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, + }}; + } + } + + static std::vector<BlockSide> all() { + return { + Front, Back, Top, Bottom, Left, Right, + }; + } +private: + Value m_side; +}; + +} \ No newline at end of file diff --git a/src/World/BlockType.hpp b/src/World/BlockType.hpp new file mode 100644 index 0000000..e35daf8 --- /dev/null +++ b/src/World/BlockType.hpp @@ -0,0 +1,11 @@ +#pragma + +namespace MC { + +enum class BlockType : uint8_t { + Air, + Dirt, + Grass, +}; + +} \ No newline at end of file diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp new file mode 100644 index 0000000..73aab3d --- /dev/null +++ b/src/World/Chunk.cpp @@ -0,0 +1,72 @@ +#include "Chunk.hpp" +#include "BlockSide.hpp" + +namespace MC { + +void Chunk::set(uint32_t x, uint32_t y, uint32_t z, BlockType type) { + m_blocks[x][y][z].type = type; +} + +Mesh Chunk::mesh() { + std::vector<Vector<3>> positions{}; + std::vector<Vector<2>> tex_coords{}; + std::vector<uint32_t> indices{}; + + for (int x = 0; x < CHUNK_WIDTH; x++) { + for (int y = 0; y < CHUNK_HEIGHT; y++) { + for (int z = 0; z < CHUNK_WIDTH; z++) { + auto type = m_blocks[x][y][z].type; + if (type == BlockType::Air) { + continue; + } + + for (auto side: BlockSide::all()) { + auto side_tex_coords = Chunk::face_tex_coords(type, side); + auto side_positions = side.face(); + + for (auto& position : side_positions) { + position = position + Vector<3>{static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)}; + } + + uint32_t s = positions.size(); + + positions.insert(positions.end(), side_positions.begin(), side_positions.end()); + tex_coords.insert(tex_coords.end(), side_tex_coords.begin(), side_tex_coords.end()); + indices.insert(indices.end(), {s, s + 1, s + 3, s + 1, s + 2, s + 3}); + } + } + } + } + + return {positions, tex_coords, indices}; +} + +std::array<Vector<2>, 4> Chunk::face_tex_coords(BlockType type, BlockSide side) { + switch (type) { + case BlockType::Dirt: + return {{ + {0.5f, 0.0f}, {1.0f, 0.0f}, {1.0f, 0.5f}, {0.5f, 0.5f}, + }}; + case BlockType::Grass: + switch (side) { + case BlockSide::Front: + case BlockSide::Back: + case BlockSide::Left: + case BlockSide::Right: + return {{ + {0.5f, 1.0f}, {0.0f, 1.0f}, {0.0f, 0.5f}, {0.5f, 0.5f}, + }}; + case BlockSide::Top: + return {{ + {0.0f, 0.0f}, {0.5f, 0.0f}, {0.5f, 0.5f}, {0.0f, 0.5f}, + }}; + case BlockSide::Bottom: + return {{ + {0.5f, 0.0f}, {1.0f, 0.0f}, {1.0f, 0.5f}, {0.5f, 0.5f}, + }}; + } + default: + return {}; + }} + +} diff --git a/src/World/Chunk.hpp b/src/World/Chunk.hpp new file mode 100644 index 0000000..eafc613 --- /dev/null +++ b/src/World/Chunk.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include <cstdint> +#include "BlockType.hpp" +#include "../Mesh.hpp" +#include "BlockSide.hpp" + +#define CHUNK_WIDTH 8 +#define CHUNK_HEIGHT 8 + +namespace MC { + +class Chunk { +public: + Chunk() : m_blocks{} {}; + + void set(uint32_t x, uint32_t y, uint32_t z, BlockType type); + + Mesh mesh(); +private: + static std::array<Vector<2>, 4> face_tex_coords(BlockType type, BlockSide side); + + struct BlockData { + BlockType type; + }; + + BlockData m_blocks[CHUNK_WIDTH][CHUNK_HEIGHT][CHUNK_WIDTH]; +}; + +} diff --git a/src/World/Generator.cpp b/src/World/Generator.cpp new file mode 100644 index 0000000..5b8b9b5 --- /dev/null +++ b/src/World/Generator.cpp @@ -0,0 +1,26 @@ +#include "Generator.hpp" + +namespace MC { + +Chunk Generator::generate(uint32_t _x, uint32_t _y) { + Chunk chunk; + + for (int y = 0; y < CHUNK_HEIGHT; y++) { + BlockType type = BlockType::Air; + if (y < CHUNK_HEIGHT / 2) { + type = BlockType::Dirt; + } else if (y == CHUNK_HEIGHT / 2) { + type = BlockType::Grass; + } + + for (int x = 0; x < CHUNK_WIDTH; x++) { + for (int z = 0; z < CHUNK_WIDTH; z++) { + chunk.set(x, y, z, type); + } + } + } + + return chunk; +} + +} \ No newline at end of file diff --git a/src/World/Generator.hpp b/src/World/Generator.hpp new file mode 100644 index 0000000..8e67ee1 --- /dev/null +++ b/src/World/Generator.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include <cstdint> +#include "Chunk.hpp" + +namespace MC { + +class Generator { +public: + Generator() = default; + + Chunk generate(uint32_t x, uint32_t y); +}; + +} diff --git a/src/main.cpp b/src/main.cpp index 58c7b8a..d69dccd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,7 @@ #include "Mouse.hpp" #include "Texture.hpp" #include "Image/PPMParser.hpp" +#include "World/Generator.hpp" #define APP_NAME "Meowcraft" @@ -56,37 +57,11 @@ void run() { auto image = MC::Image::PPMParser(MC::Assets::Images::atlas).parse(); auto texture = MC::Texture(image); - MC::Mesh shape({ - {-1.0f, 1.0f, 1.0f}, {1.0f, 1.0f, 1.0f}, {1.0f, -1.0f, 1.0f}, {-1.0f, -1.0f, 1.0f}, - {-1.0f, 1.0f, -1.0f}, {1.0f, 1.0f, -1.0f}, {1.0f, -1.0f, -1.0f}, {-1.0f, -1.0f, -1.0f}, - - {-1.0f, 1.0f, -1.0f}, {-1.0f, 1.0f, 1.0f}, {-1.0f, -1.0f, 1.0f}, {-1.0f, -1.0f, -1.0f}, - {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f, -1.0f}, {1.0f, -1.0f, -1.0f}, {1.0f, -1.0f, 1.0f}, - - {-1.0f, 1.0f, -1.0f}, {1.0f, 1.0f, -1.0f}, {1.0f, 1.0f, 1.0f}, {-1.0f, 1.0f, 1.0f}, - {-1.0f, -1.0f, 1.0f}, {1.0f, -1.0f, 1.0f}, {1.0f, -1.0f, -1.0f}, {-1.0f, -1.0f, -1.0f}, - }, - { - {0.5f, 0.5f}, {0.0f, 0.5f}, {0.0f, 1.0f}, {0.5f, 1.0f}, - {0.5f, 0.5f}, {0.0f, 0.5f}, {0.0f, 1.0f}, {0.5f, 1.0f}, - - {0.5f, 0.5f}, {0.0f, 0.5f}, {0.0f, 1.0f}, {0.5f, 1.0f}, - {0.5f, 0.5f}, {0.0f, 0.5f}, {0.0f, 1.0f}, {0.5f, 1.0f}, - - {0.0f, 0.5f}, {0.5f, 0.5f}, {0.5f, 0.0f}, {0.0f, 0.0f}, - {0.5f, 0.5f}, {1.0f, 0.5f}, {1.0f, 0.0f}, {0.5f, 0.0f}, - }, - { - 0, 1, 3, 1, 2, 3, - 4, 5, 7, 5, 6, 7, - 8, 9, 11, 9, 10, 11, - 12, 13, 15, 13, 14, 15, - 16, 17, 19, 17, 18, 19, - 20, 21, 23, 21, 22, 23, - 24, 25, 27, 25, 26, 27, - }); + MC::Generator generator; + auto chunk = generator.generate(0, 0); + auto chunk_mesh = chunk.mesh(); - auto mesh = MC::Binder::load(shape); + auto mesh = MC::Binder::load(chunk_mesh); MC::Camera camera{}; camera.set_position({0.0f, 0.0f, 3.0f}); @@ -104,6 +79,9 @@ void run() { glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + uint64_t time = 0; while (!window.should_close()) { @@ -117,8 +95,7 @@ void run() { program.bind(); - auto angle = std::fmod(time / 10.0f, 360.0f); - auto model = Math::MVP::model({0.0f, 0.0f, 0.0f}, {angle, angle, 0.0f}); + auto model = Math::MVP::model({}, {}); model_uniform.set(model); auto view = Math::MVP::view(camera.position(), camera.angles()); |
