OpenGL Display List in Objective-C

Discussion in 'Mac Programming' started by Thethuthinang, Jan 3, 2011.

  1. Thethuthinang macrumors member

    Joined:
    Jan 3, 2011
    #1
    Hi, I am trying to create a display list in openGL and cannot figure out why it is not working. To trouble-shoot the problem, I have put the code for the display list inside "initWithFrame":

    Code:
    GLuint theCube;
    theCube = glGenLists(1);
    glNewList(theCube, GL_COMPILE);
    (more code)
    glEndList();
    
    I have inserted code with the function "glIsList()" to test whether the list was created. This function should return GL_TRUE if it is called after the line:

    Code:
    theCube = glGenLists(1);
    It does not. I have used basically the same code in a non-cocoa context with success. The major difference between that code and this is the objective-C environment and the way in which the view was initiated. Here, I initiated the view with the code:

    Code:
    NSOpenGLPixelFormatAttribute attrs[] = {
    NSOpenGLPFAAccelerated,
    NSOpenGLPFADepthSize, 16, 0
    };
    NSOpenGLPixelFormat* pixFmt = [[[NSOpenGLPixelFormat alloc] initWithAttributes: attrs] autorelease];
    self = [super initWithFrame: frameRect pixelFormat: pixFmt];
    return self;
    
    inserted inside "initWithRect". Is there something about my initialization that does not enable display lists to be made?
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    You may be calling OpenGL functions before you have an OpenGL context. Try putting the following line before the call to glGenLists.

    Code:
    NSOpenGLContext *ctx = [self openGLContext];
    
    The OpenGLContext is lazyily initialized, so the first call to openGLContext will create it.

    This is assuming the block of OpenGL functions is after the call to super's initWithFrame: pixelFormat:. You can't/shouldn't be doing anything before this call. (And checking it doesn't return nil).

    You may also have to move the code from initWithFrame: to awakeFromNib depending on how you're creating your NSOpenGLView-subclass object. If you're doing it by calling initWithFrame: somewhere else in your code, it will be fine. If you doing it via Interface Builder, it will have to be moved.
     
  3. Thethuthinang thread starter macrumors member

    Joined:
    Jan 3, 2011
    #3
    I put the list code in "awakeFromNib", but the list was still not generated (so I guess the openGL context was not created by the time it was called). So, I used "[self openGLContext]" and now the list is recognized (does this mean that calling the "openGLContext" method forced the finalization of the initialization?)
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    If you want to think of it that way, than yes. Really it just forces the creation of the OpenGL context if needed.
     

Share This Page