iDevGames Forums
Render to texture failing - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: Render to texture failing (/thread-8054.html)



Render to texture failing - headkaze - Aug 31, 2010 09:58 PM

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.


RE: Render to texture failing - headkaze - Aug 31, 2010 11:58 PM

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?


RE: Render to texture failing - mariocaprino - Sep 1, 2010 12:23 AM

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.


RE: Render to texture failing - headkaze - Sep 1, 2010 12:32 AM

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!


RE: Render to texture failing - arekkusu - Sep 1, 2010 01:15 AM

More specifically, you can't create NPOT textures on MBX devices. You can on SGX.


RE: Render to texture failing - headkaze - Sep 1, 2010 01:44 AM

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);



RE: Render to texture failing - arekkusu - Sep 1, 2010 08:23 PM

(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?


RE: Render to texture failing - headkaze - Sep 1, 2010 09:47 PM

(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.