pythonpygamepixelpyopenglgldrawpixels

gldrawpixels on pyopengl not drawing anything?


I create pixels buffer to be drawn with glDrawPixels. The code run ok and the pygame window shows up. But nothing to be drawn, only a blank white window shown. Even if I change the value of buffer, it doesn't draw anything.

import pygame

from OpenGL.GL import *

#initiliaze pixel buffers
buffer = bytearray(800 * 600 * 3)

for i in range(800 * 600 * 3):
    buffer[i] = 25

def main():
    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, pygame.DOUBLEBUF| pygame.OPENGL)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        glClear(GL_COLOR_BUFFER_BIT)
        glDrawPixels(800, 600, GL_RGB, GL_UNSIGNED_BYTE, buffer)
        pygame.time.wait(10)

main()

This is what it shows when it runs

any help?


Solution

  • You missed to update the display by pygame.display.flip(). When the display is an OPENGL display then this will perform a buffer swap:

    import pygame
    from OpenGL.GL import *
    windowsize = (800,600)
    
    # create 800x600 buffer with RED color
    buffer = bytearray(windowsize[0] * windowsize[1] * 3)
    for i in range(windowsize[0] * windowsize[1]):
        buffer[i*3] = 255
        buffer[i*3+1] = 0
        buffer[i*3+2] = 0
    
    def main():
        pygame.init()
    
        # create `OPENGL`, `DOUBLEBUF` display
        pygame.display.set_mode(windowsize, pygame.DOUBLEBUF| pygame.OPENGL)
    
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
    
            # set BLACK clear color and clear the color plane
            glClearColor(0, 0, 0, 1)
            glClear(GL_COLOR_BUFFER_BIT)
    
            # draw buffer to dispaly
            glDrawPixels(*windowsize, GL_RGB, GL_UNSIGNED_BYTE, buffer)
    
            # update display
            pygame.display.flip() # <----- swap buffers
    
            pygame.time.wait(10)
    
    main()