Meda
Meda

Reputation: 2946

Render to texture using a frame buffer and GLKit's GLKBaseEffect

I'm trying to draw to a texture (and then use that texture on an object) with GLKit but I'm receiving GL ERROR: 0x0502 which I think it means invalid value passed to a function. The thing is, the error is fired somewhere inside the effects prepareToDraw method. The vertex arrays seem to be set up correctly since I can draw on the default frame buffer with no problem using the same set up. Is there something I'm missing?

GLint defaultFBO;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);

GLenum status;
glBindFramebuffer(GL_FRAMEBUFFER, _boundsTextureFramebuffer);
glBindTexture(GL_TEXTURE_2D, self.backgroundTexture.name);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8_OES, 1024, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.backgroundTexture.name, 0);
status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
{
    NSLog(@"Failed to initialize the texture framebuffer");
}

effect.constantColor = [self.color vectorValue];
[effect prepareToDraw];

glBindVertexArrayOES(self.vertexArray);
glDrawElements(_data.mode, (GLsizei)self.data.indicesCount, GL_UNSIGNED_INT, (void*)0);
glBindVertexArrayOES(0);


glBindFramebuffer(GL_FRAMEBUFFER, (GLuint)defaultFBO);

Upvotes: 0

Views: 1698

Answers (1)

Rad'Val
Rad'Val

Reputation: 9231

You're missing one important thing from the code you've posted: the render buffer. Adding one might solve your problem. Here is an example off the top of my head:

    GLint defaultFBO;
    glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);

    GLint defaultRBO;
    glGetIntegerv(GL_RENDERBUFFER_BINDING, &defaultRBO);


    glGenFramebuffers(1, &_boundsTextureFramebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, _boundsTextureFramebuffer);


    glGenRenderbuffers(1, &_boundsTextureRenderbuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, _boundsTextureRenderbuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA4, width, height);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _boundsTextureRenderbuffer);

    ...

    glBindFramebuffer(GL_FRAMEBUFFER, (GLuint)defaultFBO);
    glBindRenderbuffer(GL_RENDERBUFFER, (GLuint)defaultRBO);

Upvotes: 1

Related Questions