Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#Actualnecreia

Posted 14 February 2013 - 08:10 PM

I've been battling this long enough that I feel I must throw in the towel and ask for help.  Ultimately, I'm trying to render to a FrameBufferObject, and then render that to another FrameBufferObject.  Since I couldn't get it working in my game engine, I ran out to the net and cobbled together a really basic example in order to remove impacting variables. Even after this, I just can't get it.  See here:
 
    uint16 mDisplayHeight = 1000;
    uint16 mDisplayWidth = 1600;
    int32 mVideoFlags;

    // Init SDL
    SDL_Init(SDL_INIT_VIDEO);
    mVideoFlags  = SDL_OPENGL;          // Use OpenGL in SDL
    mVideoFlags |= SDL_HWPALETTE;       // Hardware enabled palette
    const SDL_VideoInfo* sdlVideoInfo = SDL_GetVideoInfo();
    if (sdlVideoInfo->hw_available)
        mVideoFlags |= SDL_HWSURFACE;   // Hardware enabled surfaces
    else
        mVideoFlags |= SDL_SWSURFACE;   // Software enabled surfaces
    if (sdlVideoInfo->blit_hw)
        mVideoFlags |= SDL_HWACCEL;     // Hardware enabled blitting
    SDL_SetVideoMode(mDisplayWidth, mDisplayHeight, 32, mVideoFlags);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);

    // Initialize OpenGL
    glewInit();
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glDisable(GL_DEPTH_TEST);

    /** First FBO - The Render One **/
    uint32 mFinalFBOTexID = 0;
    uint32 mFinalFBOID = 0;
    glGenFramebuffers(1, &mFinalFBOID);
    glBindFramebuffer(GL_FRAMEBUFFER, mFinalFBOID);
    glGenTextures(1, &mFinalFBOTexID);
    glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mFinalFBOTexID);
    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, mDisplayWidth,
                 mDisplayHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB,
                           mFinalFBOTexID, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    /****/

    /** Second FBO - Intermediate **/
    uint32 mSecondRenderTextureID = 0;
    uint32 mSecondRenderTextureFBO = 0;
    glGenFramebuffers(1, &mSecondRenderTextureFBO);
    glBindFramebuffer(GL_FRAMEBUFFER, mSecondRenderTextureFBO);
    glGenTextures(1, &mSecondRenderTextureID);
    glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mSecondRenderTextureID);
    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 300,
                 300, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB,
                           mSecondRenderTextureID, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    /****/

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, mDisplayWidth, mDisplayHeight, 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);

    // Execution Loop
    while(true)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glBindFramebuffer(GL_FRAMEBUFFER, mFinalFBOID); // X1
        //glBindFramebuffer(GL_FRAMEBUFFER, mSecondRenderTextureFBO); //  X2

        // Draw a quad
        glDisable(GL_TEXTURE_RECTANGLE_ARB);
        glColor3f(1.0f, 1.0f, 0.0f);
        glBegin(GL_QUADS);
            glVertex2i(10, 10);
            glVertex2i(50, 10);
            glVertex2i(50, 50);
            glVertex2i(10, 50);
        glEnd();
        glEnable(GL_TEXTURE_RECTANGLE_ARB);

        /* X2
        glBindFramebuffer(GL_FRAMEBUFFER, mFinalFBOID);
        glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mSecondRenderTextureID);

        glBegin(GL_QUADS);
            glTexCoord2i(0, 100);
            glVertex2i(0, 0);
            glTexCoord2i(100, 100);
            glVertex2i(100, 0);
            glTexCoord2i(100, 0);
            glVertex2i(100, 100);
            glTexCoord2i(0, 0);
            glVertex2i(0, 100);
        glEnd();
        */

        // Draw the 2D Layer on top
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glLoadIdentity();
        glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mFinalFBOTexID);

        glBegin(GL_QUADS);
            glTexCoord2i(0, mDisplayHeight);
            glVertex2i(0, 0);
            glTexCoord2i(mDisplayWidth, mDisplayHeight);
            glVertex2i(mDisplayWidth, 0);
            glTexCoord2i(mDisplayWidth, 0);
            glVertex2i(mDisplayWidth, mDisplayHeight);
            glTexCoord2i(0, 0);
            glVertex2i(0, mDisplayHeight);
        glEnd();

        // Present the information
        SDL_GL_SwapBuffers();
    }
 
This code renders a yellow quad without any problems.  But, if I uncomment the two X2 blocks and comment out the X1 block, I get a black screen.  I can't seem to figure out how to paint to and render that second FBO.
 
Any advice? This is driving me bonkers. Am I approaching the solution wrong?

#1necreia

Posted 14 February 2013 - 11:59 AM

