【问题标题】:How Can I Render To One Face Of An OpenGL Cubemap texture如何渲染到 OpenGL Cubemap 纹理的一个面
【发布时间】:2012-12-29 14:32:03
【问题描述】:

我正在制作一个立方体贴图渲染目标类。 我已经有一个可以正常工作的 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 );
}

//这就是我的解绑函数的样子

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

过去一周我做了很多研究。我似乎无法找到一种方法,我可以通过某种方式来绑定我的整个立方体贴图,而不是绑定我的立方体贴图的一个面。这是我的总体目标。我知道在 DirectX 中,您可以通过抓取 6 个面并手动处理它们,但我如何手动处理 openGL 立方体贴图上的每个面?

【问题讨论】:

  • 请参阅:glFramebufferTexture2D 以选择当前目标是哪张脸。
  • 介意输入一个简单的例子吗?你的意思是我应该调用 glBindFrameBuffer 然后调用“glFramebufferTexture2D”

标签: opengl framebuffer rendertarget


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 2015-10-25
    • 2012-06-03
    • 2014-07-22
    • 2013-10-05
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    相关资源
    最近更新 更多