inventory render optimized
This commit is contained in:
parent
714a49685f
commit
db2108171c
@ -17,6 +17,8 @@ const int CHUNK_D = 16;
|
||||
const uint VOXEL_USER_BITS = 8;
|
||||
constexpr uint VOXEL_USER_BITS_OFFSET = sizeof(blockstate_t)*8-VOXEL_USER_BITS;
|
||||
|
||||
const int ITEM_ICON_SIZE = 48;
|
||||
|
||||
/* Chunk volume (count of voxels per Chunk) */
|
||||
constexpr int CHUNK_VOL = (CHUNK_W * CHUNK_H * CHUNK_D);
|
||||
|
||||
|
||||
@ -8,47 +8,23 @@
|
||||
#include "../graphics/Texture.h"
|
||||
#include "../graphics/Atlas.h"
|
||||
#include "../graphics/Batch3D.h"
|
||||
#include "../graphics/Framebuffer.h"
|
||||
#include "../graphics/GfxContext.h"
|
||||
#include "../window/Window.h"
|
||||
#include "../window/Camera.h"
|
||||
#include "../voxels/Block.h"
|
||||
#include "../content/Content.h"
|
||||
#include "../constants.h"
|
||||
#include "ContentGfxCache.h"
|
||||
|
||||
BlocksPreview::BlocksPreview(Assets* assets, const ContentGfxCache* cache)
|
||||
: shader(assets->getShader("ui3d")),
|
||||
atlas(assets->getAtlas("blocks")),
|
||||
cache(cache) {
|
||||
batch = std::make_unique<Batch3D>(1024);
|
||||
}
|
||||
|
||||
BlocksPreview::~BlocksPreview() {
|
||||
}
|
||||
|
||||
void BlocksPreview::begin(const Viewport* viewport) {
|
||||
this->viewport = viewport;
|
||||
shader->use();
|
||||
shader->uniformMatrix("u_projview",
|
||||
glm::ortho(0.0f, float(viewport->getWidth()),
|
||||
0.0f, float(viewport->getHeight()),
|
||||
-100.0f, 100.0f) *
|
||||
glm::lookAt(glm::vec3(2, 2, 2), glm::vec3(0.0f), glm::vec3(0, 1, 0)));
|
||||
atlas->getTexture()->bind();
|
||||
}
|
||||
|
||||
/* Draw one block preview at given screen position */
|
||||
void BlocksPreview::draw(const Block* def, int x, int y, int size, glm::vec4 tint) {
|
||||
uint width = viewport->getWidth();
|
||||
uint height = viewport->getHeight();
|
||||
|
||||
y = height - y - 1 - 35 /* magic garbage */;
|
||||
x += 2;
|
||||
|
||||
if (def->model == BlockModel::aabb) {
|
||||
x += (1.0f - def->hitbox.size()).x * size * 0.5f;
|
||||
y += (1.0f - def->hitbox.size()).y * size * 0.25f;
|
||||
}
|
||||
|
||||
glm::vec3 offset (x/float(width) * 2, y/float(height) * 2, 0.0f);
|
||||
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
|
||||
|
||||
ImageData* BlocksPreview::draw(
|
||||
const ContentGfxCache* cache,
|
||||
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),
|
||||
@ -59,11 +35,11 @@ void BlocksPreview::draw(const Block* def, int x, int y, int size, glm::vec4 tin
|
||||
// something went wrong...
|
||||
break;
|
||||
case BlockModel::block:
|
||||
batch->blockCube(glm::vec3(size * 0.63f), texfaces, tint, !def->rt.emissive);
|
||||
batch->blockCube(glm::vec3(size * 0.63f), texfaces, glm::vec4(1.0f), !def->rt.emissive);
|
||||
break;
|
||||
case BlockModel::aabb:
|
||||
batch->blockCube(def->hitbox.size() * glm::vec3(size * 0.63f),
|
||||
texfaces, tint, !def->rt.emissive);
|
||||
texfaces, glm::vec4(1.0f), !def->rt.emissive);
|
||||
break;
|
||||
case BlockModel::custom:
|
||||
case BlockModel::xsprite: {
|
||||
@ -73,10 +49,64 @@ void BlocksPreview::draw(const Block* def, int x, int y, int size, glm::vec4 tin
|
||||
right,
|
||||
size*0.5f, size*0.6f,
|
||||
texfaces[0],
|
||||
tint);
|
||||
glm::vec4(1.0f));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
batch->flush();
|
||||
return fbo->texture->readData();
|
||||
}
|
||||
|
||||
std::unique_ptr<Atlas> BlocksPreview::build(
|
||||
const ContentGfxCache* cache,
|
||||
Assets* assets,
|
||||
const Content* content
|
||||
) {
|
||||
auto indices = content->getIndices();
|
||||
size_t count = indices->countBlockDefs();
|
||||
size_t iconSize = ITEM_ICON_SIZE;
|
||||
|
||||
Shader* shader = assets->getShader("ui3d");
|
||||
Atlas* atlas = assets->getAtlas("blocks");
|
||||
|
||||
Viewport viewport(iconSize, iconSize);
|
||||
GfxContext pctx(nullptr, viewport, nullptr);
|
||||
GfxContext ctx = pctx.sub();
|
||||
ctx.cullFace(true);
|
||||
ctx.depthTest(true);
|
||||
|
||||
Framebuffer fbo(iconSize, iconSize, true);
|
||||
Batch3D batch(1024);
|
||||
batch.begin();
|
||||
|
||||
shader->use();
|
||||
shader->uniformMatrix("u_projview",
|
||||
|
||||
glm::ortho(0.0f, float(iconSize), 0.0f, float(iconSize),
|
||||
-100.0f, 100.0f) *
|
||||
glm::lookAt(glm::vec3(2, 2, 2),
|
||||
glm::vec3(0.0f),
|
||||
glm::vec3(0, 1, 0)));
|
||||
|
||||
AtlasBuilder builder;
|
||||
Window::viewport(0, 0, iconSize, iconSize);
|
||||
Window::setBgColor(glm::vec4(0.0f));
|
||||
|
||||
fbo.bind();
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
auto def = indices->getBlockDef(i);
|
||||
|
||||
glm::vec3 offset(0.1f, 0.5f, 0.1f);
|
||||
if (def->model == BlockModel::aabb) {
|
||||
offset.y += (1.0f - def->hitbox.size()).y * 0.5f;
|
||||
}
|
||||
atlas->getTexture()->bind();
|
||||
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
|
||||
|
||||
builder.add(def->name, draw(cache, &fbo, &batch, def, iconSize));
|
||||
}
|
||||
fbo.unbind();
|
||||
|
||||
Window::viewport(0, 0, Window::width, Window::height);
|
||||
return std::unique_ptr<Atlas>(builder.build(2));
|
||||
}
|
||||
|
||||
@ -6,25 +6,27 @@
|
||||
#include <memory>
|
||||
|
||||
class Assets;
|
||||
class Viewport;
|
||||
class Shader;
|
||||
class ImageData;
|
||||
class Atlas;
|
||||
class Framebuffer;
|
||||
class Batch3D;
|
||||
class Block;
|
||||
class Content;
|
||||
class ContentGfxCache;
|
||||
|
||||
class BlocksPreview {
|
||||
Shader* shader;
|
||||
Atlas* atlas;
|
||||
std::unique_ptr<Batch3D> batch;
|
||||
const ContentGfxCache* const cache;
|
||||
const Viewport* viewport;
|
||||
public:
|
||||
BlocksPreview(Assets* assets, const ContentGfxCache* cache);
|
||||
~BlocksPreview();
|
||||
static ImageData* draw(
|
||||
const ContentGfxCache* cache,
|
||||
Framebuffer* framebuffer,
|
||||
Batch3D* batch,
|
||||
const Block* block,
|
||||
int size);
|
||||
|
||||
void begin(const Viewport* viewport);
|
||||
void draw(const Block* block, int x, int y, int size, glm::vec4 tint);
|
||||
static std::unique_ptr<Atlas> build(
|
||||
const ContentGfxCache* cache,
|
||||
Assets* assets,
|
||||
const Content* content);
|
||||
};
|
||||
|
||||
#endif // FRONTEND_BLOCKS_PREVIEW_H_
|
||||
|
||||
@ -153,31 +153,26 @@ void SlotView::draw(Batch2D* batch, Assets* assets) {
|
||||
|
||||
batch->color = glm::vec4(1.0f);
|
||||
|
||||
Shader* uiShader = assets->getShader("ui");
|
||||
Viewport viewport(Window::width, Window::height);
|
||||
GfxContext ctx(nullptr, viewport, batch);
|
||||
|
||||
auto preview = frontend->getBlocksPreview();
|
||||
auto previews = frontend->getBlocksAtlas();
|
||||
auto indices = content->getIndices();
|
||||
|
||||
ItemDef* item = indices->getItemDef(stack.getItemId());
|
||||
switch (item->iconType) {
|
||||
case item_icon_type::none:
|
||||
break;
|
||||
case item_icon_type::block:
|
||||
batch->render();
|
||||
{
|
||||
GfxContext subctx = ctx.sub();
|
||||
subctx.depthTest(true);
|
||||
subctx.cullFace(true);
|
||||
|
||||
case item_icon_type::block: {
|
||||
Block* cblock = content->requireBlock(item->icon);
|
||||
preview->begin(&subctx.getViewport());
|
||||
preview->draw(cblock, coord.x, coord.y, slotSize, tint);
|
||||
}
|
||||
uiShader->use();
|
||||
batch->begin();
|
||||
batch->texture(previews->getTexture());
|
||||
|
||||
UVRegion region = previews->get(cblock->name);
|
||||
batch->rect(
|
||||
coord.x, coord.y, slotSize, slotSize,
|
||||
0, 0, 0, region, false, true, tint);
|
||||
break;
|
||||
}
|
||||
case item_icon_type::sprite: {
|
||||
size_t index = item->icon.find(':');
|
||||
std::string name = item->icon.substr(index+1);
|
||||
@ -327,12 +322,6 @@ InventoryLayout* InventoryView::getLayout() const {
|
||||
return layout.get();
|
||||
}
|
||||
|
||||
// performance disaster x2
|
||||
void InventoryView::draw(Batch2D* batch, Assets* assets) {
|
||||
Container::draw(batch, assets);
|
||||
Window::clearDepth();
|
||||
}
|
||||
|
||||
void InventoryView::drawBackground(Batch2D* batch, Assets* assets) {
|
||||
glm::vec2 coord = calcCoord();
|
||||
batch->texture(nullptr);
|
||||
|
||||
@ -126,7 +126,6 @@ public:
|
||||
|
||||
void build();
|
||||
|
||||
virtual void draw(Batch2D* batch, Assets* assets) override;
|
||||
virtual void drawBackground(Batch2D* batch, Assets* assets) override;
|
||||
|
||||
void setInventory(std::shared_ptr<Inventory> inventory);
|
||||
@ -138,7 +137,7 @@ public:
|
||||
void setSelected(int index);
|
||||
|
||||
static const int SLOT_INTERVAL = 4;
|
||||
static const int SLOT_SIZE = 48;
|
||||
static const int SLOT_SIZE = ITEM_ICON_SIZE;
|
||||
};
|
||||
|
||||
class InventoryInteraction {
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
#include "../world/Level.h"
|
||||
#include "../assets/Assets.h"
|
||||
#include "../graphics/Atlas.h"
|
||||
#include "BlocksPreview.h"
|
||||
#include "ContentGfxCache.h"
|
||||
|
||||
@ -9,8 +10,7 @@ LevelFrontend::LevelFrontend(Level* level, Assets* assets)
|
||||
: level(level),
|
||||
assets(assets),
|
||||
contentCache(std::make_unique<ContentGfxCache>(level->content, assets)),
|
||||
blocksPreview(std::make_unique<BlocksPreview>(assets, contentCache.get())) {
|
||||
|
||||
blocksAtlas(BlocksPreview::build(contentCache.get(), assets, level->content)) {
|
||||
}
|
||||
|
||||
LevelFrontend::~LevelFrontend() {
|
||||
@ -24,10 +24,10 @@ Assets* LevelFrontend::getAssets() const {
|
||||
return assets;
|
||||
}
|
||||
|
||||
BlocksPreview* LevelFrontend::getBlocksPreview() const {
|
||||
return blocksPreview.get();
|
||||
}
|
||||
|
||||
ContentGfxCache* LevelFrontend::getContentGfxCache() const {
|
||||
return contentCache.get();
|
||||
}
|
||||
|
||||
Atlas* LevelFrontend::getBlocksAtlas() const {
|
||||
return blocksAtlas.get();
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
class Atlas;
|
||||
class Level;
|
||||
class Assets;
|
||||
class BlocksPreview;
|
||||
@ -12,15 +13,15 @@ class LevelFrontend {
|
||||
Level* level;
|
||||
Assets* assets;
|
||||
std::unique_ptr<ContentGfxCache> contentCache;
|
||||
std::unique_ptr<BlocksPreview> blocksPreview;
|
||||
std::unique_ptr<Atlas> blocksAtlas;
|
||||
public:
|
||||
LevelFrontend(Level* level, Assets* assets);
|
||||
~LevelFrontend();
|
||||
|
||||
Level* getLevel() const;
|
||||
Assets* getAssets() const;
|
||||
BlocksPreview* getBlocksPreview() const;
|
||||
ContentGfxCache* getContentGfxCache() const;
|
||||
Atlas* getBlocksAtlas() const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -3,13 +3,15 @@
|
||||
#include <GL/glew.h>
|
||||
#include "Texture.h"
|
||||
|
||||
Framebuffer::Framebuffer(uint width, uint height) : width(width), height(height) {
|
||||
Framebuffer::Framebuffer(uint width, uint height, bool alpha)
|
||||
: width(width), height(height) {
|
||||
glGenFramebuffers(1, &fbo);
|
||||
bind();
|
||||
GLuint tex;
|
||||
glGenTextures(1, &tex);
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
|
||||
GLuint format = alpha ? GL_RGBA : GL_RGB;
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, nullptr);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
|
||||
@ -12,7 +12,7 @@ public:
|
||||
uint width;
|
||||
uint height;
|
||||
Texture* texture;
|
||||
Framebuffer(uint width, uint height);
|
||||
Framebuffer(uint width, uint height, bool alpha=false);
|
||||
~Framebuffer();
|
||||
|
||||
void bind();
|
||||
|
||||
@ -179,6 +179,10 @@ void Window::setBgColor(glm::vec3 color) {
|
||||
glClearColor(color.r, color.g, color.b, 1.0f);
|
||||
}
|
||||
|
||||
void Window::setBgColor(glm::vec4 color) {
|
||||
glClearColor(color.r, color.g, color.b, color.a);
|
||||
}
|
||||
|
||||
void Window::viewport(int x, int y, int width, int height){
|
||||
glViewport(x, y, width, height);
|
||||
}
|
||||
|
||||
@ -48,6 +48,7 @@ public:
|
||||
static void clear();
|
||||
static void clearDepth();
|
||||
static void setBgColor(glm::vec3 color);
|
||||
static void setBgColor(glm::vec4 color);
|
||||
static double time();
|
||||
static const char* getClipboardText();
|
||||
static DisplaySettings* getSettings();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user