2013-01-15 73 views
1

我正在制作立方体贴图渲染目标类。 我已经有一个2D工作正常。但是对于这个立方体贴图渲染目标类,我希望能够像我希望的那样渲染到特定的人脸。也许我想在“X +”面上渲染圆形,而在“Y +”面上渲染三角形。如何渲染OpenGL立方体贴图纹理

这是我的课是什么样子

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立方体贴图上手动处理每个面?

+1

请参阅:'glFramebufferTexture2D'用于选择哪个面是当前目标。 – JasonD

+0

介意输入一个简单的例子? 你的意思是我应该打电话 glBindFrameBuffer 然后调用“glFramebufferTexture2D” –

回答

4

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

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

我会试试。谢谢 –