I've been battling this long enough that I feel I must throw in the towel and ask for help.  Ultimately, I'm trying to render to a FrameBufferObject, and then render that to another FrameBufferObject.  Since I couldn't get it working in my game engine, I ran out to the net and cobbled together a really basic example in order to remove impacting variables. Even after this, I just can't get it.  See here:
 
    uint16 mDisplayHeight = 1000;
    uint16 mDisplayWidth = 1600;
    int32 mVideoFlags;

    // Init SDL
    SDL_Init(SDL_INIT_VIDEO);
    mVideoFlags  = SDL_OPENGL;          // Use OpenGL in SDL
    mVideoFlags |= SDL_HWPALETTE;       // Hardware enabled palette
    const SDL_VideoInfo* sdlVideoInfo = SDL_GetVideoInfo();
    if (sdlVideoInfo->hw_available)
        mVideoFlags |= SDL_HWSURFACE;   // Hardware enabled surfaces
    else
        mVideoFlags |= SDL_SWSURFACE;   // Software enabled surfaces
    if (sdlVideoInfo->blit_hw)
        mVideoFlags |= SDL_HWACCEL;     // Hardware enabled blitting
    SDL_SetVideoMode(mDisplayWidth, mDisplayHeight, 32, mVideoFlags);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);

    // Initialize OpenGL
    glewInit();
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glDisable(GL_DEPTH_TEST);

    /** First FBO - The Render One **/
    uint32 mFinalFBOTexID = 0;
    uint32 mFinalFBOID = 0;
    glGenFramebuffers(1, &mFinalFBOID);
    glBindFramebuffer(GL_FRAMEBUFFER, mFinalFBOID);
    glGenTextures(1, &mFinalFBOTexID);
    glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mFinalFBOTexID);
    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, mDisplayWidth,
                 mDisplayHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB,
                           mFinalFBOTexID, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    /****/

    /** Second FBO - Intermediate **/
    uint32 mSecondRenderTextureID = 0;
    uint32 mSecondRenderTextureFBO = 0;
    glGenFramebuffers(1, &mSecondRenderTextureFBO);
    glBindFramebuffer(GL_FRAMEBUFFER, mSecondRenderTextureFBO);
    glGenTextures(1, &mSecondRenderTextureID);
    glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mSecondRenderTextureID);
    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, 300,
                 300, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB,
                           mSecondRenderTextureID, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    /****/

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, mDisplayWidth, mDisplayHeight, 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);

    // Execution Loop
    while(true)
    {
        glEnable(GL_TEXTURE_RECTANGLE_ARB);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glBindFramebuffer(GL_FRAMEBUFFER, mFinalFBOID); // X1
        //glBindFramebuffer(GL_FRAMEBUFFER, mSecondRenderTextureFBO); //  X2

        // Draw a quad
        glDisable(GL_TEXTURE_RECTANGLE_ARB);
        glColor3f(1.0f, 1.0f, 0.0f);
        glBegin(GL_QUADS);
            glVertex2i(10, 10);
            glVertex2i(50, 10);
            glVertex2i(50, 50);
            glVertex2i(10, 50);
        glEnd();
        glEnable(GL_TEXTURE_RECTANGLE_ARB);

        /* X2
        glBindFramebuffer(GL_FRAMEBUFFER, mFinalFBOID);
        glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mSecondRenderTextureID);

        glBegin(GL_QUADS);
            glTexCoord2i(0, 100);
            glVertex2i(0, 0);
            glTexCoord2i(100, 100);
            glVertex2i(100, 0);
            glTexCoord2i(100, 0);
            glVertex2i(100, 100);
            glTexCoord2i(0, 0);
            glVertex2i(0, 100);
        glEnd();
        */

        // Draw the 2D Layer on top
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glLoadIdentity();
        glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mFinalFBOTexID);

        glBegin(GL_QUADS);
            glTexCoord2i(0, mDisplayHeight);
            glVertex2i(0, 0);
            glTexCoord2i(mDisplayWidth, mDisplayHeight);
            glVertex2i(mDisplayWidth, 0);
            glTexCoord2i(mDisplayWidth, 0);
            glVertex2i(mDisplayWidth, mDisplayHeight);
            glTexCoord2i(0, 0);
            glVertex2i(0, mDisplayHeight);
        glEnd();

        // Present the information
        SDL_GL_SwapBuffers();
    }
 
This code renders a yellow quad without any problems.  But, if I uncomment the two X2 blocks and comment out the X1 block, I get a black screen.  I can't seem to figure out how to paint to and render that second FBO.
 
Any advice? This is driving me bonkers. Am I approaching the solution wrong?

PARTNERS