22

I've just upgraded my MacBook Pro to Mavericks (MacOS 10.9), including Xcode. According to Apple's "OpenGL Capabilities Table", this version has support for OpenGL 4.1, but a call to glGetString(GL_VERSION) returns "1.2" and my GLSL 3.30 shader, which begins with "#version 330" refuses to load, saying that version is not supported.

Do I need to do something to Mavericks to enable 4.1 support?

bobl
  • 331
  • 1
  • 2
  • 3
  • Are you requesting a Core context? – genpfault Nov 08 '13 at 19:38
  • 6
    1.2?! Are you sure you don't mean 2.1? – Andon M. Coleman Nov 08 '13 at 19:59
  • I meet the same problem when running the sample code of OpenGL SuperBible Fifth Edition written by Richard Wright, it use glut to create context, after searching on web, people say glut is not in active development, is the problem caused by GLUT? – wenbo qiu Apr 06 '14 at 01:39
  • 3
    @wenboqiu: There is FreeGLUT, which is maintained... the problem with using it on OS X is that it goes through X11. X11 is provided on OS X through a compatibility X server called XQuartz, you cannot get a 3.2+ OpenGL context using the XQuartz server because it does not implement the necessary GLX extension to request a core profile. So that means any framework that uses 3.2 on OS X has to go through native APIs like CGL (Fullscreen and C) or NSOpenGL (Fullscreen/Windowed and Objective C). GLFW3 and many other frameworks do this, FreeGLUT still does not. – Andon M. Coleman Jun 02 '14 at 18:09

6 Answers6

23

When you request your pixel format using one of the lower-level APIs on OS X, you need to add the following to your attribute list in order to use a core profile:

CGL:

  kCGLPFAOpenGLProfile,     kCGLOGLPVersion_3_2_Core

NSOpenGL:

  NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core

Now, while the particular constant is named ...3_2Core, what it actually means is request a context that removes all deprecated features and supports at least OpenGL 3.2 (a core profile, in other words). You can get a 4.1 or 3.3 context using this same constant; in all honesty, including an actual version number in the constant was probably a poor choice.

If you do not specify this when you request your pixel format, OS X will give you kCGLOGLPVersion_Legacy or NSOpenGLProfileVersionLegacy respectively. And this will limit you to OpenGL 2.1 functionality.

If you are using a higher-level framework, then you will need to consult your API reference. However, be aware that on OS X you must have a core profile context to access anything newer than OpenGL 2.1.

Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
  • "Attribute list"? "core profile context"? Something to do with XCode, no doubt. Sadly, I've been avoiding XCode since my application is cross-platform. For the same reason, I've been using GLUT for a framework, but I'm suspecting that if I want to use anything more recent than OpenGL 2.1, I'm going to have to bite the bullet and get more involved with MacOS-specific development. Sigh. In any case, thanks. – bobl Nov 09 '13 at 04:30
  • 6
    @user2507282: When I think of Xcode I think of the monstrosity of a GUI Apple wants you to use to write software for OS X / iOS.. this really has nothing to do with that, CGL and NSOpenGL are two interfaces in OS X to creating and managing render contexts. At any rate, do not use GLUT if you want modern OpenGL while still hiding the low-level OS specific stuff; I would suggest you use something like glfw3, you must have a core profile context to use GL 3.2+ on OS X, and an ancient framework like GLUT just is not going to cut it. – Andon M. Coleman Nov 09 '13 at 05:38
  • `glGetString(GL_VERSION)` returns `4.1 ATI-1.38.3` for me yet it still refuses to compile `#version 130` shaders. It seems to accept `#version 330` shaders, though I don't yet know how to update my shader code to 330...I'm guessing I'm having a completely separate issue? – Andy Dec 31 '15 at 17:14
  • disclaimer: I'm using JOGL, but that is the output of its `glGetString` wrapper. – Andy Dec 31 '15 at 17:15
  • OS X does not implement GLSL 1.30. They implement 1.20 in the legacy driver and 3.20 in the core. You can get various extensions that touch on 1.40 and 1.50 in the legacy driver though. – Andon M. Coleman Dec 31 '15 at 18:25
14

Use the OpenGL library GLFW the latest version is 3.0.4... right after you initialize glfw init

if (!glfwInit())
{
    printf("glfwInit() fail to initualize. \n");
    glfwTerminate();
    exit(-1);
}

after you initialize glfwInit() include these 4 lines of code. these four line of code will enable you to use the highest version supported by you OS. on mac its opengl 4.1

glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);

then create your window.

_Window = glfwCreateWindow(width, height, title, 0, 0);

check to make sure it was created.

if (!_Window) {
    printf("Display window fail to create. \n");
    glfwTerminate();
    exit(-1);
}

then make it you current window using the following.

glfwMakeContextCurrent(_Window);

after you make it your cureent window all thats left to be done is to create you main loop.

while (!glfwWindowShouldClose(_Window))
{
    ........
    glfwSwapBuffers(_Window);
    glfwPollEvents();
}

make sure you includ glfwPollEvents(); in the loop this makes it possible to use the close botton to quit the window. if you having trouble compiling the library in xcode just message me on here and i will send you a copy of the library.

kanthonye
  • 149
  • 1
  • 4
2

I struggled a long time on this, and finally succeeded on using any glsl version supported by the graphics card.

There are several main points:

  1. Use CORE PROFILE
  2. Set the MAJOR.MINOR to version of GL you want to use
  3. If not the newest version of GL used, you have to enable FORWARD COMPATIBILITY.

for example, as pointed out by @kanthonye, if you are using glfw, and use gl version 3.2, these lines are needed:

glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
squid
  • 2,597
  • 1
  • 23
  • 19
  • This also worked for me, but *only* after I removed glew. glew and glfw are supposed to be compatible, but on OS X glew caused nothing but problems. OP didn't mention glew, but this was like 2 days of me trying to figure it out. I also had to import `` and `` and then it worked. – Bjorn Apr 14 '16 at 05:54
1

http://support.apple.com/kb/HT5942

the site title is "Mac computers: OpenCL and OpenGL support in OS X Mavericks - Learn about the OpenGL and OpenCL versions that are supported by your computer in OS X Mavericks."

klarheit
  • 19
  • 1
1

If you're using LWJGL (as of version 2.90), there's a mild gotcha in the javadoc header of ContextAttribs:

... This extension is not supported on MacOS X. However, in order to enable the GL 3.2 context on MacOS X 10.7 or newer, an instance of this class must be passed to LWJGL. The only valid configuration is ContextAttribs(3, 2, CONTEXT_CORE_PROFILE_BIT_ARB), anything else will be ignored.

Philip Guin
  • 1,452
  • 15
  • 21
0

If you are using SDL as your high level API there are a number of things you need to do to get 4.1 support.

First of all make sure you have SDL2 2.0.1 (or later I guess). For instance using brew:

brew update brew upgrade sdl2

Secondly, you need to specifically tell SDL to request a core profile like so

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );

Thirdly a quirk in how a core profile is requested on the mac actually requires you to request 3.2 to get 4.1 (!). I think this has been fixed in the SDL2 repository but not been released yet.

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

Martin
  • 721
  • 5
  • 8