These programs show complete sample programs for using Modern OpenGL. They are prepared for
use with a planned second edition of the book 3D Computer Graphics: A mathematical approach with OpenGL,
by Sam Buss, Cambridge University Press, 2003. The main web page for the second edition
is available here.
Some of these programs are based on older
legacy OpenGL programs
which were written for the first edition of the book.
These programs are still UNDER DEVELOPMENT. Corrections or improvements (minor and major)
will be very much welcomed!
Some programs are supplied with Visual Studio 2015 project and workspace files; however, the
source files should work with other compilers and on other systems as well.
You will
need to have OpenGL, GLFW, and GLEW installed: namely, the header files to compile programs
and the object libraries to link and run the programs. See below for more on
obtaining GLFW and GLEW.
I. Getting started with basic features.
- SimpleDrawModern
shows how to draw points, lines, line strips, line loops, and triangles.
It includes simple vertex and fragment shaders.
It accepts keyboard inputs to update the scene.
- SimpleAnimModern
shows how to draw triangles, triangle strips, and triangle fans; how to use
an orthographic projection matrix, how to use model view matrices, and to do
a simple rotational animation.
- SolarModern
animates a simple solar system with a sun, an earth and a moon. It uses
a perspective projection matrix and implements a
hierarchical use of modelview matrices.
It contains simple animation controls. It uses GlLinearMath for the
math needed for the animation, and uses GlGeomSphere to
render spheres (see GlLinearMath and GlGeomShapes below).
- SimpleDrawShaderMgr
is a "re-do" of SimpleAnimModern, but now loading the shaders's source code
from a GLSL file using the GlShaderMgr class (see below).
- BasicDrawModes
illustrates drawing multiple triangle strips in four of the basic
drawing modes for OpenGL: glDrawArrays, glDrawElements, glMultiDrawElements,
and glDrawElements with Primitive Restart.
- ConnectDotsModern
illustrates detecting mouse clicks, tracking the mouse position,
and drawing straight-line segments joining points.
II. Utilities and helper classes
- GlLinearMath.
C++ classes for vectors, matrices, quaternions, and miscellaneous
math functions.
- GlShaderMgr. C++ classes
for reading GLSL shader programs from files and for compiling and linking
them.
- GlGeomShapes.
C++ classes encapsulating some simple geometric shapes.
So far, spheres, cylinders and tori are available.
(UNDER FURTHER DEVELOPMENT)
- EduPhong is C++ code
plus GLSL shader programs for Phong lighting. This allows exploring
writing code with Phong lighting, combined with Gouraud or Phong
shading. It also allows using a texture map.
- RgbImage.
A C++ class for reading and writing textures to and from
bitmap (.bmp) files.
III. Programs with more complex shaders and more advanced OpenGL features
- SimpleLightModern
illustrates both Phong lighting with Gouraud shading and Phong lighting with Phong shading.
Allows several viewing options, including seeing the different light components
separately. Uses the GlShaderMgr class to load shaders formed from more than one
code block.
- TextureBmpModern
illustrates a simple case of how to use a single texture map.
- FourTexturesModern
illustrates how to use four texture maps at once. It also illustrates
how to use glDrawElementsBaseVertex.
- GammaCorrectTest
illustrates both gamma correction and linear-to-sRGB coding.
In gamma correction mode, you can interactively modify the gamma value
by dragging with the mouse.
- SimpleGeometryShader
shows a couple basic geometry shaders acting on triangles.
One geometry shader grows the size of
a triangle on the screen. A second geometry shader tessellates a triangle into three
subtriangles.
- ParticlesTransformFeedback
implements a simple particle system using Transform Feedback to update all particle
positions and velocities on the GPU. This shows how to generate data on the GPU,
for rendering in the next cycle without bringing the data back to the CPU.
The physics simulation uses a central gravitational
force tracking the mouse position, with an Euler-Cromer update. (See also
the Chap1TransformFeedback program below.)
- GlGeomShapesTester
is tester code used for developing GlGeomShapes. It includes examples of how to
handle multiple files and multiple shaders with GlShaderMgr. It includes a shader
program that draws normals as short vectors. It also includes a simple procedural
shader program that forms a "speckled F" pattern.
IV. Miscellaneous programs
- Chapter1Figs
includes the sample C++ and GLSL shader code from Chapter 1,
including code that renders many of the figures in that chapter.
It illustrates different
rendering modes for lines and triangles, vertex and fragment
shaders, and the use of the GlShaderMgr class.
- Chap1TransformFeedback
illustrates the use of Transform Feedback to generate data with
geometry shader (on the GPU) and transfer it back to the C++ program
to use on the CPU. This program was used to help generate a couple of the
figures in Chapter 1. (See also the ParticlesTransformFeedback program
above.)
- NoPerspective
illustrates the use of the "
noperspective
" qualifier. This
shows rather dramatically why hyperbolic interpolation is used by
default in graphics rendering.
Building C++/OpenGL programs.
If you have a compiler installed on your computer,
it very likely comes with the needed OpenGL headers
and libraries, but you still need the GLFW and GLEW headers and libraries.
GLFW provides a platform-independent (more-or-less) interface to OpenGL so that the
same code can be used for different operating systems, including Windows, Macs, and Linux.
GLEW is the "OpenGL Extension Wrangler Library": it tracks which features ("extensions")
of OpenGL are available in your computer system, and simplifies the interface
to OpenGL.
Obtaining GLFW.
GLFW files are available from
http://www.glfw.org. For Windows machines,
the header files and binary files (library files) are available for
direct download, by clicking on the "Download" header on that web page.
After you obtain the header files (.h) and the Windows library files (.lib),
install them on your system (as administrator) in
the same location as GL/glu.h and glu32.lib.
Installing the .dll files should not be necessary, as it is recommended to
use static linkage. If you do not have administrator privileges, you may
instead store the files locally.
For non-Windows machines, you will need to compile GLFW from its source code:
this is also available at http://www.glfw.org/
along with CMake files.
If you compile and link with makefiles instead of using Visual Studio projects,
you may wish to remove the #pragma commands in the .cpp files.
Obtaining GLEW.
GLEW files are available from
http://glew.sourceforge.net.
Installation directions are the same as for GLFW above.
Acknowledgements: I initially started learning Modern OpenGL
primarily from the OpenGL Programming Guide,
Ninth Edition (Kindle version), and
the Learn OpenGL website
by Joey de Vries.
Books on Modern OpenGL include the OpenGL Programming Guide
and the OpenGL Super Bible; if you get one, be sure it is for
OpenGL 4.3 or later. The LearnOpenGL website also provides a PDF version of itself
available for download as a complete online book.
For somewhat denser reading, the official specification
of OpenGL 4.5 is available online at
https://www.khronos.org/registry/OpenGL/specs/gl/glspec45.core.pdf.