Common: Optionally create framebuffer with GL textures

This commit is contained in:
Connor McLaughlin 2019-10-23 00:35:08 +10:00
parent c97089b775
commit 9410685807
2 changed files with 27 additions and 2 deletions

View File

@ -1,11 +1,12 @@
#include "gl_texture.h"
#include "YBaseLib/Assert.h"
#include "YBaseLib/Log.h"
Log_SetChannel(GL);
namespace GL {
Texture::Texture(u32 width, u32 height, GLenum format, GLenum type, const void* data /* = nullptr */,
bool linear_filter /* = false */)
bool linear_filter /* = false */, bool create_framebuffer /* = false */)
: m_width(width), m_height(height)
{
glGenTextures(1, &m_id);
@ -14,10 +15,21 @@ Texture::Texture(u32 width, u32 height, GLenum format, GLenum type, const void*
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear_filter ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear_filter ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
if (create_framebuffer)
{
glGenFramebuffers(1, &m_fbo_id);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo_id);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_id, 0);
Assert(glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
}
}
Texture::~Texture()
{
if (m_fbo_id != 0)
glDeleteFramebuffers(1, &m_fbo_id);
glDeleteTextures(1, &m_id);
}
@ -26,6 +38,12 @@ void Texture::Bind()
glBindTexture(GL_TEXTURE_2D, m_id);
}
void Texture::BindFramebuffer(GLenum target /*= GL_DRAW_FRAMEBUFFER*/)
{
DebugAssert(m_fbo_id != 0);
glBindFramebuffer(target, m_fbo_id);
}
void Texture::Unbind()
{
glBindTexture(GL_TEXTURE_2D, 0);

View File

@ -6,20 +6,27 @@ namespace GL {
class Texture
{
public:
Texture(u32 width, u32 height, GLenum format, GLenum type, const void* data = nullptr, bool linear_filter = false);
Texture(u32 width, u32 height, GLenum format, GLenum type, const void* data = nullptr, bool linear_filter = false,
bool create_framebuffer = false);
~Texture();
GLuint GetGLId() const { return m_id; }
u32 GetWidth() const { return m_width; }
u32 GetHeight() const { return m_height; }
GLuint GetGLFramebufferID() const { return m_fbo_id; }
void Bind();
void BindFramebuffer(GLenum target = GL_DRAW_FRAMEBUFFER);
static void Unbind();
private:
GLuint m_id;
u32 m_width;
u32 m_height;
GLuint m_fbo_id = 0;
};
} // namespace GL