在Macbook Air 2012上使用SDL2创建OpenGL 3.x上下文

16
据我所知,Macbook Air 2012支持OpenGL 3.2。然而,当使用SDL 2.0创建OpenGL上下文时,上下文仅支持OpenGL版本2.1。
SDL 2.0是否能够创建GL 3.2上下文?
3个回答

32

对于所有仍然在2018年10月10日之后遇到此问题的人,SDL2允许您在运行Mac OS X 10.7(Lion)及更高版本的Mac上创建OpenGL 3.2上下文。你只需要添加这段代码:

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

如果您正在运行OS X 10.10,或上述解决方案仍无法解决问题,则可以尝试更改上述示例的第二行:


SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

为了

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);

可能适合您的需求。

1

编辑:请参见https://dev59.com/AWct5IYBdhLWcg3wmOrg#13095742 -- SDL2现在应该原生支持此功能。下面的注释适用于最新SDL2版本之前的版本。

The current version (as of Wed Aug 15 21:00:33 2012 -0400; 6398:c294faf5fce5) does not support 10.7 series. However, there is a way to add support if you're willing to run an unstable SDL for kicks.

Give this a shot:

src/video/cocoa/SDL_cocoaopengl.m +90 (Cocoa_GL_CreateContext)

if(_this->gl_config.major_version == 3 &&                                                           
   _this->gl_config.minor_version == 2)                                                             
{                                                                                                   
    attr[i++] = NSOpenGLPFAOpenGLProfile;                                                           
    attr[i++] = NSOpenGLProfileVersion3_2Core;                                                      
}     

Then in your application, something along these lines.

SDL_Init(SDL_INIT_EVERYTHING);                                                                                                                                    

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

window = SDL_CreateWindow("3.2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,                
                640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);                                                                                               

context = SDL_GL_CreateContext(window);    

I am running 10.7.4 under my Mac Air from 2011 and when I run a few GL diagnostics from a 3.2 SDL enable application I get:

Driver     : cocoa
Renderer   : Intel HD Graphics 3000 OpenGL Engine
Vendor     : Intel Inc.
Version    : 3.2 INTEL-7.18.18
GLSL       : 1.50

I haven't tested much outside of this, but hopefully someone else can give it a stab and have a bit more success.

EDIT: If you're using GLEW you will want to enable glewExperimental. Note that there is a bug in the 3.2 core profile when you query GL_EXTENSIONS. It will report 1280 (as if it wasn't supported)--however that shouldn't impact you from using 1.50 shaders and so on.


那应该可以很好地完成工作,至少是暂时的。 - Captain Head
请查看David Greiner的评论。 - Justin Van Horne

0

应该是可能的

#include <stdio.h>
#include <stdlib.h>
/* If using gl3.h */
/* Ensure we are using opengl's core profile only */
#define GL3_PROTOTYPES 1
#include <GL3/gl3.h>

#include <SDL.h>
#define PROGRAM_NAME "Tutorial1"

/* A simple function that prints a message, the error code returned by SDL,
 * and quits the application */
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
}


/* Our program's entry point */
int main(int argc, char *argv[])
{
    SDL_Window *mainwindow; /* Our window handle */
    SDL_GLContext maincontext; /* Our opengl context handle */

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

    /* Request opengl 3.2 context.
     * SDL doesn't have the ability to choose which profile at this time of writing,
     * but it should default to the 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 24bit Z buffer.
     * You may need to change this to 16 or 32 for your system */
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    /* Create our window centered at 512x512 resolution */
    mainwindow = SDL_CreateWindow(PROGRAM_NAME, 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__);


    /* 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;
}

请注意,这需要SDL的特殊支持,因为MacOSX的上下文创建API在3.2和2.1中是不同的。 - Nicol Bolas
啊,是的,我也看过那个教程。但在Air上使用完全相同的代码仍会产生一个OpenGL 2.1上下文,并且在我拥有的几台Windows机器上(带有Nvidia和ATI显卡),这将创建具有最高支持版本(例如3.3)的上下文。 - Captain Head
@NicolBolas 这就是我现在想知道的...这可能只是当前版本的SDL的一个缺陷。 - Captain Head
@CaptainHead 目前是这样的。我在这里发布了一个快速的三行代码修复SDL,可以允许创建3.2上下文。它不兼容旧版OSX,但无所谓。 - Justin Van Horne

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接