diff --git a/Jukcraft/Jukcraft.vcxproj b/Jukcraft/Jukcraft.vcxproj index afa5a32..345e3c6 100644 --- a/Jukcraft/Jukcraft.vcxproj +++ b/Jukcraft/Jukcraft.vcxproj @@ -163,6 +163,7 @@ + @@ -192,6 +193,7 @@ Create + diff --git a/Jukcraft/Jukcraft.vcxproj.filters b/Jukcraft/Jukcraft.vcxproj.filters index 7854a79..259b752 100644 --- a/Jukcraft/Jukcraft.vcxproj.filters +++ b/Jukcraft/Jukcraft.vcxproj.filters @@ -136,6 +136,7 @@ world\chunk + @@ -188,5 +189,6 @@ world\chunk + \ No newline at end of file diff --git a/Jukcraft/assets/shaders/terrain/vert.glsl b/Jukcraft/assets/shaders/terrain/vert.glsl index ab89b2a..2054843 100644 --- a/Jukcraft/assets/shaders/terrain/vert.glsl +++ b/Jukcraft/assets/shaders/terrain/vert.glsl @@ -34,6 +34,7 @@ void main(void) { float blocklightMultiplier = pow(0.8, 15.0 - blockLight); float intermediateSkylightMultiplier = pow(0.8, 15.0 - skyLight * u_Daylight); float skylightMultiplier = pow(0.8, 15.0 - skyLight); + float skylightBlueTint = pow(0.8, 15.0 - skyLight * (2.0 - pow(u_Daylight, 2))); float shading = float((a_VertexData & 0x3) + 2) / 5.0f; @@ -41,9 +42,9 @@ void main(void) { vs_Out.v_TexCoords = vec3(c_TexCoords[a_VertexData >> 10 & 0x3], a_VertexData >> 2 & 0xFF); vs_Out.v_Light = vec3( - clamp(blocklightMultiplier * (1.0 + 0.5 * (1.0 - u_Daylight)), intermediateSkylightMultiplier, 1.0), - clamp(blocklightMultiplier * (1.0 + 0.25 * (1.0 - u_Daylight)), intermediateSkylightMultiplier, 1.0), - clamp(skylightMultiplier * u_Daylight, blocklightMultiplier, 1.0) + clamp(blocklightMultiplier * 1.5, intermediateSkylightMultiplier, 1.0), + clamp(blocklightMultiplier * 1.25, intermediateSkylightMultiplier, 1.0), + clamp(skylightBlueTint, blocklightMultiplier, 1.0) ); vs_Out.v_Shading = shading * ao; } \ No newline at end of file diff --git a/Jukcraft/src/core/App.cpp b/Jukcraft/src/core/App.cpp index d4a212a..fc2130a 100644 --- a/Jukcraft/src/core/App.cpp +++ b/Jukcraft/src/core/App.cpp @@ -45,27 +45,25 @@ namespace Jukcraft { void App::run() { float lastTime = static_cast(glfwGetTime()), timer = lastTime; - float deltaTimePerTick = 0.0f, renderDeltaTime = 0.0f, nowTime = 0.0f; + float partialTicks = 0.0f, nowTime = 0.0f; int frames = 0, updates = 0; float deltaTime = 0.0f; while (!window->shouldClose()) { nowTime = static_cast(glfwGetTime()); - renderDeltaTime = (nowTime - lastTime); - deltaTimePerTick += (nowTime - lastTime) * TICK_RATE; + partialTicks += (nowTime - lastTime) * TICK_RATE; lastTime = nowTime; - // - Only update at 60 tick / s - while (deltaTimePerTick >= 1.0) { - deltaTime = deltaTimePerTick / TICK_RATE; - game->tick(deltaTime); // - Update function + // - Only update at 64 tick / s + while (partialTicks >= 1.0) { + game->tick(); // - Update function updates++; - deltaTimePerTick--; + partialTicks--; } - game->renderNewFrame(renderDeltaTime); + game->renderNewFrame(partialTicks); frames++; window->endFrame(); diff --git a/Jukcraft/src/core/Camera.cpp b/Jukcraft/src/core/Camera.cpp index ca033b3..58a3416 100644 --- a/Jukcraft/src/core/Camera.cpp +++ b/Jukcraft/src/core/Camera.cpp @@ -20,9 +20,8 @@ namespace Jukcraft { } - void Camera::update(const float deltaTime) { - glm::vec3 interpolatedPos = glm::mix(player->position, player->oldPosition, player->interpolationStep); - player->interpolationStep -= deltaTime; + void Camera::update(const float partialTicks) { + glm::vec3 interpolatedPos = glm::mix(player->oldPosition, player->position, partialTicks); const glm::vec2 cursorPos = App::GetWindow().getCursorPos(); const glm::vec2 deltaCursor = cursorPos - lastCursorPos; @@ -44,18 +43,14 @@ namespace Jukcraft { if (App::GetWindow().isKeyPressed(GLFW_KEY_LEFT_SHIFT)) input.y += -1; if (App::GetWindow().isKeyPressed(GLFW_KEY_SPACE)) input.y += 1; - if (deltaTime * 20.0f > 1.0f) - speed = WALK_SPEED; - else - speed += (WALK_SPEED - speed) * deltaTime * 20; + + speed = WALK_SPEED; if (input.y > 0) { player->jump(); } if (input.x || input.z) { - const float angle = player->yaw - glm::atan((float)input.z, (float)input.x) + glm::pi() / 2; - player->acceleration.x = glm::cos(angle) * speed; - player->acceleration.z = glm::sin(angle) * speed; + player->setRelativeAccel(glm::vec3((float)input.x * speed, 0.0f, (float)input.z * speed)); } diff --git a/Jukcraft/src/core/Game.cpp b/Jukcraft/src/core/Game.cpp index b029909..f18e85f 100644 --- a/Jukcraft/src/core/Game.cpp +++ b/Jukcraft/src/core/Game.cpp @@ -41,7 +41,7 @@ namespace Jukcraft { } } - void Game::hitCallback(int button, const glm::vec3& currentBlock, const glm::vec3& nextBlock) { + void Game::hitCallback(int button, const BlockPos& currentBlock, const BlockPos& nextBlock) { switch (button) { case GLFW_MOUSE_BUTTON_LEFT: world->setBlock(nextBlock, 0); @@ -56,13 +56,13 @@ namespace Jukcraft { } - void Game::tick(const float deltaTime) { - world->tick(deltaTime); - player->tick(deltaTime); + void Game::tick() { + world->tick(); + player->tick(); } - void Game::renderNewFrame(const float deltaTime) { - camera.update(deltaTime); + void Game::renderNewFrame(const float partialTicks) { + camera.update(partialTicks); Renderer::Begin(world->getSkyColor()); world->render(); diff --git a/Jukcraft/src/core/Game.h b/Jukcraft/src/core/Game.h index 61e6c29..6590cde 100644 --- a/Jukcraft/src/core/Game.h +++ b/Jukcraft/src/core/Game.h @@ -9,8 +9,8 @@ namespace Jukcraft { class Game { public: Game(); - void hitCallback(int button, const glm::vec3& currentBlock, const glm::vec3& nextBlock); - void tick(const float deltaTime); + void hitCallback(int button, const BlockPos& currentBlock, const BlockPos& nextBlock); + void tick(); void renderNewFrame(const float deltaTime); void onMousePress(int button); void speedTime() { diff --git a/Jukcraft/src/core/Window.h b/Jukcraft/src/core/Window.h index 4e131fc..784ea5d 100644 --- a/Jukcraft/src/core/Window.h +++ b/Jukcraft/src/core/Window.h @@ -74,8 +74,7 @@ namespace Jukcraft { glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); - context = glfwCreateWindow(window.getWidth(), window.getHeight(), "", nullptr, window.handle); + context = glfwCreateWindow(window.getWidth(), window.getHeight(), "New Context", nullptr, window.handle); } ~SharedContext() { glfwMakeContextCurrent(window.handle); diff --git a/Jukcraft/src/core/util.h b/Jukcraft/src/core/util.h index ad3cf86..0b7f83e 100644 --- a/Jukcraft/src/core/util.h +++ b/Jukcraft/src/core/util.h @@ -44,6 +44,41 @@ namespace Jukcraft { uint32_t baseInstance; }; + constexpr uint8_t CHUNK_DIM = 16; + constexpr uint8_t CHUNK_HEIGHT = 128; + + constexpr uint8_t WORLD_SIZE = 2; + + + struct PerChunkData { + glm::vec3 chunkPos; + }; + + struct BlockPos : public glm::ivec3 { + + BlockPos() :glm::ivec3() {} + BlockPos(const glm::ivec3& vec) :glm::ivec3() {} + BlockPos(glm::ivec3&& vec) :glm::ivec3(vec) {} + BlockPos(int x, int y, int z) :glm::ivec3(x, y, z) {} + BlockPos(int scalar) :glm::ivec3(scalar) {} + BlockPos(const glm::ivec2& chunkPos, const glm::ivec3& localPos) + :glm::ivec3((int)CHUNK_DIM * glm::ivec3(chunkPos.x, 0, chunkPos.y) + localPos) + { + + } + + constexpr glm::ivec2 getChunkPos() const noexcept { + return { glm::floor((float)x / CHUNK_DIM), glm::floor((float)z / CHUNK_DIM) }; + } + + constexpr glm::ivec3 getLocalPos() const noexcept { + return { glm::mod((float)x, (float)CHUNK_DIM), y, glm::mod((float)z, (float)CHUNK_DIM) }; + } + + }; + + + constexpr glm::vec3 EAST = glm::vec3(1.0f, 0.0f, 0.0f); constexpr glm::vec3 WEST = glm::vec3(-1.0f, 0.0f, 0.0f); constexpr glm::vec3 UP = glm::vec3(0.0f, 1.0f, 0.0f); @@ -51,7 +86,7 @@ namespace Jukcraft { constexpr glm::vec3 SOUTH = glm::vec3(0.0f, 0.0f, 1.0f); constexpr glm::vec3 NORTH = glm::vec3(0.0f, 0.0f, -1.0f); - constexpr float TICK_RATE = 60.0f; + constexpr float TICK_RATE = 64.0f; constexpr uint8_t EAST_INDEX = 0; constexpr uint8_t WEST_INDEX = 1; @@ -91,7 +126,7 @@ namespace Jukcraft { } template - int sign(T x) { + constexpr int sign(T x) { return (x > 0) - (x <= 0); } } \ No newline at end of file diff --git a/Jukcraft/src/entity/Entity.h b/Jukcraft/src/entity/Entity.h index 672ebdc..17d0995 100644 --- a/Jukcraft/src/entity/Entity.h +++ b/Jukcraft/src/entity/Entity.h @@ -8,23 +8,34 @@ namespace Jukcraft { } Entity(const glm::vec3& initialPos, const glm::vec3& initialVelocity, const glm::vec3& initialAcceleration, float initialYaw, float initialPitch) - :position(initialPos), velocity(initialVelocity), acceleration(initialAcceleration), + :position(initialPos), velocity(initialVelocity), relativeAccel(initialAcceleration), yaw(initialYaw), pitch(initialPitch) { } virtual ~Entity() {} - virtual void tick(float delta_time) = 0; + virtual void tick() = 0; + virtual void applyPhysics() = 0; + virtual void aiStep() = 0; + virtual void move(const glm::vec3& motion) = 0; + virtual void push(const glm::vec3& motion) = 0; + virtual void render(float deltaTime) {} constexpr const glm::vec3& getPos() const { return position; } - constexpr const glm::vec3& getVelocity() const { return position; } - constexpr const glm::vec3& getAccel() const { return position; } + constexpr const glm::vec3& getVelocity() const { return velocity; } + constexpr const glm::vec3& getRelativeAccel() const { return relativeAccel; } constexpr float getYaw() const { return yaw; } constexpr float getPitch() const { return pitch; } + + void setPos(const glm::vec3& pos) { position = pos; } + void setVelocity(const glm::vec3& v) { velocity = v; } + void setRelativeAccel(const glm::vec3& a) { relativeAccel = a; } + void setYaw(float theta) { yaw = theta; } + void setPitch(float phi) { pitch = phi; } protected: glm::vec3 position; glm::vec3 velocity; - glm::vec3 acceleration; + glm::vec3 relativeAccel; float yaw, pitch; }; } \ No newline at end of file diff --git a/Jukcraft/src/entity/HitRay.cpp b/Jukcraft/src/entity/HitRay.cpp index bcde8c6..ac1b53d 100644 --- a/Jukcraft/src/entity/HitRay.cpp +++ b/Jukcraft/src/entity/HitRay.cpp @@ -13,7 +13,7 @@ namespace Jukcraft { } bool HitRay::check(int button, HitCallback callback, - float distance, const glm::vec3& currentBlock, const glm::vec3& nextBlock) { + float distance, const BlockPos& currentBlock, const BlockPos& nextBlock) { if (world.getBlock(nextBlock)) { callback(button, currentBlock, nextBlock); @@ -26,20 +26,13 @@ namespace Jukcraft { return false; } } - bool HitRay::step(int button, HitCallback callback) { - glm::ivec3 sign = glm::ivec3(1, 1, 1); - glm::vec3 localPos = position - (glm::vec3)block; - glm::vec3 absoluteVector = vector; + bool HitRay::step(int button, HitCallback callback) { + glm::ivec3 sign = glm::sign(vector); + glm::vec3 localPos = (glm::vec3)sign * (position - (glm::vec3)block); - for (uint8_t component = 0; component < 3; component++) { - if (vector[component] < 0) { - sign[component] = -1; + glm::vec3 absoluteVector = glm::abs(vector); - absoluteVector[component] = -absoluteVector[component]; - localPos[component] = -localPos[component]; - } - } float vx = absoluteVector.x, vy = absoluteVector.y, vz = absoluteVector.z; float lx = localPos.x, ly = localPos.y, lz = localPos.z; @@ -52,7 +45,7 @@ namespace Jukcraft { if (y >= -0.5f && y <= 0.5f && z >= -0.5f && z <= 0.5f) { float distance = glm::distance(glm::vec3(x, y, z), localPos); - return check(button, callback, distance, block, glm::vec3(block.x + sign.x, block.y, block.z)); + return check(button, callback, distance, block, glm::ivec3(block.x + sign.x, block.y, block.z)); } } @@ -63,7 +56,7 @@ namespace Jukcraft { if (x >= -0.5f && x <= 0.5f && z >= -0.5f && z <= 0.5f) { float distance = glm::distance(glm::vec3(x, y, z), localPos); - return check(button, callback, distance, block, glm::vec3(block.x, block.y + sign.y, block.z)); + return check(button, callback, distance, block, glm::ivec3(block.x, block.y + sign.y, block.z)); } } @@ -74,7 +67,7 @@ namespace Jukcraft { if (x >= -0.5f && x <= 0.5f && y >= -0.5f && y <= 0.5f) { float distance = glm::distance(glm::vec3(x, y, z), localPos); - return check(button, callback, distance, block, glm::vec3(block.x, block.y, block.z + sign.z)); + return check(button, callback, distance, block, glm::ivec3(block.x, block.y, block.z + sign.z)); } } diff --git a/Jukcraft/src/entity/HitRay.h b/Jukcraft/src/entity/HitRay.h index 373fef7..95f26f9 100644 --- a/Jukcraft/src/entity/HitRay.h +++ b/Jukcraft/src/entity/HitRay.h @@ -4,18 +4,18 @@ namespace Jukcraft { - using HitCallback = std::function; + using HitCallback = std::function; struct HitRay { HitRay(World& world, LivingEntity& entity); bool check(int button, HitCallback callback, - float distance, const glm::vec3& currentBlock, const glm::vec3& nextBlock); + float distance, const BlockPos& currentBlock, const BlockPos& nextBlock); bool step(int button, HitCallback callback); World& world; glm::vec3 vector; glm::vec3 position; - glm::ivec3 block; + BlockPos block; float distance; }; diff --git a/Jukcraft/src/entity/LivingEntity.cpp b/Jukcraft/src/entity/LivingEntity.cpp index 1aba0ef..fe8c6a4 100644 --- a/Jukcraft/src/entity/LivingEntity.cpp +++ b/Jukcraft/src/entity/LivingEntity.cpp @@ -21,9 +21,9 @@ namespace Jukcraft { collider.vx2 = position + glm::vec3(width / 2.0f, height, width / 2.0f); } - void LivingEntity::resolveCollisions(float deltaTime) { + void LivingEntity::resolveCollisions() { - glm::vec3 adjustedVelocity = velocity * deltaTime; + glm::vec3 adjustedVelocity = velocity; // find all the blocks we could potentially be colliding with // this step is known as "broad-phasing" @@ -45,7 +45,7 @@ namespace Jukcraft { for (int i = p.x - step_x * (steps_xz + 1); step_x * (i - (c.x + step_x * (steps_xz + 2))) < 0; i += step_x) for (int j = p.y - step_y * (steps_y + 2); step_y * (j - (c.y + step_y * (steps_y + 3))) < 0; j += step_y) for (int k = p.z - step_z * (steps_xz + 1); step_z * (k - (c.z + step_z * (steps_xz + 2))) < 0; k += step_z) { - glm::ivec3 pos = { i, j, k }; + BlockPos pos = { i, j, k }; BlockID block = world.getBlock(pos); if (!block) @@ -99,32 +99,48 @@ namespace Jukcraft { velocity.y = glm::sqrt(-2 * g.y * hmax); } - void LivingEntity::tick(float deltaTime) { - oldPosition = position; + void LivingEntity::tick() { + aiStep(); + } + + void LivingEntity::aiStep() { + applyPhysics(); + } + + void LivingEntity::applyPhysics() { + float accelMagnitude = glm::length(glm::vec2(relativeAccel.x, relativeAccel.z)); + float headingAngle = yaw - glm::atan(relativeAccel.z, relativeAccel.x) + glm::pi() / 2; + + velocity += glm::vec3( + glm::cos(headingAngle) * accelMagnitude, + relativeAccel.y, + glm::sin(headingAngle) * accelMagnitude + ) * getFriction(); - velocity += acceleration * getFriction() * deltaTime; - acceleration = glm::vec3(0.0f); + setRelativeAccel(glm::vec3(0.0f)); updateCollider(); - + onGround = false; for (uint8_t i = 0; i < 3; i++) - resolveCollisions(deltaTime); + resolveCollisions(); + + oldPosition = position; + position += velocity; + velocity += g; - position += velocity * deltaTime; - velocity += g * deltaTime; - auto comp = [](auto& a, auto& b) { return glm::abs(a) < glm::abs(b); }; + velocity *= glm::max(glm::vec3(0.0), glm::vec3(1.0) - getFriction()); - glm::vec3 deltaFriction = glm::vec3( - std::min(velocity.x * getFriction().x * deltaTime, velocity.x, comp), - std::min(velocity.y * getFriction().y * deltaTime, velocity.y, comp), - std::min(velocity.z * getFriction().z * deltaTime, velocity.z, comp) - ); - velocity -= deltaFriction; updateCollider(); + } + void LivingEntity::move(const glm::vec3& motion) { + velocity += motion; + } + void LivingEntity::push(const glm::vec3& motion) { + velocity += motion; } } \ No newline at end of file diff --git a/Jukcraft/src/entity/LivingEntity.h b/Jukcraft/src/entity/LivingEntity.h index 0b34142..62b8fd8 100644 --- a/Jukcraft/src/entity/LivingEntity.h +++ b/Jukcraft/src/entity/LivingEntity.h @@ -17,9 +17,13 @@ namespace Jukcraft { virtual ~LivingEntity(); void updateCollider(); - void resolveCollisions(float deltaTime); + void resolveCollisions(); void jump(float jumpHeight = JUMP_HEIGHT); - void tick(float deltaTime) override; + void aiStep() override; + void tick() override; + void applyPhysics() override; + void move(const glm::vec3& motion) override; + void push(const glm::vec3& motion) override; constexpr float getEyeLevel() const { return height - 0.2f; } diff --git a/Jukcraft/src/entity/player/Player.cpp b/Jukcraft/src/entity/player/Player.cpp index 0bd68d6..e5ac141 100644 --- a/Jukcraft/src/entity/player/Player.cpp +++ b/Jukcraft/src/entity/player/Player.cpp @@ -5,7 +5,7 @@ namespace Jukcraft { Player::Player(World& world, const glm::vec3& initialPos, const glm::vec3& initialVelocity, const glm::vec3& initialAcceleration, float initialYaw, float initialPitch) - :BipedEntity(world, initialPos, initialVelocity, initialAcceleration, initialYaw, initialPitch), interpolationStep(1.0f) { + :BipedEntity(world, initialPos, initialVelocity, initialAcceleration, initialYaw, initialPitch) { } @@ -13,9 +13,5 @@ namespace Jukcraft { } - void Player::tick(float deltaTime) { - interpolationStep = 1.0f; - BipedEntity::tick(deltaTime); - } } \ No newline at end of file diff --git a/Jukcraft/src/entity/player/Player.h b/Jukcraft/src/entity/player/Player.h index 899ae5c..695fad1 100644 --- a/Jukcraft/src/entity/player/Player.h +++ b/Jukcraft/src/entity/player/Player.h @@ -7,10 +7,7 @@ namespace Jukcraft { Player(World& world, const glm::vec3& initialPos = glm::vec3(0.0f), const glm::vec3& initialVelocity = glm::vec3(0.0f), const glm::vec3& initialAcceleration = glm::vec3(0.0f), float initialYaw = 0.0f, float initialPitch = 0.0f); virtual ~Player(); - - void tick(float deltaTime) override; private: - float interpolationStep; friend class Camera; }; } diff --git a/Jukcraft/src/physics/constants.h b/Jukcraft/src/physics/constants.h index 6701324..ef81181 100644 --- a/Jukcraft/src/physics/constants.h +++ b/Jukcraft/src/physics/constants.h @@ -1,14 +1,14 @@ #pragma once namespace Jukcraft { - constexpr glm::vec3 g = glm::vec3(0.0f, -32.0f, 0.0f); + constexpr glm::vec3 g = glm::vec3(0.0f, -32.0f, 0.0f) / (TICK_RATE * TICK_RATE); constexpr float JUMP_HEIGHT = 1.25f; // Changed in Minecraft 1.9 for 1.5f - constexpr float WALK_SPEED = 4.317f; + constexpr float WALK_SPEED = 4.317f / 64.0f; - constexpr glm::vec3 FRICTION = glm::vec3(20.0f, 20.0f, 20.0f); + constexpr glm::vec3 FRICTION = glm::vec3(20.0f, 20.0f, 20.0f) / TICK_RATE; - constexpr glm::vec3 DRAG_FLY = glm::vec3(5.0f, 5.0f, 5.0f); - constexpr glm::vec3 DRAG_JUMP = glm::vec3(1.8f, 0.0f, 1.8f); - constexpr glm::vec3 DRAG_FALL = glm::vec3(1.8f, 0.4f, 1.8f); + constexpr glm::vec3 DRAG_FLY = glm::vec3(5.0f, 5.0f, 5.0f) / TICK_RATE; + constexpr glm::vec3 DRAG_JUMP = glm::vec3(1.8f, 0.0f, 1.8f) / TICK_RATE; + constexpr glm::vec3 DRAG_FALL = glm::vec3(1.8f, 0.4f, 1.8f) / TICK_RATE; } diff --git a/Jukcraft/src/renderer/chunk/Mesh.cpp b/Jukcraft/src/renderer/chunk/Mesh.cpp index a7d19fb..9c6e4fc 100644 --- a/Jukcraft/src/renderer/chunk/Mesh.cpp +++ b/Jukcraft/src/renderer/chunk/Mesh.cpp @@ -6,22 +6,19 @@ namespace Jukcraft { static inline uint8_t smooth(uint8_t light, uint8_t b, uint8_t c, uint8_t d) { if (!(light && b && c && d)) { - std::array l = { - light, - b > 0 ? b : std::numeric_limits::max(), - c > 0 ? c : std::numeric_limits::max(), - d > 0 ? d : std::numeric_limits::max() - }; - - uint8_t min_val = *(std::min_element(l.begin(), l.end())); + uint8_t min_val = light; + if (b && b < min_val) + min_val = b; + if (c && c < min_val) + min_val = c; + if (d && d < min_val) + min_val = d; light = std::max(light, min_val); b = std::max(b, min_val); c = std::max(c, min_val); d = std::max(d, min_val); - - } return light + b + c + d; // To divide by 4 diff --git a/Jukcraft/src/renderer/chunk/RenderRegion.cpp b/Jukcraft/src/renderer/chunk/RenderRegion.cpp new file mode 100644 index 0000000..1730571 --- /dev/null +++ b/Jukcraft/src/renderer/chunk/RenderRegion.cpp @@ -0,0 +1 @@ +#include "pch.h" \ No newline at end of file diff --git a/Jukcraft/src/renderer/chunk/RenderRegion.h b/Jukcraft/src/renderer/chunk/RenderRegion.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/Jukcraft/src/renderer/chunk/RenderRegion.h @@ -0,0 +1 @@ +#pragma once diff --git a/Jukcraft/src/world/LightEngine.cpp b/Jukcraft/src/world/LightEngine.cpp index b5ef21c..7e7ac7b 100644 --- a/Jukcraft/src/world/LightEngine.cpp +++ b/Jukcraft/src/world/LightEngine.cpp @@ -11,7 +11,7 @@ namespace Jukcraft { for (uint32_t lz = 0; lz < CHUNK_DIM; lz++) { int ly = CHUNK_HEIGHT - 1; for (; ly >= 0; ly--) { - if (chunk->getBlock(glm::ivec3(lx, ly, lz))) + if (chunk->getBlock(BlockPos(lx, ly, lz))) break; } @@ -23,17 +23,17 @@ namespace Jukcraft { for (uint32_t ly = CHUNK_HEIGHT - 1; ly >= height; ly--) for (uint32_t lx = 0; lx < CHUNK_DIM; lx++) for (uint32_t lz = 0; lz < CHUNK_DIM; lz++) { - chunk->setSkyLight(glm::ivec3(lx, ly, lz), 15); + chunk->setSkyLight(BlockPos(lx, ly, lz), 15); } for (uint32_t lx = 0; lx < CHUNK_DIM; lx++) for (uint32_t lz = 0; lz < CHUNK_DIM; lz++) { - glm::ivec3 localPos = glm::ivec3(lx, height, lz); - glm::ivec3 LightEnginePos = Chunk::ToWorldPos(chunkPos, localPos); + glm::ivec3 localPos(lx, height, lz); + BlockPos lightEnginePos(chunkPos, localPos); skylightIncreaseQueue.push( SkyLightIncreaseNode{ - LightEnginePos, + lightEnginePos, 15 } ); @@ -41,8 +41,8 @@ namespace Jukcraft { } } - void LightEngine::increaseLight(const glm::ivec3& pos, std::shared_ptr& chunk, uint8_t light) { - chunk->setBlockLight(Chunk::ToLocalPos(pos), light); + void LightEngine::increaseLight(const BlockPos& pos, std::shared_ptr& chunk, uint8_t light) { + chunk->setBlockLight(pos.getLocalPos(), light); lightIncreaseQueue.push( BlockLightIncreaseNode{ @@ -53,9 +53,9 @@ namespace Jukcraft { propagateLightIncrease(); } - void LightEngine::decreaseLight(const glm::ivec3& pos, std::shared_ptr& chunk) { - uint8_t oldlight = chunk->getBlockLight(Chunk::ToLocalPos(pos)); - chunk->setBlockLight(Chunk::ToLocalPos(pos), 0); + void LightEngine::decreaseLight(const BlockPos& pos, std::shared_ptr& chunk) { + uint8_t oldlight = chunk->getBlockLight(pos.getLocalPos()); + chunk->setBlockLight(pos.getLocalPos(), 0); lightDecreaseQueue.push( BlockLightDecreaseNode{ @@ -67,9 +67,9 @@ namespace Jukcraft { propagateLightIncrease(); } - void LightEngine::decreaseSkyLight(const glm::ivec3& pos, std::shared_ptr& chunk) { - uint8_t oldlight = chunk->getSkyLight(Chunk::ToLocalPos(pos)); - chunk->setSkyLight(Chunk::ToLocalPos(pos), 0); + void LightEngine::decreaseSkyLight(const BlockPos& pos, std::shared_ptr& chunk) { + uint8_t oldlight = chunk->getSkyLight(pos.getLocalPos()); + chunk->setSkyLight(pos.getLocalPos(), 0); skylightDecreaseQueue.push( SkyLightDecreaseNode{ @@ -84,23 +84,24 @@ namespace Jukcraft { void LightEngine::propagateLightIncrease() { while (!lightIncreaseQueue.empty()) { const BlockLightIncreaseNode& node = lightIncreaseQueue.front(); - glm::ivec3 pos = node.pos; + BlockPos pos = node.pos; uint8_t light = node.light; lightIncreaseQueue.pop(); for (const glm::ivec3& direction : IDIRECTIONS) { - glm::ivec3 newPos = pos + direction; + BlockPos newPos = pos + direction; if (newPos.y < 0 || newPos.y > CHUNK_HEIGHT) continue; - std::optional> chunk = chunkManager.getChunk(Chunk::ToChunkPos(newPos)); + std::optional> chunk = chunkManager.getChunk(newPos.getChunkPos()); if (!chunk.has_value()) continue; - glm::ivec3 localPos = Chunk::ToLocalPos(newPos); - if (chunk.value()->getBlockLight(localPos) + 2 <= light && blocks[chunk.value()->getBlock(localPos)].isTransparent()) { + glm::ivec3 localPos = newPos.getLocalPos(); + if (chunk.value()->getBlockLight(localPos) + 2 <= light + && blocks[chunk.value()->getBlock(localPos)].isTransparent()) { chunk.value()->setBlockLight(localPos, light - 1); lightIncreaseQueue.push( BlockLightIncreaseNode{ @@ -117,22 +118,23 @@ namespace Jukcraft { void LightEngine::propagateSkyLightIncrease() { while (!skylightIncreaseQueue.empty()) { const SkyLightIncreaseNode& node = skylightIncreaseQueue.front(); - glm::ivec3 pos = node.pos; + BlockPos pos = node.pos; uint8_t skylight = node.light; skylightIncreaseQueue.pop(); for (const glm::ivec3& direction : IDIRECTIONS) { - glm::ivec3 newPos = pos + direction; + BlockPos newPos = pos + direction; if (newPos.y < 0 || newPos.y > CHUNK_HEIGHT) continue; - std::optional> chunk = chunkManager.getChunk(Chunk::ToChunkPos(newPos)); + std::optional> chunk = chunkManager.getChunk(newPos.getChunkPos()); if (!chunk.has_value()) continue; - glm::ivec3 localPos = Chunk::ToLocalPos(newPos); + glm::ivec3 localPos = newPos.getLocalPos(); - if (chunk.value()->getSkyLight(localPos) < skylight && blocks[chunk.value()->getBlock(localPos)].isTransparent()) { + if (chunk.value()->getSkyLight(localPos) < skylight + && blocks[chunk.value()->getBlock(localPos)].isTransparent()) { if (direction.y == -1) { chunk.value()->setSkyLight(localPos, skylight); skylightIncreaseQueue.push( @@ -161,21 +163,21 @@ namespace Jukcraft { void LightEngine::propagateLightDecrease() { while (!lightDecreaseQueue.empty()) { const BlockLightDecreaseNode& node = lightDecreaseQueue.front(); - glm::ivec3 pos = node.pos; + BlockPos pos = node.pos; uint8_t oldlight = node.oldlight; lightDecreaseQueue.pop(); for (const glm::ivec3& direction : IDIRECTIONS) { - glm::ivec3 newPos = pos + direction; + BlockPos newPos = pos + direction; if (newPos.y < 0 || newPos.y > CHUNK_HEIGHT) continue; - std::optional> chunk = chunkManager.getChunk(Chunk::ToChunkPos(newPos)); + std::optional> chunk = chunkManager.getChunk(newPos.getChunkPos()); if (!chunk.has_value()) continue; - glm::ivec3 localPos = Chunk::ToLocalPos(newPos); + glm::ivec3 localPos = newPos.getLocalPos(); Block block = blocks[chunk.value()->getBlock(localPos)]; if (block.getLight()) { @@ -220,20 +222,20 @@ namespace Jukcraft { void LightEngine::propagateSkyLightDecrease() { while (!skylightDecreaseQueue.empty()) { const SkyLightDecreaseNode& node = skylightDecreaseQueue.front(); - glm::ivec3 pos = node.pos; + BlockPos pos = node.pos; uint8_t oldlight = node.oldlight; skylightDecreaseQueue.pop(); for (const glm::ivec3& direction : IDIRECTIONS) { - glm::ivec3 newPos = pos + direction; + BlockPos newPos = pos + direction; if (newPos.y < 0 || newPos.y > CHUNK_HEIGHT) continue; - std::optional> chunk = chunkManager.getChunk(Chunk::ToChunkPos(newPos)); + std::optional> chunk = chunkManager.getChunk(newPos.getChunkPos()); if (!chunk.has_value()) continue; - glm::ivec3 localPos = Chunk::ToLocalPos(newPos); + glm::ivec3 localPos = newPos.getLocalPos(); Block block = blocks[chunk.value()->getBlock(localPos)]; if (block.isTransparent()) { diff --git a/Jukcraft/src/world/LightEngine.h b/Jukcraft/src/world/LightEngine.h index 4231648..04b375d 100644 --- a/Jukcraft/src/world/LightEngine.h +++ b/Jukcraft/src/world/LightEngine.h @@ -6,10 +6,10 @@ namespace Jukcraft { public: LightEngine(ChunkManager& chunkManager, const std::vector& blocks) :chunkManager(chunkManager), blocks(blocks) {} - void increaseLight(const glm::ivec3& pos, std::shared_ptr& chunk, uint8_t light); + void increaseLight(const BlockPos& pos, std::shared_ptr& chunk, uint8_t light); void initSkyLight(std::shared_ptr& chunk); - void decreaseLight(const glm::ivec3& pos, std::shared_ptr& chunk); - void decreaseSkyLight(const glm::ivec3& pos, std::shared_ptr& chunk); + void decreaseLight(const BlockPos& pos, std::shared_ptr& chunk); + void decreaseSkyLight(const BlockPos& pos, std::shared_ptr& chunk); void propagateLightIncrease(); void propagateLightDecrease(); void propagateSkyLightIncrease(); @@ -19,19 +19,19 @@ namespace Jukcraft { private: void markPositionForUpdate(std::shared_ptr&, const glm::ivec3& localPos); struct BlockLightIncreaseNode { - glm::ivec3 pos; + BlockPos pos; uint8_t light; }; struct BlockLightDecreaseNode { - glm::ivec3 pos; + BlockPos pos; uint8_t oldlight; }; struct SkyLightIncreaseNode { - glm::ivec3 pos; + BlockPos pos; uint8_t light; }; struct SkyLightDecreaseNode { - glm::ivec3 pos; + BlockPos pos; uint8_t oldlight; }; std::queue lightIncreaseQueue; diff --git a/Jukcraft/src/world/World.cpp b/Jukcraft/src/world/World.cpp index 62e873b..b29f5d8 100644 --- a/Jukcraft/src/world/World.cpp +++ b/Jukcraft/src/world/World.cpp @@ -15,14 +15,14 @@ namespace Jukcraft { ); } - void World::tick(float deltaTime) { + void World::tick() { time += 1; updateDaylight(); chunkManager.tick(); lightEngine.propagateLightIncrease(); } - void World::trySetBlock(Player& player, const glm::ivec3& worldPos, BlockID blockID) { + void World::trySetBlock(Player& player, const BlockPos& worldPos, BlockID blockID) { if (!blockID) setBlock(worldPos, blockID); @@ -32,40 +32,42 @@ namespace Jukcraft { setBlock(worldPos, blockID); } - void World::setBlock(const glm::ivec3& worldPos, BlockID blockID) { - glm::ivec2 chunkPos = Chunk::ToChunkPos(worldPos); + void World::setBlock(const BlockPos& blockPos, BlockID blockID) { + glm::ivec2 chunkPos = blockPos.getChunkPos(); std::optional> pendingChunk = chunkManager.getChunk(chunkPos); + if (!pendingChunk.has_value()) return; + std::shared_ptr& chunk = *pendingChunk; - glm::ivec3 localPos = Chunk::ToLocalPos(worldPos); + glm::ivec3 localPos = blockPos.getLocalPos(); const Block& block = blocks[blockID]; chunk->setBlock(localPos, blockID); if (!blockID) { - lightEngine.decreaseLight(worldPos, chunk); - lightEngine.decreaseSkyLight(worldPos, chunk); + lightEngine.decreaseLight(blockPos, chunk); + lightEngine.decreaseSkyLight(blockPos, chunk); } else { if (block.getLight()) { - lightEngine.increaseLight(worldPos, chunk, block.getLight()); + lightEngine.increaseLight(blockPos, chunk, block.getLight()); } else if (!block.isTransparent()) { - lightEngine.decreaseLight(worldPos, chunk); - lightEngine.decreaseSkyLight(worldPos, chunk); + lightEngine.decreaseLight(blockPos, chunk); + lightEngine.decreaseSkyLight(blockPos, chunk); } } chunkManager.updateChunkAtPosition(chunk, localPos); } - BlockID World::getBlock(const glm::ivec3& worldPos) const { - glm::ivec2 chunkPos = Chunk::ToChunkPos(worldPos); + BlockID World::getBlock(const BlockPos& blockPos) const { + glm::ivec2 chunkPos = blockPos.getChunkPos(); std::optional> pendingChunk = chunkManager.getChunk(chunkPos); if (!pendingChunk.has_value()) return 0; std::shared_ptr& chunk = *pendingChunk; - glm::ivec3 localPos = Chunk::ToLocalPos(worldPos); + glm::ivec3 localPos = blockPos.getLocalPos(); return chunk->getBlock(localPos); } diff --git a/Jukcraft/src/world/World.h b/Jukcraft/src/world/World.h index 3e2864e..d6f6ef0 100644 --- a/Jukcraft/src/world/World.h +++ b/Jukcraft/src/world/World.h @@ -7,12 +7,12 @@ namespace Jukcraft { class World { public: World(const std::vector& blocks, gfx::Shader& shader); - void tick(float deltaTime); + void tick(); void render(); - void setBlock(const glm::ivec3& worldPos, BlockID block); - void trySetBlock(Player& player, const glm::ivec3& worldPos, BlockID block); + void setBlock(const BlockPos& blockPos, BlockID block); + void trySetBlock(Player& player, const BlockPos& blockPos, BlockID block); - BlockID getBlock(const glm::ivec3& worldPos) const; + BlockID getBlock(const BlockPos& worldPos) const; void speedTime() { if (daylight <= 480) diff --git a/Jukcraft/src/world/chunk/Chunk.cpp b/Jukcraft/src/world/chunk/Chunk.cpp index 44d8deb..9ef6eef 100644 --- a/Jukcraft/src/world/chunk/Chunk.cpp +++ b/Jukcraft/src/world/chunk/Chunk.cpp @@ -78,31 +78,25 @@ namespace Jukcraft { for (uint8_t x = 0; x < CHUNK_DIM; x++) for (uint8_t z = 0; z < CHUNK_DIM; z++) { glm::ivec3 localPos = glm::ivec3(x, y, z); - BlockID block = getBlock(localPos); if (!block) continue; - + const Block& type = blockTypes[block]; - if (true) { - for (uint8_t i = 0; i < 6; i++) { - if (canRenderFacing(localPos + IDIRECTIONS[i])) { - uint8_t blocklight = getBlockLightSafe(localPos + IDIRECTIONS[i]); - uint8_t skylight = getSkyLightSafe(localPos + IDIRECTIONS[i]); - - BakedQuad bakedQuad = mesh.bakeCubeFace(localPos, i, blocklight, skylight); - // BakedQuad bakedQuad(blocklight, skylight); - mesh.pushCubeFace(type.getModel().getQuads()[i], localPos, type.getTextureLayout()[i], bakedQuad); - quadCount++; - } + + for (uint8_t i = 0; i < 6; i++) { + if (canRenderFacing(localPos + IDIRECTIONS[i])) { + uint8_t blocklight = getBlockLightSafe(localPos + IDIRECTIONS[i]); + uint8_t skylight = getSkyLightSafe(localPos + IDIRECTIONS[i]); + + BakedQuad bakedQuad = mesh.bakeCubeFace(localPos, i, blocklight, skylight); + mesh.pushCubeFace(type.getModel().getQuads()[i], localPos, type.getTextureLayout()[i], bakedQuad); + quadCount++; } } - } mesh.end(); - - drawable = true; } @@ -123,9 +117,9 @@ namespace Jukcraft { [[nodiscard]] uint8_t Chunk::getOpacitySafe(const glm::ivec3& localPos) const { if (IsOutside(localPos)) { - glm::ivec3 worldPos = Chunk::ToWorldPos(chunkPos, localPos); - glm::ivec2 newChunkPos = Chunk::ToChunkPos(worldPos); - glm::ivec3 newLocalPos = Chunk::ToLocalPos(worldPos); + BlockPos blockPos(chunkPos, localPos); + glm::ivec2 newChunkPos = blockPos.getChunkPos(); + glm::ivec3 newLocalPos = blockPos.getLocalPos(); std::optional> chunk = chunkManager.getChunk(newChunkPos); if (!chunk.has_value() || IsOutside(newLocalPos)) return 0; @@ -138,9 +132,9 @@ namespace Jukcraft { [[nodiscard]] uint8_t Chunk::getBlockLightSafe(const glm::ivec3& localPos) const { if (IsOutside(localPos)) { - glm::ivec3 worldPos = Chunk::ToWorldPos(chunkPos, localPos); - glm::ivec2 newChunkPos = Chunk::ToChunkPos(worldPos); - glm::ivec3 newLocalPos = Chunk::ToLocalPos(worldPos); + BlockPos blockPos(chunkPos, localPos); + glm::ivec2 newChunkPos = blockPos.getChunkPos(); + glm::ivec3 newLocalPos = blockPos.getLocalPos(); std::optional> chunk = chunkManager.getChunk(newChunkPos); if (!chunk.has_value() || IsOutside(newLocalPos)) return 0; @@ -153,9 +147,9 @@ namespace Jukcraft { [[nodiscard]] uint8_t Chunk::getSkyLightSafe(const glm::ivec3& localPos) const { if (IsOutside(localPos)) { - glm::ivec3 worldPos = Chunk::ToWorldPos(chunkPos, localPos); - glm::ivec2 newChunkPos = Chunk::ToChunkPos(worldPos); - glm::ivec3 newLocalPos = Chunk::ToLocalPos(worldPos); + BlockPos blockPos(chunkPos, localPos); + glm::ivec2 newChunkPos = blockPos.getChunkPos(); + glm::ivec3 newLocalPos = blockPos.getLocalPos(); std::optional> chunk = chunkManager.getChunk(newChunkPos); if (!chunk.has_value() || IsOutside(newLocalPos)) return 15; diff --git a/Jukcraft/src/world/chunk/Chunk.h b/Jukcraft/src/world/chunk/Chunk.h index 10568c7..6f8a04a 100644 --- a/Jukcraft/src/world/chunk/Chunk.h +++ b/Jukcraft/src/world/chunk/Chunk.h @@ -20,19 +20,11 @@ namespace Jukcraft { [[nodiscard]] constexpr const glm::ivec2& getChunkPos() { return chunkPos; } - template - [[nodiscard]] constexpr BlockID getBlock(const glm::vec<3, VectorType>& localPos) const; - template - void setBlock(const glm::vec<3, VectorType>& localPos, const BlockID block); - - template - [[nodiscard]] static constexpr glm::vec<2, VectorType> ToChunkPos(const glm::vec<3, VectorType>& worldPos); - template - [[nodiscard]] static constexpr glm::vec<3, VectorType> ToLocalPos(const glm::vec<3, VectorType>& worldPos); - template - [[nodiscard]] static constexpr glm::vec<3, VectorType> ToWorldPos(const glm::vec<2, VectorType>& chunkPos, const glm::vec<3, VectorType>& localPos); - template - [[nodiscard]] static constexpr bool IsOutside(const glm::vec<3, VectorType>& localPos); + [[nodiscard]] constexpr BlockID getBlock(const glm::ivec3& localPos) const; + + void setBlock(const glm::ivec3& localPos, const BlockID block); + + [[nodiscard]] static constexpr bool IsOutside(const glm::ivec3& localPos); [[nodiscard]] bool canRenderFacing(const glm::ivec3& localPos) const { return !getOpacitySafe(localPos); } [[nodiscard]] uint8_t getOpacitySafe(const glm::ivec3& localPos) const; @@ -40,16 +32,25 @@ namespace Jukcraft { [[nodiscard]] uint8_t getSkyLightSafe(const glm::ivec3& localPos) const; - template - [[nodiscard]] constexpr uint8_t getRawLight(const glm::vec<3, VectorType>& localPos) const noexcept; - template - [[nodiscard]] constexpr uint8_t getBlockLight(const glm::vec<3, VectorType>& localPos) const noexcept; - template - [[nodiscard]] constexpr uint8_t getSkyLight(const glm::vec<3, VectorType>& localPos) const noexcept; - template - void setBlockLight(const glm::vec<3, VectorType>& localPos, uint8_t value) noexcept; - template - void setSkyLight(const glm::vec<3, VectorType>& localPos, uint8_t value) noexcept; + [[nodiscard]] constexpr uint8_t getRawLight(const glm::ivec3& localPos) const noexcept { + return lightMap[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z]; + } + + [[nodiscard]] constexpr uint8_t getBlockLight(const glm::ivec3& localPos) const noexcept { + return lightMap[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z] & 0xF; + } + + [[nodiscard]] constexpr uint8_t getSkyLight(const glm::ivec3& localPos) const noexcept { + return (lightMap[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z] >> 4) & 0xF; + } + + void setBlockLight(const glm::ivec3& localPos, uint8_t value) noexcept { + lightMap[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z] = (lightMap[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z] & 0xF0) | value; + } + + void setSkyLight(const glm::ivec3& localPos, uint8_t value) noexcept { + lightMap[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z] = (lightMap[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z] & 0xF) | (value << 4); + } private: struct { std::weak_ptr east; @@ -77,54 +78,22 @@ namespace Jukcraft { friend class ChunkManager; }; - template - [[nodiscard]] constexpr uint8_t Chunk::getRawLight(const glm::vec<3, VectorType>& localPos) const noexcept { - return lightMap[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z]; - } - template - [[nodiscard]] constexpr uint8_t Chunk::getBlockLight(const glm::vec<3, VectorType>& localPos) const noexcept { - return lightMap[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z] & 0xF; - } - template - [[nodiscard]] constexpr uint8_t Chunk::getSkyLight(const glm::vec<3, VectorType>& localPos) const noexcept { - return (lightMap[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z] >> 4) & 0xF; - } - template - inline void Chunk::setBlockLight(const glm::vec<3, VectorType>& localPos, uint8_t value) noexcept { - lightMap[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z] = (lightMap[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z] & 0xF0) | value; - } - template - inline void Chunk::setSkyLight(const glm::vec<3, VectorType>& localPos, uint8_t value) noexcept { - lightMap[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z] = (lightMap[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z] & 0xF) | (value << 4); - } - template - [[nodiscard]] constexpr BlockID Chunk::getBlock(const glm::vec<3, VectorType>& localPos) const { + + + [[nodiscard]] constexpr BlockID Chunk::getBlock(const glm::ivec3& localPos) const { if (localPos.y > CHUNK_HEIGHT || localPos.x > CHUNK_DIM || localPos.z > CHUNK_DIM || localPos.y < 0 || localPos.x < 0 || localPos.z < 0) return 0; return blocks[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z]; } - template - inline void Chunk::setBlock(const glm::vec<3, VectorType>& localPos, const BlockID block) { + inline void Chunk::setBlock(const glm::ivec3& localPos, const BlockID block) { if (localPos.y > CHUNK_HEIGHT || localPos.x > CHUNK_DIM || localPos.z > CHUNK_DIM || localPos.y < 0 || localPos.x < 0 || localPos.z < 0) return; blocks[(uint8_t)localPos.y][(uint8_t)localPos.x][(uint8_t)localPos.z] = block; } - template - [[nodiscard]] static constexpr glm::vec<2, VectorType> Chunk::ToChunkPos(const glm::vec<3, VectorType>& worldPos) { - return { glm::floor((float)worldPos.x / CHUNK_DIM), glm::floor((float)worldPos.z / CHUNK_DIM) }; - } - template - [[nodiscard]] static constexpr glm::vec<3, VectorType> Chunk::ToLocalPos(const glm::vec<3, VectorType>& worldPos) { - return { glm::mod((float)worldPos.x, (float)CHUNK_DIM), worldPos.y, glm::mod((float)worldPos.z, (float)CHUNK_DIM) }; - } - template - [[nodiscard]] static constexpr glm::vec<3, VectorType> Chunk::ToWorldPos(const glm::vec<2, VectorType>& chunkPos, const glm::vec<3, VectorType>& localPos) { - return { localPos.x + chunkPos.x * CHUNK_DIM, localPos.y, localPos.z + chunkPos.y * CHUNK_DIM }; - } - template - [[nodiscard]] static constexpr bool Chunk::IsOutside(const glm::vec<3, VectorType>& localPos) { + + [[nodiscard]] constexpr bool Chunk::IsOutside(const glm::ivec3& localPos) { return (localPos.x < 0 || localPos.x >= CHUNK_DIM || localPos.y < 0 || localPos.y >= CHUNK_HEIGHT || localPos.z < 0 || localPos.z >= CHUNK_DIM); diff --git a/Jukcraft/src/world/chunk/util.h b/Jukcraft/src/world/chunk/util.h index aae52b6..f188a71 100644 --- a/Jukcraft/src/world/chunk/util.h +++ b/Jukcraft/src/world/chunk/util.h @@ -1,13 +1,5 @@ #pragma once namespace Jukcraft { - constexpr uint8_t CHUNK_DIM = 16; - constexpr uint8_t CHUNK_HEIGHT = 128; - - constexpr uint8_t WORLD_SIZE = 16; - - - struct PerChunkData { - glm::vec3 chunkPos; - }; + } \ No newline at end of file