2010-01-15 78 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中)。