This commit is contained in:
MihailRis 2024-11-14 09:30:41 +03:00
parent f0b6521c76
commit c4170c07c5
21 changed files with 255 additions and 232 deletions

View File

@ -11,25 +11,25 @@
#include "maths/UVRegion.hpp" #include "maths/UVRegion.hpp"
#include "voxels/Block.hpp" #include "voxels/Block.hpp"
ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) ContentGfxCache::ContentGfxCache(const Content* content, const Assets& assets)
: content(content) { : content(content) {
auto indices = content->getIndices(); auto indices = content->getIndices();
sideregions = std::make_unique<UVRegion[]>(indices->blocks.count() * 6); sideregions = std::make_unique<UVRegion[]>(indices->blocks.count() * 6);
auto atlas = assets->get<Atlas>("blocks"); const auto& atlas = assets.require<Atlas>("blocks");
const auto& blocks = indices->blocks.getIterable(); const auto& blocks = indices->blocks.getIterable();
for (blockid_t i = 0; i < blocks.size(); i++) { for (blockid_t i = 0; i < blocks.size(); i++) {
auto def = blocks[i]; auto def = blocks[i];
for (uint side = 0; side < 6; side++) { for (uint side = 0; side < 6; side++) {
const std::string& tex = def->textureFaces[side]; const std::string& tex = def->textureFaces[side];
if (atlas->has(tex)) { if (atlas.has(tex)) {
sideregions[i * 6 + side] = atlas->get(tex); sideregions[i * 6 + side] = atlas.get(tex);
} else if (atlas->has(TEXTURE_NOTFOUND)) { } else if (atlas.has(TEXTURE_NOTFOUND)) {
sideregions[i * 6 + side] = atlas->get(TEXTURE_NOTFOUND); sideregions[i * 6 + side] = atlas.get(TEXTURE_NOTFOUND);
} }
} }
if (def->model == BlockModel::custom) { if (def->model == BlockModel::custom) {
auto model = assets->require<model::Model>(def->modelName); auto model = assets.require<model::Model>(def->modelName);
// temporary dirty fix tbh // temporary dirty fix tbh
if (def->modelName.find(':') == std::string::npos) { if (def->modelName.find(':') == std::string::npos) {
for (auto& mesh : model.meshes) { for (auto& mesh : model.meshes) {
@ -37,7 +37,7 @@ ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets)
if (pos == std::string::npos) { if (pos == std::string::npos) {
continue; continue;
} }
if (auto region = atlas->getIf(mesh.texture.substr(pos+1))) { if (auto region = atlas.getIf(mesh.texture.substr(pos+1))) {
for (auto& vertex : mesh.vertices) { for (auto& vertex : mesh.vertices) {
vertex.uv = region->apply(vertex.uv); vertex.uv = region->apply(vertex.uv);
} }

View File

@ -22,7 +22,7 @@ class ContentGfxCache {
std::unique_ptr<UVRegion[]> sideregions; std::unique_ptr<UVRegion[]> sideregions;
std::unordered_map<blockid_t, model::Model> models; std::unordered_map<blockid_t, model::Model> models;
public: public:
ContentGfxCache(const Content* content, Assets* assets); ContentGfxCache(const Content* content, const Assets& assets);
~ContentGfxCache(); ~ContentGfxCache();
inline const UVRegion& getRegion(blockid_t id, int side) const { inline const UVRegion& getRegion(blockid_t id, int side) const {

View File

@ -14,25 +14,28 @@
#include "world/Level.hpp" #include "world/Level.hpp"
LevelFrontend::LevelFrontend( LevelFrontend::LevelFrontend(
Player* currentPlayer, LevelController* controller, Assets* assets Player* currentPlayer, LevelController* controller, Assets& assets
) : level(controller->getLevel()), ) : level(*controller->getLevel()),
controller(controller), controller(controller),
assets(assets), assets(assets),
contentCache(std::make_unique<ContentGfxCache>(level->content, assets)) contentCache(std::make_unique<ContentGfxCache>(level.content, assets))
{ {
assets->store( assets.store(
BlocksPreview::build(contentCache.get(), assets, level->content), BlocksPreview::build(
*contentCache, assets, *level.content->getIndices()
),
"block-previews" "block-previews"
); );
controller->getBlocksController()->listenBlockInteraction( controller->getBlocksController()->listenBlockInteraction(
[=](auto player, const auto& pos, const auto& def, BlockInteraction type) { [currentPlayer, controller, &assets](auto player, const auto& pos, const auto& def, BlockInteraction type) {
auto material = level->content->findBlockMaterial(def.material); const auto& level = *controller->getLevel();
auto material = level.content->findBlockMaterial(def.material);
if (material == nullptr) { if (material == nullptr) {
return; return;
} }
if (type == BlockInteraction::step) { if (type == BlockInteraction::step) {
auto sound = assets->get<audio::Sound>(material->stepsSound); auto sound = assets.get<audio::Sound>(material->stepsSound);
glm::vec3 pos {}; glm::vec3 pos {};
auto soundsCamera = currentPlayer->currentCamera.get(); auto soundsCamera = currentPlayer->currentCamera.get();
if (soundsCamera == currentPlayer->spCamera.get() || if (soundsCamera == currentPlayer->spCamera.get() ||
@ -58,10 +61,10 @@ LevelFrontend::LevelFrontend(
audio::Sound* sound = nullptr; audio::Sound* sound = nullptr;
switch (type) { switch (type) {
case BlockInteraction::placing: case BlockInteraction::placing:
sound = assets->get<audio::Sound>(material->placeSound); sound = assets.get<audio::Sound>(material->placeSound);
break; break;
case BlockInteraction::destruction: case BlockInteraction::destruction:
sound = assets->get<audio::Sound>(material->breakSound); sound = assets.get<audio::Sound>(material->breakSound);
break; break;
default: default:
break; break;
@ -83,16 +86,20 @@ LevelFrontend::LevelFrontend(
LevelFrontend::~LevelFrontend() = default; LevelFrontend::~LevelFrontend() = default;
Level* LevelFrontend::getLevel() const { Level& LevelFrontend::getLevel() {
return level; return level;
} }
Assets* LevelFrontend::getAssets() const { const Level& LevelFrontend::getLevel() const {
return level;
}
const Assets& LevelFrontend::getAssets() const {
return assets; return assets;
} }
ContentGfxCache* LevelFrontend::getContentGfxCache() const { const ContentGfxCache& LevelFrontend::getContentGfxCache() const {
return contentCache.get(); return *contentCache;
} }
LevelController* LevelFrontend::getController() const { LevelController* LevelFrontend::getController() const {

View File

@ -9,16 +9,17 @@ class ContentGfxCache;
class LevelController; class LevelController;
class LevelFrontend { class LevelFrontend {
Level* level; Level& level;
LevelController* controller; LevelController* controller;
Assets* assets; const Assets& assets;
std::unique_ptr<ContentGfxCache> contentCache; std::unique_ptr<ContentGfxCache> contentCache;
public: public:
LevelFrontend(Player* currentPlayer, LevelController* controller, Assets* assets); LevelFrontend(Player* currentPlayer, LevelController* controller, Assets& assets);
~LevelFrontend(); ~LevelFrontend();
Level* getLevel() const; Level& getLevel();
Assets* getAssets() const; const Level& getLevel() const;
ContentGfxCache* getContentGfxCache() const; const Assets& getAssets() const;
const ContentGfxCache& getContentGfxCache() const;
LevelController* getController() const; LevelController* getController() const;
}; };

View File

@ -42,8 +42,8 @@ static std::shared_ptr<Label> create_label(wstringsupplier supplier) {
// TODO: move to xml finally // TODO: move to xml finally
std::shared_ptr<UINode> create_debug_panel( std::shared_ptr<UINode> create_debug_panel(
Engine* engine, Engine* engine,
Level* level, Level& level,
Player* player, Player& player,
bool allowDebugCheats bool allowDebugCheats
) { ) {
auto panel = std::make_shared<Panel>(glm::vec2(300, 200), glm::vec4(5.0f), 2.0f); auto panel = std::make_shared<Panel>(glm::vec2(300, 200), glm::vec4(5.0f), 2.0f);
@ -94,57 +94,58 @@ std::shared_ptr<UINode> create_debug_panel(
L" emitters: " + L" emitters: " +
std::to_wstring(ParticlesRenderer::aliveEmitters); std::to_wstring(ParticlesRenderer::aliveEmitters);
})); }));
panel->add(create_label([=]() { panel->add(create_label([&]() {
return L"chunks: "+std::to_wstring(level->chunks->getChunksCount())+ return L"chunks: "+std::to_wstring(level.chunks->getChunksCount())+
L" visible: "+std::to_wstring(ChunksRenderer::visibleChunks); L" visible: "+std::to_wstring(ChunksRenderer::visibleChunks);
})); }));
panel->add(create_label([=]() { panel->add(create_label([&]() {
return L"entities: "+std::to_wstring(level->entities->size())+L" next: "+ return L"entities: "+std::to_wstring(level.entities->size())+L" next: "+
std::to_wstring(level->entities->peekNextID()); std::to_wstring(level.entities->peekNextID());
})); }));
panel->add(create_label([=]() { panel->add(create_label([&]() -> std::wstring {
const auto& vox = player->selection.vox; const auto& vox = player.selection.vox;
std::wstringstream stream; std::wstringstream stream;
stream << "r:" << vox.state.rotation << " s:" stream << "r:" << vox.state.rotation << " s:"
<< std::bitset<3>(vox.state.segment) << " u:" << std::bitset<3>(vox.state.segment) << " u:"
<< std::bitset<8>(vox.state.userbits); << std::bitset<8>(vox.state.userbits);
if (vox.id == BLOCK_VOID) { if (vox.id == BLOCK_VOID) {
return std::wstring {L"block: -"}; return L"block: -";
} else { } else {
return L"block: "+std::to_wstring(vox.id)+ return L"block: "+std::to_wstring(vox.id)+
L" "+stream.str(); L" "+stream.str();
} }
})); }));
panel->add(create_label([=]() -> std::wstring { panel->add(create_label([&]() -> std::wstring {
const auto& vox = player->selection.vox; const auto& selection = player.selection;
const auto& vox = selection.vox;
if (vox.id == BLOCK_VOID) { if (vox.id == BLOCK_VOID) {
return L"x: - y: - z: -"; return L"x: - y: - z: -";
} }
return L"x: " + std::to_wstring(player->selection.actualPosition.x) + return L"x: " + std::to_wstring(selection.actualPosition.x) +
L" y: " + std::to_wstring(player->selection.actualPosition.y) + L" y: " + std::to_wstring(selection.actualPosition.y) +
L" z: " + std::to_wstring(player->selection.actualPosition.z); L" z: " + std::to_wstring(selection.actualPosition.z);
})); }));
panel->add(create_label([=]() { panel->add(create_label([&]() {
auto eid = player->getSelectedEntity(); auto eid = player.getSelectedEntity();
if (eid == ENTITY_NONE) { if (eid == ENTITY_NONE) {
return std::wstring {L"entity: -"}; return std::wstring {L"entity: -"};
} else if (auto entity = level->entities->get(eid)) { } else if (auto entity = level.entities->get(eid)) {
return L"entity: "+util::str2wstr_utf8(entity->getDef().name)+ return L"entity: "+util::str2wstr_utf8(entity->getDef().name)+
L" uid: "+std::to_wstring(entity->getUID()); L" uid: "+std::to_wstring(entity->getUID());
} else { } else {
return std::wstring {L"entity: error (invalid UID)"}; return std::wstring {L"entity: error (invalid UID)"};
} }
})); }));
panel->add(create_label([=](){ panel->add(create_label([&](){
auto* indices = level->content->getIndices(); auto* indices = level.content->getIndices();
if (auto def = indices->blocks.get(player->selection.vox.id)) { if (auto def = indices->blocks.get(player.selection.vox.id)) {
return L"name: " + util::str2wstr_utf8(def->name); return L"name: " + util::str2wstr_utf8(def->name);
} else { } else {
return std::wstring {L"name: void"}; return std::wstring {L"name: void"};
} }
})); }));
panel->add(create_label([=](){ panel->add(create_label([&](){
return L"seed: "+std::to_wstring(level->getWorld()->getSeed()); return L"seed: "+std::to_wstring(level.getWorld()->getSeed());
})); }));
for (int ax = 0; ax < 3; ax++) { for (int ax = 0; ax < 3; ax++) {
@ -161,22 +162,22 @@ std::shared_ptr<UINode> create_debug_panel(
// Coord input // Coord input
auto box = std::make_shared<TextBox>(L""); auto box = std::make_shared<TextBox>(L"");
auto boxRef = box.get(); auto boxRef = box.get();
box->setTextSupplier([=]() { box->setTextSupplier([&player, ax]() {
return util::to_wstring(player->getPosition()[ax], 2); return util::to_wstring(player.getPosition()[ax], 2);
}); });
if (allowDebugCheats) { if (allowDebugCheats) {
box->setTextConsumer([=](const std::wstring& text) { box->setTextConsumer([&player, ax](const std::wstring& text) {
try { try {
glm::vec3 position = player->getPosition(); glm::vec3 position = player.getPosition();
position[ax] = std::stoi(text); position[ax] = std::stoi(text);
player->teleport(position); player.teleport(position);
} catch (std::exception& _){ } catch (std::exception& _){
} }
}); });
} }
box->setOnEditStart([=]() { box->setOnEditStart([&player, boxRef, ax]() {
boxRef->setText( boxRef->setText(
std::to_wstring(static_cast<int>(player->getPosition()[ax])) std::to_wstring(static_cast<int>(player.getPosition()[ax]))
); );
}); });
box->setSize(glm::vec2(230, 27)); box->setSize(glm::vec2(230, 27));
@ -184,7 +185,7 @@ std::shared_ptr<UINode> create_debug_panel(
sub->add(box, glm::vec2(20, 0)); sub->add(box, glm::vec2(20, 0));
panel->add(sub); panel->add(sub);
} }
auto& worldInfo = level->getWorld()->getInfo(); auto& worldInfo = level.getWorld()->getInfo();
panel->add(create_label([&](){ panel->add(create_label([&](){
int hour, minute, second; int hour, minute, second;
timeutil::from_value(worldInfo.daytime, hour, minute, second); timeutil::from_value(worldInfo.daytime, hour, minute, second);

View File

@ -61,8 +61,8 @@ bool Hud::showGeneratorMinimap = false;
// implemented in debug_panel.cpp // implemented in debug_panel.cpp
extern std::shared_ptr<UINode> create_debug_panel( extern std::shared_ptr<UINode> create_debug_panel(
Engine* engine, Engine* engine,
Level* level, Level& level,
Player* player, Player& player,
bool allowDebugCheats bool allowDebugCheats
); );
@ -104,8 +104,7 @@ std::shared_ptr<UINode> HudElement::getNode() const {
} }
std::shared_ptr<InventoryView> Hud::createContentAccess() { std::shared_ptr<InventoryView> Hud::createContentAccess() {
auto level = frontend->getLevel(); auto content = frontend.getLevel().content;
auto content = level->content;
auto indices = content->getIndices(); auto indices = content->getIndices();
auto inventory = player->getInventory(); auto inventory = player->getInventory();
@ -134,7 +133,7 @@ std::shared_ptr<InventoryView> Hud::createContentAccess() {
std::shared_ptr<InventoryView> Hud::createHotbar() { std::shared_ptr<InventoryView> Hud::createHotbar() {
auto inventory = player->getInventory(); auto inventory = player->getInventory();
auto content = frontend->getLevel()->content; auto content = frontend.getLevel().content;
SlotLayout slotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr); SlotLayout slotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr);
InventoryBuilder builder; InventoryBuilder builder;
@ -149,7 +148,7 @@ std::shared_ptr<InventoryView> Hud::createHotbar() {
static constexpr uint WORLDGEN_IMG_SIZE = 128U; static constexpr uint WORLDGEN_IMG_SIZE = 128U;
Hud::Hud(Engine* engine, LevelFrontend* frontend, Player* player) Hud::Hud(Engine* engine, LevelFrontend& frontend, Player* player)
: engine(engine), : engine(engine),
assets(engine->getAssets()), assets(engine->getAssets()),
gui(engine->getGUI()), gui(engine->getGUI()),
@ -178,7 +177,7 @@ Hud::Hud(Engine* engine, LevelFrontend* frontend, Player* player)
uicamera->flipped = true; uicamera->flipped = true;
debugPanel = create_debug_panel( debugPanel = create_debug_panel(
engine, frontend->getLevel(), player, allowDebugCheats engine, frontend.getLevel(), *player, allowDebugCheats
); );
debugPanel->setZIndex(2); debugPanel->setZIndex(2);
gui->add(debugPanel); gui->add(debugPanel);
@ -273,9 +272,9 @@ void Hud::updateHotbarControl() {
} }
void Hud::updateWorldGenDebugVisualization() { void Hud::updateWorldGenDebugVisualization() {
auto level = frontend->getLevel(); auto& level = frontend.getLevel();
auto generator = auto generator =
frontend->getController()->getChunksController()->getGenerator(); frontend.getController()->getChunksController()->getGenerator();
auto debugInfo = generator->createDebugInfo(); auto debugInfo = generator->createDebugInfo();
int width = debugImgWorldGen->getWidth(); int width = debugImgWorldGen->getWidth();
@ -298,9 +297,9 @@ void Hud::updateWorldGenDebugVisualization() {
int az = z - (height - areaHeight) / 2; int az = z - (height - areaHeight) / 2;
data[(flippedZ * width + x) * 4 + 1] = data[(flippedZ * width + x) * 4 + 1] =
level->chunks->getChunk(ax + ox, az + oz) ? 255 : 0; level.chunks->getChunk(ax + ox, az + oz) ? 255 : 0;
data[(flippedZ * width + x) * 4 + 0] = data[(flippedZ * width + x) * 4 + 0] =
level->chunksStorage->get(ax + ox, az + oz) ? 255 : 0; level.chunksStorage->get(ax + ox, az + oz) ? 255 : 0;
if (ax < 0 || az < 0 || if (ax < 0 || az < 0 ||
ax >= areaWidth || az >= areaHeight) { ax >= areaWidth || az >= areaHeight) {
@ -321,7 +320,7 @@ void Hud::updateWorldGenDebugVisualization() {
} }
void Hud::update(bool visible) { void Hud::update(bool visible) {
auto level = frontend->getLevel(); const auto& level = frontend.getLevel();
auto menu = gui->getMenu(); auto menu = gui->getMenu();
debugPanel->setVisible(player->debug && visible); debugPanel->setVisible(player->debug && visible);
@ -341,7 +340,7 @@ void Hud::update(bool visible) {
} }
if (blockUI) { if (blockUI) {
voxel* vox = level->chunks->get(blockPos.x, blockPos.y, blockPos.z); voxel* vox = level.chunks->get(blockPos.x, blockPos.y, blockPos.z);
if (vox == nullptr || vox->id != currentblockid) { if (vox == nullptr || vox->id != currentblockid) {
closeInventory(); closeInventory();
} }
@ -375,8 +374,7 @@ void Hud::update(bool visible) {
/// @brief Show inventory on the screen and turn on inventory mode blocking movement /// @brief Show inventory on the screen and turn on inventory mode blocking movement
void Hud::openInventory() { void Hud::openInventory() {
auto level = frontend->getLevel(); auto content = frontend.getLevel().content;
auto content = level->content;
showExchangeSlot(); showExchangeSlot();
inventoryOpen = true; inventoryOpen = true;
@ -401,8 +399,8 @@ void Hud::openInventory(
if (isInventoryOpen()) { if (isInventoryOpen()) {
closeInventory(); closeInventory();
} }
auto level = frontend->getLevel(); const auto& level = frontend.getLevel();
auto content = level->content; auto content = level.content;
secondInvView = std::dynamic_pointer_cast<InventoryView>(doc->getRoot()); secondInvView = std::dynamic_pointer_cast<InventoryView>(doc->getRoot());
if (secondInvView == nullptr) { if (secondInvView == nullptr) {
throw std::runtime_error("secondary UI root element must be 'inventory'"); throw std::runtime_error("secondary UI root element must be 'inventory'");
@ -415,7 +413,7 @@ void Hud::openInventory(
inventoryOpen = true; inventoryOpen = true;
} }
if (inv == nullptr) { if (inv == nullptr) {
inv = level->inventories->createVirtual(secondInvView->getSlotsCount()); inv = level.inventories->createVirtual(secondInvView->getSlotsCount());
} }
secondInvView->bind(inv, content); secondInvView->bind(inv, content);
add(HudElement(hud_element_mode::inventory_bound, doc, secondUI, false)); add(HudElement(hud_element_mode::inventory_bound, doc, secondUI, false));
@ -430,8 +428,8 @@ void Hud::openInventory(
if (isInventoryOpen()) { if (isInventoryOpen()) {
closeInventory(); closeInventory();
} }
auto level = frontend->getLevel(); auto& level = frontend.getLevel();
auto content = level->content; auto content = level.content;
blockUI = std::dynamic_pointer_cast<InventoryView>(doc->getRoot()); blockUI = std::dynamic_pointer_cast<InventoryView>(doc->getRoot());
if (blockUI == nullptr) { if (blockUI == nullptr) {
throw std::runtime_error("block UI root element must be 'inventory'"); throw std::runtime_error("block UI root element must be 'inventory'");
@ -443,19 +441,19 @@ void Hud::openInventory(
inventoryOpen = true; inventoryOpen = true;
} }
if (blockinv == nullptr) { if (blockinv == nullptr) {
blockinv = level->inventories->createVirtual(blockUI->getSlotsCount()); blockinv = level.inventories->createVirtual(blockUI->getSlotsCount());
} }
level->chunks->getChunkByVoxel(block.x, block.y, block.z)->flags.unsaved = true; level.chunks->getChunkByVoxel(block.x, block.y, block.z)->flags.unsaved = true;
blockUI->bind(blockinv, content); blockUI->bind(blockinv, content);
blockPos = block; blockPos = block;
currentblockid = level->chunks->get(block.x, block.y, block.z)->id; currentblockid = level.chunks->get(block.x, block.y, block.z)->id;
add(HudElement(hud_element_mode::inventory_bound, doc, blockUI, false)); add(HudElement(hud_element_mode::inventory_bound, doc, blockUI, false));
} }
void Hud::showExchangeSlot() { void Hud::showExchangeSlot() {
auto level = frontend->getLevel(); auto& level = frontend.getLevel();
auto content = level->content; auto content = level.content;
exchangeSlotInv = level->inventories->createVirtual(1); exchangeSlotInv = level.inventories->createVirtual(1);
exchangeSlot = std::make_shared<SlotView>( exchangeSlot = std::make_shared<SlotView>(
SlotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr) SlotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr)
); );
@ -487,7 +485,7 @@ void Hud::openPermanent(UiDocument* doc) {
auto invview = std::dynamic_pointer_cast<InventoryView>(root); auto invview = std::dynamic_pointer_cast<InventoryView>(root);
if (invview) { if (invview) {
invview->bind(player->getInventory(), frontend->getLevel()->content); invview->bind(player->getInventory(), frontend.getLevel().content);
} }
add(HudElement(hud_element_mode::permanent, doc, doc->getRoot(), false)); add(HudElement(hud_element_mode::permanent, doc, doc->getRoot(), false));
} }
@ -682,7 +680,7 @@ void Hud::setDebugCheats(bool flag) {
gui->remove(debugPanel); gui->remove(debugPanel);
debugPanel = create_debug_panel( debugPanel = create_debug_panel(
engine, frontend->getLevel(), player, allowDebugCheats engine, frontend.getLevel(), *player, allowDebugCheats
); );
debugPanel->setZIndex(2); debugPanel->setZIndex(2);
gui->add(debugPanel); gui->add(debugPanel);

View File

@ -73,7 +73,7 @@ class Hud : public util::ObjectsKeeper {
Assets* assets; Assets* assets;
std::unique_ptr<Camera> uicamera; std::unique_ptr<Camera> uicamera;
gui::GUI* gui; gui::GUI* gui;
LevelFrontend* frontend; LevelFrontend& frontend;
Player* player; Player* player;
/// @brief Is any overlay/inventory open /// @brief Is any overlay/inventory open
@ -130,7 +130,7 @@ class Hud : public util::ObjectsKeeper {
void showExchangeSlot(); void showExchangeSlot();
void updateWorldGenDebugVisualization(); void updateWorldGenDebugVisualization();
public: public:
Hud(Engine* engine, LevelFrontend* frontend, Player* player); Hud(Engine* engine, LevelFrontend& frontend, Player* player);
~Hud(); ~Hud();
void update(bool hudVisible); void update(bool hudVisible);

View File

@ -36,18 +36,21 @@ LevelScreen::LevelScreen(Engine* engine, std::unique_ptr<Level> levelPtr)
Level* level = levelPtr.get(); Level* level = levelPtr.get();
auto& settings = engine->getSettings(); auto& settings = engine->getSettings();
auto assets = engine->getAssets(); auto& assets = *engine->getAssets();
auto menu = engine->getGUI()->getMenu(); auto menu = engine->getGUI()->getMenu();
menu->reset(); menu->reset();
controller = std::make_unique<LevelController>(engine, std::move(levelPtr)); controller = std::make_unique<LevelController>(engine, std::move(levelPtr));
frontend = std::make_unique<LevelFrontend>(controller->getPlayer(), controller.get(), assets); frontend = std::make_unique<LevelFrontend>(
controller->getPlayer(), controller.get(), assets
worldRenderer = std::make_unique<WorldRenderer>(engine, frontend.get(), controller->getPlayer()); );
hud = std::make_unique<Hud>(engine, frontend.get(), controller->getPlayer()); worldRenderer = std::make_unique<WorldRenderer>(
engine, *frontend, controller->getPlayer()
);
hud = std::make_unique<Hud>(engine, *frontend, controller->getPlayer());
decorator = std::make_unique<Decorator>( decorator = std::make_unique<Decorator>(
*controller, *worldRenderer->particles, *assets *controller, *worldRenderer->particles, assets
); );
keepAlive(settings.graphics.backlight.observe([=](bool) { keepAlive(settings.graphics.backlight.observe([=](bool) {
@ -63,7 +66,7 @@ LevelScreen::LevelScreen(Engine* engine, std::unique_ptr<Level> levelPtr)
})); }));
animator = std::make_unique<TextureAnimator>(); animator = std::make_unique<TextureAnimator>();
animator->addAnimations(assets->getAnimations()); animator->addAnimations(assets.getAnimations());
initializeContent(); initializeContent();
} }

View File

@ -18,18 +18,18 @@
#include <glm/ext.hpp> #include <glm/ext.hpp>
std::unique_ptr<ImageData> BlocksPreview::draw( std::unique_ptr<ImageData> BlocksPreview::draw(
const ContentGfxCache* cache, const ContentGfxCache& cache,
Shader* shader, Shader& shader,
Framebuffer* fbo, const Framebuffer& fbo,
Batch3D* batch, Batch3D& batch,
const Block& def, const Block& def,
int size int size
){ ){
Window::clear(); Window::clear();
blockid_t id = def.rt.id; blockid_t id = def.rt.id;
const UVRegion texfaces[6]{cache->getRegion(id, 0), cache->getRegion(id, 1), const UVRegion texfaces[6]{cache.getRegion(id, 0), cache.getRegion(id, 1),
cache->getRegion(id, 2), cache->getRegion(id, 3), cache.getRegion(id, 2), cache.getRegion(id, 3),
cache->getRegion(id, 4), cache->getRegion(id, 5)}; cache.getRegion(id, 4), cache.getRegion(id, 5)};
glm::vec3 offset(0.1f, 0.5f, 0.1f); glm::vec3 offset(0.1f, 0.5f, 0.1f);
switch (def.model) { switch (def.model) {
@ -37,10 +37,10 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
// something went wrong... // something went wrong...
break; break;
case BlockModel::block: case BlockModel::block:
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset)); shader.uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
batch->blockCube(glm::vec3(size * 0.63f), texfaces, batch.blockCube(glm::vec3(size * 0.63f), texfaces,
glm::vec4(1.0f), !def.rt.emissive); glm::vec4(1.0f), !def.rt.emissive);
batch->flush(); batch.flush();
break; break;
case BlockModel::aabb: case BlockModel::aabb:
{ {
@ -49,39 +49,39 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
hitbox = glm::max(hitbox, box.size()); hitbox = glm::max(hitbox, box.size());
} }
offset = glm::vec3(1, 1, 0.0f); offset = glm::vec3(1, 1, 0.0f);
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset)); shader.uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
glm::vec3 scaledSize = glm::vec3(size * 0.63f); glm::vec3 scaledSize = glm::vec3(size * 0.63f);
batch->cube( batch.cube(
-hitbox * scaledSize * 0.5f * glm::vec3(1,1,-1), -hitbox * scaledSize * 0.5f * glm::vec3(1,1,-1),
hitbox * scaledSize, hitbox * scaledSize,
texfaces, glm::vec4(1.0f), texfaces, glm::vec4(1.0f),
!def.rt.emissive !def.rt.emissive
); );
} }
batch->flush(); batch.flush();
break; break;
case BlockModel::custom:{ case BlockModel::custom:{
glm::vec3 pmul = glm::vec3(size * 0.63f); glm::vec3 pmul = glm::vec3(size * 0.63f);
glm::vec3 hitbox = glm::vec3(1.0f); glm::vec3 hitbox = glm::vec3(1.0f);
glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f); glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f);
offset.y += (1.0f - hitbox).y * 0.5f; offset.y += (1.0f - hitbox).y * 0.5f;
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset)); shader.uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
const auto& model = cache->getModel(def.rt.id); const auto& model = cache.getModel(def.rt.id);
for (const auto& mesh : model.meshes) { for (const auto& mesh : model.meshes) {
for (const auto& vertex : mesh.vertices) { for (const auto& vertex : mesh.vertices) {
float d = glm::dot(glm::normalize(vertex.normal), glm::vec3(0.2, 0.8, 0.4)); float d = glm::dot(glm::normalize(vertex.normal), glm::vec3(0.2, 0.8, 0.4));
d = 0.8f + d * 0.2f; d = 0.8f + d * 0.2f;
batch->vertex((vertex.coord - poff)*pmul, vertex.uv, glm::vec4(d, d, d, 1.0f)); batch.vertex((vertex.coord - poff)*pmul, vertex.uv, glm::vec4(d, d, d, 1.0f));
} }
batch->flush(); batch.flush();
} }
break; break;
} }
case BlockModel::xsprite: { case BlockModel::xsprite: {
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset)); shader.uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
glm::vec3 right = glm::normalize(glm::vec3(1.f, 0.f, -1.f)); glm::vec3 right = glm::normalize(glm::vec3(1.f, 0.f, -1.f));
batch->sprite( batch.sprite(
right*float(size)*0.43f+glm::vec3(0, size*0.4f, 0), right*float(size)*0.43f+glm::vec3(0, size*0.4f, 0),
glm::vec3(0.f, 1.f, 0.f), glm::vec3(0.f, 1.f, 0.f),
right, right,
@ -89,24 +89,23 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
texfaces[0], texfaces[0],
glm::vec4(1.0f) glm::vec4(1.0f)
); );
batch->flush(); batch.flush();
break; break;
} }
} }
return fbo->getTexture()->readData(); return fbo.getTexture()->readData();
} }
std::unique_ptr<Atlas> BlocksPreview::build( std::unique_ptr<Atlas> BlocksPreview::build(
const ContentGfxCache* cache, const ContentGfxCache& cache,
Assets* assets, const Assets& assets,
const Content* content const ContentIndices& indices
) { ) {
auto indices = content->getIndices(); size_t count = indices.blocks.count();
size_t count = indices->blocks.count();
size_t iconSize = ITEM_ICON_SIZE; size_t iconSize = ITEM_ICON_SIZE;
auto shader = assets->get<Shader>("ui3d"); auto& shader = assets.require<Shader>("ui3d");
auto atlas = assets->get<Atlas>("blocks"); const auto& atlas = assets.require<Atlas>("blocks");
Viewport viewport(iconSize, iconSize); Viewport viewport(iconSize, iconSize);
DrawContext pctx(nullptr, viewport, nullptr); DrawContext pctx(nullptr, viewport, nullptr);
@ -118,8 +117,8 @@ std::unique_ptr<Atlas> BlocksPreview::build(
Batch3D batch(1024); Batch3D batch(1024);
batch.begin(); batch.begin();
shader->use(); shader.use();
shader->uniformMatrix("u_projview", shader.uniformMatrix("u_projview",
glm::ortho(0.0f, float(iconSize), 0.0f, float(iconSize), glm::ortho(0.0f, float(iconSize), 0.0f, float(iconSize),
-100.0f, 100.0f) * -100.0f, 100.0f) *
glm::lookAt(glm::vec3(0.57735f), glm::lookAt(glm::vec3(0.57735f),
@ -132,9 +131,9 @@ std::unique_ptr<Atlas> BlocksPreview::build(
fbo.bind(); fbo.bind();
for (size_t i = 0; i < count; i++) { for (size_t i = 0; i < count; i++) {
auto& def = indices->blocks.require(i); auto& def = indices.blocks.require(i);
atlas->getTexture()->bind(); atlas.getTexture()->bind();
builder.add(def.name, draw(cache, shader, &fbo, &batch, def, iconSize)); builder.add(def.name, draw(cache, shader, fbo, batch, def, iconSize));
} }
fbo.unbind(); fbo.unbind();

View File

@ -11,23 +11,23 @@ class Atlas;
class Framebuffer; class Framebuffer;
class Batch3D; class Batch3D;
class Block; class Block;
class Content; class ContentIndices;
class Shader; class Shader;
class ContentGfxCache; class ContentGfxCache;
class BlocksPreview { class BlocksPreview {
static std::unique_ptr<ImageData> draw( static std::unique_ptr<ImageData> draw(
const ContentGfxCache* cache, const ContentGfxCache& cache,
Shader* shader, Shader& shader,
Framebuffer* framebuffer, const Framebuffer& framebuffer,
Batch3D* batch, Batch3D& batch,
const Block& block, const Block& block,
int size int size
); );
public: public:
static std::unique_ptr<Atlas> build( static std::unique_ptr<Atlas> build(
const ContentGfxCache* cache, const ContentGfxCache& cache,
Assets* assets, const Assets& assets,
const Content* content const ContentIndices& indices
); );
}; };

View File

@ -17,9 +17,9 @@ const glm::vec3 BlocksRenderer::SUN_VECTOR (0.411934f, 0.863868f, -0.279161f);
BlocksRenderer::BlocksRenderer( BlocksRenderer::BlocksRenderer(
size_t capacity, size_t capacity,
const Content* content, const Content& content,
const ContentGfxCache* cache, const ContentGfxCache& cache,
const EngineSettings* settings const EngineSettings& settings
) : content(content), ) : content(content),
vertexBuffer(std::make_unique<float[]>(capacity * VERTEX_SIZE)), vertexBuffer(std::make_unique<float[]>(capacity * VERTEX_SIZE)),
indexBuffer(std::make_unique<int[]>(capacity)), indexBuffer(std::make_unique<int[]>(capacity)),
@ -34,7 +34,7 @@ BlocksRenderer::BlocksRenderer(
CHUNK_W + voxelBufferPadding*2, CHUNK_W + voxelBufferPadding*2,
CHUNK_H, CHUNK_H,
CHUNK_D + voxelBufferPadding*2); CHUNK_D + voxelBufferPadding*2);
blockDefsCache = content->getIndices()->blocks.getDefs(); blockDefsCache = content.getIndices()->blocks.getDefs();
} }
BlocksRenderer::~BlocksRenderer() { BlocksRenderer::~BlocksRenderer() {
@ -286,7 +286,7 @@ void BlocksRenderer::blockCustomModel(
Z = orient.axisZ; Z = orient.axisZ;
} }
const auto& model = cache->getModel(block->rt.id); const auto& model = cache.getModel(block->rt.id);
for (const auto& mesh : model.meshes) { for (const auto& mesh : model.meshes) {
if (vertexOffset + BlocksRenderer::VERTEX_SIZE * mesh.vertices.size() > capacity) { if (vertexOffset + BlocksRenderer::VERTEX_SIZE * mesh.vertices.size() > capacity) {
overflow = true; overflow = true;
@ -448,7 +448,7 @@ void BlocksRenderer::render(const voxel* voxels) {
} }
beginEnds[def.drawGroup][1] = i; beginEnds[def.drawGroup][1] = i;
} }
for (const auto drawGroup : *content->drawGroups) { for (const auto drawGroup : *content.drawGroups) {
int begin = beginEnds[drawGroup][0]; int begin = beginEnds[drawGroup][0];
if (begin == 0) { if (begin == 0) {
continue; continue;
@ -463,12 +463,12 @@ void BlocksRenderer::render(const voxel* voxels) {
continue; continue;
} }
const UVRegion texfaces[6] { const UVRegion texfaces[6] {
cache->getRegion(id, 0), cache.getRegion(id, 0),
cache->getRegion(id, 1), cache.getRegion(id, 1),
cache->getRegion(id, 2), cache.getRegion(id, 2),
cache->getRegion(id, 3), cache.getRegion(id, 3),
cache->getRegion(id, 4), cache.getRegion(id, 4),
cache->getRegion(id, 5) cache.getRegion(id, 5)
}; };
int x = i % CHUNK_W; int x = i % CHUNK_W;
int y = i / (CHUNK_D * CHUNK_W); int y = i / (CHUNK_D * CHUNK_W);
@ -508,7 +508,7 @@ void BlocksRenderer::build(const Chunk* chunk, const Chunks* chunks) {
voxelsBuffer->setPosition( voxelsBuffer->setPosition(
chunk->x * CHUNK_W - voxelBufferPadding, 0, chunk->x * CHUNK_W - voxelBufferPadding, 0,
chunk->z * CHUNK_D - voxelBufferPadding); chunk->z * CHUNK_D - voxelBufferPadding);
chunks->getVoxels(voxelsBuffer.get(), settings->graphics.backlight.get()); chunks->getVoxels(voxelsBuffer.get(), settings.graphics.backlight.get());
overflow = false; overflow = false;
vertexOffset = 0; vertexOffset = 0;
indexOffset = indexSize = 0; indexOffset = indexSize = 0;

View File

@ -27,7 +27,7 @@ struct UVRegion;
class BlocksRenderer { class BlocksRenderer {
static const glm::vec3 SUN_VECTOR; static const glm::vec3 SUN_VECTOR;
static const uint VERTEX_SIZE; static const uint VERTEX_SIZE;
const Content* const content; const Content& content;
std::unique_ptr<float[]> vertexBuffer; std::unique_ptr<float[]> vertexBuffer;
std::unique_ptr<int[]> indexBuffer; std::unique_ptr<int[]> indexBuffer;
size_t vertexOffset; size_t vertexOffset;
@ -40,8 +40,8 @@ class BlocksRenderer {
std::unique_ptr<VoxelsVolume> voxelsBuffer; std::unique_ptr<VoxelsVolume> voxelsBuffer;
const Block* const* blockDefsCache; const Block* const* blockDefsCache;
const ContentGfxCache* const cache; const ContentGfxCache& cache;
const EngineSettings* settings; const EngineSettings& settings;
util::PseudoRandom randomizer; util::PseudoRandom randomizer;
@ -137,7 +137,12 @@ class BlocksRenderer {
glm::vec4 pickSoftLight(float x, float y, float z, const glm::ivec3& right, const glm::ivec3& up) const; glm::vec4 pickSoftLight(float x, float y, float z, const glm::ivec3& right, const glm::ivec3& up) const;
void render(const voxel* voxels); void render(const voxel* voxels);
public: public:
BlocksRenderer(size_t capacity, const Content* content, const ContentGfxCache* cache, const EngineSettings* settings); BlocksRenderer(
size_t capacity,
const Content& content,
const ContentGfxCache& cache,
const EngineSettings& settings
);
virtual ~BlocksRenderer(); virtual ~BlocksRenderer();
void build(const Chunk* chunk, const Chunks* chunks); void build(const Chunk* chunk, const Chunks* chunks);

View File

@ -23,20 +23,20 @@ static debug::Logger logger("chunks-render");
size_t ChunksRenderer::visibleChunks = 0; size_t ChunksRenderer::visibleChunks = 0;
class RendererWorker : public util::Worker<std::shared_ptr<Chunk>, RendererResult> { class RendererWorker : public util::Worker<std::shared_ptr<Chunk>, RendererResult> {
Level* level; const Level& level;
BlocksRenderer renderer; BlocksRenderer renderer;
public: public:
RendererWorker( RendererWorker(
Level* level, const Level& level,
const ContentGfxCache* cache, const ContentGfxCache& cache,
const EngineSettings* settings const EngineSettings& settings
) : level(level), ) : level(level),
renderer(settings->graphics.chunkMaxVertices.get(), renderer(settings.graphics.chunkMaxVertices.get(),
level->content, cache, settings) *level.content, cache, settings)
{} {}
RendererResult operator()(const std::shared_ptr<Chunk>& chunk) override { RendererResult operator()(const std::shared_ptr<Chunk>& chunk) override {
renderer.build(chunk.get(), level->chunks.get()); renderer.build(chunk.get(), level.chunks.get());
if (renderer.isCancelled()) { if (renderer.isCancelled()) {
return RendererResult { return RendererResult {
glm::ivec2(chunk->x, chunk->z), true, MeshData()}; glm::ivec2(chunk->x, chunk->z), true, MeshData()};
@ -48,29 +48,29 @@ public:
}; };
ChunksRenderer::ChunksRenderer( ChunksRenderer::ChunksRenderer(
Level* level, const Level* level,
const Assets& assets, const Assets& assets,
const Frustum& frustum, const Frustum& frustum,
const ContentGfxCache* cache, const ContentGfxCache& cache,
const EngineSettings* settings const EngineSettings& settings
) : level(level), ) : level(*level),
assets(assets), assets(assets),
frustum(frustum), frustum(frustum),
settings(settings), settings(settings),
threadPool( threadPool(
"chunks-render-pool", "chunks-render-pool",
[=](){return std::make_shared<RendererWorker>(level, cache, settings);}, [&](){return std::make_shared<RendererWorker>(*level, cache, settings);},
[=](RendererResult& result){ [&](RendererResult& result){
if (!result.cancelled) { if (!result.cancelled) {
meshes[result.key] = std::make_shared<Mesh>(result.meshData); meshes[result.key] = std::make_shared<Mesh>(result.meshData);
} }
inwork.erase(result.key); inwork.erase(result.key);
}, settings->graphics.chunkMaxRenderers.get()) }, settings.graphics.chunkMaxRenderers.get())
{ {
threadPool.setStopOnFail(false); threadPool.setStopOnFail(false);
renderer = std::make_unique<BlocksRenderer>( renderer = std::make_unique<BlocksRenderer>(
settings->graphics.chunkMaxVertices.get(), settings.graphics.chunkMaxVertices.get(),
level->content, cache, settings *level->content, cache, settings
); );
logger.info() << "created " << threadPool.getWorkersCount() << " workers"; logger.info() << "created " << threadPool.getWorkersCount() << " workers";
} }
@ -81,7 +81,7 @@ ChunksRenderer::~ChunksRenderer() {
std::shared_ptr<Mesh> ChunksRenderer::render(const std::shared_ptr<Chunk>& chunk, bool important) { std::shared_ptr<Mesh> ChunksRenderer::render(const std::shared_ptr<Chunk>& chunk, bool important) {
chunk->flags.modified = false; chunk->flags.modified = false;
if (important) { if (important) {
auto mesh = renderer->render(chunk.get(), level->chunks.get()); auto mesh = renderer->render(chunk.get(), level.chunks.get());
meshes[glm::ivec2(chunk->x, chunk->z)] = mesh; meshes[glm::ivec2(chunk->x, chunk->z)] = mesh;
return mesh; return mesh;
} }
@ -125,7 +125,7 @@ void ChunksRenderer::update() {
bool ChunksRenderer::drawChunk( bool ChunksRenderer::drawChunk(
size_t index, const Camera& camera, Shader& shader, bool culling size_t index, const Camera& camera, Shader& shader, bool culling
) { ) {
auto chunk = level->chunks->getChunks()[index]; auto chunk = level.chunks->getChunks()[index];
if (chunk == nullptr || !chunk->flags.lighted) { if (chunk == nullptr || !chunk->flags.lighted) {
return false; return false;
} }
@ -161,7 +161,7 @@ bool ChunksRenderer::drawChunk(
void ChunksRenderer::drawChunks( void ChunksRenderer::drawChunks(
const Camera& camera, Shader& shader const Camera& camera, Shader& shader
) { ) {
const auto& chunks = *level->chunks; const auto& chunks = *level.chunks;
const auto& atlas = assets.require<Atlas>("blocks"); const auto& atlas = assets.require<Atlas>("blocks");
atlas.getTexture()->bind(); atlas.getTexture()->bind();
@ -188,7 +188,7 @@ void ChunksRenderer::drawChunks(
} }
util::insertion_sort(indices.begin(), indices.end()); util::insertion_sort(indices.begin(), indices.end());
bool culling = settings->graphics.frustumCulling.get(); bool culling = settings.graphics.frustumCulling.get();
visibleChunks = 0; visibleChunks = 0;
//if (GLEW_ARB_multi_draw_indirect && false) { //if (GLEW_ARB_multi_draw_indirect && false) {

View File

@ -39,15 +39,15 @@ struct RendererResult {
}; };
class ChunksRenderer { class ChunksRenderer {
Level* level; const Level& level;
const Assets& assets; const Assets& assets;
const Frustum& frustum; const Frustum& frustum;
const EngineSettings* settings; const EngineSettings& settings;
std::unique_ptr<BlocksRenderer> renderer; std::unique_ptr<BlocksRenderer> renderer;
std::unordered_map<glm::ivec2, std::shared_ptr<Mesh>> meshes; std::unordered_map<glm::ivec2, std::shared_ptr<Mesh>> meshes;
std::unordered_map<glm::ivec2, bool> inwork; std::unordered_map<glm::ivec2, bool> inwork;
std::vector<ChunksSortEntry> indices; std::vector<ChunksSortEntry> indices;
util::ThreadPool<std::shared_ptr<Chunk>, RendererResult> threadPool; util::ThreadPool<std::shared_ptr<Chunk>, RendererResult> threadPool;
bool drawChunk( bool drawChunk(
@ -55,11 +55,11 @@ class ChunksRenderer {
); );
public: public:
ChunksRenderer( ChunksRenderer(
Level* level, const Level* level,
const Assets& assets, const Assets& assets,
const Frustum& frustum, const Frustum& frustum,
const ContentGfxCache* cache, const ContentGfxCache& cache,
const EngineSettings* settings const EngineSettings& settings
); );
virtual ~ChunksRenderer(); virtual ~ChunksRenderer();

View File

@ -15,6 +15,7 @@
#include <iostream> #include <iostream>
#include <GL/glew.h> #include <GL/glew.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
#ifndef M_PI #ifndef M_PI
#define M_PI 3.141592 #define M_PI 3.141592
@ -23,7 +24,7 @@
const int STARS_COUNT = 3000; const int STARS_COUNT = 3000;
const int STARS_SEED = 632; const int STARS_SEED = 632;
Skybox::Skybox(uint size, Shader* shader) Skybox::Skybox(uint size, Shader& shader)
: size(size), : size(size),
shader(shader), shader(shader),
batch3d(std::make_unique<Batch3D>(4096)) batch3d(std::make_unique<Batch3D>(4096))
@ -43,14 +44,14 @@ Skybox::Skybox(uint size, Shader* shader)
sprites.push_back(skysprite { sprites.push_back(skysprite {
"misc/moon", "misc/moon",
M_PI*0.5f, glm::pi<float>()*0.5f,
4.0f, 4.0f,
false false
}); });
sprites.push_back(skysprite { sprites.push_back(skysprite {
"misc/sun", "misc/sun",
M_PI*1.5f, glm::pi<float>()*1.5f,
4.0f, 4.0f,
true true
}); });
@ -115,13 +116,13 @@ void Skybox::draw(
p_shader->uniformMatrix("u_apply", glm::mat4(1.0f)); p_shader->uniformMatrix("u_apply", glm::mat4(1.0f));
batch3d->begin(); batch3d->begin();
float angle = daytime * float(M_PI) * 2.0f; float angle = daytime * glm::pi<float>() * 2.0f;
float opacity = glm::pow(1.0f-fog, 7.0f); float opacity = glm::pow(1.0f-fog, 7.0f);
for (auto& sprite : sprites) { for (auto& sprite : sprites) {
batch3d->texture(assets.get<Texture>(sprite.texture)); batch3d->texture(assets.get<Texture>(sprite.texture));
float sangle = daytime * float(M_PI)*2.0 + sprite.phase; float sangle = daytime * glm::pi<float>()*2.0 + sprite.phase;
float distance = sprite.distance; float distance = sprite.distance;
glm::vec3 pos(-std::cos(sangle)*distance, std::sin(sangle)*distance, 0); glm::vec3 pos(-std::cos(sangle)*distance, std::sin(sangle)*distance, 0);
@ -152,15 +153,15 @@ void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality)
ready = true; ready = true;
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
cubemap->bind(); cubemap->bind();
shader->use(); shader.use();
t *= M_PI*2.0f; t *= glm::pi<float>()*2.0f;
lightDir = glm::normalize(glm::vec3(sin(t), -cos(t), 0.0f)); lightDir = glm::normalize(glm::vec3(sin(t), -cos(t), 0.0f));
shader->uniform1i("u_quality", quality); shader.uniform1i("u_quality", quality);
shader->uniform1f("u_mie", mie); shader.uniform1f("u_mie", mie);
shader->uniform1f("u_fog", mie - 1.0f); shader.uniform1f("u_fog", mie - 1.0f);
shader->uniform3f("u_lightDir", lightDir); shader.uniform3f("u_lightDir", lightDir);
shader->uniform1f("u_dayTime", dayTime); shader.uniform1f("u_dayTime", dayTime);
if (glm::abs(mie-prevMie) + glm::abs(t-prevT) >= 0.01) { if (glm::abs(mie-prevMie) + glm::abs(t-prevT) >= 0.01) {
for (uint face = 0; face < 6; face++) { for (uint face = 0; face < 6; face++) {
@ -206,10 +207,16 @@ void Skybox::refreshFace(uint face, Cubemap* cubemap) {
{0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, -1.0f},
{0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f},
}; };
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, cubemap->getId(), 0); glFramebufferTexture2D(
shader->uniform3f("u_xaxis", xaxs[face]); GL_FRAMEBUFFER,
shader->uniform3f("u_yaxis", yaxs[face]); GL_COLOR_ATTACHMENT0,
shader->uniform3f("u_zaxis", zaxs[face]); GL_TEXTURE_CUBE_MAP_POSITIVE_X + face,
cubemap->getId(),
0
);
shader.uniform3f("u_xaxis", xaxs[face]);
shader.uniform3f("u_yaxis", yaxs[face]);
shader.uniform3f("u_zaxis", zaxs[face]);
mesh->draw(); mesh->draw();
} }

View File

@ -27,7 +27,7 @@ struct skysprite {
class Skybox { class Skybox {
std::unique_ptr<Framebuffer> fbo; std::unique_ptr<Framebuffer> fbo;
uint size; uint size;
Shader* shader; Shader& shader;
bool ready = false; bool ready = false;
FastRandom random; FastRandom random;
glm::vec3 lightDir; glm::vec3 lightDir;
@ -46,7 +46,7 @@ class Skybox {
); );
void refreshFace(uint face, Cubemap* cubemap); void refreshFace(uint face, Cubemap* cubemap);
public: public:
Skybox(uint size, Shader* shader); Skybox(uint size, Shader& shader);
~Skybox(); ~Skybox();
void draw( void draw(

View File

@ -57,38 +57,39 @@ bool WorldRenderer::showChunkBorders = false;
bool WorldRenderer::showEntitiesDebug = false; bool WorldRenderer::showEntitiesDebug = false;
WorldRenderer::WorldRenderer( WorldRenderer::WorldRenderer(
Engine* engine, LevelFrontend* frontend, Player* player Engine* engine, LevelFrontend& frontend, Player* player
) )
: engine(engine), : engine(engine),
level(frontend->getLevel()), level(frontend.getLevel()),
player(player), player(player),
assets(*engine->getAssets()), assets(*engine->getAssets()),
frustumCulling(std::make_unique<Frustum>()), frustumCulling(std::make_unique<Frustum>()),
lineBatch(std::make_unique<LineBatch>()), lineBatch(std::make_unique<LineBatch>()),
batch3d(std::make_unique<Batch3D>(BATCH3D_CAPACITY)), batch3d(std::make_unique<Batch3D>(BATCH3D_CAPACITY)),
modelBatch(std::make_unique<ModelBatch>( modelBatch(std::make_unique<ModelBatch>(
MODEL_BATCH_CAPACITY, assets, *level->chunks, engine->getSettings() MODEL_BATCH_CAPACITY, assets, *level.chunks, engine->getSettings()
)), )),
particles(std::make_unique<ParticlesRenderer>( particles(std::make_unique<ParticlesRenderer>(
assets, *level, &engine->getSettings().graphics assets, level, &engine->getSettings().graphics
)),
texts(std::make_unique<TextsRenderer>(
*batch3d, assets, *frustumCulling
)), )),
texts(std::make_unique<TextsRenderer>(*batch3d, assets, *frustumCulling)),
guides(std::make_unique<GuidesRenderer>()), guides(std::make_unique<GuidesRenderer>()),
chunks(std::make_unique<ChunksRenderer>( chunks(std::make_unique<ChunksRenderer>(
level, assets, *frustumCulling, frontend->getContentGfxCache(), &engine->getSettings() &level,
)) assets,
{ *frustumCulling,
frontend.getContentGfxCache(),
engine->getSettings()
)) {
auto& settings = engine->getSettings(); auto& settings = engine->getSettings();
level->events->listen( level.events->listen(
EVT_CHUNK_HIDDEN, EVT_CHUNK_HIDDEN,
[this](lvl_event_type, Chunk* chunk) { chunks->unload(chunk); } [this](lvl_event_type, Chunk* chunk) { chunks->unload(chunk); }
); );
auto assets = engine->getAssets(); auto assets = engine->getAssets();
skybox = std::make_unique<Skybox>( skybox = std::make_unique<Skybox>(
settings.graphics.skyboxResolution.get(), settings.graphics.skyboxResolution.get(),
assets->get<Shader>("skybox_gen") assets->require<Shader>("skybox_gen")
); );
} }
@ -108,12 +109,12 @@ void WorldRenderer::setupWorldShader(
shader.uniform1f("u_gamma", settings.graphics.gamma.get()); shader.uniform1f("u_gamma", settings.graphics.gamma.get());
shader.uniform1f("u_fogFactor", fogFactor); shader.uniform1f("u_fogFactor", fogFactor);
shader.uniform1f("u_fogCurve", settings.graphics.fogCurve.get()); shader.uniform1f("u_fogCurve", settings.graphics.fogCurve.get());
shader.uniform1f("u_dayTime", level->getWorld()->getInfo().daytime); shader.uniform1f("u_dayTime", level.getWorld()->getInfo().daytime);
shader.uniform2f("u_lightDir", skybox->getLightDir()); shader.uniform2f("u_lightDir", skybox->getLightDir());
shader.uniform3f("u_cameraPos", camera.position); shader.uniform3f("u_cameraPos", camera.position);
shader.uniform1i("u_cubemap", 1); shader.uniform1i("u_cubemap", 1);
auto indices = level->content->getIndices(); auto indices = level.content->getIndices();
// Light emission when an emissive item is chosen // Light emission when an emissive item is chosen
{ {
auto inventory = player->getInventory(); auto inventory = player->getInventory();
@ -152,15 +153,15 @@ void WorldRenderer::renderLevel(
frustumCulling->update(camera.getProjView()); frustumCulling->update(camera.getProjView());
} }
level->entities->render( level.entities->render(
assets, assets,
*modelBatch, *modelBatch,
culling ? frustumCulling.get() : nullptr, culling ? frustumCulling.get() : nullptr,
delta, delta,
pause pause
); );
particles->render(camera, delta * !pause);
modelBatch->render(); modelBatch->render();
particles->render(camera, delta * !pause);
auto& shader = assets.require<Shader>("main"); auto& shader = assets.require<Shader>("main");
setupWorldShader(shader, camera, settings, fogFactor); setupWorldShader(shader, camera, settings, fogFactor);
@ -176,7 +177,7 @@ void WorldRenderer::renderLevel(
void WorldRenderer::renderBlockSelection() { void WorldRenderer::renderBlockSelection() {
const auto& selection = player->selection; const auto& selection = player->selection;
auto indices = level->content->getIndices(); auto indices = level.content->getIndices();
blockid_t id = selection.vox.id; blockid_t id = selection.vox.id;
auto& block = indices->blocks.require(id); auto& block = indices->blocks.require(id);
const glm::ivec3 pos = player->selection.position; const glm::ivec3 pos = player->selection.position;
@ -214,7 +215,7 @@ void WorldRenderer::renderLines(
if (player->debug && showEntitiesDebug) { if (player->debug && showEntitiesDebug) {
auto ctx = pctx.sub(lineBatch.get()); auto ctx = pctx.sub(lineBatch.get());
bool culling = engine->getSettings().graphics.frustumCulling.get(); bool culling = engine->getSettings().graphics.frustumCulling.get();
level->entities->renderDebug( level.entities->renderDebug(
*lineBatch, culling ? frustumCulling.get() : nullptr, ctx *lineBatch, culling ? frustumCulling.get() : nullptr, ctx
); );
} }
@ -224,7 +225,7 @@ void WorldRenderer::renderHands(
const Camera& camera, float delta const Camera& camera, float delta
) { ) {
auto& entityShader = assets.require<Shader>("entity"); auto& entityShader = assets.require<Shader>("entity");
auto indices = level->content->getIndices(); auto indices = level.content->getIndices();
// get current chosen item // get current chosen item
const auto& inventory = player->getInventory(); const auto& inventory = player->getInventory();
@ -292,7 +293,7 @@ void WorldRenderer::draw(
PostProcessing* postProcessing PostProcessing* postProcessing
) { ) {
timer += delta * !pause; timer += delta * !pause;
auto world = level->getWorld(); auto world = level.getWorld();
const Viewport& vp = pctx.getViewport(); const Viewport& vp = pctx.getViewport();
camera.aspect = vp.getWidth() / static_cast<float>(vp.getHeight()); camera.aspect = vp.getWidth() / static_cast<float>(vp.getHeight());
@ -346,10 +347,10 @@ void WorldRenderer::renderBlockOverlay(const DrawContext& wctx) {
int x = std::floor(player->currentCamera->position.x); int x = std::floor(player->currentCamera->position.x);
int y = std::floor(player->currentCamera->position.y); int y = std::floor(player->currentCamera->position.y);
int z = std::floor(player->currentCamera->position.z); int z = std::floor(player->currentCamera->position.z);
auto block = level->chunks->get(x, y, z); auto block = level.chunks->get(x, y, z);
if (block && block->id) { if (block && block->id) {
const auto& def = const auto& def =
level->content->getIndices()->blocks.require(block->id); level.content->getIndices()->blocks.require(block->id);
if (def.overlayTexture.empty()) { if (def.overlayTexture.empty()) {
return; return;
} }
@ -365,7 +366,7 @@ void WorldRenderer::renderBlockOverlay(const DrawContext& wctx) {
batch3d->begin(); batch3d->begin();
shader.uniformMatrix("u_projview", glm::mat4(1.0f)); shader.uniformMatrix("u_projview", glm::mat4(1.0f));
shader.uniformMatrix("u_apply", glm::mat4(1.0f)); shader.uniformMatrix("u_apply", glm::mat4(1.0f));
auto light = level->chunks->getLight(x, y, z); auto light = level.chunks->getLight(x, y, z);
float s = Lightmap::extract(light, 3) / 15.0f; float s = Lightmap::extract(light, 3) / 15.0f;
glm::vec4 tint( glm::vec4 tint(
glm::min(1.0f, Lightmap::extract(light, 0) / 15.0f + s), glm::min(1.0f, Lightmap::extract(light, 0) / 15.0f + s),

View File

@ -28,16 +28,11 @@ class PostProcessing;
class DrawContext; class DrawContext;
class ModelBatch; class ModelBatch;
class Assets; class Assets;
class Emitter;
struct EngineSettings; struct EngineSettings;
namespace model {
struct Model;
}
class WorldRenderer { class WorldRenderer {
Engine* engine; Engine* engine;
Level* level; const Level& level;
Player* player; Player* player;
const Assets& assets; const Assets& assets;
std::unique_ptr<Frustum> frustumCulling; std::unique_ptr<Frustum> frustumCulling;
@ -77,7 +72,7 @@ public:
static bool showChunkBorders; static bool showChunkBorders;
static bool showEntitiesDebug; static bool showEntitiesDebug;
WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player); WorldRenderer(Engine* engine, LevelFrontend& frontend, Player* player);
~WorldRenderer(); ~WorldRenderer();
void draw( void draw(

View File

@ -46,7 +46,7 @@ void scripting::on_frontend_render() {
lua::emit_event( lua::emit_event(
lua::get_main_state(), lua::get_main_state(),
pack.id + ":.hudrender", pack.id + ":.hudrender",
[&](lua::State* L) { return 0; } [](lua::State* L) { return 0; }
); );
} }
} }

View File

@ -96,6 +96,10 @@ World* Level::getWorld() {
return world.get(); return world.get();
} }
const World* Level::getWorld() const {
return world.get();
}
void Level::onSave() { void Level::onSave() {
auto& cameraIndices = content->getIndices(ResourceType::CAMERA); auto& cameraIndices = content->getIndices(ResourceType::CAMERA);
for (size_t i = 0; i < cameraIndices.size(); i++) { for (size_t i = 0; i < cameraIndices.size(); i++) {

View File

@ -50,6 +50,8 @@ public:
World* getWorld(); World* getWorld();
const World* getWorld() const;
/// Spawns object of class T and returns pointer to it. /// Spawns object of class T and returns pointer to it.
/// @param T class that derives the Object class /// @param T class that derives the Object class
/// @param args pass arguments needed for T class constructor /// @param args pass arguments needed for T class constructor