LocalWorld optimization

master
aurailus 2019-04-18 20:11:34 -07:00
parent 7ac199127d
commit d83e0d5f7e
21 changed files with 90 additions and 105 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 86 KiB

After

Width:  |  Height:  |  Size: 4.6 KiB

View File

@ -47,11 +47,11 @@ void l_register_block::api(std::string identifier, sol::table data) {
}
BlockModel* model = BlockModel::from_lua_def(*modelOptional, *texTable, *game->textures, visible, culls);
BlockModel* model = BlockModel::from_lua_def(*modelOptional, *texTable, game->defs.textures(), visible, culls);
BlockDef* def = new BlockDef(identifier, model, solid, selectionBox);
game->blocks->registerBlock(def);
game->defs.blocks().registerBlock(def);
}
l_register_block::l_register_block(GameScene *game, LuaParser *parser) : LuaApi(game, parser) {

View File

@ -5,16 +5,17 @@
#include "GameDefs.h"
GameDefs::GameDefs(std::string tex_path) {
textures = TextureAtlas(512);
textures.loadFromDirectory(std::move(tex_path));
textureAtlas = TextureAtlas(512);
textureAtlas.loadFromDirectory(std::move(tex_path));
// textureAtlas.loadFromDirectory("../res/tex/gui");
blocks = BlockAtlas(&textures);
blockAtlas = BlockAtlas(&textureAtlas);
}
BlockAtlas &GameDefs::getBlockAtlas() {
return blocks;
BlockAtlas &GameDefs::blocks() {
return blockAtlas;
}
TextureAtlas &GameDefs::getTextureAtlas() {
return textures;
TextureAtlas &GameDefs::textures() {
return textureAtlas;
}

View File

@ -13,13 +13,13 @@ public:
GameDefs() = default;
explicit GameDefs(std::string tex_path);
BlockAtlas& getBlockAtlas();
TextureAtlas& getTextureAtlas();
BlockAtlas& blocks();
TextureAtlas& textures();
~GameDefs() = default;
private:
TextureAtlas textures;
BlockAtlas blocks;
TextureAtlas textureAtlas;
BlockAtlas blockAtlas;
};

View File

@ -58,7 +58,8 @@ TextureAtlas::TextureAtlas(unsigned int width, unsigned int height) {
data[i * 4 + 3] = 0;
}
t.loadFromBytes(data, width, height);
t = new Texture();
t->loadFromBytes(data, width, height);
createMissingTexture();
@ -140,7 +141,7 @@ std::shared_ptr<AtlasRef> TextureAtlas::addTexture(unsigned char* data, std::str
ref->tileX = (int)space.x;
ref->tileY = (int)space.y;
t.updateTexture((int)space.x * 16, (int)space.y * 16, pixelWidth, pixelHeight, data);
t->updateTexture((int)space.x * 16, (int)space.y * 16, pixelWidth, pixelHeight, data);
ref->uv = {(space.x * 16) / width, (space.y * 16) / height,
(space.x * 16 + pixelWidth) / width, (space.y * 16 + pixelHeight) / height};
@ -150,7 +151,7 @@ std::shared_ptr<AtlasRef> TextureAtlas::addTexture(unsigned char* data, std::str
}
Texture &TextureAtlas::getTexture() {
return t;
return *t;
}
glm::vec4 TextureAtlas::getTextureUVs(std::string& name) {

View File

@ -32,7 +32,7 @@ private:
glm::vec2 findSpace(int w, int h);
unsigned int width = 0, height = 0;
Texture t;
Texture* t = nullptr;
std::vector<bool> empty;
std::map<std::string, std::shared_ptr<AtlasRef>> textures;

View File

@ -5,7 +5,7 @@
#include "DebugGui.h"
#include "../../../util/TransPos.h"
DebugGui::DebugGui(glm::vec2 bufferSize) {
DebugGui::DebugGui(glm::vec2 bufferSize, Texture* tex) {
displayMode = 0;
fontTexture = new Texture((char*)"../res/tex/gui/font.png");
@ -13,7 +13,7 @@ DebugGui::DebugGui(glm::vec2 bufferSize) {
whiteHistTexture= new Texture((char*)"../res/tex/gui/histogram_white.png");
transWhiteHistTexture= new Texture((char*)"../res/tex/gui/histogram_white_transparent.png");
texAtlas = new RectEntity(glm::vec4(0, 0, 0, 1));
texAtlas = new RectEntity(tex);
texAtlas->setScale({512, 512, 1});
texAtlas->setPos({8, 300, 0});
@ -350,8 +350,4 @@ void DebugGui::changeVisibilityState(int state) {
fpsText->setVisible(displayMode != 1);
fpsHist->setVisible(displayMode != 1);
fpsBack->setVisible(displayMode != 1);
}
void DebugGui::setAtlasTexture(Texture* tex) {
texAtlas->setTexture(tex);
}
}

View File

@ -15,9 +15,7 @@
class DebugGui : public DrawableGroup {
public:
explicit DebugGui(glm::vec2 bufferSize);
void setAtlasTexture(Texture* tex);
explicit DebugGui(glm::vec2 bufferSize, Texture* tex);
void bufferResized(glm::vec2 bufferSize);

View File

@ -4,8 +4,8 @@
#include "BlockModelEntity.h"
BlockModelEntity::BlockModelEntity(TextureAtlas* texAtlas) {
this->texAtlas = texAtlas;
BlockModelEntity::BlockModelEntity(GameDefs& defs) :
defs(defs) {
auto m = new Mesh();
setMesh(m);
}
@ -41,7 +41,7 @@ void BlockModelEntity::addFaces(unsigned int &indOffset, vector<float> &vertices
n += 0.005f;
if (n > 7) n = 0;
auto uv = texAtlas->getTextureUVs(tex);
auto uv = defs.textures().getTextureUVs(tex);
for (MeshPart *mp : meshParts) {

View File

@ -8,10 +8,11 @@
#include "../../../game/entity/Entity.h"
#include "../../../def/block/graph/BlockModel.h"
#include "../../../def/GameDefs.h"
class BlockModelEntity : public Entity {
public:
explicit BlockModelEntity(TextureAtlas *texAtlas);
explicit BlockModelEntity(GameDefs &defs);
void setModel(BlockModel& model);
void clearModel();
@ -20,7 +21,7 @@ private:
float n = 0;
TextureAtlas *texAtlas;
GameDefs& defs;
};

View File

@ -6,59 +6,47 @@
#include "../../api/func/l_register_block.h"
#include "../../api/func/l_register_blockmodel.h"
#include "../entity/world/WireframeEntity.h"
#include "../entity/world/BlockModelEntity.h"
GameScene::GameScene(ClientState* state) :
Scene(state),
GameScene::GameScene(ClientState* state) : Scene(state),
defs("../res/tex/game"),
world(defs),
server("127.0.0.1", 12345),
gameGui(state->renderer->getCamera()->getBufferDimensions()),
debugGui(state->renderer->getCamera()->getBufferDimensions()) {
textures = new TextureAtlas(512);
textures->loadFromDirectory("../res/tex/game");
blocks = new BlockAtlas(textures);
debugGui.setAtlasTexture(&textures->getTexture());
debugGui(state->renderer->getCamera()->getBufferDimensions(), &defs.textures().getTexture()) {
LuaParser p;
p.init();
//TODO: Remove
//Register APIs here
l_register_block(this, &p);
l_register_blockmodel(this, &p);
p.doFile("../res/lua/file.lua");
//The scene requires the blockAtlas for meshing and handling inputs.
//TODO: make this use contentmgr
world = new LocalWorld(blocks);
//TODO: make this use contentmgr
auto blockBreak = new BlockModelEntity(textures);
auto blockBreak = new BlockModelEntity(defs);
entities.push_back(blockBreak);
//Wireframe
auto wireframe = new WireframeEntity({0, 0, 0}, {1, 1, 1}, 0.01);
entities.push_back(wireframe);
player = new Player();
player->create(world, state->renderer->getCamera(), wireframe, blockBreak);
server = new ServerConnection("127.0.0.1", 12345, entities);
server->init();
player.create(&world, &defs, state->renderer->getCamera(), wireframe, blockBreak);
gui.push_back(&gameGui);
gui.push_back(&debugGui);
server.init(entities);
}
void GameScene::update() {
server->update(*player);
server.update(player);
auto window = state->renderer->getWindow();
player->update(window->input, state->deltaTime, window->getDeltaX(), window->getDeltaY());
player.update(window->input, state->deltaTime, window->getDeltaX(), window->getDeltaY());
if (state->renderer->resized) {
debugGui.bufferResized(state->renderer->getCamera()->getBufferDimensions());
@ -67,16 +55,16 @@ void GameScene::update() {
state->renderer->resized = false;
}
while (!server->chunkPackets.empty()) {
auto it = server->chunkPackets.begin();
while (!server.chunkPackets.empty()) {
auto it = server.chunkPackets.begin();
Packet* p = *it;
server->chunkPackets.erase(it);
world->loadChunkPacket(p);
server.chunkPackets.erase(it);
world.loadChunkPacket(p);
}
//TODO: Make this use contentmgr
debugGui.update(player, world, blocks, state->fps, (int)world->getMeshChunks()->size(), drawCalls, server->serverSideChunkGens, server->recvPackets);
world->update();
debugGui.update(&player, &world, &defs.blocks(), state->fps, (int)world.getMeshChunks()->size(), drawCalls, server.serverSideChunkGens, server.recvPackets);
world.update();
if (window->input.isKeyPressed(GLFW_KEY_F1)) {
hudVisible = !hudVisible;
@ -98,8 +86,8 @@ void GameScene::draw() {
renderer.begin();
renderer.enableTexture(&textures->getTexture());
drawCalls = world->render(renderer);
renderer.enableTexture(&defs.textures().getTexture());
drawCalls = world.render(renderer);
for (auto entity : entities) {
entity->draw(renderer);

View File

@ -15,6 +15,9 @@
#include "world/Player.h"
#include "net/ServerConnection.h"
#include "../entity/world/PlayerEntity.h"
#include "../entity/world/WireframeEntity.h"
#include "../entity/world/BlockModelEntity.h"
#include "../../def/GameDefs.h"
class GameScene : public Scene {
public:
@ -25,12 +28,11 @@ public:
void cleanup() override;
public:
ServerConnection* server;
Player* player;
LocalWorld* world;
GameDefs defs;
BlockAtlas* blocks;
TextureAtlas* textures;
ServerConnection server;
Player player;
LocalWorld world;
std::vector<Drawable*> entities;
std::vector<Drawable*> gui;
@ -38,8 +40,6 @@ public:
DebugGui debugGui;
GameGui gameGui;
int c = 0;
int drawCalls;
bool debugVisible = true;

View File

@ -5,15 +5,16 @@
#include "ServerConnection.h"
ServerConnection::ServerConnection(std::string address, unsigned short port, std::vector<Drawable *> &entities) {
ServerConnection::ServerConnection(std::string address, unsigned short port) {
this->port = port;
this->address = std::move(address);
this->entities = new DrawableGroup();
entities.push_back(this->entities);
}
void ServerConnection::init() {
void ServerConnection::init(std::vector<Drawable*> &entities) {
entities.push_back(this->entities);
handler = NetHandler(address, port, 3, 3000);
if (handler.getState() != NetHandler::CLIENT) {

View File

@ -18,9 +18,9 @@
class ServerConnection {
public:
ServerConnection(std::string address, unsigned short port, std::vector<Drawable*> &entities);
ServerConnection(std::string address, unsigned short port);
void init();
void init(std::vector<Drawable*> &entities);
void update(Player &player);
void cleanup();

View File

@ -8,19 +8,17 @@
#include "LocalWorld.h"
#include "../../../util/Vec.h"
LocalWorld::LocalWorld(BlockAtlas *atlas) :
meshGenStream(*atlas, dimension),
worldGenStream(55) {
blockAtlas = atlas;
}
LocalWorld::LocalWorld(GameDefs& defs) :
meshGenStream(defs, dimension),
worldGenStream(55),
defs(defs) {}
void LocalWorld::loadChunkPacket(Packet *p) {
worldGenStream.pushBack(p);
}
void LocalWorld::commitChunk(glm::vec3 pos, std::shared_ptr<BlockChunk> c) {
dimension.addChunk(pos, c);
dimension.addChunk(pos, std::move(c));
attemptMeshChunk(pos);
}
@ -100,7 +98,7 @@ void LocalWorld::update() {
auto finishedChunks = worldGenStream.update();
lastGenUpdates = 0;
for (auto chunk : finishedChunks) {
for (const auto &chunk : finishedChunks) {
commitChunk(chunk->pos, chunk);
lastGenUpdates++;
}
@ -132,7 +130,7 @@ void LocalWorld::setBlock(glm::vec3 pos, int block) {
bool LocalWorld::solidAt(glm::vec3 pos) {
int blockId = getBlock(pos);
if (blockId == -1) return true;
return blockAtlas->getBlock(blockId)->isSolid();
return defs.blocks().getBlock(blockId)->isSolid();
}
std::shared_ptr<BlockChunk> LocalWorld::getChunk(glm::vec3 chunkPos) {
@ -143,9 +141,9 @@ std::unordered_map<glm::vec3, MeshChunk*, VecUtils::compareFunc>* LocalWorld::ge
return &meshChunks;
}
BlockAtlas *LocalWorld::getBlockAtlas() {
return blockAtlas;
}
//BlockAtlas *LocalWorld::blocks() {
// return blockAtlas;
//}
int LocalWorld::render(Renderer &renderer) {
int count = 0;

View File

@ -22,10 +22,11 @@
#include "MeshGenStream.h"
#include "../../../util/Vec.h"
#include "../../../world/Dimension.h"
#include "../../../def/GameDefs.h"
class LocalWorld {
public:
explicit LocalWorld(BlockAtlas* atlas);
explicit LocalWorld(GameDefs& defs);
void update();
@ -46,11 +47,11 @@ public:
bool solidAt(glm::vec3 pos);
BlockAtlas* getBlockAtlas();
// BlockAtlas* blocks();
int lastGenUpdates = 0, lastMeshUpdates = 0;
private:
BlockAtlas* blockAtlas;
GameDefs& defs;
WorldInterpolationStream worldGenStream;
Dimension dimension;

View File

@ -7,15 +7,15 @@
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
MeshGenStream::MeshGenStream(BlockAtlas &a, Dimension &d) :
atlas(a),
dimension(d) {
MeshGenStream::MeshGenStream(GameDefs &defs, Dimension &dimension) :
defs(defs),
dimension(dimension) {
queuedTasks.reserve((unsigned long) TOTAL_QUEUE_SIZE);
threads.reserve(THREADS);
for (int i = 0; i < THREADS; i++) {
threads.emplace_back(atlas);
threads.emplace_back(defs.blocks());
}
}
@ -72,7 +72,6 @@ MeshGenStream::Thread::Thread(BlockAtlas &atlas) :
atlas(atlas) {
thread = new std::thread(MeshGenStream::threadFunction, this);
thread->detach();
}
void MeshGenStream::threadFunction(MeshGenStream::Thread *thread) {
@ -107,7 +106,6 @@ MeshGenStream::~MeshGenStream() {
for (auto& t : threads) {
t.keepAlive = false;
t.thread->join();
delete t.thread;
}
}
@ -127,7 +125,7 @@ std::vector<bool>* MeshGenStream::getAdjacentsCull(glm::vec3 pos) {
int z = (i == 4) ? 0 : (i == 5) ? 15 : k;
auto block = chunk->getBlock(x, y, z);
culls->push_back(atlas.getBlock(block)->isCulling());
culls->push_back(defs.blocks().getBlock(block)->isCulling());
}
}
}

View File

@ -14,6 +14,7 @@
#include "graph/MeshGenerator.h"
#include "../../../util/Vec.h"
#include "../../../world/Dimension.h"
#include "../../../def/GameDefs.h"
class MeshGenStream {
public:
@ -21,7 +22,7 @@ public:
static const int THREADS = 4;
static const int TOTAL_QUEUE_SIZE = THREADS * THREAD_QUEUE_SIZE;
explicit MeshGenStream(BlockAtlas& a, Dimension& d);
explicit MeshGenStream(GameDefs& defs, Dimension& dimension);
~MeshGenStream();
bool spaceInQueue();
@ -73,7 +74,7 @@ private:
static void threadFunction(Thread* thread);
Dimension& dimension;
BlockAtlas& atlas;
GameDefs& defs;
std::vector<glm::vec3> queuedTasks;
std::unordered_set<glm::vec3, VecUtils::compareFunc> queuedMap;

View File

@ -14,11 +14,12 @@ Player::Player() {
pointingAtBlock = false;
}
void Player::create(LocalWorld *world, Camera *camera, WireframeEntity *wireframe, BlockModelEntity *blockBreak) {
void Player::create(LocalWorld *world, GameDefs *defs, Camera *camera, WireframeEntity *wireframe, BlockModelEntity *blockBreak) {
this->camera = camera;
this->world = world;
this->wireframe = wireframe;
this->blockBreak = blockBreak;
this->defs = defs;
}
void Player::update(InputManager &input, double delta, double mouseX, double mouseY) {
@ -35,7 +36,7 @@ void Player::update(InputManager &input, double delta, double mouseX, double mou
auto at = world->getBlock(rayEnd);
if (at > 0) {
auto def = world->getBlockAtlas()->getBlock(at);
auto def = defs->blocks().getBlock(at);
auto sBox = def->getSelectionBox();
if (rayEnd.x >= pointedAt.x + sBox.a.x && rayEnd.y >= pointedAt.y + sBox.a.y && rayEnd.z >= pointedAt.z + sBox.a.z &&

View File

@ -21,7 +21,7 @@ public:
Player();
void create(LocalWorld* world, Camera* camera, WireframeEntity* wireframe, BlockModelEntity* blockBreak);
void create(LocalWorld* world, GameDefs* defs, Camera* camera, WireframeEntity* wireframe, BlockModelEntity* blockBreak);
void update(InputManager &input, double delta, double mouseX, double mouseY);
@ -52,6 +52,7 @@ private:
glm::vec3 vel;
Camera* camera;
GameDefs* defs;
LocalWorld* world;
WireframeEntity *wireframe;

View File

@ -56,7 +56,6 @@ WorldInterpolationStream::Thread::Thread(MapGen *gen) {
this->gen = gen;
thread = std::thread(WorldInterpolationStream::threadFunction, this);
thread.detach();
}
void WorldInterpolationStream::threadFunction(WorldInterpolationStream::Thread *thread) {