NSOpenGL frustrations!

Discussion in 'Mac Programming' started by TheLee, Nov 6, 2006.

  1. TheLee macrumors member

    Aug 5, 2006

    So I've been working on a game as a side project and am finishing up a map editor. Part of the map editor is a map rendering button. You press it, it should render the map, using OpenGL (via NSOpenGLView/Context).

    However, the major problem is the fact that for some reason depth isn't working at all!

    This is the code from my drawRect: method -
        static BOOL firstTime = TRUE;
        static GLfloat wtf = 0.0f;
        if ( firstTime ) {
            [glContext setView:openGLView];
            firstTime = FALSE;
        // clear the buffer
        // move the origin back
        glTranslatef( 0.0f, 0.0f, wtf );
        wtf -= .05f;
        // render the map
        // [mapRenderer drawMap:targetMap];
        // test: draw a square
        glBegin( GL_QUADS );
            glColor3f( -wtf/3, -wtf/3, -wtf/3 );
            glVertex3f( 1.0f, 1.0f, 0.0f );
            glVertex3f(-1.0f, 1.0f, 0.0f );
            glVertex3f(-1.0f,-1.0f, 0.0f );
            glVertex3f( 1.0f,-1.0f, 0.0f );
        DEBUG_OUTPUT2( "wtf? %f", wtf );
        // flush the buffer
        [glContext flushBuffer];
    If you'll notice, I commented out the actual map rendering routine and replaced it with a simple square rendering.

    wtf is a variable (aptly named for my situation) that basically increases how far the camera is translated "back" on the z axis. I render the square a given color based on wtf so I can get an immediate visual signal of what's going on. DEBUG_OUTPUT2() is just a macro tha spits something out onto stderr.

    what *should* happen is that there should be a square that progressively gets further away and brighter as it does so. what *actually* happens is that the entire viewport is whatever color the square is until wtf = -1.0000, at which point the screen goes black and nothing more happens (visually, at least).

    I'm relatively certain that I set everything else up correctly (but i guess clearly this means I haven't). Can anyone help?? I'll put up some more code if that'll help figure this out.
  2. MrFrankly macrumors regular

    Jan 11, 2006
  3. TheLee thread starter macrumors member

    Aug 5, 2006
  4. TheLee thread starter macrumors member

    Aug 5, 2006
    never mind, i figured it out. Apparently making calls to OpenGL before you set the target drawing view (via setView:) doesn't do anything, so all of my GL settings were being discarded. I simply moved my gl initialization directly after I set the target view and everythign works fine. Cocoa sometimes annoys me.
  5. gekko513 macrumors 603


    Oct 16, 2003
    I'm glad you figured it out, but I wouldn't blame Cocoa for this. It can't assume which OpenGL context you intend to use before you set it somehow. There can be more than one OpenGL context in an application both on screen and off screen.

Share This Page