14

I'm following book "OpenGL Programming Guide 8th Edition". I just want to run the first program introduced in the book on my Mac.

It's Mavericks + Xcode 4.6.1 + Intel HD graphics 4000. So the problem is, the shader can't be compiled.

Shader codes:

#version 410 core

layout(location = 0) in vec4 vPosition;

void
main()
{
    gl_Position = vPosition;
}

And the error message is:

Shader compilation failed: ERROR: 0:1: '' :  version '410' is not supported
ERROR: 0:1: '' : syntax error #version
ERROR: 0:3: 'layout' : syntax error syntax error

I tried version 420/400/330, none of them works.

By the way, the program uses latest glew 1.10(http://glew.sourceforge.net), and I found that I have to set "glewExperimental = GL_TRUE;" before calling glewInit. Otherwise "glGenVertexArray" is a NULL pointer. So I'm wondering maybe glew doesn't support Mavericks?

t.niese
  • 39,256
  • 9
  • 74
  • 101
Mark Zhang
  • 353
  • 2
  • 5
  • 12
  • 2
    Did you select the core profile for your rendering? I don't use glew, I only use glfw to create the rendering context and window, and the standard headers. On MacOS I did not really need glew because macos has all functions and constants the system/driver supports. – t.niese Jan 05 '14 at 08:28
  • An additional note because I forgot to mention, MacOS only has the `legacy` and the `3_2Core` profile that you can select. But for `3_2Core` it returns highest profile the system/gpu supports that is backwards compatible to `3_2Core`. – t.niese Jan 05 '14 at 08:47
  • 1
    Thanks, but how to "select the core profile for your rendering"? – Mark Zhang Jan 05 '14 at 09:32
  • That depends on what you use to create your rendering context. (GLUT, Native MacOS functions, glwf, QT, ...) – t.niese Jan 05 '14 at 09:45
  • I bet you can remove `layout(location = 0)` and `#version 410 core` and it will work. – joschuck Jan 05 '14 at 10:27
  • @joschuck It doesn't work, I tried that. The shader compiler complains the "in" is unknown. – Mark Zhang Jan 05 '14 at 10:30
  • @Mark what happens when you change it to `attribute` – joschuck Jan 05 '14 at 10:33
  • 1
    Added `glut` tag to your question out of your comment. As I don't use glut on macos I can just guess that `glutInitDisplayMode(GLUT_3_2_CORE_PROFILE)` would be the right command. Probably you already use `glutInitDisplayMode` with other options then you need to add `GLUT_3_2_CORE_PROFILE` using _OR_ so that it would probably look something like this `glutInitDisplayMode(GLUT_3_2_CORE_PROFILE | GLUT_RGBA | GLUT_DOUBLE);` – t.niese Jan 05 '14 at 10:35
  • @joschuck `varying` and `attribute` are deprecated, so when learning modern OpenGL (3.2+) you should not use them anymore. – t.niese Jan 05 '14 at 10:40
  • @t.niese Yes that's true, i wanted to make sure that the shader compiles somehow – joschuck Jan 05 '14 at 10:45
  • @t.niese Yes, it works! Although this is not documented in glutInitDisplayMode's manual. Thanks, t.niese! – Mark Zhang Jan 05 '14 at 10:51
  • Hm... how do I change t.niese's comments to answer? – Mark Zhang Jan 05 '14 at 10:53
  • 1
    @Mark I'll create an answer out of it later. I just wrote it as comment because it was more guessing by experience then knowing if it really works ;) – t.niese Jan 05 '14 at 10:54
  • error: use of undeclared identifier 'GLUT_3_3_CORE_PROFILE' – Nicholas Jela May 16 '22 at 15:49

2 Answers2

31

MacOS uses Legacy Profile as default for all created OpenGL context. Therefor by default only OpenGL up to 2.1 and GLSL up to 1.20 is supported.

To use OpenGL 3.2+ you need to switch to the Core Profile. The naming there is a little bit confusing because it stats only 3.2Core profile, but actually this 3.2 or later (every OpenGL profile that is supported by the system/driver that is backwards compatible to 3.2)

For glut (depends on the version of glut if it works) the command on MacOS is:

glutInitDisplayMode(GLUT_3_2_CORE_PROFILE | ...  )

Where | ... would be the other options you want to pass to glutInitDisplayMode.

About glew, normally you don't require glew on MacOS due the way how the OpenGL layer is implemented in MacOS. You are restricted to the OpenGL features MacOS provides/exposes. So either the features are available via the headers of MacOS or not. There header would be #include <OpenGL/gl3.h> where also the naming is missleading, it does not mean only OpenGL 3, it is the same like with the context.

I would recommend to use GLFW it is a great cross platform library similar to GLUT but as I think better to use. There you would switch the context like this:

 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
t.niese
  • 39,256
  • 9
  • 74
  • 101
  • 1
    This is pretty good for OpenGL newbies. Sometimes we can understand opengl functions easily but that is another story if you wanna run it on your platform, because you will have to deal with some other frameworks/libraries just like glut/glew. – Mark Zhang Jan 05 '14 at 11:15
  • 1
    t.niese is correct. This fixed my issue like a charm. I'm using SDL 2 for window management. I've included the required code for SDL 2 below. `SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);` `SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);` `SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);` – Nicholas Hazen Feb 23 '14 at 09:56
  • Hi guys, for some reason i'm unable to get `glutInitDisplayMode( GLUT_3_2_CORE_PROFILE );` working. Could you please tell me what I'm doing wrong. Here is my code. https://gist.github.com/dhirajbodicherla/11238004 – Dhiraj Apr 24 '14 at 01:04
  • 1
    @DhirajBodicherla When a window is created the current display mode is used for its settings, as you call `glutInitDisplayMode( GLUT_3_2_CORE_PROFILE );` **after** `glutCreateWindow` this changes have no effect to the OpenGL context of that window. So you need to place it **before** the `glutCreateWindow`. As you already have `glutInitDisplayMode` in line _85_ (in the `createGlutWindow` function) you could just add it there, so that you won't overwrite those settings: `glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_3_2_CORE_PROFILE );` – t.niese Apr 24 '14 at 05:21
  • Hi @t.niese thank you so much for the reply. But whenever I add GLUT_3_2_CORE_PROFILE, xcode says undeclared identifier. How much should I declare its value to? – Dhiraj Apr 24 '14 at 13:03
  • @DhirajBodicherla For future question or if my answer does not help you: You shouldn't ask questions as comments, but open a new question and as they are somehow related you should reference this question in yours. Back to your question. If XCode tells you that `GLUT_3_2_CORE_PROFILE ` is undeclared but the other `glut` functions work, then it is either an older GLUT (`GLUT_MACOSX_IMPLEMENTATION < 4`), or not the glut shipped with OS X. If this does not help you, then please open a new question, leaf me a comment here with the link to it. – t.niese Apr 24 '14 at 13:12
  • @t.niese I didn't quite get in which file and after which line to put these? I'm using the GLFW library as well, shader version 330 core. Just trying to understand your answer here, and whether it's relevant for my particular case that I just stated. – Matt Mar 18 '20 at 00:58
  • Thanks, I was able to run the program writing those lines after `glfwInit()` and before `glfwCreateWindow()` I'm using GLFW. – Juan P. Ortiz Apr 12 '20 at 03:00
  • @JuanP.Ortiz the comment section is only there to discuss questions/problems regarding the answer (or question). It should not be used for `Thanks, …` only comments. – t.niese Apr 12 '20 at 11:32
  • @t.niese then only remove `thanks` from my comment, the rest is related to the actual implementation with some useful hint. – Juan P. Ortiz Apr 13 '20 at 12:16
  • This is the right answer. You need to run these lines after glfwInit or it won't take effect. – CPerson Sep 08 '22 at 13:26
5

It can be that your driver does not support the GLSL version you require. I had this problem on a laptop with Intel HD card on fresh install of Ubuntu half a year ago.

Use glGetString to figure out which version is available to you: http://www.opengl.org/sdk/docs/man/xhtml/glGetString.xml. For example,

printf("Supported GLSL version is %s.\n", (char *)glGetString(GL_SHADING_LANGUAGE_VERSION));

Here you can find about glewExperimental: http://www.opengl.org/wiki/Extension_Loading_Library

user3127029
  • 131
  • 4
  • 1
    Thanks. This is what I got: "Supported GLSL version is: 1.20". Why my system only supports such an old GLSL version? Is there something wrong with my Mavericks? – Mark Zhang Jan 05 '14 at 09:27
  • @Mark by default MacOS uses the [**Legacy Profile**](https://developer.apple.com/graphicsimaging/opengl/capabilities/GLInfo_1090.html) supporting only OpenGL up to 2.1 and GLSL 1.20. You need to tell MacOS to use the [**Core Profile**](https://developer.apple.com/graphicsimaging/opengl/capabilities/), how to do that depends on the way you create your rendering context. – t.niese Jan 05 '14 at 09:50
  • @t.niese Cool. I use GLUT which ships with Xcode. I guess it's openglut? Actually in that book's sample program, there are 2 lines which are removed by me(because these 2 functions are not defined in MacOSX's glut library): "glutInitContextVersion(4, 3)" & "glutInitContextProfile". I skimmed the "glut.h", but didn't find similar functions so now I've no idea how to do that... – Mark Zhang Jan 05 '14 at 10:31
  • I would first advise to move to GLFW:GlutMainLoop() has serious architectural flaws. – user3127029 Jan 05 '14 at 10:49
  • Also give this great tutorial a try: [link](http://www.opengl-tutorial.org). Don't forget to check that you agree to their license agreement: [link](http://www.opengl-tutorial.org/download/)! – user3127029 Jan 05 '14 at 10:58
  • @user3127029 Thanks, and yes, I really like it's license agreement. :) – Mark Zhang Jan 05 '14 at 11:19