Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
OpenGL 4 Shading Language Cookbook, Second Edition

You're reading from   OpenGL 4 Shading Language Cookbook, Second Edition Acquiring the skills of OpenGL Shading Language is so much easier with this cookbook. You'll be creating graphics rather than learning theory, gaining a high level of capability in modern 3D programming along the way.

Arrow left icon
Product type Paperback
Published in Dec 2013
Publisher Packt
ISBN-13 9781782167020
Length 394 pages
Edition 2nd Edition
Tools
Arrow right icon
Authors (2):
Arrow left icon
David A Wolff David A Wolff
Author Profile Icon David A Wolff
David A Wolff
David Wolff David Wolff
Author Profile Icon David Wolff
David Wolff
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Getting Started with GLSL FREE CHAPTER 2. The Basics of GLSL Shaders 3. Lighting, Shading, and Optimization 4. Using Textures 5. Image Processing and Screen Space Techniques 6. Using Geometry and Tessellation Shaders 7. Shadows 8. Using Noise in Shaders 9. Particle Systems and Animation 10. Using Compute Shaders Index

Using GLM for mathematics

Mathematics is core to all of computer graphics. In earlier versions, OpenGL provided support for managing coordinate transformations and projections using the standard matrix stacks (GL_MODELVIEW and GL_PROJECTION). In recent versions of core OpenGL however, all of the functionality supporting the matrix stacks has been removed. Therefore, it is up to us to provide our own support for the usual transformation and projection matrices, and then to pass them into our shaders. Of course, we could write our own matrix and vector classes to manage this, but some might prefer to use a ready-made, robust library.

One such library is GLM (OpenGL Mathematics) written by Christophe Riccio. Its design is based on the GLSL specification, so the syntax is very similar to the mathematical support in GLSL. For experienced GLSL programmers, this makes GLM very easy to use and familiar. Additionally, it provides extensions that include functionality similar to some of the much-missed OpenGL functions such as glOrtho, glRotate, or gluLookAt.

Getting ready

Since GLM is a header-only library, installation is simple. Download the latest GLM distribution from http://glm.g-truc.net. Then, unzip the archive file, and copy the glm directory contained inside to anywhere in your compiler's include path.

How to do it...

To use the GLM libraries, it is simply a matter of including the core header file, and headers for any extensions. For this example, we'll include the matrix transform extension as follows:

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

Then the GLM classes are available in the glm namespace. The following is an example of how you might go about making use of some of them:

glm::vec4 position = glm::vec4( 1.0f, 0.0f, 0.0f, 1.0f );
glm::mat4 view = glm::lookAt( glm::vec3(0.0,0.0,5.0),glm::vec3(0.0,0.0,0.0),glm::vec3(0.0,1.0,0.0) );
glm::mat4 model(1.0f);   // The identity matrix
model = glm::rotate( model, 90.0f, glm::vec3(0.0f,1.0f,0.0) );
glm::mat4 mv = view * model;
glm::vec4 transformed = mv * position;

How it works...

The GLM library is a header-only library. All of the implementation is included within the header files. It doesn't require separate compilation and you don't need to link your program to it. Just placing the header files in your include path is all that's required!

The previous example first creates a vec4 (four coordinate vector) representing a position. Then it creates a 4 x 4 view matrix by using the glm::lookAt function. This works in a similar fashion to the old gluLookAt function. Here, we set the camera's location at (0, 0, 5), looking towards the origin, with the "up" direction in the direction of the y-axis. We then go on to create the model matrix by first storing the identity matrix in the variable model (via the single argument constructor), and multiplying by a rotation matrix using the glm::rotate function. The multiplication here is implicitly done by the glm::rotate function. It multiplies its first parameter by the rotation matrix (on the right) that is generated by the function. The second parameter is the angle of rotation (in degrees), and the third parameter is the axis of rotation. Since before this statement, model is the identity matrix, the net result is that model becomes a rotation matrix of 90 degrees around the y-axis.

Finally, we create our modelview matrix (mv) by multiplying the view and model variables, and then using the combined matrix to transform the position. Note that the multiplication operator has been overloaded to behave in the expected way.

There's more...

It is not recommended to import all of the GLM namespace by using the following command:

using namespace glm;

This will most likely cause a number of namespace clashes. Instead, it is preferable to import symbols one at a time, as needed. For example:

#include <glm/glm.hpp>
using glm::vec3;
using glm::mat4;

Using the GLM types as input to OpenGL

GLM supports directly passing a GLM type to OpenGL using one of the OpenGL vector functions (with the suffix v ). For example, to pass a mat4 named proj to OpenGL we can use the following code:

glm::mat4 proj = glm::perspective( viewAngle, aspect, nearDist, farDist );
glUniformMatrix4fv(location, 1, GL_FALSE, &proj[0][0]);

See also

  • The Qt SDK includes many classes for vector/matrix mathematics, and is another good option if you're already using Qt
  • The GLM website http://glm.g-truc.net has additional documentation and examples
You have been reading a chapter from
OpenGL 4 Shading Language Cookbook, Second Edition - Second Edition
Published in: Dec 2013
Publisher: Packt
ISBN-13: 9781782167020
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime