0

I'm trying to work with a framebuffer object that I have no control of how it is being set. So I'm creating an offscreen fbo with depth to draw my texture. Then draw it to the main framebuffer. I am at the point where I can see the texture from the resources using OpenGL profiler, but I just cant seem to get it to draw to the main framebuffer.

Here is my code

GLsizei glSizeWidthEquiv = width;
        GLsizei glSizeHeightEquiv = height;

        GLint originalFramebuffer;
        glGetIntegerv(GL_FRAMEBUFFER_BINDING, &originalFramebuffer);

        if(fbo == 0){
            glGenFramebuffers(1, &fbo);
        }
        glBindFramebuffer(GL_FRAMEBUFFER, fbo);

        //Creating texture for framebuffer
        if(texColorBuffer == 0){
            glGenTextures(1, &texColorBuffer);
        }
        glBindTexture([outTex target], texColorBuffer);
        //Having it as null means the texture's data will be created dynamically
        //Using RGBA since the alpha layer is going to be needed
        glTexImage2D([outTex target], 0, GL_RGBA_FLOAT16_APPLE, glSizeWidthEquiv, glSizeHeightEquiv, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

        glTexParameteri([outTex target], GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri([outTex target], GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        //Attach texture to framebuffer
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, [outTex target], texColorBuffer, 0);

        //Creating render buffer
        if(renderBuffer == 0){
            glGenRenderbuffers(1, &renderBuffer);
        }
        glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, glSizeWidthEquiv, glSizeHeightEquiv);

        //Binding the renderbuffer to the framebuffer
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderBuffer);

        //Check framebuffer
        if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
            NSLog(@"Framebuffer incomplete noob\n");
        }else{
            NSLog(@"Framebuffer COMPLETE!\n");
        }
        //Start to use the frame buffer
        glBindFramebuffer(GL_FRAMEBUFFER, fbo);
        glViewport(0, 0, glSizeWidthEquiv, glSizeHeightEquiv);
        //cleaning texture: cleaning code here

        //initating glTex0
        glActiveTexture(GL_TEXTURE0);

        //setting up texture stuff
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                    glClearColor(0, 0, 0, 1);
        glDisable(GL_CULL_FACE);

        glColor3f(1,0,0);


        glBegin(GL_QUADS);
        //box 1 in front
        glVertex3f(0, 0, 0);
        glVertex3f(500, 0, 0);
        glVertex3f(500, 500, 0);
        glVertex3f(0, 500, 0);

        //box 2 at the back
        glColor3f(0, 1, 0);
        glVertex3f(-250, -250, 50);
        glVertex3f(250, -250, 50);
        glVertex3f(250, 250, 50);
        glVertex3f(-250, 250, 50);
        glEnd();

        glFogCoordf(9);
        //End of framebuffer usage
        glBindFramebuffer(GL_FRAMEBUFFER, originalFramebuffer);
        glViewport(0, 0, (GLsizei)[outTex width], (GLsizei)[outTex height]);
        [self glGetError];

        glActiveTexture(GL_TEXTURE0);
        glBindTexture([outTex target], texColorBuffer);
        glCopyTexSubImage2D([outTex target], 0, 0, 0, 0, 0, glSizeWidthEquiv, glSizeHeightEquiv);


        glFlush();

This part of the program is at the render portion and I set the fbo,renderBuffer and texColorBuffer as 0 on initialize for me to keep it from generating new buffers.

So essentially I am creating the buffer until its complete, then rebinding the fbo I created to draw my shape. Switch back to the original fbo and try to draw it.

The [outTex target] being used here is GL_TEXTURE_RECTANGLE_ARB.

This is what I have in my texture portion of OpenGL Profiler: http://img.photobucket.com/albums/v442/ardo/ScreenShot2013-10-27at112742PM.png

@GMasucci

I'm not entirely sure if this is the correct way of creating an sscce, but here is a code that is easily copy pasted, include the libraries, GLUT and OpenGL. The code used here is piggy-backed off of www.videotutorialsrock.com lighting lessons so that I have a similar environment.

#include <iostream>
#include <stdlib.h>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

GLuint fbo,texColorBuffer,renderBuffer;
float  camMatrix [4][4];
//Initializes 3D rendering
void initRendering() {
    glEnable(GL_DEPTH_TEST);

}

//Called when the window is resized
void handleResize(int w, int h) {
    glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}

//Draws the 3D scene
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(0.0f, 0.0f, -8.0f);


if(fbo == 0){
    glGenFramebuffers(1, &fbo);
}
glBindFramebuffer(GL_FRAMEBUFFER, fbo);

if(texColorBuffer == 0){
    glGenTextures(1, &texColorBuffer);
}
glBindTexture(GL_TEXTURE_2D, texColorBuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 400, 400, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);

if(renderBuffer == 0){
    glGenRenderbuffers(1, &renderBuffer);
}
glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 400, 400);

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderBuffer);

if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
    printf("Error in framebuffer completeness \n");
}
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glViewport(0, 0, 400, 400);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glActiveTexture(GL_TEXTURE0);

glBegin(GL_QUADS);
glColor3f(1,0,0);
glVertex3f(0, 0, 0);
glVertex3f(200, 0, 0);
glVertex3f(200, 200, 0);
glVertex3f(0, 200, 0);

glColor3f(0, 1, 0);
glVertex3f(-100, -100, -1);
glVertex3f(100, -100, -1);
glVertex3f(100, 100, -1);
glVertex3f(-100, 100, -1);

glEnd();

glFogCoordf(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, (GLsizei)400, (GLsizei)400);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texColorBuffer);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 400, 400);

glutSwapBuffers();
}

void update(int value) {
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);

//set fbo,texBuffer and renderbuffer to 0
fbo = 0;
texColorBuffer = 0;
renderBuffer = 0;

//Create the window
glutCreateWindow("Lighting - videotutorialsrock.com");
initRendering();

//Set handler functions
glutDisplayFunc(drawScene);
glutReshapeFunc(handleResize);

glutTimerFunc(25, update, 0); //Add a timer

glutMainLoop();
return 0;
}
ardowz
  • 47
  • 8
  • Any chance you could post an [sscce](http://sscce.org/) which would help us to see how we can help? As at the moment from what you have above we might all try our best and still avoid an error which lies outwith what you have placed above as the code. Cheers:) – GMasucci Oct 28 '13 at 11:39

1 Answers1

0

Never using glCopyTexSubImage2D before, this is my best interpretation from the opengl man pages.

from http://www.opengl.org/sdk/docs/man/xhtml/glCopyTexSubImage2D.xml

Blockquote glCopyTexSubImage2D replaces a rectangular portion of a two-dimensional texture image, cube-map texture image or a linear portion of a number of slices of a one-dimensional array texture with pixels from the current GL_READ_BUFFER (rather than from main memory, as is the case for glTexSubImage2D).

It sounds like you are replacing what is in texColorBuffer with originalFramebuffer.

The way that I draw framebuffer objects to the screen is with textured fullscreen quads as in the following:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0, 1);
glPushMatrix();
    GLfloat identity[4][4];
    for (int i=0; i<4; ++i){
        for (int j=0; j<4; ++j){
            identity[i][j] = 0;
        }
    }
    identity[0][0] = identity[1][1] = identity[2][2] = identity[3][3] = 1;
    glMatrixMode(GL_MODELVIEW);
    glLoadMatrixf(identity);
    glMatrixMode(GL_PROJECTION);
    glLoadMatrixf(identity);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture([outTex target], texColorBuffer);

    glBegin(GL_QUADS);
        glVertex2f(-1,-1);
        glTexCoord2f(0, 0);

        glVertex2f( 1,-1);
        glTexCoord2f(1, 0);

        glVertex2f( 1, 1);
        glTexCoord2f(1, 1);

        glVertex2f(-1, 1);
        glTexCoord2f(0, 1);
    glEnd();
glPopMatrix();
tamato
  • 838
  • 7
  • 15
  • I tried binding the texture from the fbo and draw a quad like your example but only ended up with the color of one of the squares I included in my pseudo SSCCE. Also In it, I created 2 squares of different z coordinates to differentiate its distance and I wanted to render it including its z coordinates. – ardowz Oct 28 '13 at 22:23
  • yeah only the green one – ardowz Oct 29 '13 at 14:53
  • I got it to work by using glBlitframebuffer, apparently I was adding extra framebuffers that I did not need, which just keeps getting rendered off screen. – ardowz Nov 11 '13 at 18:47