当前位置: 首页 > 知识库问答 >
问题:

OpenGL 4.1不能在Mavericks上运行

赵俊侠
2023-03-14

我用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);
}

删除不推荐使用的函数后运行的新代码。当我运行这段代码时,我得到了分段错误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;
}

共有1个答案

华泽语
2023-03-14

您要求的是GL3.2核心配置文件(尽管您的标题谈到了4.1)。您正在使用的几乎所有GL命令在核心上下文中都不可用。矩阵堆栈不见了,固定函数流水线不见了,即时模式不见了。除了glviewport()glclear()之外,您的所有总帐调用都是无效的,只会生成总帐错误。

你必须完全重写这一点,使其与现代GL工作。注意,在OSX上,没有兼容性配置文件。因此,您要么获得具有2.x上下文的旧内容,要么获得GL>=3.x而删除了旧内容。

 类似资料:
  • 我有一个C++SFMLOpenGL4.1渲染引擎,我想移植到OS X10.9小牛。SFML似乎不支持OS X上的OpenGL4(或3)。如果我想坚持使用C++(我更喜欢避免Obj C)来获得OS X中的渲染上下文,我有什么选择?

  • 我的mac是2012年中期的MacBook Pro。它应该支持OpenGL4.1根据应用。 那么如何编译版本330着色器呢?

  • 这些天我正在学习微服务架构,我需要运行Kafka来遵循一些教程。然而,zookeeper-server-start在cmd上运行Kafka的第一步对我来说不起作用。它说“命令的语法不正确” 我遵循了下面的过程 > 下载kafka和extarct(https://kafka.apache.org/downloads-kafka2.11-2.1.0.tgz(二进制下载)) bin\windows\zo

  • 我创建了一个JasperReport应用程序,它在tomcat服务器上运行良好。但是当我使用相同的jar在Jboss上运行时,它会显示错误 原因:java.lang.ClassCastException:org.apache.xerces.jaxp.DocumentBuilderFactoryImpl无法强制转换为javax.xml.parsers.DocumentBuilderFactor.ne

  • 我安装了最新版本的Java,当我尝试运行eclipse时,它会说: JVM的版本不适合本产品。版本:需要或更高版本。 为什么?我该怎么办?