Skip to content

Instantly share code, notes, and snippets.

@Reapor-Yurnero
Created November 29, 2017 07:39
Show Gist options
  • Save Reapor-Yurnero/afd29049a32970b8fca80f5804ab429c to your computer and use it in GitHub Desktop.
Save Reapor-Yurnero/afd29049a32970b8fca80f5804ab429c to your computer and use it in GitHub Desktop.
GLUT Configuration Guide

GLUT Setup Guldeline

Author: Xiaohan Fu, Yue YAO, Zhengyang Feng, Chenggang Wu

Date: 2017/11/27


Glut stands for Open[GL] [U]tility [T]ools. It's a library designed to ease programmers work while dealing with OpenGL programmes. OpenGL stands for Open Graphics Library. As its name suggests, it is a library for drawing figures (and more) on the screen. Applications that use this technology includes, games (counter-strike), design softwares (AutoCAD, etc.), visual designing (Photoshop, Premiere, etc.), phones (Android smartphones), and Matlab!

This guildline aims to help Windows and MacOS users set up GLUT environment. For Linux users, we believe you could handle it by yourself.

Windows

GLUT hasn't been updated since 1998. That means the latest version of it might be even older than most of you. Hence, we choose its predecessor FreeGlut instead.

  • Download FreeGLut for MinGW here. (The readme it offered may be to complex for you, just follow our instructions here)

You will see /bin, /lib and /include three directories. In these directories, there should be also a sub-directory x64, where files for 64-bit are stored. Outside is the 32-bit version files. Which one you should choose depends on the version of your gcc. Type in gcc -v in your terminal/cmd to check. For 32-bit gcc, use all 32-bit files in the following steps and vice versa.

  • Copy the dynamic library file inside /bin (*.dll) into both C:\Windows\System32 and C:\Windows\SysWOW64 directory. (same file in two directories)
  • Copy all header files (*.h) into your compiler's include\GL directory. In most cases, it should be something like C:\MinGW\include\GL or C:\TDM-GCC\include\GL.
  • Copy all libraries (*.a) into compiler's lib directory. That is something like C:\MinGW\lib\ or C:\TDM-GCC\lib\.

Strictly follow the above steps and you will be safe.

The next issue is compilation. To test, you could refer to the sample code in APPENDIX.

Compilation in Command Line

Assume there's a glut plotting code plot.cpp. To compile it, type the following in command line and will generate an executable file plot.

g++ -std=c++11 -pedantic -Wall -o plot plot.cpp -lglu32 -lfreeglut -lopengl32 -Wl,--subsystem,windows

Some thing need to notice:

  • -lglu32 supports you using basic GLUT functions, which is mandatory in this course.
  • -Wl,--subsystem,windows ensures the executable runs as a Windows GUI application rather than a console application.
  • The linker flags -lglu32 -lfreeglut -lopengl32 should always be in the tail of your compiling command to prevent some bug.

Configuration for CLion

For your project using FreeGlut, apply the following changes to your corresponding CMakeLists.txt.

Add one line after add_executable line, so that now it is

add_executable(project_name ...)
target_link_libraries(project_name opengl32 glu32 freeglut)

Make sure your project_name are consistent in both lines.

Configuration for Code::Blocks

Open Code::Blocks, find Settings -> Compilers -> Global Compiler Settings -> Linker Settings, hit Add at the bottom, change the folder to ...\MinGW\lib, add library files libopengl32.a, libglu32.a, libfreeglut.a, and hit Save.

Including Libraries

Generally, you just need to follow the lecture slides using

#include <GL/glut.h>

But if you are an advanced user of FreeGlut and would use some extended functions offered by FreeGlut, we warmly welcome it and in that case, you should use

#include <GL/freeglut.h>

instead.

But under most conditions, your program would not be successfully compiled just with this. You need to try adding

#include <windows.h>

at the very beginning of your code. Note that this order does matter.

If you are tired of adding this line for all your source codes, one easier way is to add this line in freeglut.h or glut.h directly, which is also the recommended solution.

MacOS

Building FreeGLut is complex on MacOS and GLUT is originally contained in MacOS. Hence, we choose to make our life easier and use GLUT directly on Mac. There's nothing you need to do for environment setup. Then we talk about the compilation.

Compilation in Command Line

Things are a little bit different here. Assume there's a glut plotting code plot.cpp. To compile it, type in the following in command line and will generate an executable file plot.

g++ -std=c++11 -pedantic -Wall -o plot plot.cpp -framework OpenGL -framework GLUT

Configuration for CLion

Add the following line to your CMakeLists.txt and reload it.

set(CMAKE_EXE_LINKER_FLAGS "-framework OpenGL -framework GLUT")

Configuration for XCode

  • Create a new CPP project with Command Line Tool.
  • In the "Linked Frameworks and Libraries" area click the "+" button, and select "OpenGL.framework"
  • Do the same thing for "GLUT.framework"

Including Libraries

Different from Windows, you should use

#include <GLUT/glut.h>

instead.

Something More

If you have carefully read through this documentation, you should have found that your code of OpenGL may not be portable (platform independent). Hence, for ease of our grading, please do clearly state in README on which plaform, Windows, MacOS or Linux you successfully compiled and ran your program.

APPENDIX

Sample code fetch from http://math.hws.edu/bridgeman/courses/324/s06/doc/opengl.html

// For MacOS
// #include <GLUT/glut.h>

// For Windows
// #include <windows.h>
// #include <GL/freeglut.h>

// Omit the warnings that some functions are out of date.

void display () {

    /* clear window */
    glClear(GL_COLOR_BUFFER_BIT);

    /* draw scene */
    glutSolidTeapot(.5);

    /* flush drawing routines to the window */
    glFlush();

}

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

    /* initialize GLUT, using any commandline parameters passed to the 
       program */
    glutInit(&argc,argv);

    /* setup the size, position, and display mode for new windows */
    glutInitWindowSize(500,500);
    glutInitWindowPosition(0,0);
    glutInitDisplayMode(GLUT_RGB);

    /* create and set up a window */
    glutCreateWindow("hello, teapot!");
    glutDisplayFunc(display);

    /* tell GLUT to wait for events */
    glutMainLoop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment