在OpenGL中混合2D和3D(使用pyglet)
我正在尝试在Pyglet中使用OpenGL将2D和3D结合起来,也就是说,先绘制一个3D场景,然后切换到正交投影,接着在上面绘制一些东西。我先绘制3D的内容,然后把投影矩阵推入栈中,接着使用glOrtho创建一个正交投影矩阵,绘制2D的内容,最后再把之前的矩阵从栈中弹出。3D的内容绘制得很好,但不知为什么2D的部分根本没有绘制出来,即使单独绘制也不行。以下是代码:
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)
2 个回答
0
在 draw2d()
函数里,试着先用 glDisable(GL_TEXTURE_2D)
关闭纹理,然后用 glColor3ub(255,255,255)
设置颜色为白色,再开始画你的三角形。
如果你之后要再调用 world.draw()
并且它使用了带纹理的图形,记得要重新用 glEnable(GL_TEXTURE_2D)
打开纹理。
2
我建议你在每次渲染的时候,完全重置模型视图和投影矩阵,然后在从3D切换到2D的时候,不要使用推入/弹出操作。
不过,我怀疑你可能在使用不正确的坐标,这导致场景绘制在了裁剪平面之外。特别是,我有点怀疑你把近裁剪平面设置为零。通常情况下,2D元素的z值是0。
试着把近裁剪平面设置为-1。
我也有点不明白你为什么在draw2d中调用glTranslatef(0, 0.0, -z),我觉得这样做没必要。