Using GLEW, GLFW, and GLM

来源:互联网 发布:守望先锋cg 知乎 编辑:程序博客网 时间:2024/05/26 08:42


What Are These Libraries?

  • GLEW is on OpenGL library for handling OpenGL extensions. Users interested in diving into its source code can use git to clone its repository.

  • GLUT has been the defacto standard library for many OpenGL programmers, but it has not been actively maintained for decades. The last update to GLUT 3.x specification was dated in 1996. The most logical alternative to GLUT is FreeGLUT, however installation on Windows machines requires manual compile steps. I’m trying to look for a simpler alternative for my students.

After some research, I found GLFW. Windows users can doanload either the 32-bit or 64-bitversion of the library.

  • GLM is a header-only math library designed for OpenGL programmers. It provides functions and classes for dealing with vectors, matrices, quaternions, and related operations.

This document shows how to use these libraries, mainly on Windows machines. At the end, I also show instructions for using these libraries with CLion. Using these libraries on OSX or Linux is relatively easier; specific instructions for Linux and OSX are provided at the end of this document.

The main steps are described below.

Step1: Install MinGW Using Win-Builds

If you have a previous version of MinGW, it is strongly recommended that you uninstall it first.

Windows users must install MinGW (32-bit or 64-bit) for CLion to compile C/C++ programs. To setup MinGW, first download win-build package manager. Linux/OSX users most likely already have g++ installed.

On the next screen, select the installation type (Native Windows), select the architecture (32- or 64-bit) to match your Windows. Click the Browse button to select the top directory of your MinGW installation (the following screenshot assumes a 64-bit architecture). Click “OK” to continue with installation.

On the next dialog window, click the “Process” button on the upper right corner of the window.

Edit the PATH environment variable to include C:\MinGW64\bin (or whatever name you chose during installation).

Step 2: Verify gcc and g++ are installed

From the command prompt type

gccg++

If the response is something other than “Command not found”, your compilers are installed correctly.

Step 3: Install GLFW

Windows users can download either 32-bit or 64-bit binary. Unzip the file directly under C:\ drive, (i.e. do not extract the files to C:\MyCourseWork, for instance), then rename the top installation directory to C:\GLFW. When the installation is complete, you will find the following subdirectories (among others):

  • C:/GLFW/include/
  • C:/GLFW/lib-mingw

OSX/Linux users should first use cmake to compile GLFW from source. After downloading GLFW source code, do the following:

123456
cd <top-dir-of-your-glfw-installation>mkdir buildcd buildcmake ..makemake install

If everything went smoothly, GLFW header files will be installed at /usr/local/include and its static library will be installed at /usr/local/lib.

Test your GLFW installation by typing the following program:

12345678910111213141516171819202122
#undef GLFW_DLL#include <GLFW/glfw3.h>#include <cstdlib>#include <iostream>using namespace std;int main() {  if (!glfwInit()) {    cerr << "Can't initialize GLFW" << endl;    exit (EXIT_FAILURE);  }  GLFWwindow *win;  win = glfwCreateWindow (450, 300, "Hello", NULL, NULL);  glfwMakeContextCurrent(win);  while (!glfwWindowShouldClose(win)) {    glfwWaitEvents();  }  glfwDestroyWindow(win);  glfwTerminate();  return 0;}

The #undef GLFW_DLL directive will compie the non-DLL version of the GLFW functions. Assuming the above code is saved as main.cpp, compile it with the following command line:

g++ -std=c++11 -IC:/GLFW/include -LC:/GLFW/lib-mingw main.cpp -o yay -lglfw3 -lopengl32

If the code is compiled using the DLL version of GLFW functions, the compiler options are:

g++ -std=c++11 -IC:/GLFW/include -LC:/GLFW/lib-mingw main.cpp -o yay C:/GLFW/lib-mingw/glfw3dll.a -lglfw3 -lopengl32 -lglu32 -lgdi32

The additional flags for the above compile are:

  • -Ic:/GLFW/include the include directory for GLFW header files
  • -LC:/GLFW/lib-mingw and -LC:/GLFW/lib-mingw\glfw3dll.a the link directory and library archive for GLFW (DLL) functions

When everything goes well, you fill find a new file yay.exe, but running the program will show an error message (missing glfw3.dll). Copy the missing file from c:/GLFW/lib-mingw to the current directory. After copying the .dll, you can now run yay.exe

yay

Step 4: Install GLEW Windows Binaries

Download and unzip the Windows binary of GLEW. Follow the same step as installing GLFW above (extract to C:\ drive and rename). The online installation instructions direct you to copy certain files to certain location of your windows, but I skipped those instructions.

After the installation you will find the following directories:

  • C:\GLEW\include
  • C:\GLEW\bin\Release\Win32
  • C:\GLEW\bin\Release\x64
  • C:\GLEW\lib\Release\Win32
  • C:\GLEW\lib\Release\x64

