Create window crash on mac

imyt wrote on Sunday, February 23, 2014:

Hi everyone,

I’m new to mac os so I decided to use GLFW. However I cannot even create a window, I called glfwInit first and then glfwCreateWindow, but glfwCreateWindow failed with the following error message “Error: Failed to retrieve context version string
”. Did anyone encountered the same problem before?

I’m using Mac OSX 10.9 and GLFW is built with Cocoa, NSGL and OpenGL.

xdread wrote on Friday, March 07, 2014:

I have been having a look at the source of GLFW, and located the error messages in the file context.c. GLFW gives you that error message when a call to an Open GL function named glGetString() returns 0, and you should theoretically get the real error message by calling glGetError(). I am not sure why GLFW doesn’t do that, but I assume from the error codes returned by Open GL that the error message is irrelevant anyway.

The codes are:
GL_INVALID_ENUM - Which would never happen, since GLFW are using the correct enums.
GL_INVALID_OPERATION - Which would never happen in your case, since you are using OS X Maverich core profile, forward compatible. (glBegin and glEnd is deprecated)

If you can post your code here? Or compare with my minimal working code below?

Anyway, here is my minimalistic code to make GLFW work:

:::c++
 #include <OpenGL/gl3.h> //(My code compiles without this line)
 #define GLFW_INCLUDE_GLCOREARB
 #include "GLFW/glfw3.h"

//Main()
    glfwSetErrorCallback(error_callback);

    if (!glfwInit()) {
        exit(EXIT_FAILURE);
    }

    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);

    GLFWwindow* window = glfwCreateWindow(640, 480, "GLFW", NULL, NULL);

    if (!window)
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
 
    glfwMakeContextCurrent(window);

//Setup shaders and things here
//Maybe glfwSwapInterval(1) here?

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        glfwSwapBuffers(window);
        
        glfwPollEvents();  //Needed, or you will get a spinning beach ball
    }
    
    glfwDestroyWindow(window);
    
    glfwTerminate();

//error_callback()
void error_callback(int error, const char* description)
{
    fputs(description, stderr);
    std::cout << std::endl;
}

EDIT: I would insist on you learning “modern” Open GL with shaders, even if it seems very hard, but if you insist on using the soon to be obsolete old style Open GL, just set the version to be 2.1 and remove the window hints for core and forward compatibility. You don’t have backwards compatibility for any version higher than that on Mac.

Also note that even though Open GL is a certain version, the shader language may NOT be the same version: http://renderingpipeline.com/2012/03/shader-model-and-glsl-versions/

And last, a good source to learn more: http://open.gl

elmindreda wrote on Friday, March 07, 2014:

#include <OpenGL/gl3.h> //(My code compiles without this line)
#define GLFW_INCLUDE_GLCOREARB

These two lines have the same result. You only need one of them.