简体   繁体   English

在opengl中混合2d和3d(使用pyglet)

[英]mixing 2d and 3d in opengl (using pyglet)

I am trying to mix 2d and 3d in opengl in pyglet, ie draw a 3d scene then switch to orthographic projection and draw stuff over the top. 我试图在pyglet中混合使用opengl中的2d和3d,即绘制一个3d场景,然后切换到正交投影并在顶部绘制内容。 I draw the 3d stuff, push the projection matrix to the stack, do a glOrtho projection matrix, draw the 2d stuff, then pop the previous matrix off the stack. 我绘制3d东西,将投影矩阵推到堆栈,做一个glOrtho投影矩阵,绘制2d的东西,然后从堆栈中弹出前一个矩阵。 The 3d stuff draws fine but for some reason the 2d part isn't drawing at all, even on its own. 3d的东西很好,但由于某种原因,2d部分根本没有绘制,即使是单独的。 Here's the code: 这是代码:

class Window(pyglet.window.Window):

    # resolution
    width, height = 1024, 786

    def __init__(self, width, height):

        # initialise window
        super(Window, self).__init__(width, height)

        # set title
        self.set_caption("OpenGL Doss")

        # call update() at 30fps
        pyglet.clock.schedule_interval(self.update, 1 / 30.0)

        glEnable(GL_TEXTURE_2D)         # enable textures
        glShadeModel(GL_SMOOTH)         # smooth shading of polygons
        glClearColor(0.0, 0.0, 0.0, 0.0)

        glClearDepth(1.0)

        glDepthFunc(GL_LEQUAL)           
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)   # make stuff look nice

        self.world = World()            # initialise world

        self.label = pyglet.text.Label('Hello, world',
                          font_name='Times New Roman',
                          font_size=20,
                          width=10, height=10)

    def on_resize(self, width, height):
        print 'on resize'
        if height == 0:
            height = 1
        glViewport(0, 0, width, height) # specify viewport

        # load perspective projection matrix
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45, 1.0 * width / height, 0.1, 100.0)
        #glLoadIdentity()

    def on_draw(self):
        self.set3d()

        # draw 3d stuff
        self.world.draw()

        self.set2d()

        # draw 2d stuff
        self.draw2d()

        self.unSet2d()

    def update(self, dt):
        "called at set interval during runtime"
        #maze = self.world.maze
        maze_platform = self.world.maze_platform

        pacman = maze_platform.maze.pacman

        maze_platform.update()

        # send it world pointer
        pacman.update(self.world)



    def on_key_press(self, symbol, modifiers):
        control.press(symbol, modifiers)

    def on_key_release(self, symbol, modifiers):
        control.release(symbol, modifiers)

    def set3d(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glEnable(GL_DEPTH_TEST)         # enable depth testing
        # reset modelview matrix
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

    def set2d(self):

        glDisable(GL_DEPTH_TEST)
        # store the projection matrix to restore later
        glMatrixMode(GL_PROJECTION)
        glPushMatrix()

        # load orthographic projection matrix
        glLoadIdentity()
        #glOrtho(0, float(self.width),0, float(self.height), 0, 1)
        far = 8192
        glOrtho(-self.width / 2., self.width / 2., -self.height / 2., self.height / 2., 0, far)

        # reset modelview
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

        #glClear(GL_COLOR_BUFFER_BIT)



    def unSet2d(self):

        # load back the projection matrix saved before
        glMatrixMode(GL_PROJECTION)
        glPopMatrix() 

    def draw2d(self):
        z=-6
        n=100
        glTranslatef(0, 0.0, -z)


        glBegin(GL_TRIANGLES)
        glVertex3f(0.0, n, 0.0)
        glVertex3f(-n, -n, 0)
        glVertex3f(n, -n, 0)
        glEnd()


def main():
    window = Window(Window.width, Window.height)
    pyglet.app.run()
    print 'framerate:', pyglet.clock.get_fps(), '(error checking = %s)' % pyglet.options['debug_gl']

if __name__ == '__main__': main()
    #command = 'main()'
    #cProfile.run(command)

I would recommend that you fully reset the modelview and projection matrices on each render, and then don't use push/pop when you go from 3d to 2d. 我建议您完全重置每个渲染上的模型视图和投影矩阵,然后在从3d到2d时不要使用push / pop。

However, I suspect that you are using bad coordinates so the scene is drawing outside the clip planes. 但是,我怀疑您使用的是不良坐标,因此场景正在剪裁平面之外绘制。 In partciular I am a tad suspicious of putting the near clipping plane at zero. 在partciular中,我有点怀疑将近剪裁平面置于零。 Normally 2d elements are drawn with z=0. 通常用z = 0绘制2d元素。

Try putting the near clip-plane at -1. 尝试将近剪裁平面置于-1。

I'm also a bit unsure why you're calling glTranslatef(0, 0.0, -z) in draw2d, I wouldn't bother. 我也有点不确定为什么你在draw2d中调用glTranslatef(0,0.0,-z),我不会打扰。

In draw2d() try glDisable(GL_TEXTURE_2D) and glColor3ub(255,255,255) before drawing your triangle. draw2d()在绘制三角形之前尝试glDisable(GL_TEXTURE_2D)glColor3ub(255,255,255)

Make sure to re- glEnable(GL_TEXTURE_2D) before calling world.draw() again if it uses textured geometry. 如果使用纹理几何体,请确保在再次调用world.draw()之前重新glEnable(GL_TEXTURE_2D)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM