Appears as an oval

Member
Posts: 131
Joined: 2010.08
Post: #1
When I use OpenGL to render a circle, it appears as an oval unless the aspect ratio is 1/1. How to render independently of the aspect ratio?
Code:
- (void)awakeFromNib
{
    NSSize viewBounds = [self bounds].size;
    float height = viewBounds.height;
    float width = viewBounds.width;
    [[self window] setContentSize:NSMakeSize(width, height)];
    [[self window] setContentAspectRatio:NSMakeSize(width, height)];
}
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Change your projection.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #3
Could you tell how.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
No, this is a basic question which is much better served by a tutorial than a forum post. GIYF.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #5
It is not explained in http://developer.apple.com/mac/library/d...-CH403-SW2. I can not find it in OpenGL Programming Guide. The word "projection" is written so many times in it that using search is not practical. Could you mention a tutorial in which it is explained.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #6
See The OpenGL FAQ, in particular 9.011.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #7
That does not explain how to solve the problem.
Quote this message in a reply
Moderator
Posts: 3,574
Joined: 2003.06
Post: #8
(Aug 9, 2010 12:21 PM)Miglu Wrote:  I can not find it in OpenGL Programming Guide.

It's in there. Read chapter 3, "Viewing". At least that was the chapter in the third edition. You're looking for information about the projection matrix.

Anyway, you can try this for now, but you really need to study up on how this works:

Code:
- (void) drawRect:(NSRect)rect
{
    glClear(GL_COLOR_BUFFER_BIT);

    // setup projection matrix
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();

    glOrtho(-MY_ASPECT_RATIO, MY_ASPECT_RATIO, -1.0, 1.0, -1.0, 1.0);

    // setup modelview matrix
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    [self ball];

    // pop modelview matrix
    glPopMatrix();

    // pop projection matrix
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

    [[self openGLContext] flushBuffer];
}
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #9
The first google hit for "OpenGL projection" is pretty helpful: http://www.cprogramming.com/tutorial/ope...tions.html The first function the article mentions, glOrtho(), is the one you are looking for even.

That said, OneSadCookie and arekkusu are right that you need to find a tutorial to explain how projection matrices work, read a bunch of the docs, or both. Otherwise your next question is inevitably going to be what to do with glOrtho(). You are only going to find projections as explained by a larger tutorial such as this one: http://nehe.gamedev.net/data/lessons/les...?lesson=17

This also isn't the direct answer that you were looking for, but hopefully points you a little closer in the right direction.

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: 312
Joined: 2006.10
Post: #10
There is also a nice laymen's term description of the projection space here: http://duriansoftware.com/joe/An-intro-t...ction.html
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #11
Ah, yeah that is a much better tutorial on transformation matrices.

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: 131
Joined: 2010.08
Post: #12
Thanks. Now I understand what orthographic and perspective projections are, and using glOrtho works. However, why did the circle look like an oval? It is in the center of the view, so a normal in its center intersects the near clipping plane's center, so it should look like a circle even in perspective projection.
Quote this message in a reply
Moderator
Posts: 3,574
Joined: 2003.06
Post: #13
The GL sets a default projection matrix, which I think is glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); (don't quote that though, as I can't recall exactly), so if you don't explicitly set it yourself, that's what you get. That projection would mean that no matter what size the viewport is, left is always -1, right is always 1, etc. In that case, it all cascades down: non-square Cocoa view == non-square glViewport == non-square projection == non-square objects. You break the chain by setting your projection to compensate for the non-square glViewport. Then it'd look like: non-square Cocoa view == non-square glViewport != aspect compensated projection == square objects.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #14
The default matrices are identity.
Quote this message in a reply
Moderator
Posts: 3,574
Joined: 2003.06
Post: #15
Ah right, thanks for the clarification.
Quote this message in a reply
Post Reply