Under the lib\Release\....\ you’ll find two library files glew32.lib (for non-static linking) and glew32s.lib (for static linking). We will use the static library

12345678910111213141516171819202122232425
#define GLEW_STATIC#include <GLEW/glew.h>#undef GLFW_DLL#include <GLFW/glfw3.h>#include <cstdlib>#include <iostream>using namespace std;int main() {  if (!glfwInit()) {    cerr << "Can't initialize GLFW" << endl;    exit (EXIT_FAILURE);  }  GLFWwindow *win;  win = glfwCreateWindow (450, 300, "Hello", NULL, NULL);  glfwMakeContextCurrent(win);  glewInit();  while (!glfwWindowShouldClose(win)) {    glfwWaitEvents();  }  glfwDestroyWindow(win);  glfwTerminate();  return 0;}

Compile the above code using the following:

g++ -std=c++11 -IC:/GLFW/include -IC:/GLEW/include -LC:/GLFW/lib-mingw main.cpp -LC:/GLEW/lib/Release/x64 -o yay C:/GLFW/lib-mingw/glfw3dll.a -lglew32s -lglfw3 -lopengl32 -lglu32 -lgdi32

The two additional flags here are:

  • -IC:/GLEW/include (the include directory for the hader file glew.h)
  • -LC:/GLEW/lib/Release/x64 (for the 64-bit version of the static library)

Step 5: Install GLM

Download and unzip GLM or clone it from GitHub. This is a header only C++ library, no further installations steps required. Test your GLM installation by typing the following program:

12345678910111213
#include <glm/vec3.hpp>#include <glm/gtx/io.hpp>#include <iostream>using namespace std;using glm::vec3;int main () {    vec3 i_am_zero;    cout << i_am_zero << endl;    return 0;}

Assuming GLM is installed at C:/GLM the above code is glmtest.cpp, compile the program as follows:

g++ -std=c++11 -IC:/GLM  glmtest.cpp  -o runmerunme

Expect to see output of a zero vector.

After the three libraries (GLEW, GLFW, and GLM) are installed, you can now compile your OpenGL program from the command line:

123456
g++ -std=c++11 \   -IC:/GLFW/include -IC:/GLEW/include -IC:/GLM \   -LC:/GLFW/lib-mingw -LC:/GLEW/lib/Release/x64 \   main.cpp  __place_more_cpp_file_here__   -o yay C:/GLFW/lib-mingw/glfw3dll.a \   -lglew32s -lglfw3 -lopengl32 -lglu32 -lgdi32

If you see a warning “corrupt .drectve at end of def file”, ignore it.

The above command is split into several lines for clarity. If you prefer to use an IDE or Linux “make” tool proceed to the next step.

Step 6: Prepare CMakeLists.txt [for CLion]

To speedup development time, I recommend you use cmake, a multi-platform development tool for setting up project for various IDEs. The following list of IDEs is just a small fraction of what cmake supports:

  • Visual Studio
  • Codeblock
  • Eclipse
  • Xcode
  • Unix make
  • KDevelop

Use cmake --help to see the complete list and use cmake -G to generate files for a specific project. For instance to generate an Xcode project:

123
mkdir buildcd buildcmake -G"Xcode" __path_to_CMakeList.txt___

Because cmake generates lots of files, it is recommended that we create a separate directory (buildin the above example) when running it.

To use cmake, we must create/edit a text file CMakeList.txt. If you plan to use CLion (the next step), it is not necessary to download cmake yourself because CLion comes bundled with cmake.

In the directory where you save your .cpp and .h files, create/edit CMakeLists.txt and add the following:

  • include_directories("C:/GLFW/include" "C:/GLEW/include" "C:/GLM")
  • link_directories("C:/GLFW/lib-mingw" "C:/GLEW/lib/Release/x64")
  • target_link_libraries(....... glfw3 glew32s glu32 opengl32)

See the following example:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
cmake_minimum_required(VERSION 2.8.4)project(sample)set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")if (NOT WIN32)set(CMAKE_MODULE_PATH /usr/local/lib/cmake /usr/local/lib/x86_64-linux-gnu/cmake)set(CMAKE_PREFIX_PATH /usr/local/lib/cmake/glfw)endif (NOT WIN32)find_package (PkgConfig REQUIRED)find_package (OpenGL REQUIRED)if (WIN32)  include_directories("C:/GLFW/include" "C:/glm" "C:/glew/include")  link_directories("C:/glew/lib/Release/x64" "C:/GLFW/lib-mingw" )else (WIN32)  find_package (glfw3 REQUIRED)  find_package (GLM REQUIRED)  find_package (GLEW REQUIRED STATIC)endif (WIN32)if (APPLE)  include_directories(/usr/local/include)  find_library(COCOA_LIBRARY Cocoa REQUIRED)  find_library(IOKIT_LIBRARY IOKit REQUIRED)  find_library(COREVID_LIBRARY CoreVideo REQUIRED)endif (APPLE)set(SOURCE_FILES main.cpp __add_other_cpp_files_here__)## "sample" is the name of the executableadd_executable(sample ${SOURCE_FILES})  target_link_libraries (sample          ${GLFW3_LIBRARY}          ${OPENGL_LIBRARIES}          ${GLEW_LIBRARY}          ${COCOA_LIBRARY} ${COREVID_LIBRARY} ${IOKIT_LIBRARY})if (WIN32)  target_link_libraries (sample          ${OPENGL_LIBRARIES} glfw3 glew32s glu32 opengl32)endif (WIN32)if (UNIX)  target_link_libraries (sample          ${OPENGL_LIBRARIES}          ${GLFW3_LIBRARY}          ${GLEW_LIBRARY}          Xxf86vm pthread Xrandr Xinerama Xi Xcursor)endif (UNIX)

