Dawn/src/dawn/scene/components/display/SimpleRenderTargetQuad.cpp

65 lines
1.7 KiB
C++

// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SimpleRenderTargetQuad.hpp"
using namespace Dawn;
SimpleRenderTargetQuad::SimpleRenderTargetQuad(SceneItem *i) :
SceneItemComponent(i),
renderTarget(nullptr)
{
}
std::vector<std::shared_ptr<SceneItemComponent>> SimpleRenderTargetQuad::getDependencies() {
return {
(this->meshHost = this->item->getComponent<MeshHost>())
};
}
void SimpleRenderTargetQuad::onStart() {
assertNotNull(this->meshHost, "SimpleRenderTargetQuad::onStart: MeshHost cannot be null");
// Create quad mesh
this->meshHost->mesh.createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
// Create teardown effect and keep track.
auto initial = useEffectWithTeardown([&]{
// No teardown needed.
if(this->renderTarget == nullptr) return evtResized = [&]{ };
// Update mesh
QuadMesh::bufferQuadMesh(
&this->meshHost->mesh,
glm::vec2(0, 0), glm::vec2(0, 0),
glm::vec2(
((RenderTarget*)this->renderTarget)->getWidth(),
((RenderTarget*)this->renderTarget)->getHeight()
),
glm::vec2(1, 1),
0, 0
);
// Subscribe to resize event.
evtResized = useEvent([&](
RenderTarget &target,
const float_t w,
const float_t h
) {
QuadMesh::bufferQuadMesh(
&this->meshHost->mesh,
glm::vec2(0, 0), glm::vec2(0, 0),
glm::vec2(w, h), glm::vec2(1, 1),
0, 0
);
}, this->renderTarget->eventRenderTargetResized);
// Teardown
return evtResized;
}, this->renderTarget);
// Invoke immediate
initial();
}