Wednesday, August 15, 2012

Setup Eclipse C++ and OpenGL support on Ubuntu Linux

Currently I am using Ubuntu 12.04 and Eclipse Version 3.7.2. I am trying to write little note how to setup Ubuntu Linux and Eclipse C++ for developing openGL purpose.

We usually use Glut, FreeGlut and Mesa for developing openGL in Linux  system. We need to get update from Ubuntu repository before installing openGL development resource and then second step install the openGL library.
sudo apt-get update
sudo apt-get install libglu1-mesa-dev freeglut3-dev mesa-common-dev
After resource got installed, we need to setup Eclipse IDE, Eclipse can be done by install from Ubuntu repository with CDT Support.
sudo apt-get install eclipse eclipse-cdt eclipse-cdt-jni
After all tools installed, now it's time to start setup Eclipse IDE and then after Eclipse started, we need to create new C++ project using menu File->New->C++ Project or using key shortcut Shift+Alt+N.


Select Empty Project and Cross GCC then give project name to whatever, in this case I use "opengl_glut" as project name. Then select finish button to completed creating project.

Before start create code, we need to add some linker option, so when linker process the library can be found by linker program. To add glut library on the linker process can be complete by using rigth click on the opengl_glut project then select properties.


Choose C/C++ Build Section, select Setting then select Libraries on C++ Linker section. Add "glut" and "GLU" library on the project then select Ok Button to apply changes.

Now we can start openGL code, create main.cpp by right click on the opengl_glut project and select New->File then give File Name as entry point "main.cpp". Add this code to main.cpp to check if our Eclipse setting is correct:


#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!!!");
    glutIdleFunc(main_loop_function);
    GL_Setup(window_width, window_height);
    glutMainLoop();
}


Build the code by right click on the opengl_glut then select Build Project menu, this should compile without any error. Then select menu Run->Run or using shortcut Ctrl+F11. If no error, the sample should become to be like this...then we completed all setting setup step for openGL development environment using Eclipse in Ubuntu Linux system.


13 comments:

  1. Thanks a lot for those tips!

    PHP 5 developer

    ReplyDelete
  2. Nice entry, it helped me a lot to move from visual studio to eclipse, I just got one problem, well actually it isn't a problem but I want to clear this out... basically eclipse displays every single gl function as an error. the project compiles and runs well but the editor is kinda bugging me (ha! see what I did there? "bugging me?" lol).

    ReplyDelete
  3. For those of you who s till get the problem with the undefined reference to basically all OpenGL functions and methods - in the G++ Linker settings in the "Library search path (-L)" add the path where the linker can find those libraries. In my case it was /usr/lib/ (simply look for libglut.so etc.). After that it worked like a charm. Thanks for the tutorial. ;)

    ReplyDelete
    Replies
    1. still having all the problem, its not finding any of the open gl functions, how do you find the links in the file and how do you add the linkers?? thank you!!

      Delete
  4. Thanks for the tutorial. But even after adding the "Library search path(-L)" to the linker settings, I still get the errors saying "'glEnable' is defined in DSO /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1 so try adding it to the linker command line". Please help.

    ReplyDelete
  5. As of Ubuntu 13.04, there are two additional steps you should take:
    1.) In Build Settings, add GL in addition to glut and GLU.
    2.) In Run/Debug settings, add the environment variable "DISPLAY" with a value of :0.0

    Cheers!

    ReplyDelete
  6. Thank you for the tutorial butt the problem is at the last step when i am running the code it gives me this error message "freeglut (/home/a/wspace_c++/opengl_glut/Debug/opengl_glut): failed to open display ''
    "

    ReplyDelete
  7. For those who still can't get it to work even after adding the link address (in my case
    /usr/lib/x86_64-linux-gnu/mesa/ and
    /usr/lib/).
    Just add GL to the cross G++ linker library. (hence, glut, GLU and GL one at a time). That fixed it for me.

    ReplyDelete
  8. Hi. You need to add also "GL" in the linker option of eclipse "glut" and "GLU" is not enough!

    ReplyDelete
  9. Add : glut GLU and GL
    Add there paths as well.
    to find paths you can use locate command

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete