Small error with glViewport and gluPerspective?
I'm working on an OpenGL app that displays a single object with perspective inside a window. When the user resizes the window, the field of view changes accordingly. For example, when the height of the window is 1024 pixels, the fovy is 60 degrees, and when the window is halved to 512 pixels, the fovy is 30 degrees, etc. Changes to the window's width work similarly.
The goal is for the object to always take up the exact same number of pixels, regardless of the window size. This works perfectly when resizing horizontally, however, when resizing vertically, the object changes size by a few percent. I can't seem to figure out why. Can anyone find my problem, or, propose an alternative solution? Thanks in advance. Here's my code:
The goal is for the object to always take up the exact same number of pixels, regardless of the window size. This works perfectly when resizing horizontally, however, when resizing vertically, the object changes size by a few percent. I can't seem to figure out why. Can anyone find my problem, or, propose an alternative solution? Thanks in advance. Here's my code:
Code:
#import "GLView.h"
#import <GLUT/glut.h>
@implementation GLView
- (id) initWithFrame:(NSRect)frame
{
NSOpenGLPixelFormatAttribute attrs[] =
{
NSOpenGLPFADoubleBuffer,
NSOpenGLPFANoRecovery,
NSOpenGLPFAAccelerated,
NSOpenGLPFAWindow,
NSOpenGLPFAColorSize, 24,
NSOpenGLPFAAlphaSize, 8,
0
};
NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
self = [super initWithFrame:frame pixelFormat:pixelFormat];
return self;
}
- (void) drawRect:(NSRect)bounds
{
[[self openGLContext] makeCurrentContext];
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -3.0);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3d(1.0, 1.0, 1.0);
glutWireCube(1.0);
[[self openGLContext] flushBuffer];
[self setNeedsDisplay:YES];
}
- (void) reshape
{
[[self openGLContext] makeCurrentContext];
NSSize size = [self bounds].size;
double aspect = size.width / size.height;
double fovy = 60.0 * size.height / 1024.0;
glViewport(0, 0, (int)size.width, (int)size.height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fovy, aspect, 0.1, 10.0);
}
@end
The problem is with
You won't get away without doing a bit of trigonometry to figure out what the real angle should be, instead of this linear approximation.
Code:
double fovy = 60.0 * size.height / 1024.0;You won't get away without doing a bit of trigonometry to figure out what the real angle should be, instead of this linear approximation.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Is FBO Size Tied To glViewport? | AnotherJake | 13 | 6,814 |
Mar 1, 2008 05:13 PM Last Post: AnotherJake |
|
| picking glOrtho vs gluPerspective | rhiannon | 0 | 3,081 |
Jun 6, 2005 02:20 PM Last Post: rhiannon |
|
| gluPerspective() after glOrtho | tmgergo | 2 | 4,679 |
Nov 19, 2004 07:58 AM Last Post: ThemsAllTook |
|
| how small is a "small" display list? | Diplomtennis | 5 | 3,583 |
Oct 31, 2004 10:38 AM Last Post: Hog |
|
| glOrtho versus gluPerspective and glPixels... | WhatMeWorry | 9 | 7,056 |
Aug 5, 2004 01:44 AM Last Post: NCarter |
|

