PDA

View Full Version : how to render to texture?



rhmazza
09-04-2002, 04:37 PM
How can I render to a texture in OpenGL? Can I do it normally? Or do I need some extension?

Pa3PyX
09-05-2002, 01:14 AM
To render TO a texture? That is, you want to render a screen and then save it and load it as a texture?

That can be done with normal OpenGL - it's the glReadPixels() call. This will read a rectangular array of pixels at the position and dimensions you specify, and store it in main memory at the buffer that you provide, using the current glReadBuffer and glPixelStore parameters.

Then, you can check to see whether that image satisfies texture requirement (that is, that each dimension is a power of 2 and neither of them exceeds the maximum allowed texture size for your OpenGL implementation (obtained with glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize) http://www.opengl.org/discussion_boards/ubb/wink.gif. If these conditions are not met, scale the image using gluScaleImage() call.

Then, use glTexImage2D() call to load your image as a texture, as usual.

Be aware though, that glReadPixels, glDrawPixels and glCopyPixels are broken in many hardware OpenGL implementations... Also, glReadPixels is slow (just think about the size of the image that needs to be sent over PCI bus), so it's not a good idea to do that every darn single frame of your animation.

[This message has been edited by Pa3PyX (edited 09-05-2002).]

rIO
09-05-2002, 01:20 AM
Visit nehe for a tutorial on that topic, or my home page for an "for dummies" render to texture class..

hope it helps...

Mazy
09-05-2002, 03:01 AM
you can render to a pbuffer and bind the pbuffer as an texture

rhmazza
09-05-2002, 03:35 AM
Thanks for the responses, I have to look into them. I will say though that whatever I end up doing, it will in fact need to be done every frame since what I'm trying to do is image feedback. But I won't be doing much else. That's like what a lot of winamp visualizations do, but I'm looking to do it myself rather than by pugging into winamp. I know this sort of thing isn't very hard in DirectX, but I'd rather use OpenGL.

[This message has been edited by rhmazza (edited 09-05-2002).]