Draw to texture using FBOs - aspect ratio issues

Member
Posts: 110
Joined: 2009.07
Post: #1
Hi folks,

I'm on my second game using FBOs and have run into some odd problems.
I'm trying to create a background texture procedurally to save some load-time.

Here's my initial setup code for all my frame buffers:

Code:
    GLenum status;
    
// Render to texture frame buffer for space background
    glBindTexture(GL_TEXTURE_2D, spaceTextureFramebufferID);    
    glGenFramebuffersOES(1, &spaceTextureFramebufferID);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, spaceTextureFramebufferID);
    glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, spaceTextureID, 0);    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); // VITAL! Otherwise any calls to 'Set viewport' will be attached to this bloody thing

    status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
    if(status != GL_FRAMEBUFFER_COMPLETE_OES)
    {
        DBOut(@"failed to make complete framebuffer object %x", status);
    }    
// End of render to texture frame buffer for space background
    
    glGenFramebuffersOES(1, &viewFramebuffer);
    glGenRenderbuffersOES(1, &viewRenderbuffer);
    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
    
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backBufferWidth);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backBufferHeight);
    
    if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
    {
        DBOut(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
        return NO;
    }
    
    return YES;

On my first render pass, I set up the texture. Here I'm just drawing a line to check it's all working:

Code:
    const unsigned char *col; // Two pointers to two different RGBA fields
    
    needNewSpaceTexture = NO;
    col = [game.solarSystem getPColourPair];
    glLoadIdentity();

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, spaceTextureFramebufferID); // draw to this
// -------------------------------
// REMEMBER THIS LINE HERE
    glViewport(0, 0, SPACE_DIMENSION, SPACE_DIMENSION);
// -------------------------------
    
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);    // Clear The Screen
        
    [self draw2DLineX1: 0 Y1: 0 X2: SPACE_DIMENSION Y2:SPACE_DIMENSION withRGBA: gRGBA[COLOUR_WHITE]]; // A basic draw line - trust me, it works
    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); // Stop the render texture being the primary Frame Buffer

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glViewport(0, 0, backBufferWidth, backBufferHeight);

Now, if I comment out that glViewport line (which I did accidentally at one point) then the aspect ratio is correct and the line draws from one corner of the texture at 45 degrees... and then stops about 2/3 of the way across. For some reason the edge is being masked. I presume it's because the viewport isn't big enough.

However if I uncomment the line (and set the nice, square viewport), the line drawn (and any sprites) are drawn squished, indicating that the aspect ratio is wrong.

Anyone have any ideas why my glViewport doesn't seem to be giving me the correct aspect ratio?

Any help appreciated. OGL isn't my strong point. I thought I understood it fairly well, but this has been driving me nuts all day.

http://www.fluttermind.com
Fluttermind - Games for Everyone
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
Are you stretching the square FBO texture to a non-square portion of the screen? If you are then, you need to be setting the projection matrix to match the FBO dimensions or the actual viewport dimensions?

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 110
Joined: 2009.07
Post: #3
(Jul 15, 2010 11:33 AM)Skorche Wrote:  Are you stretching the square FBO texture to a non-square portion of the screen? If you are then, you need to be setting the projection matrix to match the FBO dimensions or the actual viewport dimensions?

I'm being a bit slow today, so I'm not sure what you mean.
If you mean: are you stretching the texture, thus distorting it, the answer is 'no'.
I test the process out by drawing the texture on screen. The texture shows up as a square of the correct dimensions with a weirdly angled line on it.

If you mean 'are you altering the projection matrix as well as the making the glViewport call' the answer is 'no'. The only consideration I'm making for the size of this texture (it's 256 x 256) is the glViewport call. I've not seen anyone do this this in any of FBO examples, so I didn't think of doing it myself.

http://www.fluttermind.com
Fluttermind - Games for Everyone
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Understanding multisampled FBOs Coyote 12 8,798 Nov 27, 2009 01:01 AM
Last Post: arekkusu
  Texture mapping single 3D object with one draw call jeonghyunhan 1 2,712 Jul 13, 2009 06:05 AM
Last Post: ThemsAllTook
  NPOT FBOs in OpenGL 2.0 cjcaufield 5 3,992 Jun 22, 2009 11:23 PM
Last Post: cjcaufield
  Open GL texture Issues ThorPrime 5 3,817 Jun 13, 2005 11:13 PM
Last Post: ThorPrime
  Ortho + Aspect Ratios jSTIN 2 2,891 Apr 6, 2005 09:21 AM
Last Post: jSTIN