From b5d48aa4c7698a84e1e90326b781856c7443dd68 Mon Sep 17 00:00:00 2001 From: eduardodoria Date: Sun, 6 Oct 2024 11:00:19 -0300 Subject: [PATCH] Created global texture framebuffer --- engine/core/Engine.cpp | 24 +++++++++++++++++ engine/core/Engine.h | 6 +++++ engine/core/script/binding/CoreClassesLua.cpp | 1 + engine/core/subsystem/RenderSystem.cpp | 26 ++++++++++++------- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/engine/core/Engine.cpp b/engine/core/Engine.cpp index f3c84631..7017a29e 100644 --- a/engine/core/Engine.cpp +++ b/engine/core/Engine.cpp @@ -62,6 +62,8 @@ bool Engine::showCursor = true; thread_local bool Engine::asyncThread = false; Semaphore Engine::drawSemaphore; +Framebuffer* Engine::framebuffer = nullptr; + //-----Supernova user events----- FunctionSubscribe Engine::onViewLoaded; FunctionSubscribe Engine::onViewChanged; @@ -409,6 +411,14 @@ bool Engine::isViewLoaded(){ return viewLoaded; } +void Engine::setFramebuffer(Framebuffer* framebuffer){ + Engine::framebuffer = framebuffer; +} + +Framebuffer* Engine::getFramebuffer(){ + return Engine::framebuffer; +} + void Engine::calculateCanvas(){ Engine::canvasWidth = preferredCanvasWidth; Engine::canvasHeight = preferredCanvasHeight; @@ -473,6 +483,10 @@ void Engine::systemViewLoaded(){ viewLoaded = true; onViewLoaded.call(); + + if (framebuffer){ + framebuffer->create(); + } for (int i = 0; i < scenes.size(); i++){ scenes[i]->load(); @@ -530,6 +544,16 @@ void Engine::systemViewChanged(){ viewRect.setRect(viewX, viewY, viewWidth, viewHeight); + if (framebuffer){ + framebuffer->setWidth(screenWidth); + framebuffer->setHeight(screenHeight); + + if (framebuffer->isCreated()){ + framebuffer->destroy(); + framebuffer->create(); + } + } + for (int i = 0; i < scenes.size(); i++){ scenes[i]->updateSizeFromCamera(); } diff --git a/engine/core/Engine.h b/engine/core/Engine.h index 9f8e0d6a..c631444a 100644 --- a/engine/core/Engine.h +++ b/engine/core/Engine.h @@ -57,6 +57,7 @@ #include "util/FunctionSubscribe.h" #include "math/Rect.h" #include "util/ThreadUtils.h" +#include "texture/Framebuffer.h" #include void init(); @@ -155,6 +156,8 @@ namespace Supernova { thread_local static bool asyncThread; static Semaphore drawSemaphore; + + static Framebuffer* framebuffer; static bool transformCoordPos(float& x, float& y); static void calculateCanvas(); @@ -237,6 +240,9 @@ namespace Supernova { static bool isAsyncThread(); static bool isViewLoaded(); + static void setFramebuffer(Framebuffer* framebuffer); + static Framebuffer* getFramebuffer(); + //-----Supernova API functions----- static void systemInit(int argc, char* argv[]); static void systemViewLoaded(); diff --git a/engine/core/script/binding/CoreClassesLua.cpp b/engine/core/script/binding/CoreClassesLua.cpp index 7511c638..c17ae815 100644 --- a/engine/core/script/binding/CoreClassesLua.cpp +++ b/engine/core/script/binding/CoreClassesLua.cpp @@ -187,6 +187,7 @@ void LuaBinding::registerCoreClasses(lua_State *L){ .addStaticFunction("endAsyncThread", &Engine::endAsyncThread) .addStaticFunction("isAsyncThread", &Engine::isAsyncThread) .addStaticFunction("isViewLoaded", &Engine::isViewLoaded) + .addStaticProperty("framebuffer", &Engine::getFramebuffer, &Engine::setFramebuffer) .addStaticProperty("onViewLoaded", [] () { return &Engine::onViewLoaded; }, [] (lua_State* L) { Engine::onViewLoaded = L; }) .addStaticProperty("onCanvasChanged", [] () { return &Engine::onViewChanged; }, [] (lua_State* L) { Engine::onViewChanged = L; }) diff --git a/engine/core/subsystem/RenderSystem.cpp b/engine/core/subsystem/RenderSystem.cpp index eb87ef42..b71eb975 100644 --- a/engine/core/subsystem/RenderSystem.cpp +++ b/engine/core/subsystem/RenderSystem.cpp @@ -123,9 +123,8 @@ void RenderSystem::createFramebuffer(CameraComponent& camera){ void RenderSystem::updateFramebuffer(CameraComponent& camera){ if (camera.framebuffer->isCreated()){ camera.framebuffer->destroy(); + createFramebuffer(camera); } - - createFramebuffer(camera); } void RenderSystem::createEmptyTextures(){ @@ -2571,7 +2570,7 @@ void RenderSystem::update(double dt){ pipelines |= PIP_DEFAULT; } - if (camera.renderToTexture){ + if (camera.renderToTexture || Engine::getFramebuffer()){ pipelines |= PIP_RTT; } @@ -2860,7 +2859,14 @@ void RenderSystem::draw(){ } if (!camera.renderToTexture){ - camera.render.startFrameBuffer(); + if (Engine::getFramebuffer()){ + if (!Engine::getFramebuffer()->isCreated()){ + Engine::getFramebuffer()->create(); + } + camera.render.startFrameBuffer(&Engine::getFramebuffer()->getRender()); + }else{ + camera.render.startFrameBuffer(); + } camera.render.applyViewport(Engine::getViewRect()); }else{ if (!camera.framebuffer->isCreated()){ @@ -2881,7 +2887,7 @@ void RenderSystem::draw(){ updateSkyViewProjection(sky, camera); } - drawSky(sky, camera.renderToTexture); + drawSky(sky, camera.renderToTexture || Engine::getFramebuffer()); } for (int i = 0; i < transforms->size(); i++){ @@ -2954,7 +2960,7 @@ void RenderSystem::draw(){ if (!mesh.transparent || !camera.transparentSort){ //Draw opaque meshes if transparency is not necessary - drawMesh(mesh, transform, camera, cameraTransform, camera.renderToTexture, instmesh, terrain); + drawMesh(mesh, transform, camera, cameraTransform, camera.renderToTexture || Engine::getFramebuffer(), instmesh, terrain); }else{ transparentMeshes.push({&mesh, instmesh, terrain, &transform, transform.distanceToCamera}); } @@ -2968,7 +2974,7 @@ void RenderSystem::draw(){ isText = true; } if (transform.visible) - drawUI(ui, transform, camera.renderToTexture); + drawUI(ui, transform, camera.renderToTexture || Engine::getFramebuffer()); }else if (signature.test(scene->getComponentType())){ PointsComponent& points = scene->getComponent(entity); @@ -2980,13 +2986,13 @@ void RenderSystem::draw(){ } if (transform.visible) - drawPoints(points, transform, cameraTransform, camera.renderToTexture); + drawPoints(points, transform, cameraTransform, camera.renderToTexture || Engine::getFramebuffer()); }else if (signature.test(scene->getComponentType())){ LinesComponent& lines = scene->getComponent(entity); if (transform.visible) - drawLines(lines, transform, cameraTransform, camera.renderToTexture); + drawLines(lines, transform, cameraTransform, camera.renderToTexture || Engine::getFramebuffer()); } @@ -3005,7 +3011,7 @@ void RenderSystem::draw(){ TransparentMeshesData meshData = transparentMeshes.top(); //Draw transparent meshes - drawMesh(*meshData.mesh, *meshData.transform, camera, cameraTransform, camera.renderToTexture, meshData.instmesh, meshData.terrain); + drawMesh(*meshData.mesh, *meshData.transform, camera, cameraTransform, camera.renderToTexture || Engine::getFramebuffer(), meshData.instmesh, meshData.terrain); transparentMeshes.pop(); }