[PyOpenGL-Users] Pygame surfaces as PyOpenGL textures?
Brought to you by:
mcfletch
From: Jan E. <ch...@in...> - 2001-11-10 11:59:31
|
Hi, Well, I got bitten by the OpenGL bug, and did some small tests with pygame and pyopengl. All works just fine, and speed of development is way faster than with C/C++. Did a small terrain rendering that is both slow and ugly, but which seems to do what it should do. See a snapshot at: http://www.infa.abo.fi/~chakie/cm/snapshot4.jpg However, no terrain without trees. Simple trees are easiest to do with a single billboarded texture. It does need to be partially transparent though. So, the problem is how I can get pyopengl to use a texture that is partially transparent. Ideally I'd like to use simple colorkey transparency, i.e. set a color as transparent. That part works nicely in pygame. I can do this: surface = pygame.image.load ( 'tree.bmp' ) surface.set_colorkey ( (255, 0, 255) ) to set magenta as transparent. Works fine as long as blitted within pygame. To create a texture I use the folloing black magic: # convert to a string image = pygame.image.tostring ( surface, "RGBA" ) ix, iy = surface.get_size () # generate a texture id tree = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, tree ) # do some Rocket Science(tm) glPixelStorei(GL_UNPACK_ALIGNMENT,1) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST) gluBuild2DMipmaps(GL_TEXTURE_2D, 4, ix, iy, GL_RGBA, GL_UNSIGNED_BYTE, image) Now, this works quite fine, but no transparency is still visible. Before rendering the quads for the trees I do the following spells: glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) Depending on how I fiddle with all kinds of parameters I end up with either trees with magenta borders (no transparency) or then just a few pixels visible (too much transparency). :) Has anybody done this before? I looked at NeHe:s tutorials, where the above code partially comes from, but he uses "pil" for loading the images, and I found no way to set transparency using that. Pil didn't seem to like any kinds of images that has an alpha channel in the image, which was the reason for trying to it using pygame. I know I do something very wrong here, but it's very easy as OpenGL texture handling is a deep, deep swamp until you have learnt the rules of the game. Apart from this little problem the rendering was way faster than I thought it would be. Loading data from disk and doing normal calculations is a bit slow, but that's done only once. By using display lists heavily I have IMHO quite ok performance when doing software rendering only. I don't think my old G450 can even do 3D, and at least not on Linux. A feature I like very much with pyopengl is that it reports errors, i.e. when I try to do something illegal in, say, a glBegin(GLFOO) I get a stack trace if I do something that's not allowed. In C/C++ I have to manually check the error flag after each call to see if something is wrong. Regards, Chakie -- Many an ancient lord's last words had been: "You can't kill me because I've got magic aaargh...." -- Terry Pratchett, Interesting Times |