GETTING STARTED
1.2Installation and Configuration
Graphics programming has a reputation for being among the most challenging computer science topics to learn. These days, graphics programming is shader based—that is, some of the program is written in a standard language such as C++ or Java for running on the CPU and some is written in a special-purpose shader language for running directly on the graphics card (GPU). Shader programming has a steep learning curve, so that even drawing something simple requires a convoluted set of steps to pass graphics data down a “pipeline.” Modern graphics cards are able to process this data in parallel, and so the graphics programmer must understand the parallel architecture of the GPU, even when drawing simple shapes.
The payoff, however, is extraordinary power. The blossoming of stunning virtual reality in videogames and increasingly realistic effects in Hollywood movies can be greatly attributed to advances in shader programming. If reading this book is your entrée into 3D graphics, you are taking on a personal challenge that will reward you not only with pretty pictures but with a level of control over your machine that you never imagined was possible. Welcome to the exciting world of computer graphics programming!
1.1LANGUAGES AND LIBRARIES
Modern graphics programming is done using a graphics library. That is, the programmer writes code which invokes functions in a predefined library (or set of libraries) that provide support for lower-level graphical operations. There are many graphics libraries in use today, but the most common library for platform-independent graphics programming is called OpenGL (Open Graphics Library). This book describes how to use OpenGL for 3D graphics programming in C++.
Using OpenGL with C++ requires configuring several libraries. At this time there is a dizzying array of options, depending on one’s individual needs. In this section, we describe which libraries are needed, some common options for each, and the option(s) that we will use throughout the book. Details on how to install and configure these libraries for use on your specific platform can be found in the Appendices.
In summary, you will need languages and libraries for the following functions:
•C++ development environment
•OpenGL / GLSL
•window management
•extension library
•math library
•texture management
It is likely that the reader will need to do several preparatory steps to ensure that each of these are installed and properly accessible on his/her system. In the following subsections we briefly describe each of them; see the Appendices for details on how to install and/or configure them for use.
1.1.1C++
C++ is a general-purpose programming language that first appeared in the mid-1980s. Its design, and the fact that it is generally compiled to native machine code, make it an excellent choice for systems that require high performance, such as 3D graphics computing. Another advantage of C++ is that the OpenGL call library is C based.
Many C++ development environments are available. In this textbook we recommend using Microsoft Visual Studio [VS17] if using a PC, and Xcode [XC18] if using a Macintosh. Descriptions for installing and configuring each of them, depending on your platform, are given in the Appendices.
1.1.2OpenGL / GLSL
Version 1.0 of OpenGL appeared in 1992 as an “open” alternative to vendor-specific Application Programming Interfaces (APIs) for computer graphics. Its specification and development was managed and controlled by the OpenGL Architecture Review Board (ARB), a then newly formed group of industry participants. In 2006 the ARB transferred control of the OpenGL specification to the Khronos Group, a nonprofit consortium which manages not only the OpenGL specification but a wide variety of other open industry standards.
Since its beginning OpenGL has been revised and extended regularly. In 2004, version 2.0 introduced the OpenGL Shading Language (GLSL), allowing “shader programs” to be installed and run directly in graphics pipeline stages.
In 2009, version 3.1 removed a large number of features that had been deprecated, to enforce the use of shader programming as opposed to earlier approaches (referred to as “immediate mode”).1 Among the more recent features, version 4.0 (in 2010) added a tessellation stage to the programmable pipeline.
This textbook assumes that the user is using a machine with a graphics card that supports at least version 4.3 of OpenGL. If you are not sure which version of OpenGL your GPU supports, there are free applications available on the web that can be used to find out. One such application is GLView, by a company named “realtechvr” [GV16].
1.1.3Window Management
OpenGL doesn’t actually draw to a computer screen. Rather, it renders to a frame buffer, and it is the job of the individual machine to then draw the contents of the frame buffer onto a window on the screen. There are various libraries that support doing this. One option is to use the windowing capabilities provided by the operating system, such as the Microsoft Windows API. This is generally impractical and requires a lot of low-level coding. GLUT is a historically popular option; however, it is deprecated. A modernized extension is freeglut. Other related options are CPW, GLOW, and GLUI.
One of the most popular options, and the one used in this book, is GLFW, which has built-in support for Windows, Macintosh, Linux, and other systems [GF17]. It can be downloaded from www.glfw.org, and it must be compiled on the machine where it is to be used (we describe those steps in the Appendices).
OpenGL is organized around a set of base functions and an extension mechanism used to support new functionality as technologies advance. Modern versions of OpenGL, such as those found in version 4+ as we use in this book, require identifying the extensions available on the GPU. There are commands built into core OpenGL for doing this, but they involve several rather convoluted lines of code that would need to be performed for each modern command used—and in this book we use such commands constantly. Therefore, it has become standard practice to use an extension library to take care of these details, and to make modern OpenGL commands available to the programmer directly. Examples are Glee, GLLoader, GLEW, and more recently GL3W and GLAD.
A commonly used library among those listed is GLEW, which stands for OpenGL Extension Wrangler. It is available for a variety of operating systems including Windows, Macintosh, and Linux [GE17]. GLEW is not a perfect choice; for example, it requires an additional DLL. Recently, many developers are choosing GL3W or GLAD. They have the advantage of being automatically updated, but they also require that Python be installed. For these reasons, in this book we have opted to use GLEW. It can be downloaded at glew.sourceforge.net. Complete instructions for installing and configuring GLEW are given in the appendices.
1.1.5Math Library
3D graphics programming makes heavy use of vector and matrix algebra. For this reason, use of OpenGL is greatly facilitated by accompanying it with a function library or class package to support common mathematical tasks. Two such libraries that are frequently used with OpenGL are Eigen and vmath, the latter being used in the popular OpenGL SuperBible [SW15].
Arguably the most popular, and the one used in this book, is OpenGL Mathematics, usually called GLM. It is a header-only C++ library compatible with Windows, Macintosh, and Linux [GM17]. GLM commands conveniently use the same naming conventions as those in GLSL, making it easy to go back and forth when reading C++ and GLSL code used in a particular application. GLM is available for download at glm.g-truc.net.
GLM provides classes and basic math functions related to graphics concepts, such as vector, matrix, and quaternion. It also contains a variety of utility classes for creating and using common 3D graphics structures, such as perspective and look-at matrices. It was first released in 2005, and it is maintained by Christophe Riccio [GM17]. Instructions for installing GLM are given in the appendices.
1.1.6Texture Management
Starting with Chapter 5, we will use image files to add “texture” to the objects in our graphics scenes. This means that we will frequently need to load such image files into our C++/OpenGL code. It is possible to code a texture image loader from scratch; however, given the wide variety of image file formats, it is generally preferable to use a texture loading library. Some examples are FreeImage, DevIL, OpenGL Image (GLI), and Glraw. Probably the most commonly used OpenGL image loading library is Simple OpenGL Image Loader (SOIL), although it has become somewhat outdated.
The texture image loading library used in this book is SOIL2, an updated fork of SOIL. Like the previous libraries we have chosen, SOIL2 is compatible with a wide variety of platforms [SO17], and detailed installation and configuration instructions are given in the appendices.
1.1.7Optional Libraries
There are many other helpful libraries that the reader may wish to utilize. For example, in this book we show how to implement a simple “OBJ” model loader from scratch. However, as we will see, it doesn’t handle many of the options available in the OBJ standard. Some examples of more sophisticated OBJ importers are Assimp and tinyobjloader. For our examples, we will just use our simple model loader described and implemented in this book.
1.2INSTALLATION AND CONFIGURATION
While developing the C++ edition of this book, we wrestled with the best approach for including the platform-specific configuration information necessary to run the example programs. Configuring a system for using OpenGL with C++ is considerably more complicated than the equivalent configuration using Java, which can be described in just a few short paragraphs (as can be seen in the Java edition of the book [GC18]). Ultimately, we opted to separate installation and configuration information into individual platform-specific Appendices. We hope that this will provide each reader with a single relevant place to look for information regarding his/her specific system, while at the same time avoiding bogging down the rest of the text with platform-specific details which may not be relevant to every reader. In this edition, we provide detailed configuration instructions for Microsoft Windows in Appendix A, and for the Apple Macintosh in Appendix B.
References
[GC18] |
V. Gordon and J. Clevenger, Computer Graphics Programming in OpenGL with Java, 2nd ed. (Mercury Learning, 2018). |
[GE17] |
OpenGL Extension Wrangler (GLEW), accessed October 2018, http://glew.sourceforge.net/ |
[GF17] |
Graphics Library Framework (GLFW), accessed October 2018, http://www.glfw.org/ |
[GM17] |
OpenGL Mathematics (GLM), accessed December 2017, http://glm.g-truc.net/0.9.8/index.html |
[GV16] |
GLView, realtech-vr, accessed October 2018, http://www.realtech-vr.com/glview/ |
[SO17] |
Simple OpenGL Image Library 2 (SOIL2), SpartanJ, accessed October 2018, https://bitbucket.org/SpartanJ/soil2 |
[SW15] |
G. Sellers, R. Wright Jr., and N. Haemel, OpenGL SuperBible: Comprehensive Tutorial and Reference, 7th ed. (Addison-Wesley, 2015). |
[VS17] |
Microsoft Visual Studio downloads, accessed October 2018, https://www.visualstudio.com/downloads |
[XC18] |
Apple Developer site for Xcode, accessed January 2018, https://developer.apple/com/xcode |
1Despite this, many graphics card manufacturers (notably NVIDIA) continue to support deprecated functionality.