4

I'm trying to learn OpenGL and the material is using #version 330 in shaders. I can compile it successfully, but when I try to run it, it complains Version 330 is not supported.

In my source code, I use free glut and OpenGL framework in Xcode. If I also include these two lines of code

glutInitContextVersion(3,1); glutInitContextFlags(GLUT_CORE_PROFILE);

it cannot be compiled.

My mac is MacBook Pro Mid 2012. It should support OpenGL4.1 according to apply.

So how can I compile version 330 shaders?

Negelis
  • 376
  • 4
  • 17

1 Answers1

1

OS X requires that you request a 3.2 core profile in order to receive a 3.3 or later context. This is because 3.2 finally removed the functionally that was deprecated in 3.0.

So if you want to use a #version 330 shader then your code should look like this:

glutInitContextVersion(3,2);
glutInitContextFlags(GLUT_CORE_PROFILE);

EDIT Apparently X11 doesn't support OpenGL higher than 2.1 on OS X. As such, I suggest you switch to GLFW.

Community
  • 1
  • 1
Michael Morris
  • 319
  • 3
  • 13
  • 1
    I have added it in my source code. But it still doesn't work. The source code can be compiled successfully. But when I run it, it complains: X Error of failed request: BadRequest (invalid request code or no such operation) Major opcode of failed request: 34 (X_UngrabKey) Serial number of failed request: 29 Current serial number in output stream: 29 I don't know the reason of this. – Negelis Apr 09 '14 at 00:03
  • 1
    @Negelis: That's part of your problem as well... to get a 3.2+ core profile context on OS X, you have to use the native CGL or NSOpenGL APIs. If your software is going through the legacy X server, you will forever be limited to OpenGL 2.1 on OS X. I have to wonder why it is configured to use an X server rather than Quartz; wherever you got your port of FreeGLUT from they built it using X instead of the native APIs for OS X. That X OpCode is wrong by the way. GLX Major OpCode 34 is `glXCreateContextAttribsARB`, which is what ***other platforms*** use to request `GLX_CONTEXT_MAJOR_VERSION_ARB`. – Andon M. Coleman Apr 09 '14 at 00:43
  • 1
    @Negelis: It is not surprising that it reports the wrong OpCode though, since it knows nothing about either the `GLX_ARB_create_context` or `GLX_ARB_create_context_profile` extensions. Again, the important thing here is to find a port of FreeGLUT for OS X that was not built on top of X or better yet to use something like GLFW3 or SDL2. Apple does not support X beyond providing a compatibility X server layered on top of Quartz to make porting existing UNIX applications easier. The GL functionality exposed by this X server is frozen at GL 2.1 and it does not understand new GLX extensions either. – Andon M. Coleman Apr 09 '14 at 00:52
  • 1
    @Coleman: Thanks very much! I think I'm much clearer what's wrong here now. – Negelis Apr 09 '14 at 01:06
  • 1
    @MichaelMorris: I was referring to XQuartz this entire time :) That is Apple's X server that is layered on top of Quartz. It does not implement the newer stuff from GLX, and the only way to get a 3.2+ core context on OS X is to use CGL or NSOpenGL. GLX and AGL cannot be used because they were never updated when GL profiles were added in OS X 10.7. Under the hood FreeGLUT usually uses GLX on UX platforms, but if it is OS X native, it will probably use NSOpenGL because that is the only native API on OS X that lets you do *both* windowed and fullscreen rendering. – Andon M. Coleman Apr 09 '14 at 18:01
  • 1
    @MichaelMorris: Technically Apple did not write XQuartz, it is open source... a lot of OS X is actually open source. But the important point is that its implementation of GLX does not support `GLX_ARB_create_context_profile`, so there's no way to get anything newer than a 2.1 context. Since XQuartz is open source, this may change in the future but last I checked the version of XQuartz Apple distributes still does not support it. – Andon M. Coleman Apr 09 '14 at 18:11