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

Started by
3 comments, last by eleidon 9 years, 10 months ago

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: http://i.imgur.com/0SDGrge.jpg

I bind the texture to a FBO like this:


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:


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:


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!

Advertisement

Based on the size of the squashed smiley face, your offscreen render target is 512x256 pixels. When you transfer the 512x256 pixel size texture onto the 320x240 screen the image gets squashed.

What code do you use to copy the offscreen texture to the screen?

My current game project Platform RPG

Based on the size of the squashed smiley face, your offscreen render target is 512x256 pixels. When you transfer the 512x256 pixel size texture onto the 320x240 screen the image gets squashed.

What code do you use to copy the offscreen texture to the screen?

Argh, I feel so stupid now -- the reason why things weren't properly drawn on the render texture was because I wasn't using its actual power of two dimensions!

E.g. my projection code when having a render target should've been:


glViewport(0, 0, textureWidthPowerOfTwo, textureHeightPowerOfTwo);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho((GLdouble) 0,
        (GLdouble) textureWidthPowerOfTwo,
        (GLdouble) 0,
        (GLdouble) textureHeightPowerOfTwo,
        0.0, 1.0);
glMatrixMode(GL_MODELVIEW);

Now everything seems to work perfectly and it's all because you helped me put my thinking on the right track.

Thanks so much for your time -- really appreciate it! biggrin.png

Keep in mind, FBO is not core to 2.1 - so *if* you're using pure 2.1+shaders for legacy card support, you might consider not using FBOs. Yes, most modern cards that still use 2.1 also have FBO extension, but not all of them. PBO would be what's needed.

Ah OK, I'm mostly just playing around with OpenGL currently to just get the hang of it, but that's good to know!

Thanks for the heads up! smile.png

This topic is closed to new replies.

Advertisement