c++ - 为什么我总是使用 openGL 2.1 版?[openGL/SDL]

标签 c++ opengl sdl

我正在尝试将 openGL 与 SDL 一起使用,每当我在运行时检查我的版本时,它总是返回我正在使用 openGL 2.1 版。现在根据我的理解,包括 gl3.h 为您提供了 openGL 的 3.2+ 功能。除此之外,我特别要求使用 4.1 版的 openGL,并且显然仍在运行 2.1。有人可以告诉我我做错了什么吗?我正在运行 OSX Yosemite。

#include <iostream>
//Using SDL and standard IO
#include <SDL2/SDL.h>
#define GL_GLEXT_PROTOTYPES 1
//#include <SDL2/SDL_opengl.h>
#include <GLUT/glut.h>
#include <stdio.h>
#include <OpenGL/gl3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <string.h>

#ifdef __APPLE__
#define glGenVertexArrays glGenVertexArraysAPPLE
#define glBindVertexArray glBindVertexArrayAPPLE
#define glDeleteVertexArrays glDeleteVertexArraysAPPLE
#endif


using namespace std;


//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;



bool SetOpenGLAttributes()
{
    // Set our OpenGL version.
    // SDL_GL_CONTEXT_CORE gives us only the newer version, deprecated functions are disabled
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    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 Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL );
        if( window == NULL )
        {
            printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
        }
        else
        {

            //creating new context
            SDL_GL_CreateContext(window);

            //GLuint vertexArrayID;
            // glGenVertexArrays(1, &vertexArrayID);


            SetOpenGLAttributes();

            printf("%s", "This is your version");
            printf("%s\n", glGetString(GL_VERSION));

            SDL_GL_SetSwapInterval(1);
            glEnable(GL_DEPTH_TEST);



            SDL_GL_SwapWindow(window);

            bool running = true;
            while(running){
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                glFlush();
                //drawCube(.5);
                SDL_GL_SwapWindow(window);
                SDL_Delay(17);

            }


        }
    }


    //Destroy window
    //SDL_DestroyWindow( window );

    //Quit SDL subsystems
    //SDL_Quit();

    return 0;

最佳答案

您在创建上下文后调用 SetOpenGLAttributes。尝试在 SDL_GL_CreateContext(window); 之前调用它。

关于c++ - 为什么我总是使用 openGL 2.1 版?[openGL/SDL],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38385852/

相关文章:

c++ - 对头文件的混淆

c++ - QAbstractTableModel::header 数据和 QML TableView

c++ - 打开 : check if nested parallesim

c++ - 使用 extern "C"时 C++ 代码中的函数重载

c++ - C++ 中的模板化 is_in() 函数(检查数组是否包含字符串)

c++ - 如何制作一个开放的立方体(缺少一个表面)而不影响在 OpenGL 中镜像或切换边的位置?

opengl - 附加到纹理的帧缓冲区是否必须绑定(bind)到纹理单元?

c++ - OpenGL glViewport()(重置坐标)

c - 在 C 中使用 DLL

c++ - SDL_DestroyRenderer 上的 SDL2 内存泄漏