On 2010-01-31 15:54, Alexandre Quessy wrote: > Hello, > I am trying to upload a PixBuf to an OpenGL texture. I want it to be as > optimized as possible. Of course, I'll first need to get it work > properly, but I also want it to be quick, since I will upload one to a > texture on every frame rendering of my application. What is the best way > to do so? > > Right now, I have something like this: > > > pixels = buffer.get_pixels() > w = image.get_width() # 320 > h = image.get_height() # 240 > glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_id) > glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, w, h, 0, > GL_RGB, GL_UNSIGNED_BYTE, pixels) > glPixelStorei(GL_UNPACK_ALIGNMENT, 1) > glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST) > glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST) > > My example is not quite working yet, (see > http://bitbucket.org/aalex/toonplayer/src/tip/scripts/gl-pixbuf.py ) but > I am working to get it OK. I am worried, though, that converting a > PixBuf to a Python string will slow things down a lot. Maybe should I > use gtk.gdk.PixBuf.get_pixels_array() instead of get_pixels()? But > still, will that be quick enough? The most optimized thing to do would > be to create a C function that would look like this: > > void pixbuf_to_texture(int w, int h, GdkPixBuf buffer) > { > glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, 3, w, h, 0, GL_RGB, > GL_UNSIGNED_BYTE, gdk_pixbuf_get_pixels(buffer)); > } > > Does anyone have advices regarding this issue? > Thanks a lot !
The only way to see if something is fast enough for you would be to try it. Admittedly if you're using OpenGL hardware it's kinda hard to test on enough cards that you're sure it'll be fast enough everywhere. I recommend that you get it working with the simple code first, then improve the speed from there. That said, using get_pixels_array() will be faster, because it doesn't copy the image data. This assumes that glTexImage2D can take an array argument without copying it itself. There are a couple of other things you can do to improve performance: - If your texture doesn't change size, use glTexSubImage2D rather than glTexImage2D after the first. This will avoid freeing and allocating big blocks of video memory, instead copying data into the same block each time. - Use a "Pixel Buffer Object" to move data into the texture instead. You'll need the "ARB_pixel_buffer_object" extension. This pages has a good description of what they are and how to use them: http://www.songho.ca/opengl/gl_pbo.html -- Tim Evans Applied Research Associates NZ http://www.aranz.com/ _______________________________________________ pygtk mailing list pygtk@daa.com.au http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://faq.pygtk.org/