基础结构¶
struct FramebufferSpecification
{
uint32_t Width, Height;
// FramebufferFormat Format =
uint32_t Samples = 1;
bool SwapChainTarget = false;
};
class Framebuffer
{
public:
virtual void Bind() = 0;
virtual void Unbind() = 0;
virtual uint32_t GetColorAttachmentRendererID() const = 0;
virtual const FramebufferSpecification& GetSpecification() const = 0;
static Ref<Framebuffer> Create(const FramebufferSpecification& spec);
};
OpenGL结构¶
class OpenGLFramebuffer : public Framebuffer
{
public:
OpenGLFramebuffer(const FramebufferSpecification& spec);
virtual ~OpenGLFramebuffer();
void Invalidate(); //作废,重新创建帧缓冲
virtual void Bind() override;
virtual void Unbind() override;
virtual uint32_t GetColorAttachmentRendererID() const override { return m_ColorAttachment; }
virtual const FramebufferSpecification& GetSpecification() const override { return m_Specification; }
private:
uint32_t m_RendererID;
uint32_t m_ColorAttachment, m_DepthAttachment;
FramebufferSpecification m_Specification;
};
创建细节¶
帧缓冲->颜色附件->深度/模板附件->颜色附件纹理参数
void OpenGLFramebuffer::Invalidate()
{
glCreateFramebuffers(1, &m_RendererID); //创建绑定帧缓冲
glBindFramebuffer(GL_FRAMEBUFFER, m_RendererID);
glCreateTextures(GL_TEXTURE_2D, 1, &m_ColorAttachment); //创建颜色附件纹理
glBindTexture(GL_TEXTURE_2D, m_ColorAttachment);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Specification.Width, m_Specification.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);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_ColorAttachment, 0); //帧缓冲纹理
//创建深度-模板附件纹理
glCreateTextures(GL_TEXTURE_2D, 1, &m_DepthAttachment);
glBindTexture(GL_TEXTURE_2D, m_DepthAttachment);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH24_STENCIL8, m_Specification.Width, m_Specification.Height);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, m_Specification.Width, m_Specification.Height, 0,
// GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, m_DepthAttachment, 0);
//帧缓冲完整性检查
CORE_DEBUG_ASSERT(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, "Framebuffer is incomplete!");
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}