glOrtho和RenderToTexture

时间:2012-09-15 13:00:53

标签: opengl render-to-texture

我想将正投影投影到纹理中。

网格的渲染效果很好,但是当我添加FrameBuffer时,屏幕保持黑色。

创建FBO:

def createFBO(self):
    self.fbo = glGenFramebuffersEXT(1)
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, self.fbo)
    self.depthbuffer = glGenRenderbuffersEXT(1)
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, self.depthbuffer)
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height)
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, self.depthbuffer)
    ''' create the texture and bind it to the framebuffer '''
    self.texture = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, self.texture)
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, None);
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,  GL_TEXTURE_2D, self.texture, 0);

渲染对象:

def renderMesh(self):
    #glViewport(0, 0, width, height);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-64, 64, -64, 64, 1.0, 1000.0);

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    gluLookAt(64,64,100,64,64,-1,0,-1,0)

    if True:
        glEnable(GL_CLIP_PLANE0)
        glEnable(GL_CLIP_PLANE1)
        glClipPlane(GL_CLIP_PLANE0, (0,0,1,-1 * self.start + self.diff))       
        glClipPlane(GL_CLIP_PLANE1, (0,0,-1,self.start)) 

    #glClearColor(0.5,0.5,0.75,1.0)
    # render the mesh
    #glTranslatef(-64,-64,-64)
    glEnableClientState(GL_VERTEX_ARRAY) # Enable something in OpenGL 
    glEnableClientState(GL_COLOR_ARRAY) # Enable something in OpenGL
    glBindBuffer(GL_ARRAY_BUFFER,self.vbo[1]) 
    glColorPointer(3,GL_FLOAT,0,None) # Tell OpenGL that it contains only ColorValues
    #Now the vertex Buffer with positions
    glBindBuffer(GL_ARRAY_BUFFER,self.vbo[0]) 
    glVertexPointer(3,GL_FLOAT,0,None) # Tell OpenGL that it contains the Positions for each Points
    glDrawArrays(GL_TRIANGLES,0,len(self.verts)+len(self.color))    # Merge both

    glDisableClientState(GL_COLOR_ARRAY); 
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisable(GL_CLIP_PLANE0)
    glDisable(GL_CLIP_PLANE1)

渲染纹理:

   def renderPlaneWithTexture(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45.0,1.0*width/height,1.0,1000.0)
        glMatrixMode(GL_MODELVIEW)
        gluLookAt(0,0,1,0,0,0,0,1,0)
        glClearColor(0,0,0,0)        
        glEnable(GL_TEXTURE_2D)
        glBegin(GL_QUADS)        
        glNormal3f(0.0, 1.0, 0.0)
        glTexCoord2f(0.0, 0.0)
        glVertex3f(-1, 1, -1)
        glTexCoord2f(1.0, 0.0)
        glVertex3f(1, 1, -1)
        glTexCoord2f(1.0, 1.0)
        glVertex3f(1, -1, -1)
        glTexCoord2f(0.0, 1.0)
        glVertex3f(-1, -1, -1)    
        glEnd();
        glDisable(GL_TEXTURE_2D)

MainGameLoop:

glClearColor(0.0,0.0,0.0,1.0)
if self.useFBO:        
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, self.fbo);   
    self.renderMesh()
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 
    self.renderPlaneWithTexture()
else:
    self.renderMesh()
pygame.display.flip()

出了什么问题?为什么渲染到纹理时我的renderWindow仍然是黑色的?没有渲染纹理一切正常......

1 个答案:

答案 0 :(得分:3)

在渲染到FBO之前,必须取消绑定作为FBO颜色附件附加的纹理。理想情况下,在绑定FBO之前解除绑定。解除绑定是通过绑定纹理ID 0

来完成的