Click here to Skip to main content
Click here to Skip to main content
Go to top

Setting up an OpenGL development environment in Ubuntu Linux

, 16 Apr 2011
Rate this:
Please Sign up or sign in to vote.
How to set up the OpenGL and freeglut libraries for development in Ubuntu.

Introduction

This article deals with setting up the OpenGL and freeglut libraries in Ubuntu, in order to develop graphics applications using the said technologies. OpenGL is a platform independent graphics API, and freeglut is the successor of the OpenGL Utility Toolkit (GLUT) which enables a generic approach to creating and handling windows and other functions in different platforms. This article describes how these two packages can be installed in Ubuntu, and employed in a simple C++ program in order to test if they are installed and working properly.

Prerequisites

There are a few things that need to be accounted for before OpenGL and freeglut can be set up and used by the system.

  • A working Ubuntu system (the system I worked on was running Ubuntu 10.10).
  • If a graphics card is employed, the necessary drivers should be installed (see this link).
  • The packages necessary for C/C++ compilation should be installed. The best way is to install the build-essential package, which contains everything needed (see this link).

Installing the OpenGL and freeglut Libraries

In order to ensure that we select the latest versions of the libraries, navigate to www.debian.org and select the debian packages option (found under the 'Developers corner' top menu item):

Scrolling down in the Packages page, you would come to the 'Search the contents of packages' section. In essence, what we need to search for are the header files gl.h and glut.h, in order to get the OpenGL and freeglut packages containing them. Additionally, the latest version of the packages will be offered, which is what we need. Search for gl.h as shown in the below image (set the display option to 'packages that contain files named like this'):

fileSearch.png

Once the search results are displayed, we have to select the package that is specified for /usr/include/GL. Thus, in the below image, the package that needs to be installed would be the 'mesa-common-dev' package:

searchResults.png

The same procedure has to be followed for the file glut.h. Search for the glut.h header file, and select the package specified for /usr/include/GL (the package would be "freeglut3-dev" if you searched correctly). In general, if you need to install a package, but are not sure which it is, or if it is the latest version, you can search in the above mentioned manner if you know the name of a file in the package.

Once we know the package names for OpenGL (mesa-common-dev) and freeglut (freeglut3-dev), installing them is a matter of opening a terminal window and entering the apt-get command for the packages:

  • sudo apt-get install mesa-common-dev
  • sudo apt-get install freeglut3-dev

Note that if any of the packages or both are already installed in your system, the kernel will give the appropriate message when you enter the above commands, notifying you that the packages already are installed, and are the latest version.

If the installation went smoothly, you would be able to see the following content in your /usr/include/GL directory location:

GLfolder.png

Now that the OpenGL and freeglut libraries are configured in the system, the next step would be to use them in a simple program and test if everything works correctly, and to see if we have an environment where we can start designing and coding OpenGL graphics applications.

Testing

To test whether the OpenGL and freeglut libraries work properly, we will write a simple program utilizing the library APIs. The C++ program will use the code in the OpenGL Red book (this should be a definite read if you are planning to develop using OpenGL) found under the heading "A very simple OpenGL program". The following code listing shows the program:

#include "GL/freeglut.h"
#include "GL/gl.h"

/* display function - code from:
     http://fly.cc.fer.hr/~unreal/theredbook/chapter01.html
This is the actual usage of the OpenGL library. 
The following code is the same for any platform */
void renderFunction()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    glBegin(GL_POLYGON);
        glVertex2f(-0.5, -0.5);
        glVertex2f(-0.5, 0.5);
        glVertex2f(0.5, 0.5);
        glVertex2f(0.5, -0.5);
    glEnd();
    glFlush();
}

/* Main method - main entry point of application
the freeglut library does the window creation work for us, 
regardless of the platform. */
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("OpenGL - First window demo");
    glutDisplayFunc(renderFunction);
    glutMainLoop();    
    return 0;
}

Once you enter the above code in a text editor and save it as a CPP file, firstWindow.cpp for example, open up a terminal window and navigate to the location of firstWindow.cpp. Here, you will compile the program to get an executable, by typing g++ firstWindow.cpp -lglut. The -lglut is to link the file to the freeglut library. If everything goes OK, you will get the traditional a.out file. Running the executable by typing ./a.out at the command line should display the results of the code listing above:

firstWindow.png

And that's all there is to it. From this point onwards, it is a matter of using the different OpenGL functions and customizing how you write programs. If this first hurdle of trying to compile and run an OpenGL program is successfully crossed, the rest is a matter of learning how to program in OpenGL and being creative.

Points of Interest

I was looking into setting up OpenGL and freeglut along with SDL (for easy mouse and keyboard events processing), and maybe OpenAL (for audio). There are good rendering engines out there (check out Ogre3D), but if you are looking at developing games, everything (rendering, input, audio, physics, etc.) comes into play. I will write more articles in the future, if I come across any new interesting developments Smile | :)

History

  • 15 April 2011 - Initial write-up and publishing of article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mark Sinnathamby
Software Developer Air Liquide Industrial Services
Singapore Singapore
Mark is a software developer at Air Liquide Industrial Services - Singapore, where he helps Develop, Deploy, and Maintain large scale enterprise applications in the Asia-Pacific region. While his main work experience converges on .Net technologies, he has proficient experience in C++, HTML5/CSS/Javascript Web technologies, mobile development, OpenGL and DirectX API's, as well as a bit of J2EE. He loves to dabble with Game programming in his spare time, and blogs about his development experience at http://markfaction.wordpress.com and http://gamecoderlogic.wordpress.com.
Follow on   Twitter

Comments and Discussions

 
QuestionUpdate to GL3 or GLES2? PinmemberBram Stolk6-Aug-13 18:05 
QuestionSetting-up-OpenGL PinmemberMember 1015617515-Jul-13 2:16 
GeneralExcellent tutorial [modified] PinmemberAndyUk0623-Apr-13 11:51 
GeneralRe: Excellent tutorial PinmemberMark Sinnathamby24-Apr-13 5:29 
GeneralBeautiful PinmemberMember 81903282-Nov-12 22:09 
GeneralRe: Beautiful PinmembermarkFaction3-Nov-12 6:12 
Questiontest program PinmemberPratiksha Jhalani24-Aug-12 23:50 
AnswerRe: test program PinmembermarkFaction26-Aug-12 8:29 
GeneralMy vote of 5 Pinmemberbhermse26-Oct-11 17:37 
GeneralMy vote of 4 PinmemberDebojyoti Majumder17-Apr-11 6:59 
GeneralFormatting [modified] PinmvpJohn Simmons / outlaw programmer15-Apr-11 3:38 
GeneralRe: Formatting PinmembermarkFaction15-Apr-11 20:50 
GeneralRe: Formatting PinmvpJohn Simmons / outlaw programmer16-Apr-11 0:23 
GeneralRe: Formatting PinmembermarkFaction16-Apr-11 4:23 
GeneralRe: Formatting PinmvpJohn Simmons / outlaw programmer18-Apr-11 23:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 16 Apr 2011
Article Copyright 2011 by Mark Sinnathamby
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid