调用glGetString时出现访问冲突错误

问题描述 投票:1回答:2

我正在使用SDL2 + GLAD在C ++中创建一个OpenGL应用程序。在我的main函数中,我有以下代码:

#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;
}

但是,当我运行它时,我收到以下错误:

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

打印OpenGL loaded消息,并且Visual Sutio显示错误在printf("Vendor:%s\n", glGetString(GL_VENDOR));线上抛出。

我已确保在解决方案的属性窗口中正确链接SDL2和GLAD。可能导致此错误的原因是什么?

c++ opengl sdl-2 glad
2个回答
3
投票

Glad Loader-Generator必须由gladLoadGLgladLoadGLLoader初始化,在SDL_GL_CreateContext创建和制作当前OpenGL上下文之后。

另见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)glfwGetProcAddress))
{
    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));

0
投票

如果生成错误,glGetString返回0.然后printf尝试访问地址0处的内存。或者glGetString不可用,因为未加载OpenGL库。因此,初始化OpenGL可能存在一些问题。尝试在SDL_GL_LoadLibrary中指定OpenGL DLL模块的完整路径

请确保SDL_GL_LoadLibrary返回0,否则请致电SDL_GetError()获取更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.