Render to texture failing

Apprentice
Posts: 14
Joined: 2010.08
Post: #1
I am trying to setup a FBO so I can render to a texture. It works fine in simulator but fails on iPhone 3G with the message "failed to make complete framebuffer object 8cd6". The error code is GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_OES.

Here is the code

Code:
GLuint framebuffer;
glGenFramebuffersOES(1, &framebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);

GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 320, 480, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, texture, 0);

glBindTexture(GL_TEXTURE_2D,0);

GLuint depthRenderbuffer;
glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, 320, 480);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);

if(status != GL_FRAMEBUFFER_COMPLETE_OES)
    NSLog(@"failed to make complete framebuffer object %x", status);

I've followed the way it's done on Apple's website topic Using Framebuffer Objects to Render to a Texture.

I have researched this for hours now and cannot figure out why it's failing.
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2010.08
Post: #2
Even putting this code into a blank OpenGL ES template in XCode it fails on the 3G (running iOS 4.0.1). Can someone please help?
Quote this message in a reply
Member
Posts: 23
Joined: 2010.08
Post: #3
Having a quick look through your code I believe you can't create a Non-Power-Of-Two textures on the iPhone. Try putting a glGetError() after your glTexImage2D() to test if it fails.

There might be other errors but hopefully that will give you a head start.

The Monkey Hustle - Now available on the App Store!
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2010.08
Post: #4
Oh wow, what an idiot I am. Thankyou so much mariocaprino that was it. I can't believe I have spent so much time trying to figure this out!
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #5
More specifically, you can't create NPOT textures on MBX devices. You can on SGX.
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2010.08
Post: #6
How can I check if I need to make the texture POT? Any glGetIntegerv() I can check?

Another problem I've encountered is when I switch back to my normal rendering the depth buffer is no longer working.

I have this code

Code:
if(m_textureRenderer->IsActive())
{
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, m_textureRenderer->GetFrameBuffer());
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, m_textureRenderer->GetDepthBuffer());
}
else
{
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, m_defaultFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, m_depthRenderbuffer);
}

If I don't active the texture rendering then it renders fine, it's only when I go to texture rendering then back again that the depth buffer no longer works.
Looks like all I need is

Code:
if(m_textureRenderer->IsActive())
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, m_textureRenderer->GetFrameBuffer());
else
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, m_defaultFramebuffer);

And to restore the default depth buffer

Code:
glBindRenderbufferOES(GL_RENDERBUFFER_OES, m_depthRenderbuffer);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, m_depthRenderbuffer);
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #7
(Sep 1, 2010 01:44 AM)headkaze Wrote:  How can I check if I need to make the texture POT?

Check for the APPLE_texture_2D_limited_npot extension, as listed in the platform notes.

Quote:I have this code

What are you expecting glBindRenderbuffer to do here?
Quote this message in a reply
Apprentice
Posts: 14
Joined: 2010.08
Post: #8
(Sep 1, 2010 08:23 PM)arekkusu Wrote:  What are you expecting glBindRenderbuffer to do here?

I replied to my own post which seems to add to the previous post with a HR between them.

What I wanted to do was switch my game from rendering to the screen to a texture (for a transition) then back again.

I've already figured it out, I just had to add the glFramebufferRenderbufferOES call instead of just binding it otherwise the depth buffer does not work.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  iPad,render to texture, low framerate ldynasa 0 2,596 Nov 13, 2010 12:34 AM
Last Post: ldynasa
  GameKit bluetooth failing on iPad? Cirdan 0 3,778 Sep 3, 2010 01:20 AM
Last Post: Cirdan
  Performance problems with render to texture method mbw234 6 5,286 Jun 25, 2010 01:47 AM
Last Post: Jamie W
  Multi-touch failing Sumaleth 1 2,645 Feb 24, 2010 08:49 PM
Last Post: Sumaleth
  OpenAL alGenSources failing captainfreedom 1 3,836 May 26, 2009 07:45 AM
Last Post: captainfreedom