I will try to show you briefly how to create Linux OpenGL applications.
We check if the graphics card and settings are done correctly, using the command:
1 2 3 4 5 6 7 8 9 10 11 | $ glxinfo name of display: :0.0 display: :0 screen: 0 direct rendering: Yes server glx vendor string: NVIDIA Corporation server glx version string: 1.4 server glx extensions: GLX_EXT_visual_info, GLX_EXT_visual_rating, GLX_SGIX_fbconfig, GLX_SGIX_pbuffer, GLX_SGI_video_sync, GLX_SGI_swap_control, GLX_EXT_texture_from_pixmap, GLX_ARB_multisample, GLX_NV_float_buffer ... |
As freeglut devel package provides libraries for OpenGL. Let’s install that package.
1 | # yum -y install freeglut-devel |
Then we see if we have the necessary files.
1 2 3 4 5 6 7 | $ cd /usr/include/GL ... GL]$ ls freeglut_ext.h gl.h glut.h GLwMDrawAP.h glx_mangle.h internal freeglut.h gl_mangle.h GLwDrawA.h glxext.h glxmd.h freeglut_std.h glu.h GLwDrawAP.h glx.h glxproto.h glext.h glu_mangle.h GLwMDrawA.h glxint.h glxtokens.h |
Let’s try a simple OpenGL example. This example uses glut functions.
The code source is shown below with explanations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include <GL/glut.h> // define the size of the window #define window_width 640 #define window_height 480 // Main loop function void main_loop_function() { // Z angle to totate static float angle; // Clear color screen // And depth , is 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 , you can use any model vertex 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 : the color buffers and 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 function int main(int argc, char** argv) { glutInit(&argc, argv); glutInitWindowSize(window_width, window_height); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutCreateWindow("www.free-tutorials.org simple GLUT example."); glutIdleFunc(main_loop_function); GL_Setup(window_width, window_height); glutMainLoop(); } |
1 | $ gcc test.c -I /usr/include/GL -L /usr/include/GL -lGL -lGLU -lglut -o test |
We run the application now.
I wait for your opinion and possibly a more complex example.