[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Images -> OpenGL Textures?



Josh Fishman wrote:
> Sorry if this has been discussed to death, but I can't find how to get
> Python-compatible images (preferably SDL_Images) to work as OpenGL
> textures. Does anyone know where I could look, or perhaps even have a
> snippet of code which demonstrates how to do this?

it's easy to do... once you know how to do it :]

this function should do the trick for you, using pygame/SDL_image to 
load the images..


def load_opengl_texture(filename, texid=0):
     "load and bind an opengl texture"
     imgsurf = pygame.image.load(filename)
     imgstring = pygame.image.tostring(imgsurf, "RGBA", 1)
     width, height = imgsurf.get_size()
     glBindTexture(GL_TEXTURE_2D, texid)
     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
                  GL_RGBA, GL_UNSIGNED_BYTE, imgstring)


the main 'work' here is done in the pygame.image.tostring() which 
converts the surface into a string of bytedata. pyopengl can apply 
this bytedata to a gl texture. the 3rd "1" argument in tostring() 
causes the image to be flipped vertically, which is how opengl 
expects its images.


____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org