【问题标题】:OpenGL and GLUT in Eclipse on OS XOS X 上 Eclipse 中的 OpenGL 和 GLUT
【发布时间】:2011-01-05 08:05:42
【问题描述】:

我一直在尝试在 OS X 上使用 CDT 在 Eclipse 中设置 OpenGL 和 GLUT 库,但没有取得多大成功。我似乎无法让 eclipse 真正意识到 GLUT 在哪里。它目前给我的错误是我有一个未解决的包含 GL/glut.h。在网上环顾四周,我发现我应该在 gcc 链接器设置中使用 -framework GLUT 标志,但这似乎无效。

【问题讨论】:

    标签: c++ macos opengl eclipse-cdt glut


    【解决方案1】:

    好的。我让它在 X11 中工作。我只能让它在 X11 上运行的原因是因为操作系统上的 OpenGL 库似乎是针对 64 位架构的,但是如果我们使用 32 位架构,eclipse 只会编译代码。也许如果这个问题得到解决,我们可以使用 OS X 预装的库。此外,也许操作系统上有一个 32 位版本,我们可以使用它,但我似乎找不到它。不过,我对使用 X11 进行学习感到满意。

    首先创建您的 C++ 项目。然后,由于您无法使用 eclipse 编译 64 位代码,请添加以下...

    然后您需要设置库和链接。为此,请执行以下操作:

    最后你需要设置一个 DISPLAY 变量。

    在尝试运行 X11 之前。

    尝试下面的代码来获取我在我的机器上运行的东西。希望它对你有用!

    //#include <GL/gl.h>
    //#include <GL/glu.h>
    #include <GL/glut.h>
    #define window_width  640
    #define window_height 480
    // Main loop
    void main_loop_function() {
        // Z angle
        static float angle;
        // Clear color (screen)
        // And depth (used internally to block obstructed objects)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        // Load identity matrix
        glLoadIdentity();
        // Multiply in translation matrix
        glTranslatef(0, 0, -10);
        // Multiply in rotation matrix
        glRotatef(angle, 0, 0, 1);
        // Render colored quad
        glBegin( GL_QUADS);
        glColor3ub(255, 000, 000);
        glVertex2f(-1, 1);
        glColor3ub(000, 255, 000);
        glVertex2f(1, 1);
        glColor3ub(000, 000, 255);
        glVertex2f(1, -1);
        glColor3ub(255, 255, 000);
        glVertex2f(-1, -1);
        glEnd();
        // Swap buffers (color buffers, makes previous render visible)
        glutSwapBuffers();
        // Increase angle to rotate
        angle += 0.25;
    }
    // Initialze OpenGL perspective matrix
    void GL_Setup(int width, int height) {
        glViewport(0, 0, width, height);
        glMatrixMode( GL_PROJECTION);
        glEnable( GL_DEPTH_TEST);
        gluPerspective(45, (float) width / height, .1, 100);
        glMatrixMode( GL_MODELVIEW);
    }
    // Initialize GLUT and start main loop
    int main(int argc, char** argv) {
        glutInit(&argc, argv);
        glutInitWindowSize(window_width, window_height);
        glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
        glutCreateWindow("GLUT Example!!!");
        glutDisplayFunc(main_loop_function);
        glutIdleFunc(main_loop_function);
        GL_Setup(window_width, window_height);
        glutMainLoop();
    }
    

    【讨论】:

    • 这个答案让我找到了我正在寻找的解决方案。我现在已经正确配置了所有内容。我必须对提供的代码进行的唯一更改是根据 Brian 的回答将 include 语句更改为 #include
    【解决方案2】:

    根据您在 OS X 中安装的 GLUT 库,您的包含可能会有所不同。

    在我的系统上我必须使用:

    #include <GLUT/glut.h>
    

    为了确保我的代码是跨平台的,我使用以下预处理器语句:

    #if defined(__APPLE__) && defined(__MACH__)
    # include <GLUT/glut.h>
    #else 
    #  include <GL/glut.h>
    #endif
    

    这可能会解决一些问题或您的问题。

    【讨论】:

    • 我的意思是说我确实将包含更改为状态 GLUT/glut.h 但这似乎仍然没有解决问题。我相信我安装的 GLUT 库来自 macports 版本。我也确实安装了 XCode 并从那里获得了所有库。
    【解决方案3】:


    我写了一篇关于如何设置 Eclipse 以在 C/C++ 中开发 OpenGL(和 GLUT)应用程序的文章和WindowsMac OS X 中的 Java(如果您有兴趣)。 它包含准备好系统所需的所有步骤和所有知识。

    您可以在这里找到它:
    Setup Eclipse to develop OpenGL & GLUT apps in Java & C/C++ on Windows & MAC OS X!

    【讨论】:

      【解决方案4】:

      MacPorts 的默认安装目录是 /opt/local。可能是 /opt/local 未添加到 Eclipse 中的编译器包含路径。要么,要么重新安装 Xcode 以在 Xcode 库的默认包含路径上为您提供 GLUT/glut.h(然后您可能需要将其添加到 eclipse 中?我不运行 OS X,所以我不能说 Xcode installdir 是什么是,但它看起来可能在 /Developer 或 /Library/Developer/Shared 中)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-04
        • 2012-06-30
        • 2020-07-17
        • 2012-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多