From 49da2ad7011ca1e544ed603b5a6d1f14ca169614 Mon Sep 17 00:00:00 2001 From: Nicole Collings <100Toby1@gmail.com> Date: Fri, 22 Nov 2019 00:59:55 -0800 Subject: [PATCH] Organized and Optimized TransPos, renamed to Space - Clean up VecUtils --- CMakeLists.txt | 10 +- src/CMakeLists.txt | 2 +- src/def/gen/MapGen.cpp | 10 +- src/def/gen/MapGenJob.h | 2 +- src/game/hud/DebugGui.cpp | 42 +++--- src/game/hud/DebugGui.h | 2 +- src/game/scene/world/LocalWorld.cpp | 8 +- src/game/scene/world/LocalWorld.h | 2 +- src/game/scene/world/MeshGenStream.cpp | 2 +- src/game/scene/world/MeshGenStream.h | 2 +- src/game/scene/world/Player.cpp | 6 +- src/game/scene/world/graph/MeshChunk.cpp | 2 +- src/game/scene/world/graph/MeshChunk.h | 2 +- src/game/scene/world/graph/MeshGenerator.cpp | 16 +-- src/lua/api/type/LuaLocalPlayer.cpp | 2 +- src/server/conn/ServerPlayer.cpp | 6 +- src/server/conn/ServerPlayer.h | 2 +- src/server/world/ServerWorld.cpp | 11 +- src/server/world/ServerWorld.h | 2 +- src/server/world/WorldGenStream.h | 2 +- src/util/Pointer.h | 1 - src/util/Space.h | 140 +++++++++++++++++++ src/util/TransPos.h | 105 -------------- src/util/Util.h | 1 - src/util/Vec.h | 38 ++--- 25 files changed, 220 insertions(+), 198 deletions(-) create mode 100644 src/util/Space.h delete mode 100644 src/util/TransPos.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 667960af..1ad07f1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,8 +72,8 @@ endif() target_compile_definitions(${MAIN_EXEC_NAME} PUBLIC SOL_ALL_SAFETIES_ON) # Test Build -#add_subdirectory(test) -#add_executable(${TEST_EXEC_NAME} test/Main.cpp) -# -#target_link_libraries(${TEST_EXEC_NAME} Zepha_Core) -#target_link_libraries(${TEST_EXEC_NAME} zeusTest) +add_subdirectory(test) +add_executable(${TEST_EXEC_NAME} test/Main.cpp) + +target_link_libraries(${TEST_EXEC_NAME} Zepha_Core) +target_link_libraries(${TEST_EXEC_NAME} Zepha_Test) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6328c317..f52df393 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -91,7 +91,7 @@ set(ZEPHA_SRC server/world/WorldGenStream.h game/scene/world/WorldInterpolationStream.cpp game/scene/world/WorldInterpolationStream.h - util/TransPos.h + util/Space.h game/scene/world/MeshGenStream.cpp game/scene/world/MeshGenStream.h util/Vec.h diff --git a/src/def/gen/MapGen.cpp b/src/def/gen/MapGen.cpp index 6316f0d4..ac82d762 100644 --- a/src/def/gen/MapGen.cpp +++ b/src/def/gen/MapGen.cpp @@ -173,7 +173,7 @@ void MapGen::buildDensityMap(MapGenJob &job, const glm::vec3& worldPos) { glm::vec3 lp; for (int m = 0; m < 4096; m++) { - VecUtils::indAssignVec(m, lp); + Vec::indAssignVec(m, lp); job.density[m] = sTerrainFinal.get(lp) - (lp.y + worldPos.y * 16); } } @@ -189,7 +189,7 @@ void MapGen::buildElevationMap(std::array, 64 short depth = 16; - if (chunk.first.density[VecUtils::vecToInd(x, 15, z)] > 0) { + if (chunk.first.density[Space::Block::index({x, 15, z})] > 0) { if (localPos.y < 3) { unsigned short index = localPos.x + 4 * (localPos.y + 1 + 4 * localPos.z); upperChunk = &chunks[index].first; @@ -200,7 +200,7 @@ void MapGen::buildElevationMap(std::array, 64 } for (int j = 0; j < 16; j++) { - int ind = VecUtils::vecToInd(x, j, z); + int ind = Space::Block::index({x, j, z}); if (upperChunk->density[ind] <= 0) { depth = j; @@ -211,7 +211,7 @@ void MapGen::buildElevationMap(std::array, 64 else depth = 0; for (int y = 15; y >= 0; y--) { - int ind = VecUtils::vecToInd(x, y, z); + int ind = Space::Block::index({x, y, z}); if (chunk.first.density[ind] > 0) { depth = std::min(depth + 1, 16); @@ -233,7 +233,7 @@ void MapGen::populateChunk(std::pair& chunk, const glm:: glm::vec3 lp; for (int m = 0; m < 4096; m++) { - VecUtils::indAssignVec(m, lp); + Vec::indAssignVec(m, lp); auto biome = biomes.getBiomeAt(sTemperature.get(lp), sHumidity.get(lp), sRoughness.get(lp)); chunk.second->biomes[m] = biome.index; diff --git a/src/def/gen/MapGenJob.h b/src/def/gen/MapGenJob.h index 75dc41f3..cec2ac75 100644 --- a/src/def/gen/MapGenJob.h +++ b/src/def/gen/MapGenJob.h @@ -7,7 +7,7 @@ #include #include #include -#include "../../util/TransPos.h" +#include "../../util/Space.h" struct MapGenJob { std::array density {}; diff --git a/src/game/hud/DebugGui.cpp b/src/game/hud/DebugGui.cpp index 8369ac9a..a5c375cc 100644 --- a/src/game/hud/DebugGui.cpp +++ b/src/game/hud/DebugGui.cpp @@ -97,25 +97,26 @@ void DebugGui::update(Player& player, LocalWorld& world, LocalDefs& defs, double } { //Top-left Data - glm::vec3 footPos = TransPos::roundPos(player.getPos()) - glm::vec3(0, 0.02, 0); + glm::vec3 footPos = glm::floor(player.getPos()) - glm::vec3(0, 0.02, 0); unsigned int blockID = world.getBlock(footPos); std::string on = defs.defs().fromId(blockID).identifier; - unsigned int biomeID = world.getBiome(TransPos::roundPos(player.getPos())); + unsigned int biomeID = world.getBiome(glm::floor(player.getPos())); std::string biome = defs.gen().biomeFromId(biomeID).identifier; - glm::vec3 playerPos = TransPos::roundPos(player.getPos()); - glm::vec3 chunkPos = TransPos::chunkFromVec(playerPos); + glm::vec3 playerPos = glm::floor(player.getPos()); + glm::vec3 chunkPos = Space::Chunk::world::fromBlock(playerPos); //Block Coordinates offset from respective container - glm::vec3 posOffsetFromChunk = TransPos::chunkLocalFromVec(playerPos); - glm::vec3 posOffsetFromBlock = TransPos::mapBlockLocalFromVec(playerPos); - glm::vec3 posOffsetFromRegion = TransPos::regionLocalFromVec(playerPos); + //TODO: Reimplement these functions + glm::vec3 posOffsetFromChunk = Space::Block::relative::toChunk(playerPos); + glm::vec3 posOffsetFromBlock = Space::Block::relative::toMapBlock(playerPos); + glm::vec3 posOffsetFromRegion = Space::Block::relative::toRegion(playerPos); - glm::vec3 chunkCoordinate = TransPos::Dimension::chunkOffsetFromMapBlock(chunkPos); - glm::vec3 mapBlockCoordinate = TransPos::Dimension::mapBlockOffsetFromRegion(chunkPos); - glm::vec3 regionCoordinate = TransPos::Dimension::regionFromVec(chunkPos); + glm::vec3 chunkCoordinate = chunkPos; + glm::vec3 mapBlockCoordinate = Space::MapBlock::world::fromChunk(chunkPos); + glm::vec3 regionCoordinate = Space::Region::world::fromChunk(chunkPos); std::ostringstream str; @@ -141,18 +142,15 @@ void DebugGui::update(Player& player, LocalWorld& world, LocalDefs& defs, double PointedThing thing = player.getPointedThing(); if (thing.thing == PointedThing::Thing::BLOCK) { - std::string face = "NONE"; - if (thing.thing == PointedThing::Thing::BLOCK) { - Dir faceDir = thing.target.block.face; - std::string face = - (faceDir == Dir::TOP) ? "TOP" : - (faceDir == Dir::BOTTOM) ? "BOTTOM" : - (faceDir == Dir::LEFT) ? "LEFT" : - (faceDir == Dir::RIGHT) ? "RIGHT" : - (faceDir == Dir::FRONT) ? "FRONT" : - (faceDir == Dir::BACK) ? "BACK" : - "NONE"; - } + Dir faceDir = thing.target.block.face; + std::string face = + (faceDir == Dir::TOP) ? "TOP" : + (faceDir == Dir::BOTTOM) ? "BOTTOM" : + (faceDir == Dir::LEFT) ? "LEFT" : + (faceDir == Dir::RIGHT) ? "RIGHT" : + (faceDir == Dir::FRONT) ? "FRONT" : + (faceDir == Dir::BACK) ? "BACK" : + "NONE"; str << "Pointing At: " << defs.defs().blockFromId(thing.target.block.blockId).identifier << std::endl; str << "Pointed Position: " << vecToString(thing.target.block.pos) << std::endl; str << "Pointed Face: " << face << std::endl; diff --git a/src/game/hud/DebugGui.h b/src/game/hud/DebugGui.h index e37aa79c..838d5870 100644 --- a/src/game/hud/DebugGui.h +++ b/src/game/hud/DebugGui.h @@ -7,7 +7,7 @@ #include #include "../graph/window/Window.h" #include "../graph/drawable/DrawableGroup.h" -#include "../../util/TransPos.h" +#include "../../util/Space.h" #include "../../util/Util.h" #include "../../util/Ray.h" #include "components/compound/GUILabelledGraph.h" diff --git a/src/game/scene/world/LocalWorld.cpp b/src/game/scene/world/LocalWorld.cpp index ff8ddbdf..e499545c 100644 --- a/src/game/scene/world/LocalWorld.cpp +++ b/src/game/scene/world/LocalWorld.cpp @@ -133,8 +133,8 @@ int LocalWorld::getMeshChunkCount() { } unsigned int LocalWorld::getBlock(glm::vec3 pos) { - auto chunkPos = TransPos::chunkFromVec(TransPos::roundPos(pos)); - auto local = TransPos::chunkLocalFromVec(TransPos::roundPos(pos)); + auto chunkPos = Space::Chunk::world::fromBlock(pos); + auto local = Space::Block::relative::toChunk(pos); auto chunk = getChunk(chunkPos); if (chunk != nullptr) return chunk->getBlock(local); @@ -142,8 +142,8 @@ unsigned int LocalWorld::getBlock(glm::vec3 pos) { } unsigned short LocalWorld::getBiome(glm::vec3 pos) { - auto chunkPos = TransPos::chunkFromVec(TransPos::roundPos(pos)); - auto local = TransPos::chunkLocalFromVec(TransPos::roundPos(pos)); + auto chunkPos = Space::Chunk::world::fromBlock(pos); + auto local = Space::Block::relative::toChunk(pos); auto chunk = getChunk(chunkPos); if (chunk != nullptr) return chunk->getBiome(local); diff --git a/src/game/scene/world/LocalWorld.h b/src/game/scene/world/LocalWorld.h index e11ccfd0..082135d5 100644 --- a/src/game/scene/world/LocalWorld.h +++ b/src/game/scene/world/LocalWorld.h @@ -15,7 +15,7 @@ #include "../../../def/LocalDefinitionAtlas.h" #include "../../../world/chunk/BlockChunk.h" #include "../../../def/gen/MapGen.h" -#include "../../../util/TransPos.h" +#include "../../../util/Space.h" #include "../../../util/Vec.h" #include "../../../world/LocalDimension.h" #include "../../../def/LocalDefs.h" diff --git a/src/game/scene/world/MeshGenStream.cpp b/src/game/scene/world/MeshGenStream.cpp index 75616221..48483a45 100644 --- a/src/game/scene/world/MeshGenStream.cpp +++ b/src/game/scene/world/MeshGenStream.cpp @@ -64,7 +64,7 @@ std::vector MeshGenStream::update() { u.thisChunk = shared_ptr(chunk); int ind = 0; - for (glm::vec3 dir : VecUtils::getCardinalVectors()) { + for (glm::vec3 dir : Vec::cardinalVectors) { std::shared_ptr adjacent = dimension.getChunk(pos + dir); u.adjacentChunks[ind++] = shared_ptr(adjacent); if (adjacent == nullptr) goto breakAddTask; diff --git a/src/game/scene/world/MeshGenStream.h b/src/game/scene/world/MeshGenStream.h index 8151611e..d58385e3 100644 --- a/src/game/scene/world/MeshGenStream.h +++ b/src/game/scene/world/MeshGenStream.h @@ -66,6 +66,6 @@ private: std::array noiseSampler; std::vector queuedTasks; - std::unordered_set queuedMap; + std::unordered_set queuedMap; }; diff --git a/src/game/scene/world/Player.cpp b/src/game/scene/world/Player.cpp index abc46923..7e734c79 100644 --- a/src/game/scene/world/Player.cpp +++ b/src/game/scene/world/Player.cpp @@ -115,9 +115,9 @@ void Player::findPointedThing(InputManager &input) { for (Ray ray(this); ray.getLength() < LOOK_DISTANCE; ray.step(LOOK_PRECISION)) { glm::vec3 rayEnd = ray.getEnd(); - glm::vec3 roundedPos = TransPos::roundPos(rayEnd); + glm::vec3 roundedPos = glm::floor(rayEnd); - auto currChunkPos = TransPos::chunkFromVec(roundedPos); + auto currChunkPos = Space::Chunk::world::fromBlock(roundedPos); if (currChunkPos != chunkPos || blockChunk == nullptr) { chunkPos = currChunkPos; blockChunk = world.getChunk(chunkPos); @@ -125,7 +125,7 @@ void Player::findPointedThing(InputManager &input) { unsigned int blockID = 0; if (blockChunk != nullptr) { - blockID = blockChunk->getBlock(TransPos::chunkLocalFromVec(roundedPos)); + blockID = blockChunk->getBlock(Space::Block::relative::toChunk(roundedPos)); } auto& boxes = defs.defs().blockFromId(blockID).sBoxes; diff --git a/src/game/scene/world/graph/MeshChunk.cpp b/src/game/scene/world/graph/MeshChunk.cpp index 1b8a302b..2f5c3891 100644 --- a/src/game/scene/world/graph/MeshChunk.cpp +++ b/src/game/scene/world/graph/MeshChunk.cpp @@ -11,7 +11,7 @@ void MeshChunk::create(std::vector &vertices, std::vector(TransPos::CHUNK_SIZE)); + model = glm::translate(model, pos * static_cast(16)); renderer.setModelMatrix(model); mesh->draw(); } diff --git a/src/game/scene/world/graph/MeshChunk.h b/src/game/scene/world/graph/MeshChunk.h index b47090d0..20164a4f 100644 --- a/src/game/scene/world/graph/MeshChunk.h +++ b/src/game/scene/world/graph/MeshChunk.h @@ -10,7 +10,7 @@ #include #include "ChunkRenderElem.h" #include "../../../entity/Entity.h" -#include "../../../../util/TransPos.h" +#include "../../../../util/Space.h" class MeshChunk : public ChunkRenderElem, Drawable { public: diff --git a/src/game/scene/world/graph/MeshGenerator.cpp b/src/game/scene/world/graph/MeshGenerator.cpp index dccc564d..66068983 100644 --- a/src/game/scene/world/graph/MeshGenerator.cpp +++ b/src/game/scene/world/graph/MeshGenerator.cpp @@ -28,7 +28,7 @@ MeshGenerator::MeshGenerator(MeshDetails* meshDetails, LocalDefs& defs, std::sha glm::vec3 biomeTint = defs.gen().biomeFromId(chunk->getBiome(i)).biomeTint; if (model.visible) { - VecUtils::indAssignVec(i, off); + Vec::indAssignVec(i, off); vis = off; @@ -75,16 +75,16 @@ MeshGenerator::MeshGenerator(MeshDetails* meshDetails, LocalDefs& defs, std::sha } BlockDef& MeshGenerator::getBlockAt(const glm::vec3 &pos) { - if (pos.x < 0 || pos.x >= TransPos::CHUNK_SIZE || pos.y < 0 || pos.y >= TransPos::CHUNK_SIZE || pos.z < 0 || pos.z >= TransPos::CHUNK_SIZE) { + if (pos.x < 0 || pos.x >= 16 || pos.y < 0 || pos.y >= 16 || pos.z < 0 || pos.z >= 16) { - if (pos.x == TransPos::CHUNK_SIZE) return atlas.blockFromId(adjacent[0]->getBlock(pos - glm::vec3(TransPos::CHUNK_SIZE, 0, 0))); - if (pos.x == -1) return atlas.blockFromId(adjacent[1]->getBlock(pos + glm::vec3(TransPos::CHUNK_SIZE, 0, 0))); + if (pos.x == 16) return atlas.blockFromId(adjacent[0]->getBlock(pos - glm::vec3(16, 0, 0))); + if (pos.x == -1) return atlas.blockFromId(adjacent[1]->getBlock(pos + glm::vec3(16, 0, 0))); - if (pos.y == TransPos::CHUNK_SIZE) return atlas.blockFromId(adjacent[2]->getBlock(pos - glm::vec3(0, TransPos::CHUNK_SIZE, 0))); - if (pos.y == -1) return atlas.blockFromId(adjacent[3]->getBlock(pos + glm::vec3(0, TransPos::CHUNK_SIZE, 0))); + if (pos.y == 16) return atlas.blockFromId(adjacent[2]->getBlock(pos - glm::vec3(0, 16, 0))); + if (pos.y == -1) return atlas.blockFromId(adjacent[3]->getBlock(pos + glm::vec3(0, 16, 0))); - if (pos.z == TransPos::CHUNK_SIZE) return atlas.blockFromId(adjacent[4]->getBlock(pos - glm::vec3(0, 0, TransPos::CHUNK_SIZE))); - if (pos.z == -1) return atlas.blockFromId(adjacent[5]->getBlock(pos + glm::vec3(0, 0, TransPos::CHUNK_SIZE))); + if (pos.z == 16) return atlas.blockFromId(adjacent[4]->getBlock(pos - glm::vec3(0, 0, 16))); + if (pos.z == -1) return atlas.blockFromId(adjacent[5]->getBlock(pos + glm::vec3(0, 0, 16))); } return atlas.blockFromId(chunk->getBlock(pos)); diff --git a/src/lua/api/type/LuaLocalPlayer.cpp b/src/lua/api/type/LuaLocalPlayer.cpp index 70876db6..16606e3a 100644 --- a/src/lua/api/type/LuaLocalPlayer.cpp +++ b/src/lua/api/type/LuaLocalPlayer.cpp @@ -14,7 +14,7 @@ sol::table LuaLocalPlayer::get_pos(sol::this_state s) { } sol::table LuaLocalPlayer::get_block_pos(sol::this_state s) { - glm::vec3 pos = TransPos::roundPos(player.getPos()); + glm::vec3 pos = glm::floor(player.getPos()); return sol::state_view(s).create_table_with("x", pos.x, "y", pos.y, "z", pos.z); } diff --git a/src/server/conn/ServerPlayer.cpp b/src/server/conn/ServerPlayer.cpp index 7e2df991..4a606034 100644 --- a/src/server/conn/ServerPlayer.cpp +++ b/src/server/conn/ServerPlayer.cpp @@ -10,14 +10,14 @@ ServerPlayer::ServerPlayer(glm::vec3 pos, unsigned int connectID, const std::str connectID(connectID), username(username) { - mapBlock = TransPos::mapBlockFromVec(pos); + mapBlock = Space::MapBlock::world::fromBlock(pos); lastMapBlock = mapBlock - glm::vec3(200); //Force resending of all chunks around getPlayer. changedMapBlocks = true; } void ServerPlayer::setPos(glm::vec3 pos) { this->pos = pos; - glm::vec3 newMapBlock = TransPos::mapBlockFromVec(pos); + glm::vec3 newMapBlock = Space::MapBlock::world::fromBlock(pos); if (newMapBlock != mapBlock) { if (!changedMapBlocks) { @@ -41,5 +41,5 @@ float ServerPlayer::getAngle() { } glm::vec3 ServerPlayer::getChunkPos() { - return TransPos::chunkFromVec(pos); + return Space::Chunk::world::fromBlock(pos); } \ No newline at end of file diff --git a/src/server/conn/ServerPlayer.h b/src/server/conn/ServerPlayer.h index 2bb04693..07cd1da4 100644 --- a/src/server/conn/ServerPlayer.h +++ b/src/server/conn/ServerPlayer.h @@ -5,7 +5,7 @@ #pragma once #include -#include "../../util/TransPos.h" +#include "../../util/Space.h" class ServerPlayer { public: diff --git a/src/server/world/ServerWorld.cpp b/src/server/world/ServerWorld.cpp index 3ff26412..a6a60afd 100644 --- a/src/server/world/ServerWorld.cpp +++ b/src/server/world/ServerWorld.cpp @@ -9,8 +9,8 @@ #include "../../util/Timer.h" #include "../../util/Util.h" -const static int MB_GEN_H = 2; -const static int MB_GEN_V = 2; +const static int MB_GEN_H = 3; +const static int MB_GEN_V = 3; ServerWorld::ServerWorld(unsigned int seed, ServerDefs& defs, ServerClients& clients) : seed(seed), @@ -97,7 +97,7 @@ void ServerWorld::update() { {mapBlock.x - MB_GEN_H, mapBlock.y - MB_GEN_V, mapBlock.z - MB_GEN_H}, {mapBlock.x + MB_GEN_H, mapBlock.y + MB_GEN_V, mapBlock.z + MB_GEN_H}}; - if (isInBounds(TransPos::mapBlockFromVec(chunk->pos * 16), bounds)) { + if (isInBounds(Space::MapBlock::world::fromChunk(chunk->pos), bounds)) { sendChunk(chunk->pos, *client); } } @@ -118,6 +118,7 @@ void ServerWorld::update() { void ServerWorld::sendChunk(glm::vec3 pos, ServerClient &peer) { auto chunk = dimension.getChunk(pos); + assert(chunk != nullptr); auto serialized = chunk->serialize(); Packet r(PacketType::CHUNK); @@ -150,7 +151,7 @@ void ServerWorld::setBlock(glm::vec3 pos, unsigned int block) { Serializer::encodeIntVec3(b.data, pos); Serializer::encodeInt(b.data, block); - auto chunkPos = TransPos::chunkFromVec(TransPos::roundPos(pos)); + auto chunkPos = Space::Chunk::world::fromBlock(pos); for (auto &client : clientList.clients) { if (client->hasPlayer()) { @@ -160,7 +161,7 @@ void ServerWorld::setBlock(glm::vec3 pos, unsigned int block) { {mapBlock.x - MB_GEN_H, mapBlock.y - MB_GEN_V, mapBlock.z - MB_GEN_H}, {mapBlock.x + MB_GEN_H, mapBlock.y + MB_GEN_V, mapBlock.z + MB_GEN_H}}; - if (isInBounds(TransPos::mapBlockFromVec(chunkPos * 16), bounds)) { + if (isInBounds(Space::MapBlock::world::fromChunk(chunkPos), bounds)) { b.sendTo(client->getPeer(), PacketChannel::BLOCK); } } diff --git a/src/server/world/ServerWorld.h b/src/server/world/ServerWorld.h index f084a2f6..7aeccbc0 100644 --- a/src/server/world/ServerWorld.h +++ b/src/server/world/ServerWorld.h @@ -36,7 +36,7 @@ private: WorldGenStream* genStream = nullptr; ServerDimension dimension; - std::unordered_set generateQueueMap; + std::unordered_set generateQueueMap; std::vector generateQueueList; unsigned int seed; diff --git a/src/server/world/WorldGenStream.h b/src/server/world/WorldGenStream.h index a810f5be..52e128c8 100644 --- a/src/server/world/WorldGenStream.h +++ b/src/server/world/WorldGenStream.h @@ -56,7 +56,7 @@ private: MapGen gen; std::vector queuedTasks; - std::unordered_set queuedMap; + std::unordered_set queuedMap; }; #pragma clang diagnostic pop \ No newline at end of file diff --git a/src/util/Pointer.h b/src/util/Pointer.h index 13d946dc..2ae37776 100644 --- a/src/util/Pointer.h +++ b/src/util/Pointer.h @@ -8,4 +8,3 @@ #define sptr std::shared_ptr #define uptr std::unique_ptr -#define wptr std::weak_ptr diff --git a/src/util/Space.h b/src/util/Space.h new file mode 100644 index 00000000..0f3faea8 --- /dev/null +++ b/src/util/Space.h @@ -0,0 +1,140 @@ +// +// Created by aurailus on 24/03/19. +// + +#pragma once + + +#include +#include +#include + +namespace Space { + const static int CHUNK_SIZE = 16; + const static int MAPBLOCK_SIZE = 4; + const static int REGION_SIZE = 4; + + const static int CHUNK_BLOCK_LENGTH = CHUNK_SIZE; + const static int MAPBLOCK_BLOCK_LENGTH = CHUNK_BLOCK_LENGTH * MAPBLOCK_SIZE; + const static int REGION_BLOCK_LENGTH = MAPBLOCK_BLOCK_LENGTH * REGION_SIZE; + + const static int MAPBLOCK_CHUNK_LENGTH = MAPBLOCK_SIZE; + const static int REGION_CHUNK_LENGTH = MAPBLOCK_CHUNK_LENGTH * REGION_SIZE; + + // Private helper methods + namespace { + inline glm::vec3 localFromGlobal(const glm::vec3 &pos, int size) { + auto round = glm::floor(pos); + return glm::vec3 { + (round.x < 0) ? size - 1 + static_cast(round.x + 1) % size : static_cast(round.x) % size, + (round.y < 0) ? size - 1 + static_cast(round.y + 1) % size : static_cast(round.y) % size, + (round.z < 0) ? size - 1 + static_cast(round.z + 1) % size : static_cast(round.z) % size + }; + } + + inline glm::vec3 sectionFromGlobal(glm::vec3 pos, int size) { + auto round = glm::floor(pos); + return {std::floor(round.x / size), std::floor(round.y / size), std::floor(round.z / size)}; + } + } + + namespace Region { + namespace world { + // Get a Region world position from a MapBlock's world position. + static inline glm::vec3 fromMapBlock(const glm::vec3& mapBlock) { + return sectionFromGlobal(mapBlock, REGION_SIZE); + } + + // Get a Region world position from a Chunk's world position. + static inline glm::vec3 fromChunk(const glm::vec3 &chunk) { + return sectionFromGlobal(chunk, REGION_CHUNK_LENGTH); + } + + // Get a Region world position from a Block's world position. + static inline glm::vec3 fromBlock(const glm::vec3 &chunk) { + return sectionFromGlobal(chunk, REGION_BLOCK_LENGTH); + } + } + } + + namespace MapBlock { + namespace relative { + // Get a MapBlock's relative position in its Region from its world position. + static inline glm::vec3 toRegion(const glm::vec3& pos) { + return localFromGlobal(pos, MAPBLOCK_SIZE); + } + } + + namespace world { + // Get a MapBlock world position from a Chunk's world position. + static inline glm::vec3 fromChunk(const glm::vec3 &chunk) { + return sectionFromGlobal(chunk, MAPBLOCK_SIZE); + } + + // Get a MapBlock world position from a Block's world position. + static inline glm::vec3 fromBlock(const glm::vec3 &vec) { + return sectionFromGlobal(vec, MAPBLOCK_BLOCK_LENGTH); + } + } + + // Get the index of a MapBlock within its Region from its local or world position. + static inline unsigned int index(const glm::vec3& vec) { + glm::vec3 local = MapBlock::relative::toRegion(vec); + unsigned int ind = static_cast(local.x + REGION_SIZE * (local.y + REGION_SIZE * local.z)); + return ind; + } + } + + namespace Chunk { + namespace relative { + // Get a Chunk's relative position in its MapBlock from its world position. + static inline glm::vec3 toMapBlock(const glm::vec3& pos) { + return localFromGlobal(pos, MAPBLOCK_CHUNK_LENGTH); + } + + // Get a Chunk's relative position in its Region from its world position. + static inline glm::vec3 toRegion(const glm::vec3& pos) { + return localFromGlobal(pos, REGION_CHUNK_LENGTH); + } + } + + namespace world { + // Get a Chunk world position from a Block's world position. + static inline glm::vec3 fromBlock(const glm::vec3& pos) { + return sectionFromGlobal(pos, CHUNK_BLOCK_LENGTH); + } + } + + // Get the index of a Chunk within its MapBlock from its local or world position. + static inline unsigned int index(const glm::vec3& vec) { + glm::vec3 local = Chunk::relative::toMapBlock(vec); + return static_cast(local.x + MAPBLOCK_SIZE * (local.y + MAPBLOCK_SIZE * local.z)); + } + } + + namespace Block { + namespace relative { + // Get a Block's relative position to its Chunk from its world position. + static inline glm::vec3 toChunk(const glm::vec3& pos) { + return localFromGlobal(pos, CHUNK_BLOCK_LENGTH); + } + + // Get a Block's relative position to its MapBlock from its world position. + static inline glm::vec3 toMapBlock(const glm::vec3& pos) { + return localFromGlobal(pos, MAPBLOCK_BLOCK_LENGTH); + } + + // Get a Block's relative position in its Region from its world position. + static inline glm::vec3 toRegion(const glm::vec3& pos) { + return localFromGlobal(pos, REGION_BLOCK_LENGTH); + } + } + + // Get the index of a Block within its Chunk from its local or world position. + static inline unsigned int index(const glm::vec3& vec) { + glm::vec3 local = Block::relative::toChunk(vec); + return static_cast(local.x + CHUNK_SIZE * (local.y + CHUNK_SIZE * local.z)); + } + } +} + diff --git a/src/util/TransPos.h b/src/util/TransPos.h deleted file mode 100644 index 4a5ef7ec..00000000 --- a/src/util/TransPos.h +++ /dev/null @@ -1,105 +0,0 @@ -// -// Created by aurailus on 24/03/19. -// - -#pragma once - - -#include -#include - -namespace TransPos { - const static int CHUNK_SIZE = 16; //Must be a multiple of 16 - const static int MAPBLOCK_SIZE = 4; - const static int REGION_SIZE = 4; - - const static int CHUNK_BLOCK_LENGTH = CHUNK_SIZE; - const static int MAPBLOCK_BLOCK_LENGTH = CHUNK_BLOCK_LENGTH * MAPBLOCK_SIZE; - const static int REGION_BLOCK_LENGTH = MAPBLOCK_BLOCK_LENGTH * REGION_SIZE; - - const static int MAPBLOCK_CHUNK_LENGTH = MAPBLOCK_SIZE; - const static int REGION_CHUNK_LENGTH = MAPBLOCK_CHUNK_LENGTH * REGION_SIZE; - - static inline glm::vec3 roundPos(glm::vec3 vec) { - return { floor(vec.x), floor(vec.y), floor(vec.z) }; - } - - namespace { - inline glm::vec3 localFromGlobal(glm::vec3 &pos, int size) { - auto round = roundPos(pos); - glm::vec3 out; - out.x = (round.x < 0) ? ((size - 1) + (((int) round.x + 1) % size)) : (((int) round.x) % size); - out.y = (round.y < 0) ? ((size - 1) + (((int) round.y + 1) % size)) : (((int) round.y) % size); - out.z = (round.z < 0) ? ((size - 1) + (((int) round.z + 1) % size)) : (((int) round.z) % size); - return out; - } - - inline glm::vec3 sectionFromGlobal(glm::vec3 pos, int size) { - auto round = roundPos(pos); - return {std::floor(round.x / size), std::floor(round.y / size), std::floor(round.z / size)}; - } - } - - - - //Return a local position vector relative to the respective structure - - static inline glm::vec3 regionLocalFromVec(glm::vec3 vec) { - return localFromGlobal(vec, REGION_BLOCK_LENGTH); - } - - static inline glm::vec3 mapBlockLocalFromVec(glm::vec3 vec) { - return localFromGlobal(vec, MAPBLOCK_BLOCK_LENGTH); - } - - static inline glm::vec3 chunkLocalFromVec(glm::vec3 vec) { - return localFromGlobal(vec, CHUNK_BLOCK_LENGTH); - } - - //Returns the Chunk Position from a pos vector - - static inline glm::vec3 chunkFromVec(glm::vec3 vec) { - return sectionFromGlobal(vec, CHUNK_BLOCK_LENGTH); - } - - static inline glm::vec3 mapBlockFromVec(glm::vec3 vec) { - return sectionFromGlobal(vec, MAPBLOCK_BLOCK_LENGTH); - } - - namespace Dimension { - //Used to get the chunk position from a global coordinate in LocalDimension - - //Retrieve the region pos from a block pos - static inline glm::vec3 regionFromVec(glm::vec3 vec) { - return sectionFromGlobal(vec, REGION_CHUNK_LENGTH); - } - - //Retrieve the raw (chunk pos of bottom left corner) pos of a region - static inline glm::vec3 regionRawFromRegionVec(glm::vec3 vec) { - return vec * glm::vec3(REGION_CHUNK_LENGTH); - } - - //Retrieve the offset of a mapblock from it's region using a chunk position - static inline glm::vec3 mapBlockOffsetFromRegion(glm::vec3 vec) { - return sectionFromGlobal(localFromGlobal(vec, REGION_CHUNK_LENGTH), MAPBLOCK_CHUNK_LENGTH); - } - - //Retrieve the offset of a chunk from it's mapblock using a chunk position - static inline glm::vec3 chunkOffsetFromMapBlock(glm::vec3 vec) { - return localFromGlobal(vec, MAPBLOCK_CHUNK_LENGTH); - } - } - - - - // Used to get indices from various vectors - - static inline unsigned int mapBlockIndFromVec(glm::vec3 vec) { - return (unsigned int)(vec.x + REGION_SIZE * (vec.y + REGION_SIZE * vec.z)); - } - - static inline unsigned int chunkIndFromVec(glm::vec3 vec) { - return (unsigned int)(vec.x + MAPBLOCK_SIZE * (vec.y + MAPBLOCK_SIZE * vec.z)); - } -}; - diff --git a/src/util/Util.h b/src/util/Util.h index 6af01918..d93b347e 100644 --- a/src/util/Util.h +++ b/src/util/Util.h @@ -64,7 +64,6 @@ namespace Util { return x; } - static glm::vec4 hexToColorVec(std::string hex) { glm::vec4 color {}; diff --git a/src/util/Vec.h b/src/util/Vec.h index 4b140fbf..4bf9c6dd 100644 --- a/src/util/Vec.h +++ b/src/util/Vec.h @@ -8,44 +8,34 @@ #include #include #include -#include "TransPos.h" +#include "Space.h" -namespace VecUtils { +namespace Vec { struct compareFunc { size_t operator()(const glm::vec3& k)const { return std::hash()(k.x) ^ std::hash()(k.y) ^ std::hash()(k.z); } }; - static inline std::array getCardinalVectors() { - return { - glm::vec3(1, 0, 0), glm::vec3(-1, 0, 0), - glm::vec3(0, 1, 0), glm::vec3(0, -1, 0), - glm::vec3(0, 0, 1), glm::vec3(0, 0, -1), - }; + const static std::array cardinalVectors = { + glm::vec3 {1, 0, 0}, glm::vec3 {-1, 0, 0}, + glm::vec3 {0, 1, 0}, glm::vec3 {0, -1, 0}, + glm::vec3 {0, 0, 1}, glm::vec3 {0, 0, -1} }; static inline void indAssignVec(int ind, glm::vec3& vec) { - vec.z = ind / (TransPos::CHUNK_SIZE * TransPos::CHUNK_SIZE); - ind -= ((int)vec.z * TransPos::CHUNK_SIZE * TransPos::CHUNK_SIZE); - vec.y = ind / TransPos::CHUNK_SIZE; - vec.x = ind % TransPos::CHUNK_SIZE; + vec.z = ind / (16 * 16); + ind -= ((int)vec.z * 16 * 16); + vec.y = ind / 16; + vec.x = ind % 16; } static inline glm::vec3* indToVec(int ind) { - int z = ind / (TransPos::CHUNK_SIZE * TransPos::CHUNK_SIZE); - ind -= (z * TransPos::CHUNK_SIZE * TransPos::CHUNK_SIZE); - int y = ind / TransPos::CHUNK_SIZE; - int x = ind % TransPos::CHUNK_SIZE; + int z = ind / (16 * 16); + ind -= (z * 16 * 16); + int y = ind / 16; + int x = ind % 16; return new glm::vec3(x, y, z); } - - static inline unsigned int vecToInd(int x, int y, int z) { - return (unsigned int)(x + TransPos::CHUNK_SIZE * (y + TransPos::CHUNK_SIZE * z)); - } - - static inline unsigned int vecToInd(const glm::vec3& vec) { - return (unsigned int)(vec.x + TransPos::CHUNK_SIZE * (vec.y + TransPos::CHUNK_SIZE * vec.z)); - } };