Reputation: 2471
I want to render an orthographic projection into texture.
The rendering of the mesh is working good, but when I add the FrameBuffer the screen stays black.
Create the 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);
Render the object:
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)
Render the texture:
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()
Where is something going wrong? Why is my renderWindow still black when using render to texture? Without render to texture everything works fine...
Upvotes: 0
Views: 610
Reputation: 162297
You must unbind the texture attached as the FBO's color attachment, before you can render to the FBO. Ideally you unbind it, before binding the FBO. Unbinding is done by binding texture ID 0
Upvotes: 2