initializing the openGL display in objC

Hi all,

Writing my first objC/openGL program (I’m used to just C) on my Mac, and I’m having a bit of trouble getting my NSOpenGLView window to display properly when the program first starts.

Right now, when the program starts, it shows the axes drawn, but with no rotation. To get it to display correctly I have to activate the “reset view” option I’ve put into the program, and after that it seems to work fine. How can I get the program to correctly read in the rotation variables before it draws the initial view?

Code below. Thanks,
-Jason

#import “GLObjPosPreview.h”
#include <OpenGL/gl.h>
#include <GLUT/glut.h>

@implementation GLObjPosPreview

  • (void) awakeFromNib:(id)sender
    {
    theta = -45;
    phi = -30;
    }

  • (id) init{
    self = [super init];
    theta = -45;
    phi = -30;
    return self;
    }

  • (void) drawSomething{
    glColor3f(0.0f, 0.0f, 1.0f); //draws the X-axis in blue
    glBegin(GL_LINES);{
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(5.0, 0.0, 0.0);
    }
    glEnd();
    glColor3f(1.0f, 0.0f, 0.0f); //draws the Y-axis in red
    glBegin(GL_LINES);{
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 5.0, 0.0);
    }
    glEnd();
    glColor3f(0.0f, 1.0f, 0.0f); //draws the Z-axis in green
    glBegin(GL_LINES);{
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(0.0, 0.0, 5.0);
    }
    glEnd();
    }

  • (void) drawRect:(NSRect)bounds{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glEnable(GL_DEPTH_TEST_;

    glPerspective(90, 1.0, 1.0, 16.0); //set the view properties
    gluLookAt(7.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); //set the eye position with Z-axis up
    glRotate(theta, 0, -1, 0); //rotate the Z-axis of the view
    glRotate(phi, 0, 0, 1); //rotate the view around the Z-axis
    [self drawSomething];
    glFlush();
    }

  • (void) resetView{
    zoom = 90;
    theta = -45;
    phi = -30;
    [self drawRect:[self bounds]];
    }

@end

just an update - I’ve found that I can get it to work by calling the resetView function in the first line of drawRect as a conditional:

  • (void) drawRect:(NSRect)bounds{
    if(firstrun==1){ firstrun=0; [self resetView];}

    [self drawSomething];
    glFlush();
    }

but ideally I’d like to avoid using this method, I would think there would be a more natural way to do it? Any Suggestions?