Skip to content

Commit

Permalink
Created global texture framebuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Oct 6, 2024
1 parent 7d09f0c commit b5d48aa
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
24 changes: 24 additions & 0 deletions engine/core/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void()> Engine::onViewLoaded;
FunctionSubscribe<void()> Engine::onViewChanged;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
Expand Down
6 changes: 6 additions & 0 deletions engine/core/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include "util/FunctionSubscribe.h"
#include "math/Rect.h"
#include "util/ThreadUtils.h"
#include "texture/Framebuffer.h"
#include <atomic>

void init();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions engine/core/script/binding/CoreClassesLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; })
Expand Down
26 changes: 16 additions & 10 deletions engine/core/subsystem/RenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down Expand Up @@ -2571,7 +2570,7 @@ void RenderSystem::update(double dt){
pipelines |= PIP_DEFAULT;
}

if (camera.renderToTexture){
if (camera.renderToTexture || Engine::getFramebuffer()){
pipelines |= PIP_RTT;
}

Expand Down Expand Up @@ -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()){
Expand All @@ -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++){
Expand Down Expand Up @@ -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});
}
Expand All @@ -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>())){
PointsComponent& points = scene->getComponent<PointsComponent>(entity);
Expand All @@ -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>())){
LinesComponent& lines = scene->getComponent<LinesComponent>(entity);

if (transform.visible)
drawLines(lines, transform, cameraTransform, camera.renderToTexture);
drawLines(lines, transform, cameraTransform, camera.renderToTexture || Engine::getFramebuffer());

}

Expand All @@ -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();
}
Expand Down

0 comments on commit b5d48aa

Please sign in to comment.