2010-01-15 60 views
8

我一直在尝试在OS X上用Eclipse创建OpenGL和GLUT库,并且CDT的成功率不是很高。我似乎无法实现eclipse来实际了解GLUT的位置。它目前给我的错误是我有一个未解决的包含GL/glut.h。在网上查看我发现我应该在gcc链接器设置中使用-framework GLUT标志,但这看起来无效。OS X上的Eclipse中的OpenGL和GLUT

回答

7

好的。我在X11中工作。我只能在X11上工作的原因是因为OS上的OpenGL库似乎是64位体系结构,但如果我们使用32位体系结构,eclipse只会编译代码。也许如果这个问题得到解决,我们可以使用OS X预装库。另外,也许在操作系统上有一个32位版本可以使用,但我似乎无法找到它。但是,我满足于将X11用于学习目的。

首先创建你的C++项目。然后,因为你不能在64位使用eclipse添加以下代码编译...

alt text http://img132.imageshack.us/img132/5163/step1c32bit.png

alt text http://img251.imageshack.us/img251/5163/step1c32bit.png

alt text http://img132.imageshack.us/img132/2325/step1linker32bit.png

然后你需要你的库和链接设置。要做到这一点做到以下几点:

alt text http://img29.imageshack.us/img29/1904/step2linkerglglu.png

alt text http://img196.imageshack.us/img196/4313/step2glut.png

最后你需要设置显示变量。

alt text http://img40.imageshack.us/img40/7306/step3display.png

,然后再尝试运行启动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(); 
} 
+1

这个答案让我想到了我正在寻找的解决方案。我现在已经正确配置了一切。我必须用提供的代码做的唯一改变是根据Brian的回答将include语句更改为#include 。 – fastrack20 2010-01-18 18:21:13

3

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

在我的系统必须使用:

#include <GLUT/glut.h> 

要确保我的代码是跨平台我使用下面的预处理器声明:

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

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

+0

我的意思是说,我确实改变了包括国家GLUT/glut.h但这似乎仍然没有固定的问题。我相信我安装的GLUT库来自macports版本。我也安装了XCode并从那里安装了所有的库。 – fastrack20 2010-01-15 01:32:44

1

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