2013-01-15 76 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

我會試試。謝謝 –