iDevGames Forums
Draw to texture using FBOs - aspect ratio issues - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Draw to texture using FBOs - aspect ratio issues (/thread-7864.html)



Draw to texture using FBOs - aspect ratio issues - Madrayken - Jul 15, 2010 10:36 AM

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.


RE: Draw to texture using FBOs - aspect ratio issues - Skorche - Jul 15, 2010 11:33 AM

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?


RE: Draw to texture using FBOs - aspect ratio issues - Madrayken - Jul 15, 2010 11:47 AM

(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.