python - pyopengl 上的 gldrawpixels 没有绘制任何东西?

标签 python pygame pixel pyopengl gldrawpixels

我创建了要使用 glDrawPixels 绘制的像素缓冲区。代码运行正常并显示 pygame 窗口。但没有任何东西可绘制,只显示一个空白的白色窗口。即使我更改缓冲区的值,它也不会绘制任何内容。

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

有什么帮助吗?

最佳答案

您错过了 pygame.display.flip() 更新显示。当显示器是 OPENGL 显示器时,这将执行缓冲区交换:

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

关于python - pyopengl 上的 gldrawpixels 没有绘制任何东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59844176/

相关文章:

python - Jinja2:将一个列表中的项目与另一个列表中的项目进行比较

python - Scrapy MultiCSVItemPipeline 导出一些空项目

python - 如何在pygame中将 "print"blit到屏幕上

python - 如何在pygame中绘制透明线

c# - 如何将WPF英寸单位转换为Winforms像素,反之亦然?

html - 如何让一个像素在 HTML 中改变它的颜色?

python - 无法在 python 中调度 com 对象

python - Pandas tz_localize : Infer dst when localizing timezone in data with duplicates

python - Pygame打开窗口并立即崩溃

css - 在同一规则中混合百分比和像素是否存在问题?