Python:3.6.6
pyglet版本:1.3.2
操作系统(两者均复制):Windows 10主机,Ubuntu 14.04来宾(不带3d加速的Virtualbox)
import pyglet
class Window(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.batch = pyglet.graphics.Batch()
self.position = [0, 0, 300.0]
self.vertices_list = []
# BLUE rect
self.add_rect("blue.png", x=0, y=0, z=0)
# RED rect
self.add_rect("red.png", x=10, y=10, z=10)
# Expects RED will be in front of BLUE, but it isn't
# BLUE on top regardless of z value. I tried -10/10, etc.
pyglet.clock.schedule_interval(self.move_red_forward, 1 / 60)
def add_rect(self, file_name, x, y, z):
image = pyglet.image.load(file_name)
image_grid = pyglet.image.ImageGrid(image, 1, 1)
texture_grid = image_grid.get_texture_sequence()
texture_group = pyglet.graphics.TextureGroup(texture_grid)
x_ = (texture_group.texture.width + x)
y_ = (texture_group.texture.height + y)
tex_coords = ('t3f', texture_grid[0].texture.tex_coords)
vert_list = self.batch.add(
4, pyglet.gl.GL_QUADS,
texture_group,
('v3f', (x, y, z,
x_, y, z,
x_, y_, z,
x, y_, z)),
tex_coords)
self.vertices_list.append(vert_list)
def move_red_forward(self, dt):
# move z += 1 for all corners
red_vertices = self.vertices_list[1].vertices
red_vertices[2] += 1
red_vertices[5] += 1
red_vertices[8] += 1
red_vertices[11] += 1
def set_3d(self):
width, height = self.get_size()
pyglet.graphics.glEnable(pyglet.graphics.GL_BLEND)
pyglet.graphics.glBlendFunc(
pyglet.graphics.GL_SRC_ALPHA,
pyglet.graphics.GL_ONE_MINUS_SRC_ALPHA)
pyglet.graphics.glViewport(0, 0, width, height)
pyglet.graphics.glMatrixMode(pyglet.graphics.GL_PROJECTION)
pyglet.graphics.glLoadIdentity()
pyglet.graphics.gluPerspective(90, width / height, 0.1, 6000.0)
pyglet.graphics.glMatrixMode(pyglet.graphics.GL_MODELVIEW)
pyglet.graphics.glLoadIdentity()
x, y, z = self.position
pyglet.graphics.glTranslatef(x, y, -z)
def on_draw(self):
self.clear()
self.set_3d()
self.batch.draw()
if __name__ == '__main__':
window = Window(width=1400, height=800, caption='Pyglet', resizable=True)
pyglet.app.run()
蓝色矩形在红色前面绘制。我希望z
轴负责定义应该在前面绘制的对象。
我该如何实现?
还是在了解这种过程时错过了什么?
答案 0 :(得分:2)
您必须启用Depth Test
pyglet.graphics.glEnable(pyglet.graphics.GL_DEPTH_TEST)
请注意,默认深度功能为GL_LESS
,如果片段在同一位置绘制,并且新片段的深度大于或等于深度,则这会导致片段被丢弃的片段。
如果未启用深度测试,则以后的几何图形总是绘制在前者的顶部。
如果要将红色图像混合在蓝色图像上,则必须禁用深度测试,并且必须在蓝色图像之后绘制红色图像。对于Blending,绘制顺序至关重要。后一个对象通过混合函数(glBlendFunc
)和混合方程式(glBlendEquation
)与前一个对象混合。
更改图像的顺序:
self.add_rect("red.png", x=10, y=10, z=10)
self.add_rect("blue.png", x=0, y=0, z=0)