Taxxodium
2004.09.27, 02:53 PM
So, I'm starting to get the hang of programming in OpenGL and so far I'm very pleased with the results. However, I can't get glOrtho to work. When I use it I get a black screen, no graphics, no nothing.
Using gluPerspective does work.
here's some of my initialization code, perhaps there's something missing in there. I'm using Cocoa and so all the code you see here belongs in a subclass of NSOpenGLView
- (id)initWithCoder:(NSCoder*)coder {
self = [super initWithCoder:coder];
NSOpenGLPixelFormatAttribute attrs[] = {
//NSOpenGLPFAFullScreen,
NSOpenGLPFAScreenMask, CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDispla y),
NSOpenGLPFAColorSize, 24,
NSOpenGLPFADepthSize, 16,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
0
};
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
[self setPixelFormat:pixelFormat];
[pixelFormat release];
pixelFormat = nil;
[[self openGLContext] makeCurrentContext];
[self initOpenGL];
[self initObjects];
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(refreshView:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:refreshTimer forMode:NSEventTrackingRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer:refreshTimer forMode:NSModalPanelRunLoopMode];
return self;
}
- (void)initOpenGL {
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_TEXTURE_2D);
glEnable(GL_COLOR_MATERIAL);
}
- (void)reshape {
float ratio;
NSSize bounds = [self bounds].size;
ratio = bounds.width / bounds.height;
glViewport(0, 0, [self frame].size.width, [self frame].size.height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, [self frame].size.width, [self frame].size.height, 0.0f, -100.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
my camera is set at z = -5.0f, x and y are 0.0f
replacing glOrtho with gluPerspective(45.0, ratio, 0.1f, 100.0f); does show me my graphics.
Thanks in advance!
Using gluPerspective does work.
here's some of my initialization code, perhaps there's something missing in there. I'm using Cocoa and so all the code you see here belongs in a subclass of NSOpenGLView
- (id)initWithCoder:(NSCoder*)coder {
self = [super initWithCoder:coder];
NSOpenGLPixelFormatAttribute attrs[] = {
//NSOpenGLPFAFullScreen,
NSOpenGLPFAScreenMask, CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDispla y),
NSOpenGLPFAColorSize, 24,
NSOpenGLPFADepthSize, 16,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
0
};
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
[self setPixelFormat:pixelFormat];
[pixelFormat release];
pixelFormat = nil;
[[self openGLContext] makeCurrentContext];
[self initOpenGL];
[self initObjects];
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(refreshView:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:refreshTimer forMode:NSEventTrackingRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer:refreshTimer forMode:NSModalPanelRunLoopMode];
return self;
}
- (void)initOpenGL {
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_TEXTURE_2D);
glEnable(GL_COLOR_MATERIAL);
}
- (void)reshape {
float ratio;
NSSize bounds = [self bounds].size;
ratio = bounds.width / bounds.height;
glViewport(0, 0, [self frame].size.width, [self frame].size.height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, [self frame].size.width, [self frame].size.height, 0.0f, -100.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
my camera is set at z = -5.0f, x and y are 0.0f
replacing glOrtho with gluPerspective(45.0, ratio, 0.1f, 100.0f); does show me my graphics.
Thanks in advance!