iDevGames Forums
openGL n00b looking for FBO drawing examples or direction - 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: openGL n00b looking for FBO drawing examples or direction (/thread-1402.html)



openGL n00b looking for FBO drawing examples or direction - auptown - Apr 24, 2009 08:49 AM

Hi --

I'm still in the steep slope of the openGL ES learning curve, although I have managed to draw a nice shape (over 2000 triangles!) and wrap it in a texture.

But now I want to modify the texture, specifically, I want to add a bunch of small markers at setup time (not each frame draw), and then wrap the shape with the modified texture. The locations of the markers will differ from time to time, they are loaded from my server dynamically before setup.

The main texture is 1024 x 1024, and the small markers might be 32x32 or even 16x16.

I have read enough to think that what I need to do is to create and then draw to a framebuffer object, and then use that, or a texture made from that, as the texture to wrap my shape.

Does anyone know where I might find some example code that does this, or a tutorial that I can study that covers this?

Any direction greatly appreciated!


openGL n00b looking for FBO drawing examples or direction - arekkusu - Apr 24, 2009 10:40 AM

Apple provides FBO sample code for OpenGL. The concepts are exactly the same in ES.

See also Nvidia's sample code and the FBO Wiki page.


openGL n00b looking for FBO drawing examples or direction - auptown - Apr 24, 2009 11:04 AM

arekkusu Wrote:Apple provides FBO sample code for OpenGL. The concepts are exactly the same in ES.

See also Nvidia's sample code and the FBO Wiki page.


arekkusu --

thanks! I'm sure I have enough there to get this done.

Actually, I'm part way there already, but my object looks all white, so I'm probably just missing a few key things.

have a great day


openGL n00b looking for FBO drawing examples or direction - auptown - Apr 25, 2009 09:55 AM

Hi

Wow, I learned a lot from the links, in fact I *think* I have a strategy in place, but alas I'm getting the "all white texture". Here is the code, I call setupFbo() once, before I setup things like the matrtix mode and glOrtho, and then I call renderFbo() right after that.

In drawView(), I'm just trying to use the texture (fboTexture) in place of the bgWorldMap texture, which is 1024 x 1024. My idea is that if I can get this to work, I can add to the renderFbo function to put in the other small textures that I need to add.

So, if anyone can see something dumb that I'm doing, or missing, that would cause an all-white texture I would appreciate it!

The setup code, called once:

BTW if I un-comment out the glEnable(GL_DEPTH) I get all black.
Code:
GLuint extFBO;
GLuint depthBuffer;
GLuint fboTexture;

-(void)setupFBO{
    
    glGenFramebuffersOES(1, &extFBO);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, extFBO);
    
    glGenRenderbuffersOES(1, &depthBuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthBuffer);
    glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, 1024, 1024);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthBuffer);
    
//    glEnable(GL_DEPTH_TEST);
    
    glGenTextures(1, &fboTexture);
    glBindTexture(GL_TEXTURE_2D, fboTexture);
    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_RGBA,  1024, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, fboTexture, 0);
    
    GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
    if( status != GL_FRAMEBUFFER_COMPLETE_OES ){
        NSLog(@"bad setupFBO status");
    }
    
}


The render code, called once:

Code:
-(void)renderFBO{

    const GLfloat vertices[] = {
        -512., -512.,
        512., -512.,
        -512.,  512.,
        512.,  512.,
    };
    
    glViewport(0,0,1024, 1024);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, extFBO);        // actually already bound, above
//    glPushAttrib(GL_VIEWPORT_BIT);
    
    // Render as normal here
    // output goes to the FBO and it’s attached buffers
    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();    
    
//    glBlendFunc(GL_SRC_ALPHA, GL_ZERO);        // GL_ZERO or GL_ONE
    glActiveTexture(GL_TEXTURE0);
    glEnable(GL_TEXTURE_2D);    
    glEnableClientState(GL_VERTEX_ARRAY);
    
    // draw the background texture to the fbo
    glBindTexture(GL_TEXTURE_2D, bgWorldMap);
    glGenerateMipmapOES(GL_TEXTURE_2D);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glVertexPointer(2, GL_FLOAT, 0, vertices);
    glColor4f(1, 1, 1, 1);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);    
    
//    glPopAttrib();
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
    glDisable(GL_TEXTURE_2D);
//    glDisable(GL_DEPTH_TEST);
    
}

And then in drawView(), this works, shows the map

Code:
glBindTexture(GL_TEXTURE_2D, bgWorldMap);    // this was working
    // glBindTexture(GL_TEXTURE_2D, fboTexture);

And this does not work, shows all white:

Code:
//    glBindTexture(GL_TEXTURE_2D, bgWorldMap);    // this was working
    glBindTexture(GL_TEXTURE_2D, fboTexture);

thanks!!!!!!


openGL n00b looking for FBO drawing examples or direction - lookitsash - Oct 29, 2009 08:44 AM

did you ever find a solution to your issue with the white texture?


openGL n00b looking for FBO drawing examples or direction - Eskema - Nov 4, 2009 02:16 AM

auptown Wrote:
Code:
-(void)renderFBO{

    const GLfloat vertices[] = {
        -512., -512.,
        512., -512.,
        -512.,  512.,
        512.,  512.,
    };

I think this is the main problem, the vertex are always in 0.0 to 1.0 range, 512.0 gets out of range, and that's why you get a black or white texture