inventory render optimized
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user