簡體   English   中英

小精靈 OpenGL。 如何基於“ z”軸在其他物體前面繪制矩形

[英]Pyglet. OpenGL. How to draw rect in front of other based on “z” axis

環境:

的Python:3.6.6
pyglet版本:1.3.2
操作系統(兩者均復制):Windows 10主機,Ubuntu 14.04來賓(沒有3d加速的Virtualbox)

碼:

github中的代碼

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軸負責定義應該在前面繪制的對象。

題:

我該如何實現?
還是在了解這種過程時錯過了某些東西?

您必須啟用深度測試

pyglet.graphics.glEnable(pyglet.graphics.GL_DEPTH_TEST)

注意,默認的深度函數是GL_LESS ,如果片段在同一位置繪制,並且新片段的深度大於或等於前一個片段的深度,則這會導致片段被丟棄。

如果未啟用深度測試,則以后的幾何圖形總是繪制在前者的頂部。


如果要將紅色圖像融合到藍色圖像上,則必須禁用深度測試,並且必須在藍色圖像之后繪制紅色圖像。 對於混合 ,繪制順序至關重要。后一個對象通過混合功能( glBlendFunc )和混合方程式( glBlendEquation )與前一個對象混合。
更改圖像的順序:

 self.add_rect("red.png", x=10, y=10, z=10) 
 self.add_rect("blue.png", x=0, y=0, z=0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM