Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: glGenBuffers returns 0

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2011

    glGenBuffers returns 0


    I am working on OS X Mountain Lion and Xcode 4.6. I am using the Cocoa APIs to create my windows and handle events. I can't seem to get a vertex buffer generated for some reason. I use NSLog() to print the value of my buffer, but it comes out as 0. Could someone please assist me? Here is my code, it is inside of a custom NSOpenGLView:

    Code :
    @implementation MyOpenGLView
    static const GLfloat g_vertex_buffer_data[] = {
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f,  1.0f, 0.0f,
        NSOpenGLPixelFormatAttribute attrs[] =
            NSOpenGLPFADepthSize, 24,
            // Must specify the 3.2 Core Profile to use OpenGL 3.2
        NSOpenGLPixelFormat *pf = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];
        if (!pf)
            NSLog(@"No OpenGL pixel format");
        NSOpenGLContext *context = [[[NSOpenGLContext alloc]initWithFormat:pf shareContext:nil]autorelease];
        [self setPixelFormat:pf];
        [self setOpenGLContext:context];
        GLuint vertexBuffer;
        glGenBuffers(1, &vertexBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
    - (id)initWithFrame:(NSRect)frame
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code here.
        return self;
    - (void)drawRect:(NSRect)dirtyRect
        glClearColor(0.0, 0.0, 0.0, 1.0);
        NSLog(@"drawRect: is called");

    I get a black window like expected with no errors or warning. My vertex buffer object is 0 for some reason.

  2. #2
    Junior Member Regular Contributor
    Join Date
    Nov 2012
    Oldenburg, Germany
    Not knowing anything about MACs I may tap in the dark, but: Your gl-specific code seems ok, so it has got to be something else. What I notice is that you don't check for "context" to be non-NULL respectively to be initialized successfully. I'd check that, although I do not know if the black Screen rules this out.
    Last edited by hlewin; 07-23-2013 at 09:46 PM.

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2011
    Thanks! Though my context was set up correctly, you led me to think something context-wise was wrong; you were right. Apparently on OS X any OpenGL calls pertaining to setting up OpenGL must go in prepareOpenGL: in the NSOpenGLView subclass. Thanks Again!
    Last edited by Blakeasd; 07-24-2013 at 05:54 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