Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 eleidon   Members   -  Reputation: 185

Like
0Likes
Like

Posted 23 June 2014 - 01:10 PM

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!

 



Sponsor:

#2 HappyCoder   Members   -  Reputation: 2884

Like
1Likes
Like

Posted 23 June 2014 - 05:20 PM

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?



#3 eleidon   Members   -  Reputation: 185

Like
0Likes
Like

Posted 24 June 2014 - 06:46 AM

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


Edited by eleidon, 24 June 2014 - 05:01 PM.


#4 3TATUK2   Members   -  Reputation: 730

Like
1Likes
Like

Posted 24 June 2014 - 02:41 PM

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.



#5 eleidon   Members   -  Reputation: 185

Like
0Likes
Like

Posted 24 June 2014 - 05:02 PM

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


Edited by eleidon, 24 June 2014 - 05:03 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS