OpenGL 4.1 不适用于 Mavericks

Posted

技术标签:

【中文标题】OpenGL 4.1 不适用于 Mavericks【英文标题】:OpenGL 4.1 not working on Mavericks 【发布时间】:2013-11-20 16:10:34 【问题描述】:

我用 glfw3 编写了一个简单的程序。它编译得很好,但是当我运行它时,什么都没有呈现。只是弹出一个空白屏幕。

#include </usr/include/GL/glew.h>
#include </usr/local/include/GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
static void error_callback(int error, const char* description)

    fputs(description, stderr);

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)

    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);

int main(void)

    GLFWwindow* window;
    glfwSetErrorCallback(error_callback);
    if (!glfwInit())
        exit(EXIT_FAILURE);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
    if (!window)
    
        glfwTerminate();
        exit(EXIT_FAILURE);
    
    glfwMakeContextCurrent(window);
    glfwSetKeyCallback(window, key_callback);

    //printf("OpenGL Context: %d.%d\n", glfwGetWindowParam(GLFW_CONTEXT_VERSION_MAJOR), glfwGetWindowParam(GLFW_CONTEXT_VERSION_MINOR));
    while (!glfwWindowShouldClose(window))
    
        float ratio;
        int width, height;
        glfwGetFramebufferSize(window, &width, &height);
        ratio = width / (float) height;
        glViewport(0, 0, width, height);
        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
        glBegin(GL_TRIANGLES);
        glColor3f(1.f, 0.f, 0.f);
        glVertex3f(-0.6f, -0.4f, 0.f);
        glColor3f(0.f, 1.f, 0.f);
        glVertex3f(0.6f, -0.4f, 0.f);
        glColor3f(0.f, 0.f, 1.f);
        glVertex3f(0.f, 0.6f, 0.f);
        glEnd();
        glfwSwapBuffers(window);
        //glfwPollEvents();
        glfwWaitEvents();
    
    glfwDestroyWindow(window);
    glfwTerminate();
    exit(EXIT_SUCCESS);

我在带有 AMD Radeon HD 6490M OpenGL 引擎的 2011 年初 Macbook Pro 上运行它,并在上面安装了 Mavericks。

删除不推荐使用的函数后我运行的新代码。运行此代码时出现分段错误 11。

#ifdef __APPLE__
#include </usr/include/GL/glew.h>
#else
#include <GL/glew.h>
#endif
#ifdef __APPLE__
#include </usr/local/include/GLFW/glfw3.h>
#else
#include <GL/glfw.h>
#endif
#include <stdio.h>


float points[] = 
   0.0f,  0.5f,  0.0f,
   0.5f, -0.5f,  0.0f,
  -0.5f, -0.5f,  0.0f
;

const char* vertex_shader =
"#version 400\n"
"in vec3 vp;"
"void main () "
"  gl_Position = vec4 (vp, 1.0);"
"";

const char* fragment_shader =
"#version 400\n"
"out vec4 frag_colour;"
"void main () "
"  frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"
"";

int main () 
  // start GL context and O/S window using the GLFW helper library
  if (!glfwInit ()) 
    fprintf (stderr, "ERROR: could not start GLFW3\n");
    return 1;
   
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);


  GLFWwindow* window = glfwCreateWindow (640, 480, "Hello Triangle", NULL, NULL);
  if (!window) 
    fprintf (stderr, "ERROR: could not open window with GLFW3\n");
    glfwTerminate();
    return 1;
  
  glfwMakeContextCurrent (window);

  // start GLEW extension handler
  glewInit ();

  // get version info
  const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
  const GLubyte* version = glGetString (GL_VERSION); // version as a string
  printf ("Renderer: %s\n", renderer);
  printf ("OpenGL version supported %s\n", version);


  // tell GL to only draw onto a pixel if the shape is closer to the viewer
  glEnable (GL_DEPTH_TEST); // enable depth-testing
  glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"

unsigned int vbo = 0;
glGenBuffers (1, &vbo);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW);

unsigned int vao = 0;
glGenVertexArrays (1, &vao);
glBindVertexArray (vao);
glEnableVertexAttribArray (0);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);

unsigned int vs = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (vs, 1, &vertex_shader, NULL);
glCompileShader (vs);
unsigned int fs = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (fs, 1, &fragment_shader, NULL);
glCompileShader (fs);
unsigned int shader_programme = glCreateProgram ();
glAttachShader (shader_programme, fs);
glAttachShader (shader_programme, vs);
glLinkProgram (shader_programme);

while (!glfwWindowShouldClose (window)) 
  // wipe the drawing surface clear
  glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glUseProgram (shader_programme);
  glBindVertexArray (vao);
  // draw points 0-3 from the currently bound VAO with current in-use shader
  glDrawArrays (GL_TRIANGLES, 0, 3);
  // update other events like input handling 
  glfwPollEvents ();
  // put the stuff we've been drawing onto the display
  glfwSwapBuffers (window);

  /* OTHER STUFF GOES HERE NEXT */

  // close GL context and any other GLFW resources
  glfwTerminate();
  return 0;

【问题讨论】:

MacOS X 是否支持旧式即时模式 OpenGL?而且您在这里要求核心配置文件 OpenGL 3.2,不确定它是否具有已弃用的功能... 我刚刚写了一个可能对***.com/questions/19865463/opengl-4-1-under-mavericks/…987654321有帮助的问题的答案 【参考方案1】:

您正在请求 GL 3.2 核心配置文件(尽管您的标题谈到 4.1)。您使用的几乎所有 GL 命令在核心上下文中都不可用。矩阵堆栈不见了,固定功能管道不见了,立即模式不见了。 除了 glViewport()glClear() 之外,您的所有 GL 调用都是无效的,应该只会产生 GL 错误。

您必须完全重写它才能使其与现代 GL 一起使用。请注意,在 OSX 上,没有兼容性配置文件。因此,您要么获得带有 2.x 上下文的旧内容,要么获得 GL >=3.x 并删除旧内容。

【讨论】:

我已更改代码。我有一个我在上面添加的新代码。我遇到了分段错误 11 我能够通过添加一行 glewExperimental=GL_TRUE; 使其工作。就在 glfwMakeContextCurrent(窗口)之前; @noddy:附带说明:你应该检查glewInit()的返回值。

以上是关于OpenGL 4.1 不适用于 Mavericks的主要内容,如果未能解决你的问题,请参考以下文章

OpenGL 不使用 GLFW 和 GLLoadGen 在 Mavericks 上绘图

OpenGL 3.0 不适用于 Windows 上的 Qt

OpenGL 代码适用于 Windows 但不适用于 Mac

OpenGL代码适用于GLFW,但不适用于Qt OpenGL

OpenGL gluLookat 不适用于着色器

OpenGL - 在 NDC 中计算位置适用于着色器,但不适用于“常规”程序