我正在使用pygame + pyopengl制作游戏,现在我正试图在这种背景下制作视频播放器。为此,我使用ffmpeg加载不同的视频格式,然后将每个帧转换为opengl纹理,如下所示,然后播放视频。
class Texture(object):
def __init__(self, data, w=0, h=0):
"""
Initialize the texture from 3 diferents types of data:
filename = open the image, get its string and produce texture
surface = get its string and produce texture
string surface = gets it texture and use w and h provided
"""
if type(data) == str:
texture_data = self.load_image(data)
elif type(data) == pygame.Surface:
texture_data = pygame.image.tostring(data, "RGBA", True)
self.w, self.h = data.get_size()
elif type(data) == bytes:
self.w, self.h = w, h
texture_data = data
self.texID = 0
self.load_texture(texture_data)
def load_image(self, data):
texture_surface = pygame.image.load(data).convert_alpha()
texture_data = pygame.image.tostring(texture_surface, "RGBA", True)
self.w, self.h = texture_surface.get_size()
return texture_data
def load_texture(self, texture_data):
self.texID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, self.texID)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, self.w,
self.h, 0, GL_RGBA, GL_UNSIGNED_BYTE,
texture_data)
问题是,当我加载给定视频的所有纹理时,我的RAM离开天花板,大约800mb。但是可以通过在加载时对每个纹理进行blitting来解决这个问题,如下所示。
def render():
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glDisable(GL_LIGHTING)
glEnable(GL_TEXTURE_2D)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glClearColor(0, 0, 0, 1.0)
def Draw(texture, top, left, bottom, right):
"""
Draw the image on the Opengl Screen
"""
# Make sure he is looking at the position (0,0,0)
glBindTexture(GL_TEXTURE_2D, texture.texID)
glBegin(GL_QUADS)
# The top left of the image must be the indicated position
glTexCoord2f(0.0, 1.0)
glVertex2f(left, top)
glTexCoord2f(1.0, 1.0)
glVertex2f(right, top)
glTexCoord2f(1.0, 0.0)
glVertex2f(right, bottom)
glTexCoord2f(0.0, 0.0)
glVertex2f(left, bottom)
glEnd()
def update(t): 渲染() 画(t,-0.5,-0.5,0.5,0.5)
# Check for basic Events on the pygame interface
for event in pygame.event.get():
BASIC_Game.QUIT_Event(event)
pygame.display.flip()
虽然这会将RAM消耗降低到可接受的值,但它会使加载时间大于视频长度。
我真的不明白为什么opengl以这种方式工作,但是有没有办法让纹理效率高而不首先使用blit?
答案 0 :(得分:2)
我现在无法根据您在问题中的代码进行说明,但我会猜测它是因为您正在创建新的{{1 \ n}每个帧的实例,这意味着您为视频的每一帧调用Texture
。这会为视频的每一帧在内存中分配一个新的缓冲区,然后存储一个完整的,未压缩的帧版本。
当您对图像进行blit时,您不会生成新纹理,而只是覆盖旧纹理。这是您想要的解决方案,但您实施它的方式效率低下。
有很多方法可以更改纹理中的数据,而不会在CPU上进行blitting(假设pygame blitting)以使事情变得更快,有些在此答案中列出: