can't draw primitives into FBO

Hi all,

I’m trying to draw into a texture created with an FBO, and for some reason, I get the clear color, but not the primitive. I feel like it’s got to be something silly I’m forgetting, but can’t figure it out. Can anyone help? I’m pasting my test.cpp file. I’m running on linux (Karmic Ubuntu), with NVidia gl/glx. It says the FBO is complete, and that it is rendering the box, but I don’t see the box. Thanks for any help!


#define GL_GLEXT_PROTOTYPES
#include <GL/glut.h>
#include <GL/glext.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int TEX_WIDTH = 1024;
int TEX_HEIGHT = 256;
unsigned char* texBuffer;
int components = 3;

GLuint m_framebuffer;
GLuint m_texture;
GLuint m_fbStatus;

void createSurface()
{
    int numPixels = components*TEX_WIDTH*TEX_HEIGHT;
    texBuffer = new unsigned char[numPixels];
    ::memset(texBuffer, 0xff, numPixels);
}

void addBox()
{
    if (m_fbStatus == GL_FRAMEBUFFER_COMPLETE)
    {
        printf("drawing box
");
        glDisable(GL_CULL_FACE);
        glDisable(GL_DEPTH_TEST);
        glClearColor(1.0, 0.0, 0.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
        glShadeModel(GL_FLAT);
        glColor3f(0.0, 0.0, 1.0);
        glDisable(GL_TEXTURE_2D);
        glBegin(GL_QUADS);
            //bottom left
            glColor3f(0.0, 0.0, 1.0);
            glVertex2f(100.0, 0.0);
            //top left
            glColor3f(0.0, 0.0, 1.0);
            glVertex2f(100.0, TEX_HEIGHT);
            //top right
            glColor3f(0.0, 0.0, 1.0);
            glVertex2f(200.0, TEX_HEIGHT);
            //bottom right
            glColor3f(0.0, 0.0, 1.0);
            glVertex2f(200.0, 0.0);
            glEnd();
            glFlush();
    }
}

void setupRenderToTexture(GLint internalFormat, GLenum format)
{

    glGenFramebuffers(1, &m_framebuffer);
    glGenTextures(1, &m_texture);
    glBindTexture(GL_TEXTURE_2D, m_texture);
    glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, TEX_WIDTH, TEX_HEIGHT,
            0, format, GL_UNSIGNED_BYTE, texBuffer);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texture, 0);
    //don't need a depth buffer
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);

    m_fbStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (m_fbStatus == GL_FRAMEBUFFER_COMPLETE)
    {
        printf("framebuffer complete
");
    }
    else
    {
        printf("framebuffer incomplete
");
    }

    addBox();

}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, w, 0, h);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void display()
{
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glEnable(GL_TEXTURE_2D);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

    glBegin(GL_QUADS);
        glColor3f(1.0, 0.0, 0.0);
        glTexCoord2f(0.0, 0.0);
        glVertex2f(0.0, 0.0);

        glColor3f(1.0, 1.0, 0.0);
        glTexCoord2f(0.0, 1.0);
        glVertex2f(0.0, (float)TEX_HEIGHT);

        glColor3f(0.0, 0.0, 1.0);
        glTexCoord2f(1.0, 1.0);
        glVertex2f((float)TEX_WIDTH, float(TEX_HEIGHT));

        glColor3f(0.0, 1.0, 0.0);
        glTexCoord2f(1.0, 0.0);
        glVertex2f((float)TEX_WIDTH, 0.0);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    glFlush();
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(TEX_WIDTH, TEX_HEIGHT);
    glutInitWindowPosition(100, 100);
    glutCreateWindow(argv[0]);
    createSurface();
    setupRenderToTexture(GL_RGB, GL_RGB);
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMainLoop();

    delete [] texBuffer;
    glDeleteFramebuffers(1, &m_framebuffer);
    glDeleteTextures(1, &m_texture);
    return 0;
}

what is inside ur projection and modelview matrices ?

dont forget to set ur viewport as the same of ur texture size
with glViewport(0,0,teexture_width,texture_height); before rendering to texture.

That’s a good point, I didn’t explicitly set those. So I added in

glViewport(0, 0, TEX_WIDTH, TEX_HEIGHT);
glMatrixMode(GL_PROJECTION)
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

in the addBox method, but that didn’t make any difference.

With identity matrices for projection and modelview matrices, your box vertices will be outside the viewing volume, and the whole box will be clipped away.

If you want to draw in texel coordinates, you need to set something like gluOrtho2D(0, TEX_WIDTH, 0, TEX_HEIGHT) for your FBO-rendering projection matrix.

projection matrix tell opengl how ur vertices will be mapped to the camera screen , u can do a perspective projecton or orthographic projection:
see viewing chapter in red book:
http://www.glprogramming.com/red/chapter03.html

the identity matrix in ur projection matrix will not help u because ur vertices are outside the viewing volume as kelvin said !

Thanks all for your responses. Fixed my projection stuff, and it works