This is the main program I'm trying to execute:
FrameBuffer* buffer = new FrameBuffer(960, 540);
buffer->Bind();
// render some textures
buffer->Unbind();
glViewport(0, 0, 960, 540);
Texture* texture = buffer->GetTexture();
// render the texture
However instead of the textures I see a white rectangle. If I render on a screen, I get the expected results (so there should be no problem with the rendering code). Here is the FrameBuffer class:
class FrameBuffer
{
private:
Texture* m_Texture;
unsigned int m_FrameBufferID;
unsigned int m_DepthBufferID;
unsigned int m_Width;
unsigned int m_Height;
Vector4f m_ClearColor;
public:
FrameBuffer(unsigned int width, unsigned int height)
: m_Width(width), m_Height(height), m_ClearColor(Maths::Vector4f(0, 0, 0, 0))
{
Create(m_Width, m_Height);
}
~FrameBuffer()
{
glDeleteFramebuffers(1, &m_FrameBufferID);
}
void Bind() const
{
glBindFramebuffer(GL_FRAMEBUFFER, m_FrameBufferID);
glViewport(0, 0, m_Width, m_Height);
}
void Unbind() const
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Clear() const
{
glClearColor(m_ClearColor.x, m_ClearColor.y, m_ClearColor.z, m_ClearColor.w);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
inline const Texture* GetTexture() const { return m_Texture; }
private:
void Create(unsigned int width, unsigned int height)
{
glGenFramebuffers(1, &m_FrameBufferID);
glGenRenderbuffers(1, &m_DepthBufferID);
Texture::SetFilterMode(TextureFilter::Linear);
m_Texture = new Texture(width, height, 32); // This creates a 32 bit texture (RGBA) with GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER set to linear
glBindFramebuffer(GL_FRAMEBUFFER, m_FrameBufferID);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_Texture->GetTextureID(), 0);
glBindRenderbuffer(GL_RENDERBUFFER, m_DepthBufferID);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_DepthBufferID);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
};
Everything else is tested, so the problem should be in the main program's logic or in the FrameBuffer.
By the way the viewport size iz exactly the same as the window size.