I have been having trouble finding straightforward code to render a scene to a texture in OpenGL ES (specifically for the iPhone, if that matters). I am interested in knowing the following:
- How do you render a scene to a texture in OpenGL ES?
- What parameters must you use to create a texture that is capable of being a render target in OpenGL ES?
- Are there any implications with applying this rendered texture to other primitives?
To render the scene to a texture you must use a framebuffer associated with a texture. Here is a method that i created to simplify it :
You can create the frame buffer and the texture easily :
You can later use _framebuffer to render the scene into _texture in your draw method :
Now you can do what you want with the texture. If you want to do some post processing (blur, bloom, shadow, etc...) you can !
This is how I'm doing it.
I define a texture variable (I use Apple's
Texture2D
class, but you can use an OpenGL texture id if you want), and a frame buffer:Then at some point, I create the texture, frame buffer and attach the renderbuffer. This you only need to do it once:
Every time I want to render to the texture, I do:
About your question 3, that's it, you can use the texture as if it is any other texture.