简体   繁体   English

OS X上的Eclipse中的OpenGL和GLUT

[英]OpenGL and GLUT in Eclipse on OS X

I have been trying to setup the OpenGL and GLUT libraries in Eclipse, with CDT, on OS X with not very much success. 我一直试图在OS X上使用CDT在Eclipse中设置OpenGL和GLUT库并没有取得多大成功。 I cannot seem to get eclipse to actually realize where GLUT is. 我似乎无法让eclipse真正意识到GLUT在哪里。 It is currently giving me the error that I have an unresolved inclusion GL/glut.h. 它目前给我一个错误,我有一个未解决的包含GL / glut.h。 Looking around online I found that I should be using the -framework GLUT flag in the gcc linker settings, but this seems ineffective. 在网上查看我发现我应该在gcc链接器设置中使用-framework GLUT标志,但这似乎无效。

Ok. 好。 I got it working in X11. 我在X11工作了。 The reason I could only get it working on X11 is because it seems the OpenGL libs on the OS are for the 64-bit architecture, but eclipse will only compile code if we use 32-bit architecture. 我之所以能够在X11上工作的原因是因为OS上的OpenGL库似乎是针对64位架构的,但是如果我们使用32位架构,eclipse只会编译代码。 Maybe if this got fixed we could use OS X pre-installed libraries. 也许如果这个问题得到解决,我们可以使用OS X预装的库。 Also, maybe there is a 32-bit version lying around on the OS we could use that but I can't seem to find it. 此外,也许在我们可以使用的操作系统上有一个32位版本,但我似乎无法找到它。 I, however, am content with using X11 for my learning purposes. 但是,我满足于将X11用于学习目的。

First create your C++ project. 首先创建您的C ++项目。 Then since you can't compile code in 64-bit using eclipse add the following... 那么既然你无法使用eclipse编译64位代码,请添加以下内容...

替代文字

替代文字

替代文字

Then you need your libraries and linking set up. 然后你需要你的库和链接设置。 To do this do the following: 为此,请执行以下操作:

替代文字

替代文字

Lastly you need to set a DISPLAY variable. 最后,您需要设置一个DISPLAY变量。

替代文字

Before you try running start up X11. 在您尝试运行启动X11之前。

Try the following code to get something I've got running in my machine. 尝试使用以下代码来获取我在机器中运行的内容。 Hope it works for you! 希望这对你有用!

//#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();
}

Depending on which GLUT library you installed in OS X your include might be different. 根据您在OS X中安装的GLUT库,您的包含可能会有所不同。

On my system I have to use: 在我的系统上,我必须使用:

#include <GLUT/glut.h>

To make sure my code is cross platform I use the following pre-processor statement: 为了确保我的代码是跨平台的,我使用以下预处理器语句:

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

That might fix some or your problems. 这可能会解决一些或你的问题。


I wrote an article about how to setup Eclipse to develop OpenGL (and GLUT ) applications in C/C++ and Java in both Windows and Mac OS X if you are interested. 我写了一篇关于如何设置Eclipse以在WindowsMac OS X中使用C / C ++Java开发OpenGL (和GLUT )应用程序的文章,如果您感兴趣的话。 It contains all the steps and all you need to know to have the system ready. 它包含所有步骤以及系统准备就绪所需的全部知识。

You can find it here: 你可以在这里找到它:
Setup Eclipse to develop OpenGL & GLUT apps in Java & C/C++ on Windows & MAC OS X! 设置Eclipse在Windows和MAC OS X上用Java和C / C ++开发OpenGL和GLUT应用程序!

The default install directory for MacPorts is /opt/local. MacPorts的默认安装目录是/ opt / local。 Could be /opt/local isn't added to your compiler include path in Eclipse. 可能/ opt / local未添加到Eclipse中的编译器包含路径中。 Either that, or reinstalling Xcode to give you GLUT/glut.h on the default include path for Xcode libs (which you may then need to add to eclipse? I don't run OS X so I can't say what the Xcode installdir is, but it looks like it could be in /Developer, or /Library/Developer/Shared). 要么是重新安装Xcode,要么在Xcode libs的默认包含路径上给你GLUT / glut.h(你可能需要添加到eclipse?我不运行OS X所以我不能说Xcode installdir是什么是,但看起来它可能在/ Developer,或/ Library / Developer / Shared)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM