Dav1dde / glad

Multi-Language Vulkan/GL/GLES/EGL/GLX/WGL Loader-Generator based on the official specs.
https://glad.dav1d.de/
Other
3.79k stars 447 forks source link

GL/GLFW Example Displays GLAD Version "0.1" #463

Closed makichiis closed 6 months ago

makichiis commented 7 months ago

The GL/GLFW example located at example/c/gl_glfw.c (shown below) loads GL with glfwGetProcAddress:

int version = gladLoadGL(glfwGetProcAddress);

However, when loading with GLFW (and glad2) for OpenGL 4.6, gladLoadGL(GLADloadfunc) returns 1. Thus, when retrieving the major and minor versions of OpenGL, "0.1" is generated.

I'm not sure if this is intentional, because I would expect this to return either the current GLAD version or the current GL version, however only 1 is ever returned from gladLoadGL.

I don't think this issue is cause for any immediately obvious problems with the loader, but I worry about portability in the future. Is this the incorrect way to retrieve the OpenGL version loaded by glad? If so, what should be done instead?

Thank you for reading.

Contents of example/c/gl_glfw.c

#include <glad/gl.h>
// GLFW (include after glad)
#include <GLFW/glfw3.h>

#include <stdlib.h>
#include <stdio.h>

const GLuint WIDTH = 800, HEIGHT = 600;

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}

int main(void) {
    glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "[glad] GL with GLFW", NULL, NULL);
    glfwMakeContextCurrent(window);

    glfwSetKeyCallback(window, key_callback);

    int version = gladLoadGL(glfwGetProcAddress);
    printf("GL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version)); // <-- prints "GL 0.1"

    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();

        glClearColor(0.7f, 0.9f, 0.1f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(window);
    }

    glfwTerminate();

    return 0;
}
Dav1dde commented 7 months ago

What does glGetString(GL_VERSION) return?

makichiis commented 7 months ago

Hello Dav1dde, thank you for responding so quickly.

glGetString(GL_VERSION) returns "4.6.0 Core Profile Context 24.1.1.231127", I think as expected.

Dav1dde commented 7 months ago

Mh that should work, can you debug into glad_gl_find_core_gl either via a debugger or just add a few printf's, I am mainly interested in the version string before the GLAD_IMPL_UTIL_SSCANF call and the major, minor variables after.

makichiis commented 7 months ago

Thank you for the advice. I was able to find the source of the problem. As it turns out, glad_gl_find_core_gl and GLAD_IMPL_UTIL_SSCANF are not called at any point.

It seems that GLAD_GL_IMPLEMENTATION must be defined before gl.h is included in order to be called, so I assume the version is not set otherwise. After adding #define GLAD_GL_IMPLEMENTATION before #include <glad/gl.h>, the version is properly set: image

Should I fork and PR the example with the definition of GLAD_GL_IMPLEMENTATION?

Revised source file:

#include <stdlib.h>
#include <stdio.h>

#define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h>

#include <GLFW/glfw3.h>

const GLuint WIDTH = 800, HEIGHT = 600;

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}

int main(void) {
    glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // GL version edited for version test
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "[glad] GL with GLFW", NULL, NULL);
    glfwMakeContextCurrent(window);

    glfwSetKeyCallback(window, key_callback);

    int version = gladLoadGL(glfwGetProcAddress);
    printf("GL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));

    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();

        glClearColor(0.7f, 0.9f, 0.1f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(window);
    }

    glfwTerminate();

    return 0;
}
makichiis commented 7 months ago

Issue possibly resolved by fork in PR #464. Please let me know if there are any issues.

Thank you @Dav1dde

Dav1dde commented 6 months ago

As discussed in https://github.com/Dav1dde/glad/pull/464#issuecomment-2041496499 we cannot re-produce the issue, closing it until reproducible.