Files
XCEngine/docs/api/rhi/opengl/opengl-render-target-view.md

2.4 KiB

OpenGLRenderTargetView

OpenGL 渲染目标视图实现,使用 framebuffer object (FBO)。

头文件

#include <XCEngine/RHI/OpenGL/OpenGLRenderTargetView.h>

枚举

RenderTargetType

描述
Texture2D 2D 纹理
Texture2DArray 2D 纹理数组
Texture3D 3D 纹理
TextureCube 立方体贴图
TextureCubeArray 立方体贴图数组

OpenGLRenderTargetViewDesc

struct OpenGLRenderTargetViewDesc {
    RenderTargetType type = RenderTargetType::Texture2D;
    int mipLevel = 0;
    int baseArraySlice = 0;
    int arraySize = 1;
    int layer = 0;
    uint32_t format = 0;
};

公共成员函数

OpenGLRenderTargetView()

~OpenGLRenderTargetView()

bool Initialize(unsigned int texture, const OpenGLRenderTargetViewDesc& desc)

bool Initialize(unsigned int texture, int mipLevel = 0)

bool InitializeCubemap(unsigned int cubemap, int face, int mipLevel = 0)

void Shutdown()

void Bind(unsigned int slot = 0)

void Bind(unsigned int count, const unsigned int* framebuffers, const int* drawBuffers)

void Unbind()

void Clear(float r, float g, float b, float a)

void Clear(float r, float g, float b, float a, float depth, uint8_t stencil)

unsigned int GetFramebuffer() const

unsigned int GetTexture() const

int GetMipLevel() const

int GetWidth() const

int GetHeight() const

static void BindFramebuffer(unsigned int framebuffer)

static void UnbindFramebuffer()

内部成员

成员 类型 描述
m_texture unsigned int GL texture ID
m_framebuffer unsigned int GL framebuffer ID
m_mipLevel int Mip 级别
m_width/height int 尺寸
m_type RenderTargetType 类型
m_framebuffers vector<unsigned int> 额外 framebuffer 列表

使用示例

// Create framebuffer with color attachment
OpenGLRenderTargetView rtv;
rtv.Initialize(texture.GetID());

rtv.Bind();
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
rtv.Unbind();

// MRT
OpenGLRenderTargetView rtvs[2];
rtvs[0].Initialize(texture0.GetID());
rtvs[1].Initialize(texture1.GetID());

unsigned int fbs[] = { rtvs[0].GetFramebuffer(), rtvs[1].GetFramebuffer() };
int draws[] = { 0, 1 };
rtvs[0].Bind(2, fbs, draws);
glClear(GL_COLOR_BUFFER_BIT);