pyglet 绘制黑线而不是绿色像素的问题

Problem with pyglet drawing black lines instead of green pixels

所以我是 Python 的初学者程序员,我一直在玩 pyglet,如果你 运行 这段代码我发现了一个问题:

from pyglet.gl import *

window = pyglet.window.Window(1000,500,"App",resizable=True)
window.set_minimum_size(500,250)

image =pyglet.resource.image("logo.png")

intt = None

def pixel(x1,y1):
    color = []
    size = []
    for x in range(x1):
        for y in range(y1):
            size.append(x)
            size.append(y)

            color.append(0)
            color.append(255)
            color.append(0)

    vertex_list = pyglet.graphics.vertex_list(x1 * y1,('v2i', size ),
                                                     ('c3B', color ))
    return vertex_list

@window.event
def on_draw():
    window.clear()
    #vertex_list = pixel(600,500)
    #vertex_list.draw(GL_POINTS)
    color = []
    size = []
    for x in range(100):
        for y in range(500):
            size.append(x+504)
            size.append(y)

            color.append(0)
            color.append(255)
            color.append(0)

    vertex_list = pyglet.graphics.vertex_list(100 * 500, ('v2i', size),
                                              ('c3B', color))

    vertex_list.draw(GL_POINTS)

pyglet.app.run()

您会发现显示的区域中有黑线。我试图修复它,但它不起作用,因为 pyglet 不是很有名,所以没有描述该问题的视频。我希望你知道如何修复它,因为你是我最后的希望。

如果要绘制填充区域,则绘制矩形 GL_POLYGON 图元而不是多个 GL_POINT 图元。这将更快并确保该区域被完全填满:

from pyglet.gl import *

window = pyglet.window.Window(1000,500,"App",resizable=True)
window.set_minimum_size(500,250)

def pixel(x1, y1, x2, y2):
    size = [x1, y1, x2, y1, x2, y2, x1, y2]
    color = []
    for _ in range(4):
        color += [0, 255, 0]
    vertex_list = pyglet.graphics.vertex_list(4, ('v2i', size ), ('c3B', color ))
    return vertex_list

@window.event
def on_draw():
    window.clear()
    vertex_list = pixel(500, 0, 600, 500)
    vertex_list.draw(GL_POLYGON)

pyglet.app.run()

如果你想画一个pyglet.image, then generate a texture and a pyglet.graphics.Batch:

from pyglet.gl import *

window = pyglet.window.Window(1000,500,"App",resizable=True)
window.set_minimum_size(500,250)

def quadTexture(x, y, w, h, texture):

    vertex = [x, y, x+w, y, x+w, y+h, x, y+h]
    tex = [0, 0, 1, 0, 1, 1, 0, 1]

    batch = pyglet.graphics.Batch()
    batch.add(4, GL_QUADS, texture, ('v2i', vertex ), ('t2f', tex ))
    return batch

@window.event
def on_draw():
    window.clear()
    batch.draw()

file = "logo.png"

image = pyglet.image.load(file)
tex = pyglet.graphics.TextureGroup(image.get_texture())
batch = quadTexture(20, 20, image.width, image.height, tex)
pyglet.app.run()

使用 pyglet.sprite 更容易。例如:

from pyglet.gl import *

window = pyglet.window.Window(1000,500,"App",resizable=True)
window.set_minimum_size(500,250)

@window.event
def on_draw():
    window.clear()
    sprite.draw()

file = "logo.png"
image = pyglet.image.load(file)
sprite = pyglet.sprite.Sprite(image, x=20, y=20)

pyglet.app.run()