Graphics Programming in Linux

Graphics with Xlib

This article covers some basics of graphics programming in C.

I was a very avid graphics programmer, using Turbo C (actually, using graphics.c, a library usually taught to engineering students during labs). Trying to push graphics.c to its limits, I got stuck with its very limited graphics functionality and maximum resolution. When I discussed this with a teacher, he introduced me to OpenGL. I tried a few programs, and got fascinated with the rich graphics experience and platform independence. I thought of sharing my experiences with graphics programming using OpenGL, and contacted the LFY editorial team members, who were ready to provide me a platform.

OpenGL basics
OpenGL (Open-source Graphics Library) has three major library header files: gl.h, glu.h and glut.h. The gl.h (graphics library) is a low-level header file, with which you can draw lines, polygons, colour the background or the line, etc. The glu.h (graphics library utility) is a medium-level header. It uses the lower-level OpenGL functions, such as matrices for specific viewing orientation, rendering surfaces, etc. The glut.h (graphics library utility tool-kit) is a high-level library file and a window system-independent tool-kit to hide the complexity of different window system APIs.

Now, we need a Linux-based OS (I use Kubuntu 11.10); a compiler (GCC); an editor (Kate, Gedit, Kwrite, etc); the OpenGL header files… and some basic knowledge of C programming.
Make sure your PC has an Internet connection. Open a terminal and run sudo apt-get install gcc, entering the password when prompted, to install GCC. Next, for the editor, run sudo apt-get install kate (or gedit, kwrite or whatever you prefer, instead of kate). For the OpenGL headers, run ‘sudo apt-get install freeglut3-dev.

The Hello World  first step
Now for the classic first-time program. Open the editor, and enter the following code:


void main(int argc, char**argv) { 
    glutInit(&argc, argv); 
    glutCreateWindow(“Hello World”); 

Save the file as basic.c, and to compile it, run gcc basic.c -l glut at the terminal. Run the compiled binary with ./a.out. Here, we have imported glut.h (in the GL folder). To locate it, run whereis GL and it will give you the installed location, like GL: /usr/include/GL. There are just five basic functions used to create a window for drawing. The first, glutInit(), initialises the display. Then glutInitWindowPosition (int x, int y) specifies the screen location (upper-left corner) of the window. Next, glutInitWindowSize(int x, int y) specifies the size of the window; glutCreateWindow(char *string) names the window for identification, and creates it; andGlutMainLoop() is used to display the window and begin event processing.
The compilation command specified -l glut, which means, a link with the glut library file. By default, GCC names the output compiled binary a.out; to change it, specify the -o (output file name) switch for example, gcc begin.c -l glut -o hello.
Change the background colour of a window
The OpenGL function glutDisplayFunc (void function_name) is used to specify a user function that will handle the display of a window. Let’s use this to change the colour of a window:

void display() { 
void main(int argc, char**argv) { 
    glutInit(&argc, argv); 
    glutCreateWindow(“Hello World”); 

Save it as basic2.c, compile and run it as in the earlier example.
The three GL functions we used to colour the window background are:
glClearColor(R, G, B, alpha): This is used to set the colour for a window. The numbers you can use for each colour are between 0 and 1; you can use float numbers like 0.1, 0.11, etc.
glClear(GL_COLOR_BUFFER_BIT): This clears the screen to the desired colour set by glClearColor.
glFlush() executes the commands to the screen rather than storing it in a buffer.
In the next part of the series, I plan to explore how to draw lines and triangles, and use keyboard inputs.

I would like to thank Dr Brijesh Kumar for his help and guidance.


  1. Thanks for this introduction to opengl. Just a note to say, the “Hello World” string should be enclosed in double quotes in both instances of:

    glutCreateWindow(“Hello World”);

  2. The second example, “basic2.c” is not compiling for me, althought the first one does compile. Are you sure I build the second example in exactly the same way as the first? I suspect that I need to add another library for gl, not just -l glut but maybe something like -l gl ?

  3. The first example “build.c” works only after you insert double quotes around the “Hello World” string. The second example does not build, even after the double quotes. I get the errors:

    /usr/bin/ld: basic2.o: undefined reference to symbol ‘glClear’
    //usr/lib/x86_64-linux-gnu/mesa/ error adding symbols: DSO missing from command line

    Do you think I need to add more libraries in the build line, e.g. -l gl for example? Could you publish the exact command line to type in order to compile “build2.c” ?

  4. When I execute the second example, a window flashes up for an instant then vanishes. The terminal window reports,
    “X Error of failed request: BadMatch (invalid parameter attributes)

    Major opcode of failed request: 73 (X_GetImage)” …

    This is after executing with -lGL -lglut.

    I’m running Raspbian on a Raspberry Pi. Is that the problem?

  5. Thanks. This was very helpful and clear.
    I’m running Raspbian on a Raspberry Pi 2+.

    The only problems I had were when I jumped ahead without reading the details.


Please enter your comment!
Please enter your name here