24
Aug

// How to use OpenGL/ Freeglut and CMake

To work on this post you need to have a running installation of OpenGL in your system. If you do not have it check in this post where I explain how to setup it on Linux Mint and Ubuntu : OpenGL (Glut) with Linux Mint and Ubuntu 12.04.

We are going to need two files,
The first file required is the sample program, here it is named 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;
}

The next required file is called CMakeLists.txt

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
cmake_minimum_required(VERSION 2.8)
# Project Name
PROJECT(HW_OPENGL)

#########################################################
# FIND GLUT
#########################################################
find_package(GLUT REQUIRED)
include_directories(${GLUT_INCLUDE_DIRS})
link_directories(${GLUT_LIBRARY_DIRS})
add_definitions(${GLUT_DEFINITIONS})
if(NOT GLUT_FOUND)
    message(ERROR " GLUT not found!")
endif(NOT GLUT_FOUND)
#########################################################
# FIND OPENGL
#########################################################
find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
link_directories(${OpenGL_LIBRARY_DIRS})
add_definitions(${OpenGL_DEFINITIONS})
if(NOT OPENGL_FOUND)
    message(ERROR " OPENGL not found!")
endif(NOT OPENGL_FOUND)
#########################################################
# Include Files
#########################################################
add_executable(test main.cpp)

########################################################
# Linking & stuff
#########################################################

  # create the program "test"
  target_link_libraries(test ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )

 

For the lazy ones You can download the two files in this zip file:

OpenGL Sample Program

Step 1 - Put the two files in the same folder

For instance extract this ZIP file into "~/hello_opengl"

Step 2  -  Create a build subfolder

1
2
cd ~/hello_opengl
mkdir build

Step 3 - Pre compile your project with cmake

1
2
cd ~/hello_opengl/build
cmake ..

Step 4-  Compile it

If everything was compiled correctly you should get no error messaged and now you can just compile

1
2
cd ~/hello_opengl/build
make

Step 5-  Run it

You can just run the 'test' program from the build folder

1
2
cd ~/hello_opengl/build
./test

Everything should be working fine by now.
Enjoy.

  • Mark Ison

    Fantastic thank you! I initially had an error with my cmake:

    CMake Error: The following variables are used in this project, but they are set to NOTFOUND.

    Please set them or make sure they are set and tested correctly in the CMake files:

    GLUT_Xi_LIBRARY (ADVANCED)

    linked by target "test" in directory /home/horc/OpenGL/testRun/testWindow

    GLUT_Xmu_LIBRARY (ADVANCED)

    linked by target "test" in directory /home/horc/OpenGL/testRun/testWindow

    Was solved by adding these libraries: sudo apt-get install libxmu-dev libxi-dev

    • NovaSurfer

      Try this: sudo apt-get install libxmu-dev libxi-dev

  • very good even with cmake I managed to import the project to netbeans, and enjoy the auto complete, thanks ...

  • Nicolas (french student)

    Perfect, thank you very much !

  • Akkas Uddin Zeeshan

    Why does the heading say freeGLUT when you used only GLUT? Please avoid misleading people

aSalada