2

I'm creating an OpenGL application in C++ using SDL2 + GLAD. In my main function, I have the following code:

#include <iostream>
#include <SDL.h>

#include <glad\glad.h>

int main(int argc, char *argv[]) {
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        std::cout << "SDL could not be initialized.";
        return 1;
    }

    SDL_GL_LoadLibrary(nullptr);

    SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    SDL_Window *window = SDL_CreateWindow("Hello world", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_OPENGL);

    if (window == nullptr) {
        std::cout << "SDL could not open window";
        return 1;
    }

    const SDL_GLContext context = SDL_GL_CreateContext(window);

    if (context == nullptr) {
        std::cout << "SDL could not create context";
        return 1;
    }

    printf("OpenGL loaded\n");

    printf("Vendor:          %s\n", glGetString(GL_VENDOR));
    printf("Renderer:        %s\n", glGetString(GL_RENDERER));
    printf("Version OpenGL:  %s\n", glGetString(GL_VERSION));
    printf("Version GLSL:    %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));

    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

    int w, h;
    SDL_GetWindowSize(window, &w, &h);
    glViewport(0, 0, w, h);
    glClearColor(0.0f, 0.5f, 1.0f, 0.0f);

    SDL_Event event;
    bool quit = false;
    while (!quit) {
        SDL_GL_SwapWindow(window);
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                quit = true;
            }
        }
    }

    return 0;
}

However, when I run this, I'm receiving the following error:

Exception thrown at 0x0000000000000000 in TestApp.exe: 0xC0000005: Access violation executing location 0x0000000000000000.

The OpenGL loaded message is printed, and Visual Sutio shows that the error is thrown on the printf("Vendor:%s\n", glGetString(GL_VENDOR)); line.

I've made sure to link SDL2 and GLAD correctly in the properties window for the solution. What could be causing this error?

Thomas
  • 1,123
  • 3
  • 13
  • 36

2 Answers2

6

Glad Loader-Generator has to be initialized by either gladLoadGL or gladLoadGLLoader, right after creating and making current the OpenGL context by SDL_GL_CreateContext.

See also OpenGL Loading Library - glad

e.g.:

const SDL_GLContext context = SDL_GL_CreateContext(window);
if (context == nullptr) {
    std::cout << "SDL could not create context";
    return 1;
}

if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
{
    std::cout << "Failed to initialize OpenGL context" << std::endl;
    return -1;
}

printf("OpenGL loaded\n");

printf("Vendor:          %s\n", glGetString(GL_VENDOR));
printf("Renderer:        %s\n", glGetString(GL_RENDERER));
printf("Version OpenGL:  %s\n", glGetString(GL_VERSION));
printf("Version GLSL:    %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
xyz333
  • 3
  • 2
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

If an error is generated, glGetString returns 0. And then printf is try access memory at address 0. Or glGetString is not available because OpenGL library is not loaded. So there probably you have some issue with initialization OpenGL. Try specify full path to OpenGL DLL module in SDL_GL_LoadLibrary

Please make sure SDL_GL_LoadLibrary is return 0, otherwise call SDL_GetError() for more information.

Defter
  • 214
  • 1
  • 7