test rendering ui to texture

This commit is contained in:
MihailRis 2026-02-03 23:13:07 +03:00
parent 5d5a0e8932
commit 566aa04cda
6 changed files with 80 additions and 1 deletions

View file

@ -126,6 +126,8 @@ function on_hud_open()
configure_SSAO()
hud.default_hand_controller = update_hand
gui.root.root:add("<frame size='16' color='#FF0000'><label>Hello, World</label></frame>")
end
function on_hud_render()

View file

@ -89,6 +89,10 @@ Texture* Framebuffer::getTexture() const {
return texture.get();
}
std::shared_ptr<Texture> Framebuffer::getSharedTexture() const {
return texture;
}
uint Framebuffer::getWidth() const {
return width;
}

View file

@ -13,7 +13,7 @@ class Framebuffer : public Bindable {
uint width;
uint height;
uint format;
std::unique_ptr<Texture> texture;
std::shared_ptr<Texture> texture;
public:
Framebuffer(uint fbo, uint depth, std::unique_ptr<Texture> texture);
Framebuffer(uint width, uint height, bool alpha=false);
@ -33,6 +33,8 @@ public:
/// @brief Get framebuffer color attachment
Texture* getTexture() const;
std::shared_ptr<Texture> getSharedTexture() const;
/// @brief Get framebuffer width
uint getWidth() const;
/// @brief Get framebuffer height

View file

@ -0,0 +1,42 @@
#include "Frame.hpp"
#include "assets/Assets.hpp"
#include "graphics/core/Batch2D.hpp"
#include "graphics/core/Framebuffer.hpp"
#include "graphics/core/DrawContext.hpp"
#include "graphics/core/Texture.hpp"
#include "window/Window.hpp"
#include "../GUI.hpp"
using namespace gui;
static inline constexpr int MAX_TEXTURE_SIZE = 2048;
gui::Frame::Frame(GUI& gui) : Container(gui, {}), fbo(nullptr) {}
gui::Frame::~Frame() = default;
void gui::Frame::draw(const DrawContext& pctx, const Assets& assets) {
glm::ivec2 size = getSize();
if (size.x <= 0 || size.y <= 0 ||
size.x > MAX_TEXTURE_SIZE || size.y > MAX_TEXTURE_SIZE) {
return;
}
auto& nonConstASsets = const_cast<Assets&>(assets);
if (fbo && (fbo->getWidth() != size.x || fbo->getHeight() != size.y)) {
fbo->resize(size.x, size.y);
nonConstASsets.store(fbo->getSharedTexture(), texture);
} else if (fbo == nullptr) {
fbo = std::make_unique<Framebuffer>(size.x, size.y, true);
nonConstASsets.store(fbo->getSharedTexture(), texture);
}
// ui uses flipped camera with matrix based on main viewport
setPos({0, pctx.getViewport().y - size.y});
auto ctx = pctx.sub();
ctx.setFramebuffer(fbo.get());
display::clear();
Container::draw(ctx, assets);
ctx.getBatch2D()->flush();
}

View file

@ -0,0 +1,18 @@
#pragma once
#include "Container.hpp"
class Framebuffer;
namespace gui {
class Frame final : public Container {
public:
explicit Frame(GUI& gui);
virtual ~Frame();
void draw(const DrawContext& pctx, const Assets& assets) override;
private:
std::unique_ptr<Framebuffer> fbo;
std::string texture = "gui/no_icon";
};
}

View file

@ -8,6 +8,7 @@
#include "elements/Button.hpp"
#include "elements/Canvas.hpp"
#include "elements/CheckBox.hpp"
#include "elements/Frame.hpp"
#include "elements/Image.hpp"
#include "elements/InlineFrame.hpp"
#include "elements/InputBindBox.hpp"
@ -850,6 +851,15 @@ static std::shared_ptr<UINode> read_page_box(
return menu;
}
static std::shared_ptr<UINode> read_frame(
UiXmlReader& reader, const xml::xmlelement& element
) {
auto& gui = reader.getGUI();
auto frame = std::make_shared<Frame>(gui);
read_container_impl(reader, element, *frame, true);
return frame;
}
static std::shared_ptr<UINode> read_iframe(
UiXmlReader& reader, const xml::xmlelement& element
) {
@ -867,6 +877,7 @@ UiXmlReader::UiXmlReader(gui::GUI& gui, scriptenv&& env)
contextStack.emplace("");
add("image", read_image);
add("canvas", read_canvas);
add("frame", read_frame);
add("iframe", read_iframe);
add("label", read_label);
add("panel", read_panel);