当我编译&运行我的代码来创建一个opengl 3.3或更高版本的上下文(与windows版本相同,有一个或两个额外的行),它默认为3.0,这将导致我想要移植的一些应用程序出现问题。
我正在寻找修复/解释。
来源如下:
//Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>
#include <GL/glew.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
bool handleEvent(SDL_Event& event)
{
return true;
}
int main( int argc, char* args[] )
{
//The window we'll be rendering to
SDL_Window* window = NULL;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Create window
window = SDL_CreateWindow("SDL/GLM/OpenGL Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
if( window == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
}
else
{
SDL_GLContext context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetSwapInterval(1); // set swap buffers to sync with monitor's vertical refresh rate
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glewExperimental = GL_TRUE;
glClearColor(1.0,0.0,0.0,1.0);
GLenum err = glewInit();
if (err != GLEW_OK)
{
printf("glew init failed: %s!\n", glewGetErrorString(err));
}
printf("opengl version :%s\n",glGetString(GL_VERSION));
bool running = true; // set running to true
SDL_Event sdlEvent; // variable to detect SDL events
while (running)
{ // the event loop
while (SDL_PollEvent(&sdlEvent))
{
if (sdlEvent.type == SDL_QUIT)
running = false;
else
{
running = handleEvent(sdlEvent);
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapWindow(window);
}
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
}
}
SDL_Quit();
return 0;
}
答案 0 :(得分:2)
您需要在创建窗口之前设置GL属性,而不是在创建上下文之后:
使用此功能在创建窗口之前设置OpenGL窗口属性 。
示例:
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <iostream>
int main( int argc, char** argv )
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_Window* window = SDL_CreateWindow
(
"SDL2",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
300, 300,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
);
if( NULL == window )
{
std::cerr << "Failed to create window: " << SDL_GetError() << std::endl;
SDL_Quit();
return 0;
}
SDL_GLContext context = SDL_GL_CreateContext(window);
if( NULL == context )
{
std::cerr << "Failed to create GL context: " << SDL_GetError() << std::endl;
SDL_DestroyWindow( window );
SDL_Quit();
return -1;
}
if( SDL_GL_MakeCurrent( window, context ) < 0 )
{
std::cerr << "Failed to make GL context current: " << SDL_GetError() << std::endl;
SDL_GL_DeleteContext( context );
SDL_DestroyWindow( window );
SDL_Quit();
return -1;
}
std::cout << "GL_VERSION: " << glGetString( GL_VERSION ) << std::endl;
SDL_GL_SetSwapInterval(1);
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if( GLEW_OK != err )
{
std::cerr << "Failed init GLEW: " << glewGetErrorString( err ) << std::endl;
SDL_GL_DeleteContext( context );
SDL_DestroyWindow( window );
SDL_Quit();
return -1;
}
bool running = true;
while( running )
{
SDL_Event ev;
while( SDL_PollEvent( &ev ) )
{
if ( ev.type == SDL_QUIT )
running = false;
}
glClear( GL_COLOR_BUFFER_BIT );
SDL_GL_SwapWindow( window );
}
SDL_GL_DeleteContext( context );
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}
答案 1 :(得分:0)
事实证明,mesa驱动程序不支持3.0以上的OpenGL上下文,因此驱动程序将默认为可用的最大值,即3.0。要创建3.x上下文,必须安装专有驱动程序。一旦我安装了这些驱动程序,一切正常。