Search code examples
pythonlinepyglet

Problem with pyglet drawing black lines instead of green pixels


So I'm a beginner programmer with Python I have been playing with pyglet and I've discovered a problem if you run this code:

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()

You will find that the area shown has black lines in it. I've tried to fix it but it does not work and because pyglet is not pretty famous there aren't videos that describe that problem. I hope you know how to fix it because you are my last hope.


Solution

  • If you want to draw a filled area then draw a rectangular GL_POLYGON primitive instead of multiple GL_POINT primitives. That will be much faster and ensures that the area is completely filled:

    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()
    

    If you want to draw an 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()
    

    Much easier is to use a pyglet.sprite. e.g:

    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()