我现在正试着用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版
你知道为什么会发生这种情况(使用/how来摆脱它)吗?是否有必要设置该属性?
发布于 2013-06-02 19:49:39
SDL2还没有发布,可能会包含严重的错误。在你的例子中,是SDL_GL_CreateContext中的一个bug。
在2013年3月10日做出了一个承诺来解决这个问题:SDL_GL_CreateContext() causes fatal X11 protocol errors that should just be caught instead
我的成功地用最新的SDL2提交创建了一个OpenGl 3.2上下文。
https://stackoverflow.com/questions/13292518
复制相似问题