Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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 :</p> <pre><code>void glGenTextureFromFramebuffer(GLuint *t, GLuint *f, GLsizei w, GLsizei h) { glGenFramebuffers(1, f); glGenTextures(1, t); glBindFramebuffer(GL_FRAMEBUFFER, *f); glBindTexture(GL_TEXTURE_2D, *t); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *t, 0); GLuint depthbuffer; glGenRenderbuffers(1, &amp;depthbuffer); glBindRenderbuffer(GL_RENDERBUFFER, depthbuffer); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, w, h); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthbuffer); GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); if(status != GL_FRAMEBUFFER_COMPLETE) NSLog(@"Framebuffer status: %x", (int)status); } </code></pre> <p>You can create the frame buffer and the texture easily :</p> <pre><code>GLuint _texture, _framebuffer; GLsizei w,h; float scale = [UIScreen mainScreen].scale; w = self.view.bounds.size.width * scale; h = self.view.bounds.size.height * scale; glGenTextureFromFramebuffer(&amp;_texture, &amp;_framebuffer, w, h); </code></pre> <p>You can later use _framebuffer to render the scene into _texture in your draw method :</p> <pre><code>glBindFramebuffer(GL_FRAMEBUFFER, _framebuffer); //draw here the content you want in the texture //_texture is now a texture with the drawn content //bind the base framebuffer glBindFramebuffer(GL_FRAMEBUFFER, 0); //or if you use GLKit [view bindDrawable]; //draw normaly </code></pre> <p>Now you can do what you want with the texture. If you want to do some post processing (blur, bloom, shadow, etc...) you can !</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload