linux - SDL2/GL 上下文在 linux mint 上默认为 v3.0

标签 linux opengl

当我编译并运行我的代码以创建 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;
}

最佳答案

您需要在创建窗口之前设置 GL 属性,而不是在创建上下文之后:

SDL_GL_SetAttribute()

Use this function to set an OpenGL window attribute before window creation.

例子:

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

关于linux - SDL2/GL 上下文在 linux mint 上默认为 v3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30788849/

相关文章:

c++ - 带 Eclipse CDT + MinGW + GLEW + GLFW 的 OpenGL : Undefined References

opengl - 如何在 Mac OS 中的 NSOpenGLView 和全屏上下文之间共享纹理?

opengl - 开罗 GL 后端

java - 使用 glRotatef() 时的 OpenGL 坐标问题

java - Open GL 转换

linux - perf中的硬件事件和硬件缓存事件有什么区别?

c++ - Premake 无法在 Linux 上使用 g++?

在 Linux 上运行时 angularjs $window 对象为空

c++ - 如何创建可从 C 调用的基于 Qt 的库

linux - Linux 中的特殊符号