I have had some problems and questions in general regarding setting up an OpenGL context and using it correctly on MacOSX using Snow Leopard. There are many different tutorials and code floating around the web that are all different ranging anywhere from 10.4 to 10.6. I intend to not use GLUT for my peripheral interactions and to handle my GL context.
To get to the question at hand, what i want to be able to do is be able to draw my scene as i resize the window. my reshape function isnt working correctly for some reason. I also don’t know whether i should be use the NSOpenGLView or the CustomView in the interface builder. From what I gather it seems that if you need more than the default rendering attributes that you can select in the NSOpenGLView Object then you should use a CustomView. In my case I have subclassed NSOpenGLView and connected my class to this Object in IBuilder.
At any rate, here is my code below. As stated below i really want to be able to go back and forth between ortho space(pixel space) and perspective(world space). I get a warning when trying to use gluOrtho2D()(implicit declaration of function). I am not sure how to get this to work correctly yet so any help is appreciated. Sorry for the output of my code – for some reason it isn’t wanting to format it correctly:
#import "MyOpenGLView.h"
@implementation MyOpenGLView
-(void)reshape {
NSRect rectView = [self bounds];
if( m_rectView.size.width != rectView.size.width || m_rectView.size.height != rectView.size.height ) {
glViewport( 0, 0, rectView.size.width, rectView.size.height );
m_rectView = rectView;
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}
-(void) drawRect:(NSRect)bounds {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 0.85, 0.35);
glBegin(GL_TRIANGLES);
{
glVertex3f(0.0, 0.6, 0.0);
glVertex3f(-0.2, -0.3, 0.0);
glVertex3f(0.2, -0.3, 0.0);
}
glEnd();
glFlush();
}
@end
You got a few things confused: The OpenGL context is not “started”, it is created. In the case of MacOS X a proper context is created by simply instanciating a NSOpenGLView.
Something a lot of tutorials do, but is fundamentally flawed is placing the code setting the projection and viewport in the reshape handler. The far cleaner solution is setting all state when it’s needed in the display function. This brings us to the last misconception, namely that people think, some OpenGL states could be set only once.
gluOrtho2Dis not part of OpenGL, it’s part of GLU, a very old OpenGL helper library, of very little use for today’s modern OpenGL implementations. In the case ofgluOrtho2DI never understood, why this function is there at all, becausegluOrtho2Dcould be recreated byYou see it’s just two additonal parameters.