Made LocalDimension delete distant chunks. Still a mem leak somewhere.

* Cleaned up some imports
* Reorganized some files
master
Nicole Collings 2020-02-20 11:23:12 -08:00
parent 9c48fb694d
commit a73f5d3030
37 changed files with 251 additions and 272 deletions

View File

@ -65,7 +65,7 @@ set(ZEPHA_SRC
server/conn/ClientList.h
server/conn/ServerClient.h
util/net/PacketChannel.h
game/entity/world/PlayerEntity.h
game/entity/engine/PlayerEntity.h
game/hud/components/basic/GUIRect.cpp
game/hud/components/basic/GUIRect.h
game/hud/components/basic/GUIGraph.cpp
@ -93,16 +93,16 @@ set(ZEPHA_SRC
world/LocalDimension.cpp
world/LocalDimension.h
world/region/Region.h
game/entity/world/WireframeEntity.cpp
game/entity/world/WireframeEntity.h
game/entity/engine/WireframeEntity.cpp
game/entity/engine/WireframeEntity.h
def/item/SelectionBox.h
game/graph/drawable/Drawable.h
game/graph/drawable/DrawableGroup.cpp
game/graph/drawable/DrawableGroup.h
game/graph/window/Input.cpp
game/graph/window/Input.h
game/entity/world/BlockCrackEntity.cpp
game/entity/world/BlockCrackEntity.h
game/entity/engine/BlockCrackEntity.cpp
game/entity/engine/BlockCrackEntity.h
def/texture/TextureAtlas.cpp
def/texture/TextureAtlas.h
def/texture/AtlasRef.h
@ -115,8 +115,8 @@ set(ZEPHA_SRC
world/block/PointedThing.h
game/hud/components/compound/GUILabelledGraph.cpp
game/hud/components/compound/GUILabelledGraph.cpp
game/entity/world/ParticleEntity.cpp
game/entity/world/ParticleEntity.h
game/entity/engine/ParticleEntity.cpp
game/entity/engine/ParticleEntity.h
def/ServerGame.cpp
def/ServerGame.h
def/ServerDefinitionAtlas.cpp

View File

@ -2,6 +2,8 @@
// Created by aurailus on 17/12/18.
//
#include <glm/gtc/type_ptr.hpp>
#include "Renderer.h"
Renderer::Renderer() : Renderer({1366, 768}) {};
@ -10,18 +12,18 @@ Renderer::Renderer(glm::ivec2 win) :
activeTexture(nullptr),
window(win),
world (win, 2),
entity (win, 2),
ssao (win, 1, 24),
blur (win, 1),
lighting(win, 2) {
world (win, 2),
entity(win, 2),
ssao (win, 1, 24),
blur (win, 1),
light (win, 2) {
window.initialize();
camera.create(window.getSize().x, window.getSize().y, glm::vec3(0, 1, 0));
ssao.createFromFile("./assets/shader/post/passThrough.vs", "./assets/shader/post/ssaoCalc.fs");
blur.createFromFile("./assets/shader/post/passThrough.vs", "./assets/shader/post/ssaoBlur.fs");
lighting.createFromFile("./assets/shader/post/passThrough.vs", "./assets/shader/post/deferredLighting.fs");
light.createFromFile("./assets/shader/post/passThrough.vs", "./assets/shader/post/deferredLighting.fs");
world.createFromFile("./assets/shader/world/deferredGeometryWorld.vs", "./assets/shader/world/deferredGeometryWorld.fs");
entity.createFromFile("./assets/shader/world/deferredGeometryEntity.vs", "./assets/shader/world/deferredGeometryEntity.fs");
@ -38,7 +40,7 @@ Renderer::Renderer(glm::ivec2 win) :
window.addResizeCallback("renderer", [&](glm::ivec2 win) {
ssao.windowResized(win);
blur.windowResized(win);
lighting.windowResized(win);
light.windowResized(win);
world.windowResized(win);
camera.changeWindowDimensions(win);
@ -47,23 +49,6 @@ Renderer::Renderer(glm::ivec2 win) :
});
}
//Initialize Shading Shader for Shadowmapping
// const unsigned int SHADOW_SCALE = 2048;
// glGenFramebuffers(1, &sBuffer);
// glGenTextures(1, &sDepthMap);
// glBindTexture(GL_TEXTURE_2D, sDepthMap);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_SCALE, SHADOW_SCALE, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// glBindFramebuffer(GL_FRAMEBUFFER, sBuffer);
// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, sDepthMap, 0);
// glDrawBuffer(GL_NONE);
// glReadBuffer(GL_NONE);
// glBindBuffer(GL_FRAMEBUFFER, 0);
void Renderer::update(double delta) {
//VSync 1 = On, 0 = Off
glfwSwapInterval(1);
@ -87,7 +72,7 @@ void Renderer::beginChunkDeferredCalls() {
glViewport(0, 0, static_cast<int>(world.windowSize.x * world.bufferScale), static_cast<int>(world.windowSize.y * world.bufferScale));
glBindFramebuffer(GL_FRAMEBUFFER, lighting.gBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, light.gBuffer);
glClear(GL_DEPTH_BUFFER_BIT);
const float skyColor[] = {clearColor.x, clearColor.y, clearColor.z, 1};
@ -129,9 +114,9 @@ void Renderer::endDeferredCalls() {
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, lighting.gPosition);
glBindTexture(GL_TEXTURE_2D, light.gPosition);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, lighting.gNormal);
glBindTexture(GL_TEXTURE_2D, light.gNormal);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, ssao.tex);
@ -152,30 +137,18 @@ void Renderer::endDeferredCalls() {
glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
setShader(lighting);
lighting.set(lighting.uniforms.camPosition, camera.getPos());
setShader(light);
light.set(light.uniforms.camPosition, camera.getPos());
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, blur.colorBuffer);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, lighting.gNormal);
glBindTexture(GL_TEXTURE_2D, light.gNormal);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, lighting.gColorSpec);
glBindTexture(GL_TEXTURE_2D, light.gColorSpec);
glEnable(GL_BLEND);
renderQuad();
//Used to push the depth map to the default framebuffer
//Enable when creating forward rendering stage
// glBindFramebuffer(GL_READ_FRAMEBUFFER, gBuffer);
// glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
//
// auto winSize = window.getSize();
// glBlitFramebuffer(0, 0, static_cast<int>(winSize.x), static_cast<int>(winSize.y),
// 0, 0, static_cast<int>(winSize.x), static_cast<int>(winSize.y),
// GL_DEPTH_BUFFER_BIT, GL_NEAREST);
// glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Renderer::beginGUIDrawCalls() {

View File

@ -4,11 +4,10 @@
#pragma once
#include <glm/gtx/transform.hpp>
#include <noise/noise.h>
#include "Camera.h"
#include "Texture.h"
#include "window/Window.h"
// Shaders
#include "shader/Shader.h"
#include "shader/SSAOShader.h"
#include "shader/BlurShader.h"
@ -16,7 +15,6 @@
#include "shader/WorldGeometryShader.h"
#include "shader/EntityGeometryShader.h"
#include "shader/GuiUniforms.h"
#include "meshtypes/ChunkMesh.h"
class Renderer {
public:
@ -32,10 +30,10 @@ public:
void swapBuffers();
void setShader(Shader& s);
void setClearColor(unsigned char r, unsigned char g, unsigned char b);
void toggleDepthTest(bool enable);
void clearDepthBuffer();
static void toggleDepthTest(bool enable);
static void clearDepthBuffer();
void setModelMatrix(const glm::mat4& modelMatrix);
void setBones(std::vector<glm::mat4>& transforms);
@ -57,7 +55,7 @@ private:
EntityGeometryShader entity;
SSAOShader ssao;
BlurShader blur;
LightingShader lighting;
LightingShader light;
Shader guiShader;
GuiUniforms gu;

View File

@ -4,6 +4,8 @@
#pragma once
#include <glm/vec2.hpp>
#include "Shader.h"
class BlurShader : public Shader {

View File

@ -4,6 +4,8 @@
#pragma once
#include <glm/vec2.hpp>
#include "Shader.h"
class EntityGeometryShader : public Shader {

View File

@ -4,6 +4,8 @@
#include "LightingShader.h"
#include "../../../util/Log.h"
LightingShader::LightingShader(glm::ivec2 windowSize, float bufferScale) : Shader(),
windowSize(windowSize),
bufferScale(bufferScale) {}
@ -57,7 +59,7 @@ void LightingShader::postCreate() {
glBindRenderbuffer(GL_RENDERBUFFER, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
std::cout << Log::err << "-- Lighting framebuffer incomplete! --" << std::endl;
std::cout << Log::err << "Lighting framebuffer incomplete!" << std::endl;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);

View File

@ -4,6 +4,8 @@
#pragma once
#include <glm/vec2.hpp>
#include "Shader.h"
class LightingShader : public Shader {

View File

@ -2,6 +2,8 @@
// Created by aurailus on 24/09/19.
//
#include <glm/glm.hpp>
#include "SSAOShader.h"
SSAOShader::SSAOShader(glm::ivec2 windowSize, float bufferScale, unsigned int kernelCount) : Shader(),

View File

@ -4,8 +4,10 @@
#pragma once
#include "Shader.h"
#include <random>
#include <glm/vec2.hpp>
#include "Shader.h"
class SSAOShader : public Shader {
public:

View File

@ -2,11 +2,13 @@
// Created by aurailus on 26/11/18.
//
#include <fstream>
#include <iostream>
#include <glm/gtc/type_ptr.hpp>
#include "Shader.h"
Shader::Shader() {
shaderID = 0;
}
#include "../../../util/Log.h"
void Shader::createFromString(std::string& vertexSource, std::string& fragmentSource) {
compileShader(vertexSource, fragmentSource);
@ -29,8 +31,8 @@ std::string Shader::readFile(const std::string& fileLocation) {
std::ifstream fileStream(fileLocation, std::ios::in);
if (!fileStream.is_open()) {
std::cout << Log::err << "-- Failed to open shader file '" << fileLocation << "' --" << Log::endl;
return "";
std::cout << Log::err << "Failed to open shader file '" << fileLocation << "'." << Log::endl;
exit(1);
}
std::string line;
@ -89,8 +91,8 @@ void Shader::compileShader(const std::string& vertexSource, const std::string& f
shaderID = glCreateProgram();
if (!shaderID) {
std::cout << Log::err << "-- Error creating shader program --" << Log::endl;
return;
std::cout << Log::err << "Error creating shader program." << Log::endl;
exit(1);
}
addShader(shaderID, vertexSource, GL_VERTEX_SHADER);
@ -104,8 +106,8 @@ void Shader::compileShader(const std::string& vertexSource, const std::string& f
if (!result) {
glGetProgramInfoLog(shaderID, sizeof(eLog), nullptr, eLog);
std::cout << Log::err << "-- Error linking program --\n" << eLog << Log::endl;
return;
std::cout << Log::err << "Error linking program.\n" << eLog << Log::endl;
exit(1);
}
glValidateProgram(shaderID);
@ -113,8 +115,8 @@ void Shader::compileShader(const std::string& vertexSource, const std::string& f
if (!result) {
glGetProgramInfoLog(shaderID, sizeof(eLog), nullptr, eLog);
std::cout << Log::err << "-- Error validating program --\n" << eLog << Log::endl;
return;
std::cout << Log::err << "Error validating program.\n" << eLog << Log::endl;
exit(1);
}
}

View File

@ -4,26 +4,19 @@
#pragma once
#include <GL/glew.h>
#include <string>
#include <cstring>
#include <fstream>
#include <iostream>
#include <GL/glew.h>
#include <glm/vec3.hpp>
#include <glm/mat4x4.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "../../../util/Log.h"
class Shader {
public:
Shader();
void createFromString(std::string& vertexSource, std::string& fragmentSource);
void createFromFile(const std::string& vertexFile, const std::string& fragmentFile);
virtual void postCreate() {};
GLint get(const std::string &name);
int get(const std::string &name);
void use();
static void clearShader();
@ -44,12 +37,12 @@ private:
std::string readFile(const std::string& fileLocation);
void compileShader(const std::string& vertexSource, const std::string& fragmentSource);
void addShader(GLuint program, const std::string& shaderCode, GLenum shaderType);
void addShader(uint program, const std::string& shaderCode, GLenum shaderType);
void crashIfInactive();
GLuint shaderID;
std::string vertexFile = "string";
std::string fragmentFile = "string";
unsigned int shaderID = 0;
std::string vertexFile {};
std::string fragmentFile {};
};

View File

@ -2,6 +2,8 @@
// Created by aurailus on 25/09/19.
//
#include <glm/vec2.hpp>
#include "WorldGeometryShader.h"
WorldGeometryShader::WorldGeometryShader(glm::ivec2 windowSize, float bufferScale) : Shader(),

View File

@ -4,18 +4,10 @@
#pragma once
#include "net/ClientNetworkInterpreter.h"
#include "world/LocalWorld.h"
#include "world/Player.h"
#include "../graph/scene/Scene.h"
#include "../graph/Renderer.h"
#include "../ClientState.h"
#include "../hud/DebugGui.h"
#include "../hud/GameGui.h"
#include "../entity/world/PlayerEntity.h"
#include "../entity/world/WireframeEntity.h"
#include "../entity/world/BlockCrackEntity.h"
#include "../entity/Model.h"
#include "../../def/ClientGame.h"
#include "../graph/scene/Scene.h"
class GameScene : public Scene {
public:

View File

@ -14,7 +14,7 @@
//
//#include "../../../util/Timer.h"
//#include "../../../util/net/Packet.h"
//#include "../../entity/world/PlayerEntity.h"
//#include "../../entity/engine/PlayerEntity.h"
//#include "../../graph/drawable/DrawableGroup.h"
//#include "../../../util/net/Address.h"

View File

@ -2,11 +2,12 @@
// Created by aurailus on 14/12/18.
//
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
#include "LocalWorld.h"
#include "WorldInterpolationStream.h"
#include "../net/ClientNetworkInterpreter.h"
#include "../../entity/engine/BlockCrackEntity.h"
#include "../../entity/engine/ParticleEntity.h"
LocalWorld::LocalWorld(ClientGame& defs, glm::vec3* playerPos, ClientNetworkInterpreter* server) :
dimension(defs),
@ -36,6 +37,40 @@ void LocalWorld::update(double delta) {
if (end != particles.begin()) particles.erase(particles.begin(), end + 1);
}
void LocalWorld::loadChunkPacket(std::unique_ptr<Packet> p) {
worldGenStream->pushBack(std::move(p));
}
void LocalWorld::commitChunk(std::shared_ptr<BlockChunk> c) {
dimension.setChunk(std::move(c));
}
unsigned int LocalWorld::getBlock(glm::ivec3 pos) {
return dimension.getBlock(pos);
}
void LocalWorld::setBlock(glm::ivec3 pos, unsigned int block) {
dimension.setBlock(pos, block);
}
void LocalWorld::localSetBlock(glm::ivec3 pos, unsigned int block) {
if (block == LocalDefinitionAtlas::AIR) {
auto def = defs.defs.blockFromId(getBlock(pos));
if (def.callbacks.count(Callback::BREAK_CLIENT)) {
def.callbacks[Callback::BREAK_CLIENT](defs.parser.vecToTable(pos));
}
}
else {
auto def = defs.defs.blockFromId(block);
if (def.callbacks.count(Callback::PLACE_CLIENT)) {
def.callbacks[Callback::PLACE_CLIENT](defs.parser.vecToTable(pos));
}
}
server->setBlock(pos, block);
dimension.setBlock(pos, block);
}
void LocalWorld::damageBlock(glm::vec3 pos, float amount) {
BlockCrackEntity* block = nullptr;
for (auto test : crackedBlocks) {
@ -60,14 +95,32 @@ void LocalWorld::damageBlock(glm::vec3 pos, float amount) {
}
}
void LocalWorld::finishChunks() {
auto finishedChunks = worldGenStream->update();
unsigned short LocalWorld::getBiome(glm::vec3 pos) {
auto chunkPos = Space::Chunk::world::fromBlock(pos);
auto local = Space::Block::relative::toChunk(pos);
lastGenUpdates = 0;
for (const auto &chunk : finishedChunks) {
commitChunk(chunk);
lastGenUpdates++;
}
auto chunk = getChunk(chunkPos);
if (chunk != nullptr) return chunk->getBiome(local);
return BiomeAtlas::INVALID;
}
std::shared_ptr<BlockChunk> LocalWorld::getChunk(glm::ivec3 pos) {
return dimension.getChunk(pos);
}
int LocalWorld::getMeshChunkCount() {
return dimension.getMeshChunkCount();
}
int LocalWorld::renderChunks(Renderer &renderer) {
return dimension.renderChunks(renderer);
}
void LocalWorld::renderEntities(Renderer &renderer) {
for (auto block : crackedBlocks) block->draw(renderer);
for (auto &p : particles) p->draw(renderer);
dimension.renderEntities(renderer);
}
void LocalWorld::updateBlockDamages(double delta) {
@ -105,64 +158,12 @@ void LocalWorld::updateBlockDamages(double delta) {
}
}
void LocalWorld::loadChunkPacket(std::unique_ptr<Packet> p) {
worldGenStream->pushBack(std::move(p));
}
void LocalWorld::finishChunks() {
auto finishedChunks = worldGenStream->update();
void LocalWorld::commitChunk(std::shared_ptr<BlockChunk> c) {
dimension.setChunk(std::move(c));
}
int LocalWorld::renderChunks(Renderer &renderer) {
return dimension.renderChunks(renderer);
}
void LocalWorld::renderEntities(Renderer &renderer) {
for (auto block : crackedBlocks) block->draw(renderer);
for (auto &p : particles) p->draw(renderer);
dimension.renderEntities(renderer);
}
int LocalWorld::getMeshChunkCount() {
return dimension.getMeshChunkCount();
}
unsigned short LocalWorld::getBiome(glm::vec3 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);
return BiomeAtlas::INVALID;
}
unsigned int LocalWorld::getBlock(glm::ivec3 pos) {
return dimension.getBlock(pos);
}
void LocalWorld::setBlock(glm::ivec3 pos, unsigned int block) {
dimension.setBlock(pos, block);
}
void LocalWorld::localSetBlock(glm::ivec3 pos, unsigned int block) {
if (block == LocalDefinitionAtlas::AIR) {
auto def = defs.defs.blockFromId(getBlock(pos));
if (def.callbacks.count(Callback::BREAK_CLIENT)) {
def.callbacks[Callback::BREAK_CLIENT](defs.parser.vecToTable(pos));
}
lastGenUpdates = 0;
for (const auto &chunk : finishedChunks) {
commitChunk(chunk);
lastGenUpdates++;
}
else {
auto def = defs.defs.blockFromId(block);
if (def.callbacks.count(Callback::PLACE_CLIENT)) {
def.callbacks[Callback::PLACE_CLIENT](defs.parser.vecToTable(pos));
}
}
server->setBlock(pos, block);
dimension.setBlock(pos, block);
}
std::shared_ptr<BlockChunk> LocalWorld::getChunk(glm::ivec3 pos) {
return dimension.getChunk(pos);
}

View File

@ -4,33 +4,16 @@
#pragma once
#include <iostream>
#include <unordered_set>
#include <unordered_map>
#include <thread>
#include <mutex>
#include <glm/vec3.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "World.h"
#include "../../../def/LocalDefinitionAtlas.h"
#include "../../../world/chunk/BlockChunk.h"
#include "../../../def/gen/MapGen.h"
#include "../../../util/Space.h"
#include "../../../util/Vec.h"
#include "../../../world/LocalDimension.h"
#include "../../../def/ClientGame.h"
#include "../../../world/block/PointedThing.h"
#include "../../../game/graph/drawable/DrawableGroup.h"
#include "../../entity/world/BlockCrackEntity.h"
#include "../../entity/world/ParticleEntity.h"
#include "../../../lua/api/type/LocalLuaEntity.h"
#include "graph/MeshChunk.h"
#include "WorldInterpolationStream.h"
#include "MeshGenStream.h"
class ClientNetworkInterpreter;
class WorldInterpolationStream;
class BlockCrackEntity;
class ParticleEntity;
class Renderer;
class LocalWorld : public World {
public:

View File

@ -4,6 +4,8 @@
#include "Player.h"
#include "../../../util/Ray.h"
Player::Player(LocalWorld& world, ClientGame& defs, Renderer& renderer) :
Collidable(world, defs, {{-0.3, 0, -0.3}, {0.3, 1.8, 0.3}}),

View File

@ -4,20 +4,13 @@
#pragma once
#include <iostream>
#include "LocalWorld.h"
#include "Inventory.h"
#include "../../hud/GameGui.h"
#include "../../graph/Camera.h"
#include "../../graph/drawable/DrawableGroup.h"
#include "../../entity/Collidable.h"
#include "../../entity/world/WireframeEntity.h"
#include "../../entity/world/BlockCrackEntity.h"
#include "../../../util/Ray.h"
#include "../../../util/Timer.h"
#include "../../hud/GameGuiBuilder.h"
#include "../../graph/drawable/Drawable.h"
#include "../../../world/block/PointedThing.h"
#include "../../entity/engine/WireframeEntity.h"
class Player : Collidable, public Drawable {
public:

View File

@ -2,17 +2,14 @@
// Created by aurailus on 15/12/18.
//
#pragma clang diagnostic push
#pragma ide diagnostic ignored "HidingNonVirtualFunction"
#pragma once
#include <glm/vec3.hpp>
#include <list>
#include "ChunkRenderElem.h"
#include "../../../entity/Entity.h"
#include "../../../../util/Space.h"
#include <memory>
#include "ChunkRenderElem.h"
#include "../../../graph/drawable/Drawable.h"
#include "../../../graph/meshtypes/ChunkVertex.h"
#include "../../../graph/meshtypes/ChunkMesh.h"
class MeshChunk : public ChunkRenderElem, Drawable {
public:
MeshChunk() = default;
@ -26,7 +23,4 @@ public:
private:
std::unique_ptr<ChunkMesh> mesh = nullptr;
glm::vec3 pos {};
};
#pragma clang diagnostic pop
};

View File

@ -13,8 +13,8 @@
class ServerWorld : public World {
public:
const static int MB_GEN_H = 3;
const static int MB_GEN_V = 3;
const static int MB_GEN_H = 6;
const static int MB_GEN_V = 4;
explicit ServerWorld(unsigned int seed, ServerGame& defs, ClientList& clients);

View File

@ -41,17 +41,17 @@ namespace Space {
namespace Region {
namespace world {
// Get a Region world position from a MapBlock's world position.
// Get a Region engine position from a MapBlock's engine position.
static inline glm::ivec3 fromMapBlock(const glm::ivec3& mapBlock) {
return sectionFromGlobal(mapBlock, REGION_SIZE);
}
// Get a Region world position from a Chunk's world position.
// Get a Region engine position from a Chunk's engine position.
static inline glm::ivec3 fromChunk(const glm::ivec3 &chunk) {
return sectionFromGlobal(chunk, REGION_CHUNK_LENGTH);
}
// Get a Region world position from a Block's world position.
// Get a Region engine position from a Block's engine position.
static inline glm::ivec3 fromBlock(const glm::ivec3 &chunk) {
return sectionFromGlobal(chunk, REGION_BLOCK_LENGTH);
}
@ -60,25 +60,25 @@ namespace Space {
namespace MapBlock {
namespace relative {
// Get a MapBlock's relative position in its Region from its world position.
// Get a MapBlock's relative position in its Region from its engine position.
static inline glm::ivec3 toRegion(const glm::ivec3& pos) {
return localFromGlobal(pos, MAPBLOCK_SIZE);
}
}
namespace world {
// Get a MapBlock world position from a Chunk's world position.
// Get a MapBlock engine position from a Chunk's engine position.
static inline glm::ivec3 fromChunk(const glm::ivec3 &chunk) {
return sectionFromGlobal(chunk, MAPBLOCK_SIZE);
}
// Get a MapBlock world position from a Block's world position.
// Get a MapBlock engine position from a Block's engine position.
static inline glm::ivec3 fromBlock(const glm::ivec3 &vec) {
return sectionFromGlobal(vec, MAPBLOCK_BLOCK_LENGTH);
}
}
// Get the index of a MapBlock within its Region from its local or world position.
// Get the index of a MapBlock within its Region from its local or engine position.
static inline unsigned int index(const glm::ivec3& vec) {
glm::ivec3 local = MapBlock::relative::toRegion(vec);
unsigned int ind = static_cast<unsigned int>(local.x + REGION_SIZE * (local.y + REGION_SIZE * local.z));
@ -88,25 +88,25 @@ namespace Space {
namespace Chunk {
namespace relative {
// Get a Chunk's relative position in its MapBlock from its world position.
// Get a Chunk's relative position in its MapBlock from its engine position.
static inline glm::ivec3 toMapBlock(const glm::ivec3& pos) {
return localFromGlobal(pos, MAPBLOCK_CHUNK_LENGTH);
}
// Get a Chunk's relative position in its Region from its world position.
// Get a Chunk's relative position in its Region from its engine position.
static inline glm::ivec3 toRegion(const glm::ivec3& pos) {
return localFromGlobal(pos, REGION_CHUNK_LENGTH);
}
}
namespace world {
// Get a Chunk world position from a Block's world position.
// Get a Chunk engine position from a Block's engine position.
static inline glm::ivec3 fromBlock(const glm::ivec3& pos) {
return sectionFromGlobal(pos, CHUNK_BLOCK_LENGTH);
}
}
// Get the index of a Chunk within its MapBlock from its local or world position.
// Get the index of a Chunk within its MapBlock from its local or engine position.
static inline unsigned int index(const glm::ivec3& vec) {
glm::ivec3 local = Chunk::relative::toMapBlock(vec);
return static_cast<unsigned int>(local.x + MAPBLOCK_SIZE * (local.y + MAPBLOCK_SIZE * local.z));
@ -115,23 +115,23 @@ namespace Space {
namespace Block {
namespace relative {
// Get a Block's relative position to its Chunk from its world position.
// Get a Block's relative position to its Chunk from its engine position.
static inline glm::ivec3 toChunk(const glm::ivec3& pos) {
return localFromGlobal(pos, CHUNK_BLOCK_LENGTH);
}
// Get a Block's relative position to its MapBlock from its world position.
// Get a Block's relative position to its MapBlock from its engine position.
static inline glm::ivec3 toMapBlock(const glm::ivec3& pos) {
return localFromGlobal(pos, MAPBLOCK_BLOCK_LENGTH);
}
// Get a Block's relative position in its Region from its world position.
// Get a Block's relative position in its Region from its engine position.
static inline glm::ivec3 toRegion(const glm::ivec3& pos) {
return localFromGlobal(pos, REGION_BLOCK_LENGTH);
}
}
// Get the index of a Block within its Chunk from its local or world position.
// Get the index of a Block within its Chunk from its local or engine position.
static inline unsigned int index(const glm::ivec3& vec) {
glm::ivec3 local = Block::relative::toChunk(vec);
return static_cast<unsigned int>(local.x + CHUNK_SIZE * (local.y + CHUNK_SIZE * local.z));

View File

@ -20,7 +20,7 @@ std::shared_ptr<MapBlock> Dimension::getMapBlock(glm::ivec3 mapBlockPosition) {
}
void Dimension::removeMapBlock(glm::ivec3 pos) {
auto region = getMapBlock(Space::Region::world::fromMapBlock(pos));
auto region = getRegion(Space::Region::world::fromMapBlock(pos));
if (region == nullptr) return;
auto ind = Space::MapBlock::index(pos);
region->remove(ind);
@ -61,7 +61,7 @@ bool Dimension::setBlock(glm::ivec3 pos, unsigned int block) {
}
std::shared_ptr<Region> Dimension::getOrCreateRegion(glm::ivec3 pos) {
if (regions[pos]) return regions[pos];
if (regions[pos] != nullptr) return regions[pos];
regions[pos] = std::make_shared<Region>(pos);
return regions[pos];
}
@ -70,8 +70,7 @@ std::shared_ptr<MapBlock> Dimension::getOrCreateMapBlock(glm::ivec3 mapBlockPosi
auto region = getOrCreateRegion(Space::Region::world::fromMapBlock(mapBlockPosition));
unsigned int index = Space::MapBlock::index(mapBlockPosition);
auto mapBlock = (*region)[index];
if (mapBlock) return mapBlock;
if ((*region)[index] != nullptr) return (*region)[index];
(*region).set(index, std::make_shared<MapBlock>(mapBlockPosition));
return (*region)[index];
}

View File

@ -17,7 +17,7 @@ public:
void removeRegion(glm::ivec3 pos);
std::shared_ptr<MapBlock> getMapBlock(glm::ivec3 mapBlockPosition);
void removeMapBlock(glm::ivec3 pos);
virtual void removeMapBlock(glm::ivec3 pos);
std::shared_ptr<BlockChunk> getChunk(glm::ivec3 chunkPosition);
virtual void setChunk(std::shared_ptr<BlockChunk> chunk);

View File

@ -4,30 +4,67 @@
#include "LocalDimension.h"
#include "../game/scene/world/graph/MeshChunk.h"
LocalDimension::LocalDimension(ClientGame &defs) : defs(defs), meshGenStream(std::make_unique<MeshGenStream>(defs, *this)) {}
void LocalDimension::update(double delta, glm::vec3 playerPos) {
finishMeshes();
queueMeshes();
for (auto& entities : localEntities) entities->entity->update(delta);
for (auto& entities : serverEntities) entities->entity->update(delta);
for (auto& playerEnt : playerEntities) playerEnt.update(delta);
for (auto& entity : localEntities ) entity->entity->update(delta);
for (auto& entity : serverEntities) entity->entity->update(delta);
for (auto& entity : playerEntities) entity.update(delta);
auto chunkPosOfPlayer = Space::Chunk::world::fromBlock(playerPos);
auto clientMapBlock = Space::MapBlock::world::fromBlock(playerPos);
// for (auto it = blockChunks.begin(); it != blockChunks.end();) {
// auto pos = it->first;
for (auto it = regions.cbegin(); it != regions.cend();) {
bool remove = false;
for (unsigned short m = 0; m < 64; m++) {
auto mapBlock = it->second->operator[](m);
if (!mapBlock) continue;
if (abs(clientMapBlock.x - mapBlock->pos.x) > LocalDimension::MB_STORE_H + 1
|| abs(clientMapBlock.y - mapBlock->pos.y) > LocalDimension::MB_STORE_V + 1
|| abs(clientMapBlock.z - mapBlock->pos.z) > LocalDimension::MB_STORE_H + 1) {
for (unsigned short c = 0; c < 64; c++) {
auto chunk = mapBlock->operator[](c);
if (!chunk) continue;
removeMeshChunk(chunk->pos);
}
it->second->remove(m);
if (it->second->count <= 0) {
remove = true;
it = regions.erase(it);
break;
}
}
}
if (!remove) it++;
}
// for (const auto& region : regions) {
// for (unsigned short i = 0; i < 64; i++) {
// auto mapBlock = region.second->operator[](i);
// if (mapBlock == nullptr) continue;
//
// auto diffVec = pos - chunkPosOfPlayer;
// float distance = max(abs(diffVec.x), max(abs(diffVec.y), abs(diffVec.z)));
// if (abs(clientMapBlock.x - mapBlock->pos.x) > LocalDimension::MB_STORE_H + 1
// || abs(clientMapBlock.y - mapBlock->pos.y) > LocalDimension::MB_STORE_V + 1
// || abs(clientMapBlock.z - mapBlock->pos.z) > LocalDimension::MB_STORE_H + 1) {
//
// //TODO: Don't hard code this number
// if (distance >= 24) {
// removeMeshChunk(pos);
// it = blockChunks.erase(it);
// } else {
// it++;
// for (unsigned short j = 0; j < 64; j++) {
// auto chunk = mapBlock->operator[](j);
// if (chunk == nullptr) continue;
// removeMeshChunk(chunk->pos);
// }
//
// removeMapBlock(mapBlock->pos);
// if (region.second->count == 0) break;
// }
// }
// }
}
@ -45,9 +82,8 @@ void LocalDimension::finishMeshes() {
setMeshChunk(meshChunk);
lastMeshUpdates++;
} else {
removeMeshChunk(meshDetails->pos);
}
else removeMeshChunk(meshDetails->pos);
delete meshDetails;
}
@ -201,4 +237,4 @@ bool LocalDimension::getAdjacentExists(glm::vec3 pos, bool updateAdjacents) {
if (chunk == nullptr) return false;
if (updateAdjacents) attemptMeshChunk(chunk, false);
return true;
}
}

View File

@ -4,20 +4,20 @@
#pragma once
#include <unordered_map>
#include <glm/vec3.hpp>
#include "Dimension.h"
#include "region/Region.h"
#include "../util/Vec.h"
#include "../game/scene/world/MeshGenStream.h"
#include "../game/scene/world/graph/MeshChunk.h"
#include "../lua/api/type/LocalLuaEntity.h"
#include "../lua/api/type/ServerLocalLuaEntity.h"
#include "../game/entity/world/PlayerEntity.h"
#include "../game/entity/engine/PlayerEntity.h"
#include "../game/scene/world/MeshGenStream.h"
#include "../game/scene/world/graph/ChunkRenderElem.h"
class MeshChunk;
class LocalDimension : public Dimension {
public:
const static int MB_STORE_H = 6;
const static int MB_STORE_V = 4;
explicit LocalDimension(ClientGame& defs);
void update(double delta, glm::vec3 playerPos);

View File

@ -4,12 +4,11 @@
#include "MapBlock.h"
#include "../../util/Space.h"
MapBlock::MapBlock(glm::ivec3 pos) :
pos(pos) {
for (unsigned short i = 0; i < 64; i++) {
blockChunks[i] = nullptr;
}
for(unsigned short i = 0; i < 64; i++) blockChunks[i] = nullptr;
}
std::shared_ptr<BlockChunk> MapBlock::operator[](unsigned short index) {
@ -17,11 +16,11 @@ std::shared_ptr<BlockChunk> MapBlock::operator[](unsigned short index) {
}
void MapBlock::set(unsigned short index, std::shared_ptr<BlockChunk> chunk) {
if (blockChunks[index] == nullptr) count++;
blockChunks[index] = chunk;
count++;
}
void MapBlock::remove(unsigned short index) {
if (blockChunks[index] != nullptr) count--;
blockChunks[index] = nullptr;
count--;
}

View File

@ -4,10 +4,10 @@
#pragma once
#include <glm/vec3.hpp>
#include <array>
#include <memory>
#include "../../util/Space.h"
#include <glm/vec3.hpp>
#include "../chunk/BlockChunk.h"
class MapBlock {

View File

@ -4,12 +4,11 @@
#include "Region.h"
#include "../../util/Space.h"
Region::Region(glm::ivec3 pos) :
pos(pos) {
for (unsigned short i = 0; i < 64; i++) {
mapBlocks[i] = nullptr;
}
for(unsigned short i = 0; i < 64; i++) mapBlocks[i] = nullptr;
}
std::shared_ptr<MapBlock> Region::operator[](unsigned short index) {
@ -17,11 +16,11 @@ std::shared_ptr<MapBlock> Region::operator[](unsigned short index) {
}
void Region::set(unsigned short index, std::shared_ptr<MapBlock> block) {
if (mapBlocks[index] == nullptr) count++;
mapBlocks[index] = block;
count++;
}
void Region::remove(unsigned short index) {
if (mapBlocks[index] != nullptr) count--;
mapBlocks[index] = nullptr;
count--;
}
}

View File

@ -4,10 +4,11 @@
#pragma once
#include <glm/vec3.hpp>
#include <array>
#include <memory>
#include <glm/vec3.hpp>
#include "MapBlock.h"
#include "../../util/Space.h"
class Region {
public:

View File

@ -9,7 +9,7 @@
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
TEST_CASE("BlockChunk", "[world]") {
TEST_CASE("BlockChunk", "[engine]") {
SECTION("Lighting") {
BlockChunk b;