Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenGL ES render to texture, then draw texture
    text
    copied!<p>I'm trying to render to a texture, then draw that texture to the screen using OpenGL ES on the iPhone. I'm using <a href="https://stackoverflow.com/questions/1024603/opengl-es-render-to-texture">this question</a> as a starting point, and doing the drawing in a subclass of Apple's demo EAGLView.</p> <p>Instance variables:</p> <pre><code>GLuint textureFrameBuffer; Texture2D * texture; </code></pre> <p>To initialize the frame buffer and texture, I'm doing this:</p> <pre><code>GLint oldFBO; glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &amp;oldFBO); // initWithData results in a white image on the device (works fine in the simulator) texture = [[Texture2D alloc] initWithImage:[UIImage imageNamed:@"blank320.png"]]; // create framebuffer glGenFramebuffersOES(1, &amp;textureFrameBuffer); glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer); // attach renderbuffer glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, texture.name, 0); if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) NSLog(@"incomplete"); glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFBO); </code></pre> <p>Now, if I simply draw my scene to the screen as usual, it works fine:</p> <pre><code>glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw some triangles, complete with vertex normals [contentDelegate draw]; [self swapBuffers]; </code></pre> <p>But, if I render to 'textureFrameBuffer', then draw 'texture' to the screen, the resulting image is upside down and "inside out". That is, it looks as though the normals of the 3d objects are pointing inward rather than outward -- the frontmost face of each object is transparent, and I can see the inside of the back face. Here's the code:</p> <pre><code>GLint oldFBO; glGetIntegerv(GL_FRAMEBUFFER_BINDING_OES, &amp;oldFBO); glBindFramebufferOES(GL_FRAMEBUFFER_OES, textureFrameBuffer); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw some polygons [contentDelegate draw]; glBindFramebufferOES(GL_FRAMEBUFFER_OES, oldFBO); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnable(GL_TEXTURE_2D); glColor4f(1, 1, 1, 1); [texture drawInRect:CGRectMake(0, 0, 320, 480)]; glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisable(GL_TEXTURE_2D); [self swapBuffers]; </code></pre> <p>I can flip the image rightside-up easily enough by reordering the (glTexCoordPointer) coordinates accordingly (in Texture2D's drawInRect method), but that doesn't solve the "inside-out" issue.</p> <p>I tried replacing the Texture2D texture with a manually created OpenGL texture, and the result was the same. Drawing a Texture2D loaded from a PNG image works fine.</p> <p>As for drawing the objects, each vertex has a unit normal specified, and <code>GL_NORMALIZE</code> is enabled.</p> <pre><code>glVertexPointer(3, GL_FLOAT, 0, myVerts); glNormalPointer(GL_FLOAT, 0, myNormals); glDrawArrays(GL_TRIANGLES, 0, numVerts); </code></pre> <p>Everything draws fine when it's rendered to the screen; <code>GL_DEPTH_TEST</code> is enabled and is working great.</p> <p>Any suggestions as to how to fix this? Thanks!</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