0

I must be missing something obvious in using FBO :

I call TMyForm::Init() once at the start of my application :

class TMyForm
{ ...
    private:
    Gluint mTextureId, mFboId;
    int mWidth, mHeight;
}

void TMyForm::Init()
{
    mWidth = 1920;
    mHeight = 1080;
    ...
    // create a texture object
    glEnable(GL_TEXTURE_2D);
    glClearColor ( 0.0, 0.0, 0.0, 1.0 );

    glGenTextures(1, &mTextureId);
    glBindTexture(GL_TEXTURE_2D, mTextureId);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); // automatic mipmap
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1920, 1080, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    glBindTexture(GL_TEXTURE_2D, 0);

    // create a framebuffer object
    glGenFramebuffersEXT(1, &mFboId);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFboId);

    // attach the texture to FBO color attachment point
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D,    mTextureId, 0);

    // switch back to window-system-provided framebuffer
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

And in the Draw() function I send the buffer to a card (the rendered scene is ok) and to a preview window (all black or all white depending if I switch back to default window context or the fbo context) :

void TMyForm::Draw()
{
    // set rendering destination to FBO
    glEnable(GL_TEXTURE_2D);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mFboId);

    //---
    glViewport(-1920, -1080, 1920 * 2, 1080 * 2);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(double(-iWidth)*viewport_ratio, double(iWidth)*viewport_ratio, double(-iHeight), double(iHeight), 1000.0, 100000.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    

    Render();

    glFlush();

    //--- 1 : send to card
    delete[] mBufferPlayout;
    mBufferPlayout = NULL;
    try
    {
        mBufferPlayout = new GLubyte [mWidth * mHeight * 4];

        glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
        glReadPixels(0,0, mWidth, mHeight, GL_BGRA_EXT, GL_UNSIGNED_BYTE, mBufferPlayout);

        DisplayFrame(mBufferPlayout);
    }
    catch (TSCDbException &e)    {        ShowMessage(GetLastError());    }

    //--- 2 : Texturing to the preview window :
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    glBindTexture(GL_TEXTURE_2D, mTextureId);
    //glGenerateMipmapEXT(GL_TEXTURE_2D);

    int iWidthPreview = mWidth / 2; // ie 960
    int iHeightPreview = mHeight / 2; // ie 540
    glViewport(-iWidthPreview, -iHeightPreview, iWidthPreview * 2, iHeightPreview * 2);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(double(-iWidthPreview), double(iWidthPreview), double(-iHeightPreview), double(iHeightPreview), 1000.0, 100000.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -50000.0);
        mxIncrust0 = 0.0;       myIncrust0 = 0.0;
        mxIncrust1 = iWidthPreview;    myIncrust1 = iHeightPreview;
        glBegin(GL_QUADS);
            glColor4ub(255,255,255,255);
            glTexCoord2d(0.0, 0.0); glVertex2d(mxIncrust0, myIncrust0);
            glTexCoord2d(0.0, 1.0); glVertex2d(mxIncrust0, myIncrust1);
            glTexCoord2d(1.0,1.0);  glVertex2d(mxIncrust1, myIncrust1);
            glTexCoord2d(1.0,0.0);  glVertex2d(mxIncrust1, myIncrust0);
        glEnd();
    glDisable(GL_TEXTURE_2D);
    glFlush();
    //---
    SwapBuffers(ghDC);
}

So my problem is the "part 2" which does not do what I wish : texturing the fbo content to the current window. I tried changing the "glTexCoord2d" with no success.

Arnaud
  • 109
  • 3
  • 15
  • Do you have glGetError and glCheckFramebufferStatus in your code anywhere? – Tim Mar 13 '12 at 18:15
  • Your framebuffer is still bound. – Robinson Mar 13 '12 at 18:42
  • And to unbind the framebuffer is it enough to uncomment this line ? (as I have just changed in the code above): //--- 2 : Texturing to the preview window : glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // uncommented line with this "bind to zero" I get a white window (instead of black without it). – Arnaud Mar 14 '12 at 08:26
  • Ok I found : the "glEnable(GL_TEXTURE_2D)" is only needed juste before the "glBegin(GL_QUADS)". – Arnaud Mar 14 '12 at 09:52

0 Answers0