24
Aug

// OpenGL (Glut) with Linux Mint and Ubuntu 12.04.

This time we focus on how to set up OpenGL for your Linux Mint 12 (It also should work fine with Ubuntu 12.04). For this task freeglut is going to be used. This is a open source and up to date alternative to the OpenGL Utility Toolkit. The official description of the freeglut project  says that

freeglut is a completely OpenSourced alternative to the OpenGL Utility Toolkit (GLUT) library. GLUT was originally written by Mark Kilgard to support the sample programs in the second edition OpenGL 'RedBook'. Since then, GLUT has been used in a wide variety of practical applications because it is simple, widely available and highly portable.

GLUT (and hence freeglut) allows the user to create and manage windows containing OpenGL contexts on a wide range of platforms and also read the mouse, keyboard and joystick functions.

 Step 1 - Install Compiler and necessary tools.
 Since we are going to be programming with C++ , it is important to have the tool in your computer.  This following command will install a c++ compiler and cmake which is used to manage projects.
1
sudo apt-get install g++ cmake
 Step 2 - Lets install freeglut.
1
sudo apt-get install freeglut3 freeglut3-dev
Done ! But we need to test it.

Now you can create a simple test program.

Step 3 - Test program - Create a 'main.cpp'

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
#include <GL/glut.h>


void draw(void) {

    // Black background
    glClearColor(0.0f,0.0f,0.0f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    //Draw i
    glFlush();

}

//Main program

int main(int argc, char **argv) {

    glutInit(&argc, argv);

    /*Setting up  The Display
    /    -RGB color model + Alpha Channel = GLUT_RGBA
    */

    glutInitDisplayMode(GLUT_RGBA|GLUT_SINGLE);

    //Configure Window Postion
    glutInitWindowPosition(50, 25);

    //Configure Window Size
    glutInitWindowSize(480,480);

    //Create Window
    glutCreateWindow("Hello OpenGL");


    //Call to the drawing function
    glutDisplayFunc(draw);

    // Loop require by OpenGL
    glutMainLoop();
    return 0;
}

Step 4 - Let's Compile it

The first and the easiest option to compile is by using the command :

1
g++ -lGL -lglut main.cpp  -o test

Step 5 - Run it

1
./test

Using Cmake
That is it. If you would like to use the cmake to manage and compile you project you will need to ignore step 4 and 5 and follow this post: How to use OpenGL/ Freeglut and CMake

  • Russell

    Thanks body!!

  • Russell

     Thanks buddy!!

  • Christian Neverdal Jonassen

    Have this as your Makefile:

    all:
    g++ main.cpp -lGL -lglut -o test

  • Pingback: knockoff louis vuitton purse()

  • maryam

    I could not install

    "Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
    E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?"

    could you pleas help me? !

    • reboot your PC, maybe it work.

      • windows user????

        the correct is gcc because don't have any c++ code in example

    • Kobzol

      This means that you are either not using apt-get as root or that apt-get is currently running (there cannot be two instances running at the same time).

  • bahadur

    would you please include tutorial on OpenGL 4.4 on ubuntu 12.04?

  • istirfar

    but efter the step 4 :

    In function `draw()':

    opgl.cpp:(.text+0x2a): undefined reference to `glClearColor'

    opgl.cpp:(.text+0x36): undefined reference to `glClear'

    opgl.cpp:(.text+0x3b): undefined reference to `glFlush'

    /tmp/cc3IM1s8.o: In function `main':

    opgl.cpp:(.text+0x58): undefined reference to `glutInit'

    opgl.cpp:(.text+0x64): undefined reference to `glutInitDisplayMode'

    ...........................................

    opgl.cpp:(.text+0xa9): undefined reference to `glutMainLoop'

    collect2: ld returned 1 exit status

  • /tmp/ccDeFx5o.o: In function `draw()':
    main.cpp:(.text+0x16): undefined reference to `glClearColor'
    main.cpp:(.text+0x20): undefined reference to `glClear'
    main.cpp:(.text+0x25): undefined reference to `glFlush'
    /tmp/ccDeFx5o.o: In function `main':
    main.cpp:(.text+0x49): undefined reference to `glutInit'
    main.cpp:(.text+0x53): undefined reference to `glutInitDisplayMode'
    main.cpp:(.text+0x62): undefined reference to `glutInitWindowPosition'
    main.cpp:(.text+0x71): undefined reference to `glutInitWindowSize'
    main.cpp:(.text+0x7b): undefined reference to `glutCreateWindow'
    main.cpp:(.text+0x85): undefined reference to `glutDisplayFunc'
    main.cpp:(.text+0x8a): undefined reference to `glutMainLoop'
    collect2: error: ld returned 1 exit status

    ..

  • Luis

    I got this:
    /tmp/cccXiRq1.o: In function `draw()':
    test.cpp:(.text+0x2a): undefined reference to `glClearColor'
    test.cpp:(.text+0x36): undefined reference to `glClear'
    test.cpp:(.text+0x3b): undefined reference to `glFlush'
    /tmp/cccXiRq1.o: In function `main':
    test.cpp:(.text+0x58): undefined reference to `glutInit'
    test.cpp:(.text+0x64): undefined reference to `glutInitDisplayMode'
    test.cpp:(.text+0x78): undefined reference to `glutInitWindowPosition'
    test.cpp:(.text+0x8c): undefined reference to `glutInitWindowSize'
    test.cpp:(.text+0x98): undefined reference to `glutCreateWindow'
    test.cpp:(.text+0xa4): undefined reference to `glutDisplayFunc'
    test.cpp:(.text+0xa9): undefined reference to `glutMainLoop'
    collect2: error: ld returned 1 exit status

  • Pingback: miguel()

  • Faelnirv

    Hi,

    I'have just made as you instructed but I have a little problem :

    when i use this command
    "g++ -lGL -lglut main.cpp -o test"
    the result is
    "/tmp/ccZRLJE7.o: dans la fonction « draw() »:
    main.cpp:(.text+0x16): référence indéfinie vers « glClearColor »
    main.cpp:(.text+0x20): référence indéfinie vers « glClear »
    main.cpp:(.text+0x25): référence indéfinie vers « glFlush »
    /tmp/ccZRLJE7.o: dans la fonction « main »:
    main.cpp:(.text+0x49): référence indéfinie vers « glutInit »
    main.cpp:(.text+0x53): référence indéfinie vers « glutInitDisplayMode »
    main.cpp:(.text+0x62): référence indéfinie vers « glutInitWindowPosition »
    main.cpp:(.text+0x71): référence indéfinie vers « glutInitWindowSize »
    main.cpp:(.text+0x7b): référence indéfinie vers « glutCreateWindow »
    main.cpp:(.text+0x85): référence indéfinie vers « glutDisplayFunc »
    main.cpp:(.text+0x8a): référence indéfinie vers « glutMainLoop »
    collect2: error: ld returned 1 exit status "

    would anyone have an idea in how to solve this ?

    I use linux mint Rebecca (17.1)

  • michael felix

    After installing openGL in my linux mint 18 distro and trying to compile i get this error:

    $ g++ main.cpp -lGL -lglut -o test
    In file included from /usr/include/GL/glut.h:17:0,
    from main.cpp:1:
    /usr/include/GL/freeglut_std.h:128:22: fatal error: GL/gl.h: No such file or directory
    compilation terminated.

    Any assistance on this error?? i will appreciate.

  • Vinícius Dias

    It worked perfectly with gcc but not with g++.
    When I trie to compile with g++ I get the following:
    undefined reference to `glutInit'

    And the message repeats to all glut functions.

  • Abdelaziz Mahmoud

    thanks

aSalada