OpenGL and GLUT in Eclipse on OS X

Ok. I got it working in 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. Maybe if this got fixed we could use OS X pre-installed libraries. 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. I, however, am content with using X11 for my learning purposes.

First create your C++ project. Then since you can't compile code in 64-bit using eclipse add the following...

alt text

alt text

alt text

Then you need your libraries and linking set up. To do this do the following:

alt text

alt text

Lastly you need to set a DISPLAY variable.

alt text

Before you try running start up 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.

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. 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!