简体   繁体   English

使用SDL2指定OpenGL版本

[英]Specify OpenGL version using SDL2

I am using Macbook air with OS X 10.9.2 my graphics card is (Intel HD Graphics 4000 1024 MB) Apple states that a Macbook air with the specifications as mine supports up to Opengl 4.1 you can read it here -> http://support.apple.com/kb/HT5942 . 我正在使用Mac OS Air OS X 10.9.2我的显卡是(Intel HD Graphics 4000 1024 MB)苹果声称Macbook air的规格与我的支持最高的Opengl 4.1你可以在这里阅读 - > http:// support.apple.com/kb/HT5942 however when i use SDL2 and force the system to use opengl 3.2 and when i query the OpenGL version via glGetString() i get the following line: 但是当我使用SDL2并强制系统使用opengl 3.2时,当我通过glGetString()查询OpenGL版本时,我得到以下行:

2.1 INTEL-8.24.11

my Code: 我的代码:

#include <iostream>
#include <SDL2/SDL.h>
#include <opengL/gl3.h>

using namespace std;


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



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


int main(int argc, const char * argv[])
{


SDL_Window *mainwindow; /* Our window handle */
SDL_GLContext maincontext;

if (SDL_Init(SDL_INIT_VIDEO) < 0) /* Initialize SDL's Video subsystem */
    sdldie("Unable to initialize SDL");

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);

mainwindow = SDL_CreateWindow("Hello", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                              512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (!mainwindow) /* Die if creation failed */
    sdldie("Unable to create window");

checkSDLError(__LINE__);

/* Create our opengl context and attach it to our window */
maincontext = SDL_GL_CreateContext(mainwindow);
checkSDLError(__LINE__);

cout << glGetString(GL_VERSION);

/* This makes our buffer swap syncronized with the monitor's vertical refresh */
SDL_GL_SetSwapInterval(1);

/* Clear our buffer with a red background */
glClearColor ( 1.0, 0.0, 0.0, 1.0 );
glClear ( GL_COLOR_BUFFER_BIT );
/* Swap our back buffer to the front */
SDL_GL_SwapWindow(mainwindow);
/* Wait 2 seconds */
SDL_Delay(2000);

/* Same as above, but green */
glClearColor ( 0.0, 1.0, 0.0, 1.0 );
glClear ( GL_COLOR_BUFFER_BIT );
SDL_GL_SwapWindow(mainwindow);
SDL_Delay(2000);

/* Same as above, but blue */
glClearColor ( 0.0, 0.0, 1.0, 1.0 );
glClear ( GL_COLOR_BUFFER_BIT );
SDL_GL_SwapWindow(mainwindow);
SDL_Delay(2000);

/* Delete our opengl context, destroy our window, and shutdown SDL */
SDL_GL_DeleteContext(maincontext);
SDL_DestroyWindow(mainwindow);
SDL_Quit();



return 0;
}

On OSX you need to request a Core context for post-2.1 versions. 在OSX上,您需要为2.1之后的版本请求Core上下文。

Try adding this to your attribs: 尝试将此添加到您的attribs:

SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM