简体   繁体   中英

Trouble Creating a GLFW OpenGL Window in C++

Here is the current bit of code I'm working on:

int main() {

    glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "OpenGL", NULL, NULL);

    if (nullptr == window) {
        std::cout << "Failed to create GLFW Window" << std::endl;
        glfwTerminate();

        return EXIT_FAILURE;
    }

At runtime, the window is not created, and I get the failure message. I am not understanding why window is a nullptr, when I defined it. Am I missing something? Thanks for the help.

According to the GLFW documentation, the value for the GLFW_OPENGL_PROFILE window hint must be GLFW_OPENGL_ANY_PROFILE if the requested OpenGL context version is less than 3.2 (and GLFW has a platform-independent check built-in whenever glfwCreateWindow is called ).

See: https://www.glfw.org/docs/3.3/window_guide.html#GLFW_OPENGL_PROFILE_hint

GLFW_OPENGL_PROFILE specifies which OpenGL profile to create the context for. Possible values are one of GLFW_OPENGL_CORE_PROFILE or GLFW_OPENGL_COMPAT_PROFILE, or GLFW_OPENGL_ANY_PROFILE to not request a specific profile. If requesting an OpenGL version below 3.2, GLFW_OPENGL_ANY_PROFILE must be used. If OpenGL ES is requested, this hint is ignored.

In particular the part: "If requesting an OpenGL version below 3.2, GLFW_OPENGL_ANY_PROFILE must be used."

You will get a GLFW error in your case. In particular, exactly this one - regardless of the platform/OS, which you would see if you had setup a GLFW error handler function via glfwSetErrorCallback() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM