简体   繁体   English

如何在pygame中将列表绘制到屏幕上?

[英]How can I draw a list to the screen in pygame?

I'm writing a program that calculates some pixel stuff in an OpenGL shader.我正在编写一个程序来计算 OpenGL 着色器中的一些像素内容。 With that calculation, I get a list containing every pixel that needs to be written to the screen.通过该计算,我得到一个列表,其中包含需要写入屏幕的每个像素。 Is there a way I can draw all of this data at once rather than iterating through the list and drawing every pixel?有没有一种方法可以一次绘制所有这些数据,而不是遍历列表并绘制每个像素? I ask this because it would be extremely slow to iterate through the list that contains all the million pixels for my screen and individually write these pixels to the screen.我问这个是因为遍历包含我屏幕上所有百万像素的列表并将这些像素单独写入屏幕会非常慢。 Any help would be much appreciated.任何帮助将非常感激。 Thanks.谢谢。

See How to save pygame scene as jpeg?请参阅如何将 pygame 场景另存为 jpeg?

If the color data is in the framebuffer you can read it with glReadPixels before the display is updated (before pygame.display.flip() or pygame.display.update() ).如果颜色数据在帧缓冲区中,您可以在显示更新之前使用glReadPixels读取它(在pygame.display.flip()pygame.display.update()之前)。 Use pygame.image.fromstring() to create new Surfaces from the buffer:使用pygame.image.fromstring()从缓冲区创建新的Surfaces

size = screen.get_size()
buffer = glReadPixels(0, 0, *size, GL_RGBA, GL_UNSIGNED_BYTE)
pygame.display.flip()
screen_surf = pygame.image.fromstring(buffer, size, "RGBA")

If the data is in a texture (eg stored in a compute shader) you can read it with glGetTexImage :如果数据在纹理中(例如存储在计算着色器中),您可以使用glGetTexImage读取它:

image_buffer = glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE)
image_surf = pygame.image.fromstring(image_buffer, (width, height), "RGBA")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM