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.
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 bothC:\Windows\System32
andC:\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 likeC:\MinGW\include\GL
orC:\TDM-GCC\include\GL
. - Copy all libraries (*.a) into compiler's
lib
directory. That is something likeC:\MinGW\lib\
orC:\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.
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.
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.
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.
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.
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.
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
Add the following line to your CMakeLists.txt
and reload it.
set(CMAKE_EXE_LINKER_FLAGS "-framework OpenGL -framework GLUT")
- 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"
Different from Windows, you should use
#include <GLUT/glut.h>
instead.
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.
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();
}