如何在pyglet中渲染深度缓冲区以进行阴影贴图?

时间:2015-02-21 21:51:00

标签: python opengl pyglet

我想使用pyglet在python中实现shadowmapping。 它已经可以工作,但就好像我的窗口的权力维度为2。 我想要改变两件事:

  
      
  1. 使用除2 ^ x,2 ^ y
  2. 之外的其他分辨率渲染深度缓冲区   
  3. 以高于窗口的分辨率渲染深度缓冲区。
  4.   

1.问题是,当我将分辨率更改为奇数并使用

buffers = get_buffer_manager()
self.depth_tex = buffers.get_depth_buffer().get_texture()

我收到错误: pyglet.image.ImageException:深度纹理要求缓冲区维度为2的幂

问题#2是只有窗口的大小写入深度缓冲区,其余为1.所以如果我的窗口大小为512x512并且我将z-buffer的视口设置为1024x1024,那么z -buffer有一个1024x1024的矩形,里面有一部分图片,剩下的是白色。

代码:

class MyWindow(pyglet.window.Window):
    def __init__(self):
        super(JuliaWindow, self).__init__(caption = 'julia', width=512, height=512)

    ....

    def on_draw(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # if no depth texture exists (mesh is static)
        if not self.depth_tex:
            #try it, it won't work (white buffer)
            #glViewport(0,0,1024,1024)
            glMatrixMode(GL_PROJECTION)
            glLoadIdentity()
            gluPerspective(60,self.width/float(self.height),0.5,20)

            glMatrixMode(GL_MODELVIEW)
            glLoadIdentity()
            gluLookAt(2.0,1.5,2.5,0.0,0.0,0.0,0.0,1.0,0.0)

            glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE)

            mv = (GLfloat * 16)()
            glGetFloatv(GL_MODELVIEW_MATRIX, mv)
            p = (GLfloat * 16)()
            glGetFloatv(GL_PROJECTION_MATRIX, p)

            self.shaderD.bind()
            self.batch.draw()

            self.shaderD.unbind()

            buffers = get_buffer_manager()
            self.depth_tex = buffers.get_depth_buffer().get_texture()

        glBindTexture(self.depth_tex.target, self.depth_tex.id)

        #reset viewport
        #glViewport(0,0,self.width,self.height)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(60,self.width/float(self.height),0.5,20)

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        gluLookAt(self.x, self.y, self.z,0.0,0.0,0.0,0.0,1.0,0.0)

        self.shader.bind()
        self.shader.uniform_matrixf('depthMV', list(a))
        self.shader.uniform_matrixf('depthP', list(b))

        glClear(GL_DEPTH_BUFFER_BIT)
        glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE)

        self.batch.draw()
        self.shader.unbind()

0 个答案:

没有答案