【问题标题】:OpenGL 3.x context creation using SDL2 on OSX (Macbook Air 2012)在 OSX (Macbook Air 2012) 上使用 SDL2 创建 OpenGL 3.x 上下文
【发布时间】:2012-08-14 21:35:45
【问题描述】:

据我所知,Macbook Air 2012 支持 OpenGL 3.2。然而,当使用 SDL 2.0 创建 OpenGL 上下文时,上下文只有 opengl 2.1 版。

SDL 2.0 是否可以创建 GL 3.2 上下文?

【问题讨论】:

    标签: macos opengl sdl


    【解决方案1】:

    对于截至 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);
    

    可能对你有用。

    【讨论】:

      【解决方案2】:

      编辑:见https://stackoverflow.com/a/13095742/123387——SDL2 现在应该原生支持这个。以下注释适用于最新 SDL2 版本之前的版本。

      当前版本(截至 2012 年 8 月 15 日星期三 21:00:33 -0400; 6398:c294faf5fce5) 不支持 10.7 系列。然而,有一个 如果您愿意运行不稳定的 SDL 来增加支持。

      试一试:

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

      然后在您的应用程序中,类似这些内容。

      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);    
      

      我从 2011 年开始在我的 Mac Air 下运行 10.7.4,当我运行一些 GL 我得到的来自 3.2 SDL 启用应用程序的诊断:

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

      除此之外我没有进行太多测试,但希望其他人可以 试一试,取得更大的成功。

      编辑:如果您使用 GLEW,您需要启用 glewExperimental。 注意查询时3.2 core profile有bug GL_EXTENSIONS。它将报告 1280(好像不是 支持)——但这不应该影响您使用 1.50 着色器 等等。

      【讨论】:

      • 这应该很好,至少暂时是这样。
      • 请参阅 David Greiner 的评论。
      【解决方案3】:

      那个should be possible

      #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 是不同的。
      • 啊,是的,我也看到了那个教程。使用该确切代码仍会在带有 OSX Mountain Lion 的 Air 上生成 OpenGL 2.1 上下文。在我拥有的几台 Windows 机器上(使用 Nvidia 和 ATI 卡),这会创建一个支持最高版本(例如 3.3)的上下文。
      • @NicolBolas 这就是我现在想知道的......这可能只是当前 SDL 版本的一个缺点。
      • @CaptainHead 现在是。我在这里发布了一个回复,其中包含对 SDL 的三左右快速修复,这将允许创建 3.2 上下文。它与较旧的 OSX 不兼容,但是嗯。
      猜你喜欢
      • 1970-01-01
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      相关资源
      最近更新 更多