Render in a texture

how can i set gl to render inside a texture object so i can texturize new objects with the previous scene?


i have no idea but you might try rendering to a bitmap and then reapplying it to the object but this would most likely be dog slow…

I did something similar once and what I did was create a blank texture, set the viewport to the size of the texture, render the scene for the texture, use glCopyTexImage2D() to copy the scene to the texture. Then clear the buffers and render the scene using your normal settings and your new texture.

I don’t think this is the best way, but it works although I remember it being somewhat slow.

glReadPixels the current scene, then use that for the texture in the next scene. That way you aren’t rendering the scene twice like the other two suggestions.

Hope that helps.


I agree with ffish’s assertion that glReadPixels will not require the scene to be re-rendered. However:

1 glReadPixels() will soak the bus between the video card and memory
2 the frame buffer pixels might need to be converted between formats and thus require additional overhead
3 the screen needs to be resampled, anyway
4 the new texture data will need to be copied back

You can’t avoid (1). If you’re reading from the screen, then the pixels will need to be stored in memory. You can avoid (2) by packing the data in the same format that they’re stored in the frame buffer, but this might make resampling the image more problematic depending on what you’re doing with it. (3) will need to be done in most cases; screens are not necessarily powers of two in dimension, after all, and finally, (4) needs to be done so the texture can be used.

Re-rendering the screen has the properties:

1 there is no bus traffic between the card and memory
2 less data is transferred between screen and texture memory
3 the data doesn’t need to be resample, because its implicilty done when the screen is rerendered
4 the pixels are unlikely to require conversion from frame pixels to textels format

As noted, the single disadvantage of the latter approach is that the scene needs to be rerendered. this might not be much of a problem because you can keep track of the sceen state from the first traversal. ie. you have a scene graph and traverse it once to collect rendered scene info, and you can save the graph traversal for the second time you need to draw the screen. (you thus don’t have the FULL overhead of rednering a scene, since you don’t need to perform frustum culling / state sorting / etc again, and its to a smaller viewport, anyway)

glRead approach is more efficient if the overhead of reading and converting pixels is less than the cost of redrawing a smaller copy of the scene. i’d put my money on the second case, tho’; a lot of engines use multiple passes, and this is no different.

my 2c worth,


ok guys, i’ll try the two methods and will stay with the fastest.

thank you all very much