Game Development Community

Render To Texture

by Pat Wilson · 07/30/2003 (12:39 pm) · 10 comments

Download Code File

(Please see the .h file for complete documentation)

Dynamic Texture Class
This class will allow you to render to a texture and use it as...well whatever you want. The textures created are tracked by the texture manager and, though you'll need to re-capture, are otherwise not effected by resolution-changes etc. Please note that GL captures data starting from the bottom left, this means you will need to set up your texture coordinates with the y stuff flipped, or if you are using dglDrawBitmap make sure you specify a Y-flip

Render-to-texture emulation
OpenGL supports off-screen rendering buffers but only through platform-specific means and so I chose to do off-screen rendering simulation support by letting you store temporary pixels. This is all automated with the gui-rendering, for rendering other objects you will need to perform things in the following order:

dynamicTextureObject->setUpdateRect( updateRect );
dynamicTextureObject->storePixels();
//<your rendering code here, make sure you only draw in updateRect>
dynamicTextureObject->update();
dynamicTextureObject->restorePixels();

If you chose to use the off-screen rendering emulation please note that it is not a cheap operation, and I STRONGLY recomend that you
a) Enable the setUpdateRect optimization (see the description)
b) Use it as infrequently as possible

You are better off to do things before the canvas renders the content control in GuiCanvas::renderFrame, since the buffers will already have been swapped, and the user will be viewing the front buffer, then you can mess up the back-buffer all you want, grab your textures, then let the canvas render over it all. If you do this, just be wary of what you are putting into the depth/alpha buffers. Whatever you do, do NOT call glClear more than once per frame.

Example Screenshot
The screenshot is from my game-in-progress, Gridwalkers, which is what I coded this object for. It was done by rendering the root-GUI object to a texture and then using dglDrawBitmapStretch to draw it after all the controls were rendered. As you can see, the console covers up the 'Quit' button and the GG logo, however they are still visible on the texture. The texture is updated every frame as well. In order to do this, I had to reset the update rects every frame, otherwise the canvas will only draw the dirty areas, which usually is just the area under the mouse cursor, unless a control signs up to have it's rect redrawn.

The higher resolution version of the screenshot can be found here

This is not 100% bullet-proof or heavily tested. Treat it as such. If you have any problems with this, post to the forums. I suggest putting the code in 'dgl' since that is where it seems to fit best, your milage may vary.


For more examples of what you can do with Render-To-Texture, a feedback loop, and a little pixel-shader magic:
Screen Wipe 1
Screen Wipe 2
Screen Wipe 3
(No I'm not releasing that code)

#1
07/14/2003 (3:56 pm)
This is one of those awesome resources that I haven't a clue what I'd use it for :)
Any other suggestions other than your one screeny?
#2
07/30/2003 (5:51 pm)
kewlies, could use this to render some avi (divx etc..) to texture.. hrmmm nice little movie playing on the bilboard as you drive you racing car along the road... could be used to hook into the newly talked about cutscene thread as well.. many uses ;)
#3
07/31/2003 (1:32 pm)
If you really wanted to bring your computer to its knees you could render EVERY frame you see to texture first and then texture map that onto a grid which initially is flat. The result would be exactly the same as if you had simply rendered instead. Buuuuut...now you can warp that mesh and thus warp the view. This could be used to simulate realtime refraction effects...like a shockwave that distorts the air as it passes.

If you really REALLY want to bring it to it's knees this methd could be used for real time reflections. You render the scene BEHIND the players pov multiple times and texture map THAT onto some specially bent geometry and voila! The resulting image (once rendered into a texture AGAIN) can be used as an OpenGL reflection map.
#4
08/02/2003 (7:38 am)
Yeah, but if I really, really REALLY want to bring my computer to its knees, I can just unplug it!
That'll teach 'em!
;-)
#5
08/02/2003 (2:21 pm)
Or run Windows.
#6
08/09/2003 (11:38 am)
Hiya Pat, Could this be use to create a dynamic ragdoll system
I am currently working on HUDs GUIs for my demo game. But one of the feature I would like is a ragdoll that accurately reflects the character. I just need a nudge in the right direction I got stand-in stuff but its kinda same ole same ole stuff.
#7
08/23/2003 (6:40 pm)
Johnny: There is another resource with a DTS viewer; I think the DTS Viewer is more what you want for your ragdoll.

One use for this is to do the "jumbotron" view common in some sports games and some fighting games on console units. The dreamcast did this, so as long as your careful, a modern PC ought to have no problems at all (since its hardware is much higher). Take two triangles, arrange them in a rectangle in the scene, assign them the dynamic texture.

Render the scene from camera A (the billboard's camera), to the billboard's texture, then render the scene from the normal camera. Now your billboard shows the action from a different angle from where you are.

Edit: Also, the Dreamcast games used a trick for this (I forgot) -- they copied the previously rendered frame, so they were always one frame behind.
#8
06/26/2004 (11:24 am)
Hey, I'm not that familliar with opengl, but i've implimented this resource and its working fine for what I need, I'm calling glClear, then rendering some bitmaps and text in renderframe, and then later rendering a polygon with the saved texture.. it works perfect except the background is black instead of alphaed.. any idea what I need to call to clear this out? thanks in advance
#9
02/03/2005 (10:12 am)
I'm wondering if this couldn't be combined with guiSnooper by Melv and a little extra work to make DTS based video monitors ...

[HOW]EdM|EGTGE
#10
07/20/2006 (10:58 am)
There is a TSE version of this (sorta... i did base my initial work on this resource) over here.