在 Mac OS 10.15 中打开 GL 版本 2.1 而不是 4.1

Open GL version 2.1 instead of 4.1 in Mac OS 10.15

我是 Mac 的新手,所以我不太熟悉这个 OS。

我在 Xcode 中编写了一个简单的 Open GL 程序并且它 运行 没有问题。但是,当我使用以下代码检查版本时

cout<<glGetString(GL_VENDOR)<<endl;
cout<<glGetString(GL_RENDERER)<<endl;
cout<<glGetString(GL_VERSION)<<endl;
cout<<glGetString(GL_SHADING_LANGUAGE_VERSION)<<endl;

初始化代码

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("First Test");
initRendering();
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop();

我得到以下输出

ATI Technologies Inc.
AMD Radeon Pro 5300M OpenGL Engine
2.1 ATI-3.10.15
1.20

我在其他地方的论坛上看到 Mac OS 10.15 支持 Open GL 4.1 版本,这里的显卡当然也可以支持更高版本。

所以我的问题如下:

  1. 为什么我的机器上显示 2.1
  2. 如何解决这个问题?是否有我可以输入的代码来解决问题或需要安装更多软件?

任何方向都很好。

谢谢

Edit: the answer was posted before I know he's using glut, also I recommend GLFW for Modern OpenGL 4.1+

我认为您应该先定义版本并创建上下文,使用 GLFW 等库并设置要使用的 OpenGL 配置文件。还使用 GLEW/GLAD 库进行 GL 扩展管理。 如果您使用 GLFW 和 GLEW,您可以添加此代码来定义版本并创建上下文和 window。然后再次检查版本。

#include <iostream>
#include <GL/glew.h> 
#include <GLFW/glfw3.h>

using namespace std;
int main() 
{
   // Initialize GLFW
   glfwInit();

   // Define version and compatibility settings
   glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //ver
   glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); 
   glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
   glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // for MAC ONLY
   glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

   // Create OpenGL window and context
   GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", NULL, NULL);
   glfwMakeContextCurrent(window);

   // Check for window creation failure
   if (!window) 
   {
       // Terminate GLFW
       glfwTerminate();
       return 0; 
   }

   // Initialize GLEW
   glewExperimental = GL_TRUE; glewInit();

   // your code
   cout<<glGetString(GL_VENDOR)<<endl;
   cout<<glGetString(GL_RENDERER)<<endl;
   cout<<glGetString(GL_VERSION)<<endl;
   cout<<glGetString(GL_SHADING_LANGUAGE_VERSION)<<endl;


   // Event loop
   while(!glfwWindowShouldClose(window)) 
   {
       // Clear the screen to black
       glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT);
       glfwSwapBuffers(window);
       glfwPollEvents(); 
   }

   // Terminate GLFW
   glfwTerminate(); return 0;
} 

如果您还没有安装 GLFW 和 GLEW,您可以查看本教程为 MacOS 安装它们:https://riptutorial.com/opengl/example/21105/setup-modern-opengl-4-1-on-macos--xcode--glfw-and-glew- or check this one: https://giovanni.codes/opengl-setup-in-macos/

in case it does not work and still showing 2.1 try to go to the "Energy Saver" in the system settings and deselect the "Automatic graphics switching".

GLUT 很古老,不支持常见的 macOS 功能,例如 HiDPI 或鼠标滚动。您可能想改用 GLFW 库 (see here for what you need to do for a 4.1 context)。

但是如果你真的想使用GLUT,你需要添加

glutInitContextVersion(4, 1);
glutInitContextProfile(GLUT_CORE_PROFILE);

glutInit 之后。