Objective 3D

Using Objective 3D

Here are are the simplest steps to get something on the screen using the O3D Camera.

Setting Up the Project and the .Nib file

  1. Create a new XCode Cocoa Application project.
  2. Add the O3D framework to your XCode project as well as the OpenGL Framework.
  3. Create a new Objective-C class to represent your OpenGLView
  4. 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>
    			
  5. In the header file for this class, declare your interface to be a subclass of NSOpenGLView as follows:
    @interface OGLV : NSOpenGLView { // your instance variables; OGLV should be whatever your class is named }
  6. Open up the MainMenu.nib file of your XCode project. This will launch Interface Builder.
  7. In Interface Builder, open the Library (shift + cmd + L). Find the NSOpenGLView widget and drag it into your interface window.
  8. With the NSOpenGLView in your window selected, type Shift+Apple+I to bring up its inspector
  9. Go to the Identity tab in the Inspector (second from the right). In the text field marked Class enter the name of you NSOpenGLView subclass. XCode should auto complete this for you if it has recognized your header files.
  10. In the attributes tab, check the option for Double Buffering. Set the color mode to be 32 Bit RGBA.

Defining your NSOpenGLView Subclass : Initialization

  1. 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 } 
  2. 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

  3. 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

Drawing to the screen

  1. 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.

  2. Clear our view:
    glClear(GL_COLOR_BUFFER_BIT);
  3. Load the identity for the projection matrix, and then multiply it by the camera perspective:

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    [cam applyPerspective];
    			
  4. 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];
    			
  5. 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();
    			
  6. Last but not least, flush the buffers:
    [[self openGLContext] flushBuffer];
  7. Build the project! If you didn't want to type this in by hand, you can just download the source and tweak away