0

我目前正在尝试使用 C 来关闭 OpenGL。我有当前代码:

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

#define null (void*)0

#define GL3_PROTOTYPES 1
#include <GL/gl3.h>

#include <SDL.h>
#define PROGRAM_NAME "Tutorial1"

void sdldie(const char *msg)
{
    printf("%s: %s\n", msg, SDL_GetError());
    SDL_Quit();
    exit(1);
}

void checkSDLError(int line)
{
    #ifndef NDEBUG
        const char *error = SDL_GetError();
        if(*error != '\0')
        {
            printf("SDL Error: %s\n", error);
            if(&line != null)
                printf(" + line: %i\n", line);
            SDL_ClearError();
        }
    #endif
}

int main(int argc, char *argv[])
{
    SDL_Window *mainwindow; //window handle
    SDL_GLContext maincontext; //opengl context handle

    if(SDL_Init(SDL_INIT_VIDEO) < 0)//initialize video subsystem
        sdldie("Unable to initialize SDL"); //DIE ON ERROR

    //request OGL 3.2 context (default to SDL core profile)
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    //turn on double buffering with a 24 bit Z buffer
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    mainwindow = SDL_CreateWindow(PROGRAM_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
    512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    if(!mainwindow)
        sdldie("Unable to create window");

    checkSDLError(__LINE__);

    //create an opengl context and attach it to the window
    maincontext = SDL_GL_CreateContext(mainwindow);
    checkSDLError(__LINE__);

    //VSync, turn it off later
    SDL_GL_SetSwapInterval(1);

    //clear buffer with red background
    glClearColor(1.0,0.0,0.0,1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    //swap back buffer to the front
    SDL_GL_SwapWindow(mainwindow);
    //wait 2 secs
    SDL_Delay(2000);

    //repeat with green
    glClearColor(0.0,1.0,0.0,1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    //swap back buffer to the front
    SDL_GL_SwapWindow(mainwindow);
    //wait 2 secs
    SDL_Delay(2000);

    //delete all dis shit
    SDL_GL_DeleteContext(maincontext);
    SDL_DestroyWindow(mainwindow);
    SDL_Quit();

    return 0;
}

请注意我们选择 OGL 版本的那一行(在 main 中,在初始化视频之后)...当我按照此处的代码执行此操作时,我会遇到错误:

X Error of failed request:  GLXBadFBConfig
  Major opcode of failed request:  154 (GLX)
  Minor opcode of failed request:  34 ()
  Serial number of failed request:  166
  Current serial number in output stream:  165

造成这种情况的行具体是:

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);

当它被注释掉时,一切正常(但可能与另一个 OGL 版本 D:)

我正在使用最新版本的 FOSS Radeon 驱动程序(在 Arch 上)、SDL2.0 HG 版本、最新的稳定 GLX 和 gl3.h 已手动下载...在 arch linux 和 gcc 版本 4.7.2

知道为什么会发生这种情况(/如何摆脱它)吗?设置该属性是否必要?

4

1 回答 1

0

SDL2 尚未发布,可能包含严重错误。在您的情况下,SDL_GL_CreateContext 中有一个错误。

2013 年 3 月 10 日提交了解决此问题的承诺:SDL_GL_CreateContext() 导致致命的 X11 协议错误,而应该被捕获

我的成功创建了具有最新 SDL2 提交的 OpenGL 3.2 上下文。

于 2013-06-02T11:49:38.993 回答