渲染到屏幕和FBO给出不同的结果

时间:2014-06-22 22:00:20

标签: python opengl framebuffer pyopengl

我写了一个程序来渲染一个带纹理的正方形到屏幕和FBO。我相信两种情况下的渲染结果应该相同,只是输出目的地不同。令人惊讶的是,它给了我完全不同的结果。

对于第一张图片,我直接将其输出到屏幕。这很好,没有任何问题。对于第二个图像,我将其渲染到具有纹理的FBO,然后将该纹理渲染到屏幕。在这种情况下,输出完全混乱,输出似乎只包含图像的左上角。

First image. Directly render to screen

Second image. Render to a texture and render that texture to screen

相关代码是:

def render(self):
    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)

    gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, self._fbids[1])
    gl.glBindTexture(gl.GL_TEXTURE_2D, self._texids[0])
    gl.glUseProgram(0)
    self.drawQuad2()

    gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)
    gl.glBindTexture(gl.GL_TEXTURE_2D, self._texids[1]) # This is for second, messed up output.
    # gl.glBindTexture(gl.GL_TEXTURE_2D, self._texids[0])   # This is for first, normal output.
    gl.glUseProgram(0)
    self.drawQuad2()

def drawQuad2(self):
    gl.glBegin(gl.GL_QUADS)

    gl.glTexCoord2f(0, 1)
    gl.glVertex2f(-1, 1)
    gl.glTexCoord2f(1, 1)
    gl.glVertex2f(1, 1)
    gl.glTexCoord2f(1, 0)
    gl.glVertex2f(1, -1)
    gl.glTexCoord2f(0, 0)
    gl.glVertex2f(-1, -1)

    gl.glEnd()
    gl.glFlush()

def setupTexture(self, num, rawstr, width, height):
    texids = gl.glGenTextures(num)
    for tid in texids:
        print 'texture binded %s' % tid
        gl.glBindTexture(gl.GL_TEXTURE_2D, tid)
        gl.glTexImage2D(
            gl.GL_TEXTURE_2D,
            0, gl.GL_RGBA, width, height,
            0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE,
            rawstr)
        gl.glTexParameteri(
            gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)
        gl.glTexParameteri(
            gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST)
    gl.glEnable(gl.GL_TEXTURE_2D);

    return texids

def setupFramebuffer(self, num, texids):
    assert(len(texids) == num)
    fbids = gl.glGenFramebuffers(num)
    for i in range(len(fbids)):
        print 'framebuffer binded %s with texture %s' % (fbids[i], texids[i])
        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, fbids[i])
        gl.glFramebufferTexture2D(
            gl.GL_FRAMEBUFFER, gl.GL_COLOR_ATTACHMENT0,
            gl.GL_TEXTURE_2D, texids[i], 0)
    return fbids

def reshape(self, w, h):
    if not h:
        return
    print 'reshape. w:%s, h:%s' % (w, h)

    gl.glMatrixMode(gl.GL_PROJECTION)
    gl.glLoadIdentity()

    gl.glViewport(0, 0, w, h)
    gl.glOrtho(-1, 1, -1, 1, -1, 1)

    gl.glMatrixMode(gl.GL_MODELVIEW)
    gl.glLoadIdentity()

1 个答案:

答案 0 :(得分:3)

您必须使用glViewport函数告诉OpenGL它应通过绘图操作解决的目标窗口的大小。因此,当您切换到渲染到FBO时,必须使用对目标纹理有意义的内容调用glViewport;通常是目标纹理本身。切换到渲染到窗口时,使用目标窗口大小调用glViewport

如果目标视口大于目标帧缓冲区,则会有效地导致放大。