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.
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 update
sudo apt-get install libglu1-mesa-dev freeglut3-dev mesa-common-dev
sudo apt-get install eclipse eclipse-cdt eclipse-cdt-jniAfter 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.