如何渲染到OpenGL Cubemap纹理的一个面

时间:2013-01-15 13:18:54

标签: opengl framebuffer rendertarget

我正在制作一个Cube地图渲染目标类。 我已经有一个工作正常的2D。但是对于这个立方体贴图渲染目标类,我希望能够根据需要渲染到特定的面。也许我想在“X +”面上渲染圆圈,但在“Y +”面上渲染三角形。

这就是我班级的样子

class CRenderTarget 
{
private:
    //frame buffer to render on
    unsigned  m_uifboHandle;

    //depth stencil 
    unsigned  m_uiDepthStencilHandle;

    //the texture we will render on
    unsigned  m_uiTextureHandle;

public:
    CCubeRenderTarget( void );

    ~CCubeRenderTarget( void );

    void Create( unsigned uiHeightWidth = 512,
             int iInternalFormat = 0x1908 ); //GL_RGBA

    void Bind( void );

    void UnBind( void );
}

这就是我的创建功能的样子

void Create( unsigned uiHeightWidth, int iInternalFormat )
{
        //generate our variables
    glGenFramebuffers(1, &m_uifboHandle);
    glGenRenderbuffers(1, &m_uiDepthStencilHandle);
    glGenTextures(1, &m_uiTextureHandle);

    // Initialize FBO
    glBindFramebuffer( GL_FRAMEBUFFER, m_uifboHandle);

    glBindTexture( GL_TEXTURE_CUBE_MAP, m_uiTextureHandle);

    // Pre-allocate the correct sized cube map
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, iInternalFormat, uiHeightWidth, uiHeightWidth, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, iInternalFormat, uiHeightWidth, uiHeightWidth, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, iInternalFormat, uiHeightWidth, uiHeightWidth, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, iInternalFormat, uiHeightWidth, uiHeightWidth, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, iInternalFormat, uiHeightWidth, uiHeightWidth, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, iInternalFormat, uiHeightWidth, uiHeightWidth, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

    // Must attach texture to framebuffer. Has Stencil and depth
    glBindRenderbuffer(GL_RENDERBUFFER, m_uiDepthStencilHandle);
    glRenderbufferStorage(GL_RENDERBUFFER, /*GL_DEPTH_STENCIL*/GL_DEPTH24_STENCIL8, uiHeightWidth, uiHeightWidth );
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_uiDepthStencilHandle);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_uiDepthStencilHandle);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

这是我的绑定函数的样子

void Bind( void )
{
    glBindFramebuffer( GL_FRAMEBUFFER, m_uifboHandle );
}

//这是我的unbind函数的样子

void Unbind( void )
{
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );
}

过去一周我完成了大量的研究。我似乎无法找到一种方式,我可以只是一些如何而不是绑定我的整个立方体地图只是绑定我的立方体地图的1面。这是我的总体目标。我知道在DirectX中你只需要抓住6个面并手动处理它们,但我如何在openGL立方体贴图上手动处理每个面?

1 个答案:

答案 0 :(得分:4)

绑定帧缓冲区后,需要指定应该定位的纹理级别。它应该是这样的:

glBindFramebuffer( GL_FRAMEBUFFER, m_uifboHandle );
glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X, m_uiTextureHandle, 0);