10-30-2009, 09:29 AM
#1 (permalink )
Registered Member
Join Date: Sep 2009
Posts: 2
rendering framebuffer texture not working
I have read numerous articles/tutorials online regarding drawing a texture to a framebuffer in opengl, but I am having no luck getting it to work. I am creating a framebuffer, drawing a texture to the fbo, and then drawing the fbo texture to the screen but nothing displays (just a black screen). I am at a loss. If i comment out the fbo code and draw the embedded texture directly to the screen, that works. Is there some iphone specific logic that i am missing (independent of opengl)? Here is my code:
Code:
// INITIALIZATION
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
// Load Test Texture
glGenTextures(1, &textureTest);
glBindTexture(GL_TEXTURE_2D, textureTest); // Bind Our Texture
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filtered
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtered
[self loadTexture: @"texture"]; // calls glTexImage2D with image data from png
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0, 320.0f,480.0f);
glOrthof(0,320.0f, 480.0f,0, -1.0f, 1.0f); // left, right, bottom, top
glMatrixMode(GL_MODELVIEW);
//FBO Setup
glGenTextures(1, &textureFBO); // texture handle
glBindTexture(GL_TEXTURE_2D, textureFBO); //bind it.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filtered
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtered
glGenFramebuffersOES(1, &FBO); // frame buffer
glBindFramebufferOES(GL_FRAMEBUFFER_OES, FBO); //bind the frame buffer
// attach texture to framebuffer color buffer
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, textureFBO, 0);
if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) printf("CANNOT CREATE FRAMEBUFFER\n");
// MAIN DRAWING LOOP
static const Vertex3D vertices[] = {
{ 0.0, 100.0, 0.0},
{ 100.0, 100.0, 0.0},
{ 0.0, 200.0, 0.0},
{ 100.0, 200.0, 0.0}
};
static const GLfloat texCoords[] = {
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0
};
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// Render to texture
glBindFramebufferOES(GL_FRAMEBUFFER_OES, FBO);
// Draw test texture
glBindTexture(GL_TEXTURE_2D, textureTest);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, 0); //disable any bound textures.
// End draw test texture
// Render to screen
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
// Draw FBO texture
glBindTexture(GL_TEXTURE_2D, textureFBO);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, 0); //disable any bound textures.
// End draw FBO texture
11-04-2009, 03:54 PM
#2 (permalink )
Registered Member
Join Date: May 2009
Posts: 190
I did experiment with RTT (render to texture) involving using FBOs so this may be useful:
Code:
// Setup FBO
glGenFramebuffersOES(1, &FBO);;
glBindFramebufferOES(GL_FRAMEBUFFER_OES, FBO);
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, textures[3], 0);
GLuint status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
if (status != GL_FRAMEBUFFER_COMPLETE_OES) {
NSLog(@"Failed to generate frame buffer");
}
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
// Loop
glBindFramebufferOES(GL_FRAMEBUFFER_OES, FBO);
glViewport(0, 0, 320, 480);
// Draw code
glColorMask(0, 0, 0, 1);
glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, textures[0]);
[level renderHoles:q :A];
glColorMask(1, 1, 1, 1);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
11-04-2009, 04:13 PM
#3 (permalink )
Registered Member
Join Date: Sep 2009
Posts: 2
I was able to resolve the issue. I never re-binded the framebuffer that draws directly to the screen:
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 1);
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Stats
Members: 175,670
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Yosh_K