From d655404b7a88fddbb74dc002f8b1650581273d14 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 1 Dec 2023 13:56:52 +0300 Subject: [PATCH] Added missing files --- src/frontend/BlocksPreview.cpp | 71 ++++++++++++++++++++++++++++++++++ src/frontend/BlocksPreview.h | 28 ++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 src/frontend/BlocksPreview.cpp create mode 100644 src/frontend/BlocksPreview.h diff --git a/src/frontend/BlocksPreview.cpp b/src/frontend/BlocksPreview.cpp new file mode 100644 index 00000000..c3ca5056 --- /dev/null +++ b/src/frontend/BlocksPreview.cpp @@ -0,0 +1,71 @@ +#include "BlocksPreview.h" + +#include + +#include "../graphics/Shader.h" +#include "../graphics/Texture.h" +#include "../graphics/Atlas.h" +#include "../graphics/Batch3D.h" +#include "../window/Camera.h" +#include "../voxels/Block.h" +#include "../window/Window.h" +#include "ContentGfxCache.h" + +using glm::vec4; +using glm::vec3; + +BlocksPreview::BlocksPreview(Shader* shader, + Atlas* atlas, + const ContentGfxCache* cache) + : shader(shader), atlas(atlas), cache(cache) { + batch = new Batch3D(1024); + camera = new Camera(vec3(0.0f), 1.0f); +} + +BlocksPreview::~BlocksPreview() { + delete camera; + delete batch; +} + +void BlocksPreview::begin() { + shader->use(); + shader->uniformMatrix("u_projview", + glm::ortho(0.0f, + float(Window::width), + 0.0f, + float(Window::height), -1000.0f, 1000.0f) * + glm::lookAt(vec3(2, 2, 2), vec3(0.0f), 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, vec4 tint) { + y = Window::height - y - 1; + x += 2; + y -= 35; + shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), vec3(x/float(Window::width) * 2, y/float(Window::height) * 2, 0.0f))); + 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)}; + + switch (def->model) { + case BlockModel::none: + // something went wrong... + break; + case BlockModel::block: + batch->blockCube(vec3(size * 0.63f), texfaces, tint, !def->rt.emissive); + break; + case BlockModel::aabb: + batch->blockCube(def->hitbox.size() * vec3(size * 0.63f), texfaces, tint, !def->rt.emissive); + break; + case BlockModel::xsprite: { + //batch->xSprite(size, size, texfaces[0], tint, !def->rt.emissive); + vec3 right = glm::normalize(vec3(1.f, 0.f, -1.f)); + batch->sprite(right*float(size)*0.43f+vec3(0, size*0.4f, 0), vec3(0.f, 1.f, 0.f), right, size*0.5f, size*0.6f, texfaces[0], tint); + break; + } + } + + batch->flush(); +} diff --git a/src/frontend/BlocksPreview.h b/src/frontend/BlocksPreview.h new file mode 100644 index 00000000..d48e6ca4 --- /dev/null +++ b/src/frontend/BlocksPreview.h @@ -0,0 +1,28 @@ +#ifndef FRONTEND_BLOCKS_PREVIEW_H_ +#define FRONTEND_BLOCKS_PREVIEW_H_ + +#include "../typedefs.h" +#include + +class Shader; +class Atlas; +class Batch3D; +class Camera; +class Block; +class ContentGfxCache; + +class BlocksPreview { + Shader* shader; + Atlas* atlas; + Batch3D* batch; + Camera* camera; + const ContentGfxCache* const cache; +public: + BlocksPreview(Shader* shader, Atlas* atlas, const ContentGfxCache* cache); + ~BlocksPreview(); + + void begin(); + void draw(const Block* block, int x, int y, int size, glm::vec4 tint); +}; + +#endif // FRONTEND_BLOCKS_PREVIEW_H_ \ No newline at end of file