started working with MRT

This commit is contained in:
2026-02-20 15:58:51 +01:00
parent 55dce5776a
commit 45501f153d
13 changed files with 746 additions and 36 deletions

View File

@@ -1,6 +1,6 @@
#include "RenderTexture.hpp"
TSE::GLFW::RenderTexture::RenderTexture(Vector2 v) : buffer(v)
TSE::GLFW::RenderTexture::RenderTexture(Vector2 v, uint textureCount) : buffer(v, textureCount)
{
buffer.AddResizeNotifiable(this);
}
@@ -30,6 +30,11 @@ TSE::uint TSE::GLFW::RenderTexture::GetTextureId() const
return buffer.GetTextureId();
}
TSE::uint TSE::GLFW::RenderTexture::GetTextureId(uint id) const
{
return buffer.GetTextureId(id);
}
void TSE::GLFW::RenderTexture::Update()
{
buffer.Update();

View File

@@ -13,13 +13,14 @@ namespace TSE::GLFW
public:
FrameBuffer buffer;
RenderTexture(Vector2 v);
RenderTexture(Vector2 v, uint textureCount = 1);
Vector2 size() const override;
void SetSize(Vector2 v) override;
float Width() const override;
float Height() const override;
uint GetTextureId() const override;
uint GetTextureId(uint id) const override;
void Update() override;
void Bind() override;

View File

@@ -8,9 +8,9 @@ namespace TSE::GLFW
class RenderTextureCreatorOpenGL : public IRenderTextureCreator
{
public:
inline IRenderTexture* CreateTextureHeap(Vector2 v) override
inline IRenderTexture* CreateTextureHeap(Vector2 v, uint textureCount = 1) override
{
return new RenderTexture(v);
return new RenderTexture(v, textureCount);
};
};
} // namespace name

View File

@@ -1,8 +1,9 @@
#include "FrameBuffer.hpp"
#include "Debug.hpp"
TSE::GLFW::FrameBuffer::FrameBuffer(const Vector2 &size)
TSE::GLFW::FrameBuffer::FrameBuffer(const Vector2 &size, uint textureCount)
{
textureOutputCount = textureCount;
width = size.x;
height = size.y;
CreateFBTexture();
@@ -26,7 +27,10 @@ void TSE::GLFW::FrameBuffer::Unbind()
TSE::GLFW::FrameBuffer::~FrameBuffer()
{
glDeleteFramebuffers(1,&bufferID);
glDeleteTextures(1, &textureID);
for (int i = 0; i < textureOutputCount; i++)
{
glDeleteTextures(1, &(textureIDs[i]));
}
glDeleteRenderbuffers(1, &depthRboID);
}
@@ -48,9 +52,9 @@ void TSE::GLFW::FrameBuffer::Update()
}
}
TSE::uint TSE::GLFW::FrameBuffer::GetTextureId() const
TSE::uint TSE::GLFW::FrameBuffer::GetTextureId(uint id) const
{
return textureID;
return textureIDs[id];
}
TSE::Vector2 TSE::GLFW::FrameBuffer::GetSize() const
@@ -62,9 +66,17 @@ void TSE::GLFW::FrameBuffer::Initialize()
{
glGenFramebuffers(1, &bufferID);
Bind();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
uint bufs[32];
for (int i = 0; i < textureOutputCount; i++)
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textureIDs[i], 0);
bufs[i] = GL_COLOR_ATTACHMENT0 + i;
}
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRboID);
if(textureOutputCount > 1)
glDrawBuffers(textureOutputCount, bufs);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
TSE_ERROR("Failed to create OpenGL FBO.");
@@ -75,24 +87,30 @@ void TSE::GLFW::FrameBuffer::Initialize()
void TSE::GLFW::FrameBuffer::LoadFBTexture()
{
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
for (int i = 0; i < textureOutputCount; i++)
{
glBindTexture(GL_TEXTURE_2D, textureIDs[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindRenderbuffer(GL_RENDERBUFFER, depthRboID);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, depthRboID);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
}
}
void TSE::GLFW::FrameBuffer::CreateFBTexture()
{
glViewport(0,0, width, height);
//resize
if(textureID == 0)
glGenTextures(1, &textureID);
for (int i = 0; i < textureOutputCount; i++)
{
if(textureIDs[i] == 0)
glGenTextures(1, &(textureIDs[i]));
}
if(depthRboID == 0)
glGenRenderbuffers(1, &depthRboID);
LoadFBTexture();

View File

@@ -9,18 +9,19 @@ namespace TSE::GLFW
class FrameBuffer : public buffer, public IResizable
{
private:
uint textureID = 0;
uint textureOutputCount = 1;
uint textureIDs[32] = {0};
uint depthRboID = 0;
bool shouldResize = false;
public:
FrameBuffer(const Vector2& size);
FrameBuffer(const Vector2& size, uint textureCount = 1);
void Bind() override;
void Unbind() override;
~FrameBuffer() override;
void Resize(Vector2 size);
void Update();
uint GetTextureId() const;
uint GetTextureId(uint id = 0) const;
Vector2 GetSize() const;
private:

View File

@@ -171,7 +171,7 @@ void TSE::GLFW::BasicTileMapShader::OnSubmit(const Transformable &t, float *&tar
SpriteCount = tileSet->GetCount();
SpriteScale = tm->SpriteScale;
int chunkcount = tm->GetChunkCount();
const int chunkcount = tm->GetChunkCount();
Vector2 orderedChunks[chunkcount];
tm->GetChunkPositionsInOrder(orderedChunks);
@@ -182,7 +182,7 @@ void TSE::GLFW::BasicTileMapShader::OnSubmit(const Transformable &t, float *&tar
for(auto chunkPos : orderedChunks)
{
auto chunk = tm->GetChunk(chunkPos);
int spriteCount = chunk->GetSpriteCount();
const int spriteCount = chunk->GetSpriteCount();
Vector2 spritePositions[spriteCount];
int spriteIds[spriteCount];
chunk->GetOrderedPositions(spritePositions);
@@ -208,5 +208,6 @@ void TSE::GLFW::BasicTileMapShader::OnSubmit(const Transformable &t, float *&tar
}
}
stack.Pop();
restartDrawcall(rnd);
}