Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TheLee

macrumors member
Original poster
Aug 5, 2006
71
0
Agh!

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 -
Code:
    static BOOL firstTime = TRUE;
    static GLfloat wtf = 0.0f;
    
    if ( firstTime ) {
        [glContext setView:openGLView];
        firstTime = FALSE;
    }
    
    // clear the buffer
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glLoadIdentity();
    
    // 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 );
    glEnd();
    
    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.
 

TheLee

macrumors member
Original poster
Aug 5, 2006
71
0
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.
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.