Place the above CMakeLists.txt in the same folder as your .cpp files and that folder can now be imported as a CLion project. To test whether the above file is configured correctly, try to compile the code from the command line:

123456
cd __to_the_directory_where_your_save_CMakeLists.txt__mkdir mybuild   # create a child directorycd mybuild      # change into that directorycmake ..        # CMakeLists.txt is in the parent dirmake            # compilesample          # run sample.exe

To run the executable on Linux, you have to type: ./sample.

For subsequent compiles, you just need to type make.

123
edit-your-code-againmake              # compile againsample

Step 7: CLion Setup

Once the three libraries (GLEW, GLFW, and GLM) are installed, you should be able to compile your program using CLion.

Download and install CLion. During the setup, select the top directory of your MinGW installation and select your CMake executable.

Visit Jetbrains E-store to apply for academic license.

Once you have a working copy of CLion, import your project into CLion (be sure the CMakeLists.txt is configured to match your personal settings).

Linux Users

Install mesa-common-devgit-core, and the libraries

123456
sudo apt-get install cmake git-coresudo apt-get install mesa-common-dev xorg-dev libglew-dev libglu1-mesa-devcd __to_installation_dir_of_these_packagesgit clone https://github.com/glfw/glfwgit clone https://github.com/g-truc/glm

On Linux, GLFW depends on xorg-dev and libglu1-mesa-dev packages.

Build GLFW

123456
cd __to_your_GLFW_local_clone__mkdir buildcd buildcmake ..makesudo make install  # install GLFW headers and library

After a successful compile and install you’ll find the following directories:

  • /usr/local/include/GLFW: the location of GLFW header files
  • /usr/local/lib/cmake/glfw: GLFW3 cmake config files
  • /usr/local/lib/libglfw.so.*: GLFW3 shared library

Build GLM

12345
cd __to_your_GLM_local_clone__mkdir buildcd buildcmake ..sudo make install   # install GLM headers

The installation will create:

  • /usr/local/include/glm: directory of GLM headers
  • /usr/local/lib/x86_64-linux-gnu/cmake/FindGLM.cmake: CMake config file

The following CMakeLists.txt can be used to compile your code:

1234567891011121314151617
cmake_minimum_required(VERSION 2.8.4)project(sample)set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")set(CMAKE_MODULE_PATH /usr/local/lib/cmake /usr/local/lib/x86_64-linux-gnu/cmake)set(CMAKE_PREFIX_PATH /usr/local/lib/cmake/glfw)find_package (PkgConfig REQUIRED)find_package (OpenGL REQUIRED)find_package (glfw3 REQUIRED)find_package (GLM REQUIRED)find_package (GLEW REQUIRED STATIC)set(SOURCE_FILES main.cpp __add_other_cpp_files_here__)add_executable(sample ${SOURCE_FILES})target_link_libraries (sample ${OPENGL_LIBRARIES} ${GLFW3_LIBRARY}        ${GLEW_LIBRARY} Xxf86vm pthread Xrandr Xinerama Xi Xcursor)

In addition to OpenGL related libraries, Linux programs also depend on X11-related libraries: Xxf86vm, Xrandr, Xinerama, Xi, and Xcursor.

OSX Users

Software development tools and compilers should be already installed on your OSX when your have Xcode installed. In addition, OSX users are recommended to use Homebrew for package management. After Homebrew is installed you can then install cmakepkg-config, and glew:

123
brew install cmakebrew install pkg-configbrew install glew

Clone and install GLFW and GLM using the instructions for Linux users above.

To compile using XCode:

12345
cd __to_the_directory_where_you_save_CMakeLists.txt___mkdir build        # create a new directory for cmake generated filescd buildcmake -GXcode ..   # generate the project file, read the CMakeList.txt in the parent directoryxcodebuild         # compile from the command line

When the compile succeeds, the binary executable will go to the Debug directory of the current build.

The last command will generate a new Xcode project file. Open that using Xcode 5 or Xcode 6. CMake generates three targets: ALL_BUILD, ZERO_CHECK, and the-executable-name. Switch to the last target to run the program from Xcode

0 0