1

I'm hoping to make a custom NSOpenGL view on Cocoa. However, I'm having trouble using VAOs. In particular, when run, this dummy test view:

/// OpenGLTestView.h:
@interface OpenGLTestView : NSOpenGLView

- (void)prepareOpenGL;

@end

/// OpenGLTestView.m:
#import <OpenGL/gl3.h>

@implementation OpenGLTestView

- (void)prepareOpenGL {
    GLenum error;
    GLuint vertex_array;
    glGenVertexArrays(1, &vertex_array);
    error = glGetError();
    switch (error) {
        case GL_NO_ERROR:
            printf("GL_NO_ERROR\n");
            break;
        case GL_INVALID_ENUM:
            printf("GL_INVALID_ENUM\n");
            break;
        case GL_INVALID_VALUE:
            printf("GL_INVALID_VALUE\n");
            break;
        case GL_INVALID_OPERATION:
            printf("GL_INVALID_OPERATION\n");
            break;
        case GL_INVALID_FRAMEBUFFER_OPERATION:
            printf("GL_INVALID_FRAMEBUFFER_OPERATION\n");
            break;
        case GL_OUT_OF_MEMORY:
            printf("GL_OUT_OF_MEMORY\n");
            break;
        default:
            printf("Unknown error\n");
            break;
    }
}

@end

prints this on output:

GL_INVALID_OPERATION

Does Apple do something special with VAOs on cocoa? Or is there some initialization step that I'm missing?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Litherum
  • 22,564
  • 3
  • 23
  • 27
  • Did you try `glGenVertexArraysAPPLE` (and the associated `glBindVertexArraysAPPLE`)? Otherwise I would only expect it to work if you've installed GLEW or something. – beaker Jul 12 '12 at 16:02
  • According to http://stackoverflow.com/questions/8302625/segmentation-fault-at-glgenvertexarrays-1-vao I just need to initialize GLEW. I thought I didn't need to because glGenVertexArrays is part of the regular OpenGL spec. I guess I'll research what exactly the relationship between GLEW and glGenVertexArrays is. If you post what you said again as an answer, I'll accept this as an answer. Thanks. – Litherum Jul 13 '12 at 18:26

1 Answers1

2

The problem was that I was not creating an OpenGL 3.2 context (but calling 3.2 functions on it). Using the NSOpenGLPFAOpenGLProfile attribute set to NSOpenGLProfileVersion3_2Core to create a new context, then calling - (void)setOpenGLContext:(NSOpenGLContext *)context on the NSOpenGLView with the new context solves the problem.

Litherum
  • 22,564
  • 3
  • 23
  • 27