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 "voxels/Block.hpp"
ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets)
ContentGfxCache::ContentGfxCache(const Content* content, const Assets& assets)
: content(content) {
auto indices = content->getIndices();
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();
for (blockid_t i = 0; i < blocks.size(); i++) {
auto def = blocks[i];
for (uint side = 0; side < 6; side++) {
const std::string& tex = def->textureFaces[side];
if (atlas->has(tex)) {
sideregions[i * 6 + side] = atlas->get(tex);
} else if (atlas->has(TEXTURE_NOTFOUND)) {
sideregions[i * 6 + side] = atlas->get(TEXTURE_NOTFOUND);
if (atlas.has(tex)) {
sideregions[i * 6 + side] = atlas.get(tex);
} else if (atlas.has(TEXTURE_NOTFOUND)) {
sideregions[i * 6 + side] = atlas.get(TEXTURE_NOTFOUND);
}
}
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
if (def->modelName.find(':') == std::string::npos) {
for (auto& mesh : model.meshes) {
@ -37,7 +37,7 @@ ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets)
if (pos == std::string::npos) {
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) {
vertex.uv = region->apply(vertex.uv);
}

View File

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

View File

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

View File

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

View File

@ -42,8 +42,8 @@ static std::shared_ptr<Label> create_label(wstringsupplier supplier) {
// TODO: move to xml finally
std::shared_ptr<UINode> create_debug_panel(
Engine* engine,
Level* level,
Player* player,
Level& level,
Player& player,
bool allowDebugCheats
) {
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: " +
std::to_wstring(ParticlesRenderer::aliveEmitters);
}));
panel->add(create_label([=]() {
return L"chunks: "+std::to_wstring(level->chunks->getChunksCount())+
panel->add(create_label([&]() {
return L"chunks: "+std::to_wstring(level.chunks->getChunksCount())+
L" visible: "+std::to_wstring(ChunksRenderer::visibleChunks);
}));
panel->add(create_label([=]() {
return L"entities: "+std::to_wstring(level->entities->size())+L" next: "+
std::to_wstring(level->entities->peekNextID());
panel->add(create_label([&]() {
return L"entities: "+std::to_wstring(level.entities->size())+L" next: "+
std::to_wstring(level.entities->peekNextID());
}));
panel->add(create_label([=]() {
const auto& vox = player->selection.vox;
panel->add(create_label([&]() -> std::wstring {
const auto& vox = player.selection.vox;
std::wstringstream stream;
stream << "r:" << vox.state.rotation << " s:"
<< std::bitset<3>(vox.state.segment) << " u:"
<< std::bitset<8>(vox.state.userbits);
if (vox.id == BLOCK_VOID) {
return std::wstring {L"block: -"};
return L"block: -";
} else {
return L"block: "+std::to_wstring(vox.id)+
L" "+stream.str();
}
}));
panel->add(create_label([=]() -> std::wstring {
const auto& vox = player->selection.vox;
panel->add(create_label([&]() -> std::wstring {
const auto& selection = player.selection;
const auto& vox = selection.vox;
if (vox.id == BLOCK_VOID) {
return L"x: - y: - z: -";
}
return L"x: " + std::to_wstring(player->selection.actualPosition.x) +
L" y: " + std::to_wstring(player->selection.actualPosition.y) +
L" z: " + std::to_wstring(player->selection.actualPosition.z);
return L"x: " + std::to_wstring(selection.actualPosition.x) +
L" y: " + std::to_wstring(selection.actualPosition.y) +
L" z: " + std::to_wstring(selection.actualPosition.z);
}));
panel->add(create_label([=]() {
auto eid = player->getSelectedEntity();
panel->add(create_label([&]() {
auto eid = player.getSelectedEntity();
if (eid == ENTITY_NONE) {
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)+
L" uid: "+std::to_wstring(entity->getUID());
} else {
return std::wstring {L"entity: error (invalid UID)"};
}
}));
panel->add(create_label([=](){
auto* indices = level->content->getIndices();
if (auto def = indices->blocks.get(player->selection.vox.id)) {
panel->add(create_label([&](){
auto* indices = level.content->getIndices();
if (auto def = indices->blocks.get(player.selection.vox.id)) {
return L"name: " + util::str2wstr_utf8(def->name);
} else {
return std::wstring {L"name: void"};
}
}));
panel->add(create_label([=](){
return L"seed: "+std::to_wstring(level->getWorld()->getSeed());
panel->add(create_label([&](){
return L"seed: "+std::to_wstring(level.getWorld()->getSeed());
}));
for (int ax = 0; ax < 3; ax++) {
@ -161,22 +162,22 @@ std::shared_ptr<UINode> create_debug_panel(
// Coord input
auto box = std::make_shared<TextBox>(L"");
auto boxRef = box.get();
box->setTextSupplier([=]() {
return util::to_wstring(player->getPosition()[ax], 2);
box->setTextSupplier([&player, ax]() {
return util::to_wstring(player.getPosition()[ax], 2);
});
if (allowDebugCheats) {
box->setTextConsumer([=](const std::wstring& text) {
box->setTextConsumer([&player, ax](const std::wstring& text) {
try {
glm::vec3 position = player->getPosition();
glm::vec3 position = player.getPosition();
position[ax] = std::stoi(text);
player->teleport(position);
player.teleport(position);
} catch (std::exception& _){
}
});
}
box->setOnEditStart([=]() {
box->setOnEditStart([&player, boxRef, ax]() {
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));
@ -184,7 +185,7 @@ std::shared_ptr<UINode> create_debug_panel(
sub->add(box, glm::vec2(20, 0));
panel->add(sub);
}
auto& worldInfo = level->getWorld()->getInfo();
auto& worldInfo = level.getWorld()->getInfo();
panel->add(create_label([&](){
int 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
extern std::shared_ptr<UINode> create_debug_panel(
Engine* engine,
Level* level,
Player* player,
Level& level,
Player& player,
bool allowDebugCheats
);
@ -104,8 +104,7 @@ std::shared_ptr<UINode> HudElement::getNode() const {
}
std::shared_ptr<InventoryView> Hud::createContentAccess() {
auto level = frontend->getLevel();
auto content = level->content;
auto content = frontend.getLevel().content;
auto indices = content->getIndices();
auto inventory = player->getInventory();
@ -134,7 +133,7 @@ std::shared_ptr<InventoryView> Hud::createContentAccess() {
std::shared_ptr<InventoryView> Hud::createHotbar() {
auto inventory = player->getInventory();
auto content = frontend->getLevel()->content;
auto content = frontend.getLevel().content;
SlotLayout slotLayout(-1, glm::vec2(), false, false, nullptr, nullptr, nullptr);
InventoryBuilder builder;
@ -149,7 +148,7 @@ std::shared_ptr<InventoryView> Hud::createHotbar() {
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),
assets(engine->getAssets()),
gui(engine->getGUI()),
@ -178,7 +177,7 @@ Hud::Hud(Engine* engine, LevelFrontend* frontend, Player* player)
uicamera->flipped = true;
debugPanel = create_debug_panel(
engine, frontend->getLevel(), player, allowDebugCheats
engine, frontend.getLevel(), *player, allowDebugCheats
);
debugPanel->setZIndex(2);
gui->add(debugPanel);
@ -273,9 +272,9 @@ void Hud::updateHotbarControl() {
}
void Hud::updateWorldGenDebugVisualization() {
auto level = frontend->getLevel();
auto& level = frontend.getLevel();
auto generator =
frontend->getController()->getChunksController()->getGenerator();
frontend.getController()->getChunksController()->getGenerator();
auto debugInfo = generator->createDebugInfo();
int width = debugImgWorldGen->getWidth();
@ -298,9 +297,9 @@ void Hud::updateWorldGenDebugVisualization() {
int az = z - (height - areaHeight) / 2;
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] =
level->chunksStorage->get(ax + ox, az + oz) ? 255 : 0;
level.chunksStorage->get(ax + ox, az + oz) ? 255 : 0;
if (ax < 0 || az < 0 ||
ax >= areaWidth || az >= areaHeight) {
@ -321,7 +320,7 @@ void Hud::updateWorldGenDebugVisualization() {
}
void Hud::update(bool visible) {
auto level = frontend->getLevel();
const auto& level = frontend.getLevel();
auto menu = gui->getMenu();
debugPanel->setVisible(player->debug && visible);
@ -341,7 +340,7 @@ void Hud::update(bool visible) {
}
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) {
closeInventory();
}
@ -375,8 +374,7 @@ void Hud::update(bool visible) {
/// @brief Show inventory on the screen and turn on inventory mode blocking movement
void Hud::openInventory() {
auto level = frontend->getLevel();
auto content = level->content;
auto content = frontend.getLevel().content;
showExchangeSlot();
inventoryOpen = true;
@ -401,8 +399,8 @@ void Hud::openInventory(
if (isInventoryOpen()) {
closeInventory();
}
auto level = frontend->getLevel();
auto content = level->content;
const auto& level = frontend.getLevel();
auto content = level.content;
secondInvView = std::dynamic_pointer_cast<InventoryView>(doc->getRoot());
if (secondInvView == nullptr) {
throw std::runtime_error("secondary UI root element must be 'inventory'");
@ -415,7 +413,7 @@ void Hud::openInventory(
inventoryOpen = true;
}
if (inv == nullptr) {
inv = level->inventories->createVirtual(secondInvView->getSlotsCount());
inv = level.inventories->createVirtual(secondInvView->getSlotsCount());
}
secondInvView->bind(inv, content);
add(HudElement(hud_element_mode::inventory_bound, doc, secondUI, false));
@ -430,8 +428,8 @@ void Hud::openInventory(
if (isInventoryOpen()) {
closeInventory();
}
auto level = frontend->getLevel();
auto content = level->content;
auto& level = frontend.getLevel();
auto content = level.content;
blockUI = std::dynamic_pointer_cast<InventoryView>(doc->getRoot());
if (blockUI == nullptr) {
throw std::runtime_error("block UI root element must be 'inventory'");
@ -443,19 +441,19 @@ void Hud::openInventory(
inventoryOpen = true;
}
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);
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));
}
void Hud::showExchangeSlot() {
auto level = frontend->getLevel();
auto content = level->content;
exchangeSlotInv = level->inventories->createVirtual(1);
auto& level = frontend.getLevel();
auto content = level.content;
exchangeSlotInv = level.inventories->createVirtual(1);
exchangeSlot = std::make_shared<SlotView>(
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);
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));
}
@ -682,7 +680,7 @@ void Hud::setDebugCheats(bool flag) {
gui->remove(debugPanel);
debugPanel = create_debug_panel(
engine, frontend->getLevel(), player, allowDebugCheats
engine, frontend.getLevel(), *player, allowDebugCheats
);
debugPanel->setZIndex(2);
gui->add(debugPanel);

View File

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

View File

@ -36,18 +36,21 @@ LevelScreen::LevelScreen(Engine* engine, std::unique_ptr<Level> levelPtr)
Level* level = levelPtr.get();
auto& settings = engine->getSettings();
auto assets = engine->getAssets();
auto& assets = *engine->getAssets();
auto menu = engine->getGUI()->getMenu();
menu->reset();
controller = std::make_unique<LevelController>(engine, std::move(levelPtr));
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());
frontend = std::make_unique<LevelFrontend>(
controller->getPlayer(), controller.get(), assets
);
worldRenderer = std::make_unique<WorldRenderer>(
engine, *frontend, controller->getPlayer()
);
hud = std::make_unique<Hud>(engine, *frontend, controller->getPlayer());
decorator = std::make_unique<Decorator>(
*controller, *worldRenderer->particles, *assets
*controller, *worldRenderer->particles, assets
);
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->addAnimations(assets->getAnimations());
animator->addAnimations(assets.getAnimations());
initializeContent();
}

View File

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

View File

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

View File

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

View File

@ -27,7 +27,7 @@ struct UVRegion;
class BlocksRenderer {
static const glm::vec3 SUN_VECTOR;
static const uint VERTEX_SIZE;
const Content* const content;
const Content& content;
std::unique_ptr<float[]> vertexBuffer;
std::unique_ptr<int[]> indexBuffer;
size_t vertexOffset;
@ -40,8 +40,8 @@ class BlocksRenderer {
std::unique_ptr<VoxelsVolume> voxelsBuffer;
const Block* const* blockDefsCache;
const ContentGfxCache* const cache;
const EngineSettings* settings;
const ContentGfxCache& cache;
const EngineSettings& settings;
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;
void render(const voxel* voxels);
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();
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;
class RendererWorker : public util::Worker<std::shared_ptr<Chunk>, RendererResult> {
Level* level;
const Level& level;
BlocksRenderer renderer;
public:
RendererWorker(
Level* level,
const ContentGfxCache* cache,
const EngineSettings* settings
const Level& level,
const ContentGfxCache& cache,
const EngineSettings& settings
) : level(level),
renderer(settings->graphics.chunkMaxVertices.get(),
level->content, cache, settings)
renderer(settings.graphics.chunkMaxVertices.get(),
*level.content, cache, settings)
{}
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()) {
return RendererResult {
glm::ivec2(chunk->x, chunk->z), true, MeshData()};
@ -48,29 +48,29 @@ public:
};
ChunksRenderer::ChunksRenderer(
Level* level,
const Level* level,
const Assets& assets,
const Frustum& frustum,
const ContentGfxCache* cache,
const EngineSettings* settings
) : level(level),
const ContentGfxCache& cache,
const EngineSettings& settings
) : level(*level),
assets(assets),
frustum(frustum),
settings(settings),
threadPool(
"chunks-render-pool",
[=](){return std::make_shared<RendererWorker>(level, cache, settings);},
[=](RendererResult& result){
[&](){return std::make_shared<RendererWorker>(*level, cache, settings);},
[&](RendererResult& result){
if (!result.cancelled) {
meshes[result.key] = std::make_shared<Mesh>(result.meshData);
}
inwork.erase(result.key);
}, settings->graphics.chunkMaxRenderers.get())
}, settings.graphics.chunkMaxRenderers.get())
{
threadPool.setStopOnFail(false);
renderer = std::make_unique<BlocksRenderer>(
settings->graphics.chunkMaxVertices.get(),
level->content, cache, settings
settings.graphics.chunkMaxVertices.get(),
*level->content, cache, settings
);
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) {
chunk->flags.modified = false;
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;
return mesh;
}
@ -125,7 +125,7 @@ void ChunksRenderer::update() {
bool ChunksRenderer::drawChunk(
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) {
return false;
}
@ -161,7 +161,7 @@ bool ChunksRenderer::drawChunk(
void ChunksRenderer::drawChunks(
const Camera& camera, Shader& shader
) {
const auto& chunks = *level->chunks;
const auto& chunks = *level.chunks;
const auto& atlas = assets.require<Atlas>("blocks");
atlas.getTexture()->bind();
@ -188,7 +188,7 @@ void ChunksRenderer::drawChunks(
}
util::insertion_sort(indices.begin(), indices.end());
bool culling = settings->graphics.frustumCulling.get();
bool culling = settings.graphics.frustumCulling.get();
visibleChunks = 0;
//if (GLEW_ARB_multi_draw_indirect && false) {

View File

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

View File

@ -15,6 +15,7 @@
#include <iostream>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
#ifndef M_PI
#define M_PI 3.141592
@ -23,7 +24,7 @@
const int STARS_COUNT = 3000;
const int STARS_SEED = 632;
Skybox::Skybox(uint size, Shader* shader)
Skybox::Skybox(uint size, Shader& shader)
: size(size),
shader(shader),
batch3d(std::make_unique<Batch3D>(4096))
@ -43,14 +44,14 @@ Skybox::Skybox(uint size, Shader* shader)
sprites.push_back(skysprite {
"misc/moon",
M_PI*0.5f,
glm::pi<float>()*0.5f,
4.0f,
false
});
sprites.push_back(skysprite {
"misc/sun",
M_PI*1.5f,
glm::pi<float>()*1.5f,
4.0f,
true
});
@ -115,13 +116,13 @@ void Skybox::draw(
p_shader->uniformMatrix("u_apply", glm::mat4(1.0f));
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);
for (auto& sprite : sprites) {
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;
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;
glActiveTexture(GL_TEXTURE1);
cubemap->bind();
shader->use();
t *= M_PI*2.0f;
shader.use();
t *= glm::pi<float>()*2.0f;
lightDir = glm::normalize(glm::vec3(sin(t), -cos(t), 0.0f));
shader->uniform1i("u_quality", quality);
shader->uniform1f("u_mie", mie);
shader->uniform1f("u_fog", mie - 1.0f);
shader->uniform3f("u_lightDir", lightDir);
shader->uniform1f("u_dayTime", dayTime);
shader.uniform1i("u_quality", quality);
shader.uniform1f("u_mie", mie);
shader.uniform1f("u_fog", mie - 1.0f);
shader.uniform3f("u_lightDir", lightDir);
shader.uniform1f("u_dayTime", dayTime);
if (glm::abs(mie-prevMie) + glm::abs(t-prevT) >= 0.01) {
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},
};
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 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]);
glFramebufferTexture2D(
GL_FRAMEBUFFER,
GL_COLOR_ATTACHMENT0,
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();
}

View File

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

View File

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

View File

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

View File

@ -46,7 +46,7 @@ void scripting::on_frontend_render() {
lua::emit_event(
lua::get_main_state(),
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();
}
const World* Level::getWorld() const {
return world.get();
}
void Level::onSave() {
auto& cameraIndices = content->getIndices(ResourceType::CAMERA);
for (size_t i = 0; i < cameraIndices.size(); i++) {

View File

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