你不能直接这样做。
pygame.Surface但是,您可以使用pygame.draw模块或在对象上绘图pygame.Surface.blit。用于pygame.PixelArray直接访问表面上的像素。使用像素生成 OpenGL纹理对象。该纹理可以在 OpenGL 中使用。
def surfaceToTexture(pygame_surface):
rgba_surface = pygame.image.tostring(pygame_surface, 'RGBA')
width, height = pygame_surface.get_size()
texture_obj = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture_obj)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba_surface)
glBindTexture(GL_TEXTURE_2D, 0)
return texture_obj
image = pygame.image.load('my_image.png')
texture = surfaceToTexture(image)
Run Code Online (Sandbox Code Playgroud)
在另一个方向上,您可以渲染到 OpenGL渲染缓冲区或 纹理对象(请参阅帧缓冲区)。glReadPixels使用或从 GPU 加载纹理并glGetTexImage使用.pygame.Surfacepygame.image.frombuffer
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")
Run Code Online (Sandbox Code Playgroud)
image_buffer = glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE)
image_surf = pygame.image.fromstring(image_buffer, (width, height), "RGBA")
Run Code Online (Sandbox Code Playgroud)
另请参见PyGame 是否支持 3d?、PyGame 和 ModernGL 库、PyGame 和 OpenGL 4 或PyGame 和 OpenGL 立即模式(传统 OpenGL)。
| 归档时间: |
|
| 查看次数: |
953 次 |
| 最近记录: |