Appears as an oval
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)];
}
Change your projection.
Could you tell how.
No, this is a basic question which is much better served by a tutorial than a forum post. GIYF.
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.
See The OpenGL FAQ, in particular 9.011.
That does not explain how to solve the problem.
(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];
}
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.
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.
There is also a nice laymen's term description of the projection space here: http://duriansoftware.com/joe/An-intro-t...ction.html
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.
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.
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.
The default matrices are identity.
Ah right, thanks for the clarification.

