OpenGL 3.3 + OSX + GLFW不起作用

时间:2014-08-04 17:54:41

标签: c macos opengl osx-mavericks osx-yosemite

我正在关注http://opengl-tutorials.org教程系列,并提出了一个非常基本的代码,用于在屏幕上绘制白色三角形,但我似乎无法让它工作。

我在这里使用appleshell的答案编译了glfw而没有任何其他选项:OpenGL 3.3/4.1 on Mac OSX 10.9 using GLFW library

我根据网站说明创建了一个简单的源代码:

#include <stdio.h>
#include <stdlib.h>

#include <glfw3.h>

// Need gl3.h for glGenVertexArrays() and glBindVertexArray()
#include <OpenGL/gl3.h>

// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
    -1.0f, -1.0f, 0.0f,
    1.0f, -1.0f, 0.0f,
    0.0f,  1.0f, 0.0f,
};

#define printGlErr(func) err = glGetError(); if (err) fprintf(stderr, func " error: %u at line %d\n", err, __LINE__);
GLuint err;

int main(int argc, const char * argv[]) {
    if (!glfwInit()) {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        return -1;
    }

    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL

    // Open a window and create its OpenGL context
    GLFWwindow* window;
    window = glfwCreateWindow(1024, 768, "Triangle", NULL, NULL);
    if (window == NULL) {
        fprintf(stderr, "Failed to open GLFW window.\n");
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    // Ensure we can capture the escape key being pressed below
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    // Init stuff
    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);

    // This will identify our vertex buffer
    GLuint vertexbuffer;

    // Generate 1 buffer, put the resulting identifier in vertexbuffer
    glGenBuffers(1, &vertexbuffer);
    printGlErr("glGenBuffers()");

    // The following commands will talk about our 'vertexbuffer' buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    printGlErr("glBindBuffer()");

    // Give our vertices to OpenGL.
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
    printGlErr("glBufferData()");

    do {
        // Draw stuff
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
        glVertexAttribPointer(
                              0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
                              3,                  // size
                              GL_FLOAT,           // type
                              GL_FALSE,           // normalized?
                              0,                  // stride
                              (void*)0            // array buffer offset
                              );
        printGlErr("glVertexAttribPointer()");

        glDrawArrays(GL_TRIANGLES, 0, 3);
        printGlErr("glDrawArrays()");

        glDisableVertexAttribArray(0);
        printGlErr("glDisableVertexAttribArray()");

        // Swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents();
    } while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0); // Check if the ESC key was pressed or the window was closed

    printf("Goodbye!\n");
    return(0);
}

我似乎无法让代码工作。我在glDrawArrays()函数调用上获得了GL_INVALID_OPERATION。

代码在Mavericks和Yosemite上呈现黑色窗口。这是我的硬件规格:http://support.apple.com/kb/SP653

它没有崩溃,它在ESC上退出,但没有其他事情发生(除了在控制台上发送错误代码)。使用2个可用视频板中的任何一个都可以获得相同的结果。

另外,我正在使用终端上的以下行编译源代码: gcc main.c -o triangle -framework OpenGl -framework Cocoa -framework IOKit -framework CoreVideo -I../glfw/include/GLFW -L../glfw/src -lglfw3

0 个答案:

没有答案