Click here to Skip to main content
16,016,168 members
Articles / Multimedia / OpenGL

Setting up an OpenGL development environment in Ubuntu Linux

Rate me:
Please Sign up or sign in to vote.
4.83/5 (19 votes)
16 Apr 2011CPOL4 min read 204.8K   28   16
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):

Image 1

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:

C++
#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 :)

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)


Written By
Technical Lead Exilesoft
Sri Lanka Sri Lanka
Mark is a Technical Lead at Exilesoft, whose passion lies in coding, mentoring, and fueling technical innovation. In the past, he has worked as a developer for a product engineering company, an ERP/Technical Consultant in a Fortune 500 conglomerate, and also as a senior engineer for a startup in the manufacturing and design space. His current areas of research revolve around Enterprise Architecture, Big Data, NoSQL Technology, and Machine Learning.

In his spare time, Mark experiments with (computer) game design/development, operating system internals, and compiler design. He also discusses and blogs about various topics surrounding software development, computer science, game programming, and mathematics, which can be read at markfaction.wordpress.com. Feel free to email or message him anytime and strike up a conversation.

Comments and Discussions

 
Suggestiong++ firstWindow.cpp -lglut -lGL Pin
hapebe15-Aug-15 6:30
hapebe15-Aug-15 6:30 
QuestionUpdate to GL3 or GLES2? Pin
Bram Stolk6-Aug-13 18:05
Bram Stolk6-Aug-13 18:05 
QuestionSetting-up-OpenGL Pin
Member 1015617515-Jul-13 2:16
Member 1015617515-Jul-13 2:16 
GeneralExcellent tutorial Pin
AndyUk0623-Apr-13 11:51
AndyUk0623-Apr-13 11:51 
GeneralRe: Excellent tutorial Pin
Mark Vinod Sinnathamby24-Apr-13 5:29
professionalMark Vinod Sinnathamby24-Apr-13 5:29 
GeneralBeautiful Pin
Zafarbek Ibrohimov2-Nov-12 22:09
Zafarbek Ibrohimov2-Nov-12 22:09 
GeneralRe: Beautiful Pin
Mark Vinod Sinnathamby3-Nov-12 6:12
professionalMark Vinod Sinnathamby3-Nov-12 6:12 
Questiontest program Pin
Pratiksha Jhalani24-Aug-12 23:50
Pratiksha Jhalani24-Aug-12 23:50 
AnswerRe: test program Pin
Mark Vinod Sinnathamby26-Aug-12 8:29
professionalMark Vinod Sinnathamby26-Aug-12 8:29 
GeneralMy vote of 5 Pin
bhermse26-Oct-11 17:37
bhermse26-Oct-11 17:37 
GeneralMy vote of 4 Pin
Debojyoti Majumder17-Apr-11 6:59
Debojyoti Majumder17-Apr-11 6:59 
GeneralFormatting [modified] Pin
#realJSOP15-Apr-11 3:38
professional#realJSOP15-Apr-11 3:38 
GeneralRe: Formatting Pin
Mark Vinod Sinnathamby15-Apr-11 20:50
professionalMark Vinod Sinnathamby15-Apr-11 20:50 
GeneralRe: Formatting Pin
#realJSOP16-Apr-11 0:23
professional#realJSOP16-Apr-11 0:23 
GeneralRe: Formatting Pin
Mark Vinod Sinnathamby16-Apr-11 4:23
professionalMark Vinod Sinnathamby16-Apr-11 4:23 
GeneralRe: Formatting Pin
#realJSOP18-Apr-11 23:51
professional#realJSOP18-Apr-11 23:51 

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

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