SDL_GL_SetAttribute导致某些opengl函数为null

时间:2019-01-12 15:29:06

标签: c++ opengl sdl

设置较高的值,例如SDL_GL_DEPTH_SIZE的32会导致glCreateShader为空。

我不知道是什么导致了此行为,因为在设置属性和创建上下文时没有出现任何错误。这是一个简单的示例。

#include <iostream>
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <SDL2/SDL_opengl.h>

const int WIDTH = 800;
const int HEIGHT = 600;

int main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);

    int response;
    response = SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 
        SDL_GL_CONTEXT_PROFILE_CORE);

    response = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    response = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    response = SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    if (response < 0) {
        std::cout << "Setting Attribute: DEPTH_SIZE Failed\n";
        return 0;
    }

    SDL_Window *window = SDL_CreateWindow("TestApp", WIDTH >> 1, HEIGHT >> 1, WIDTH, HEIGHT, SDL_WINDOW_OPENGL);

    if (window == nullptr) {
        std::cout << "window not setup\n";
        return 0;
    }

    SDL_GLContext context = SDL_GL_CreateContext(window);
    if (context == nullptr) {
        std::cout << "context failed\n";
        return 0;
    }

    glewExperimental = GL_TRUE;

    if (GLEW_OK != glewInit()) {
        std::cout << "Failed to initialize glew\n";
        return 0;
    }

    glViewport(0, 0, WIDTH, HEIGHT);

    int value; 
    SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
    std::cout << "Querying Depth size: " << value << "\n";

    std::cout << "glEnable: " << (glEnable ? "Available" : "NULL") << "\n";
    std::cout << "glCreateShader: " << (glCreateShader ? "Available" : "NULL") << "\n";

    SDL_GL_DeleteContext(context);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

DEPTH_SIZE = 24 bits的输出

Querying Depth size: 24
glEnable: Available
glCreateShader: Available

DEPTH_SIZE = 32 bits的输出

Querying Depth size: 32
glEnable: Available
glCreateShader: NULL

0 个答案:

没有答案