Results 1 to 2 of 2

Thread: initializing the openGL display in objC

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2008
    Posts
    2

    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) awakeFromNibid)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) drawRectNSRect)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

  2. #2
    Junior Member Newbie
    Join Date
    Jul 2008
    Posts
    2

    Re: initializing the openGL display in objC

    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) drawRectNSRect)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?

Similar Threads

  1. Initializing OpenGL in Windows 7 - WITHOUT GLUT
    By Lesrd in forum OpenGL: Basic Coding
    Replies: 11
    Last Post: 09-15-2011, 07:50 PM
  2. Using glTranslate without initializing opengl
    By Umair Aftab in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-22-2005, 07:13 AM
  3. initializing openGL window
    By mfarhan1 in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 11-14-2003, 11:34 PM
  4. Initializing openGL
    By JLawson in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 09-29-2001, 08:48 PM
  5. Initializing OpenGL
    By NewROmancer in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 07-17-2000, 10:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean