Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: OpenGL 2.1 - FBO render to texture 2D projection(?) problem

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2014
    Posts
    2

    OpenGL 2.1 - FBO render to texture 2D projection(?) problem

    Hi!

    For the past few of days I've been completely stumped by what I think is a projection issue I'm having,
    when rendering to a texture using a FBO. I've read up on many tutorials and have followed them to
    the best of my ability, by setting the viewport (glViewport) and the projection matrix (glOrtho) according to the render
    texture's dimensions, but the draw result always end up skewed unless the texture I'm currently drawing
    onto the render texture has the same width and height. If the texture has the same aspect ratio as the
    render texture but different dimensions the result wont appear skewed but incorrectly scaled instead.

    Here is a picture of me drawing a texture (the yellow square face) with the dimensions 128x128 to a
    rendertexture (red area) with 320x240 as its dimensions and then drawing the same texture (yellow square face) translated
    a bit the right without having a FBO bound -- which works correctly: i.imgur.com/0SDGrge.jpg

    I bind the texture to a FBO like this:
    Code :
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, static_cast<GLuint>(frameBufferObjID));
     
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
                                          GL_TEXTURE_2D, textureID, 0);

    When I have a render texture I set the projection matrix like this:
    Code :
    glViewport(0, 0, textureWidth, textureHeight);
     
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
     
    //I'm setting the glOrtho values like this to prevent the draw results from becoming inverted
    glOrtho((GLdouble) 0,
               (GLdouble) textureWidth,
               (GLdouble) 0,
               (GLdouble) textureHeight,
               0.0, 1.0);
    glMatrixMode(GL_MODELVIEW);

    When I don't have a render texture to draw to, these are the settings I use:
    Code :
    glViewport(0, 0, windowWidth, windowHeight);
     
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho((GLdouble) 0,
               (GLdouble) windowWidth,
               (GLdouble) windowHeight,
               (GLdouble) 0,
               0.0, 1.0);
    glMatrixMode(GL_MODELVIEW);

    As far as drawing goes, I'm simply using glDrawArrays with glVertexPointer, glColorPointer and glTexCoordPointer
    set accordingly.

    My questions is; what am I doing wrong? Thanks in advance and I'm really sorry if I missed something obvious!

  2. #2
    Junior Member Newbie
    Join Date
    Jun 2014
    Posts
    2
    Thanks to a kind soul at another forum, I was able to figure out what the cause was! Turns out I forgot and wasn't taking
    the render texture's actual power of two dimensions into account when drawing to it. E.g. the correct projection code is:
    Code :
    glViewport(0, 0, textureWidthPowerOfTwo, textureHeightPowerOfTwo);
     
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
     
    //I'm setting the glOrtho values like this to prevent the draw results from becoming inverted
    glOrtho((GLdouble) 0,
               (GLdouble) textureWidthPowerOfTwo,
               (GLdouble) 0,
               (GLdouble) textureHeightPowerOfTwo,
               0.0, 1.0);
    glMatrixMode(GL_MODELVIEW);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •