1

我正在尝试按照此处的教程在 Mac 上学习 OpenGL 。这包括设置 GLEW、GLFW 和 GLM,这是我使用自制软件完成的。作为 OpenGL、XCode 和 C++ 的新手,我花了一些时间在谷歌上搜索,但我设法找出了所有各种头文件路径、库路径和链接器参数来进行设置。

现在我在运行此代码时收到错误“地址不包含指向目标文件中的部分的部分”。由于不熟悉(坦率地说很奇怪)XCode UI,我无法追踪问题的根源。谷歌只是指向我关于 ARC 的 Objective-C 相关文章。好吧,这不是 Obj-C,而且我没有使用 ARC,所以没有运气。

有什么想法可能导致它吗?

// Include standard headers
#include <stdio.h>
#include <stdlib.h>
// Include GLEW. Always include it before gl.h and glfw.h, since it's a bit magic.
#include <GL/glew.h>
// Include GLFW
#include <GL/glfw.h>
// Include GLM
#include <glm/glm.hpp>
using namespace glm;

//http://www.opengl-tutorial.org/beginners-tutorials/tutorial-1-opening-a-window/
int main(int argv, char ** argc){
    // Initialise GLFW
    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        return -1;
    }
    glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4); // 4x antialiasing

    //http://www.glfw.org/faq.html#42__how_do_i_create_an_opengl_30_context
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
    glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    glfwOpenWindowHint(GLFW_OPENGL_PROFILE, 0); //We don't want the old OpenGL    // Open a window and create its OpenGL context
    if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
    {
        fprintf( stderr, "Failed to open GLFW window\n" );
        glfwTerminate();
        return -1;
    }
    //We can't call glGetString until the window is created (on mac).
    //http://www.idevgames.com/forums/thread-4218.html
    const GLubyte* v=glGetString(GL_VERSION);
    printf("OpenGL version: %s\n", (char*)v);


    /***** BAD CODE SOMEWHERE IN THIS BLOCK.*/
    {
        //http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/
        GLuint vertexArrayID;
        //generate a vertexArray, put the identifier in VertexArrayID
        glGenVertexArrays(1, &vertexArrayID);
        //bind it. (?)
        glBindVertexArray(vertexArrayID);

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

        // This will identify our vertex buffer
        GLuint vertexBufferId;

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

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

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

    /* END BAD CODE BLOCK */
    }

    // Initialize GLEW
    glewExperimental=true; // Needed in core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }

    glfwSetWindowTitle( "Tutorial 01" );
    // Ensure we can capture the escape key being pressed below
    glfwEnable( GLFW_STICKY_KEYS );

    do{
        // 1rst attribute buffer : vertices
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vertexBufferId);
        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
                              );

        // Draw the triangle !
        glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle

        glDisableVertexAttribArray(0);

        // Swap buffers
        glfwSwapBuffers();

    } // Check if the ESC key was pressed or the window was closed
    while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
          glfwGetWindowParam( GLFW_OPENED ) );
}
4

0 回答 0