Here are are the simplest steps to get something on the screen using the O3D Camera.
In the header file, import the O3D and OpenGL headers (the Cocoa headers should already have a line importing them):
#import <Cocoa/Cocoa.h> #import <OpenGL/OpenGL.h> #import <O3D/O3D.h>
@interface OGLV : NSOpenGLView { // your instance variables; OGLV should be whatever your class is named }
We need two methods in our NSOpenGLView subclass. The first method is called prepareOpenGL. This method is called automatically by OS X as soon as the graphics context for the NSOpenGLView is ready to be utilized and is where most of your initialization code should be entered. Add the following to your implementation file:
- (void)prepareOpenGL { // initialization code will go here }
First thing we'll do is create our camera. Declare a variable called cam of type O3D_Camera in your header file, and then place the following code into your prepareOpenGL method:
cam = [[[O3D_Camera alloc] init] retain]; [cam setPerspectiveWithFOV:45.0f aspect:1.0 zNear:.1f zFar:100];
This code loads our camera, retains it (you should release it in a dealloc call in your class) and then setsup the perspective matrix for the camera. This perspective matrix will be loaded in every frame of animation
Next we'll setup a background color for our view, again placing the code inside our prepareOpenGL method.
glClearColor( 1.0f, 1.0f, 1.0f, 1.0f ); // rgba ... this is white
Now that we've got a background color selected and our camera initialized, we can draw to the screen. We add the method:
-(void) drawRect:(NSRect)bounds { // draw code here}
to our implementation file.
glClear(GL_COLOR_BUFFER_BIT);
Load the identity for the projection matrix, and then multiply it by the camera perspective:
glMatrixMode(GL_PROJECTION); glLoadIdentity(); [cam applyPerspective];
Now we setup our model view and move the camera to its current position (by default this will be facing forward):
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); [cam load];
Draw a black rectangle:
glBegin(GL_QUADS); { glColor4f(0,0,0,1); glVertex3f(-.2, -.2, -2); // lower left glVertex3f( .2, -.2, -2); glVertex3f( .2, .2, -2); glVertex3f( -.2, .2, -2); // upper left } glEnd();
[[self openGLContext] flushBuffer];