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.0 Shading Language Cookbook

You're reading from   OpenGL 4.0 Shading Language Cookbook With over 60 recipes, this Cookbook will teach you both the elementary and finer points of the OpenGL Shading Language, and get you familiar with the specific features of GLSL 4.0. A totally practical, hands-on guide.

Arrow left icon
Product type Paperback
Published in Jul 2011
Publisher Packt
ISBN-13 9781849514767
Length 340 pages
Edition 1st Edition
Tools
Arrow right icon
Toc

Table of Contents (16) Chapters Close

OpenGL 4.0 Shading Language Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Getting Started with GLSL 4.0 FREE CHAPTER 2. The Basics of GLSL Shaders 3. Lighting, Shading Effects, and Optimizations 4. Using Textures 5. Image Processing and Screen Space Techniques 6. Using Geometry and Tessellation Shaders 7. Shadows 8. Using Noise in Shaders 9. Animation and Particles Index

Sending data to a shader using uniform variables


Vertex attributes provide one avenue for providing input to shaders, a second technique is uniform variables. Uniform variables are intended to be used for data that may change relatively infrequently compared to per-vertex attributes. In fact, it is simply not possible to set per-vertex attributes with uniform variables. For example, uniform variables are well suited for the matrices used for modeling, viewing, and projective transformations.

Within a shader, uniform variables are read-only. Their values can only be changed from outside the shader, via the OpenGL API. However, they can be initialized within the shader by assigning to a constant value along with the declaration.

Uniform variables can appear in any shader within a shader program, and are always used as input variables. They can be declared in one or more shaders within a program, but if a variable with a given name is declared in more than one shader, its type must be the same in all shaders. In other words, the uniform variables are held in a shared uniform namespace for the entire shader program.

In this recipe, we'll draw the same triangle as in previous recipes in this chapter, however, this time we'll rotate the triangle using a uniform matrix variable.

Getting ready

We'll use the following vertex shader.

#version 400

layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexColor;

out vec3 Color;

uniform mat4 RotationMatrix;

void main()
{
    Color = VertexColor;
    gl_Position = RotationMatrix * vec4(VertexPosition,1.0); 
}

Note the variable RotationMatrix is declared using the uniform qualifier. We'll provide the data for this variable from the OpenGL program. The RotationMatrix is used to transform VertexPosition before assigning it to the default output position variable gl_Position.

We'll use the same fragment shader as in previous recipes.

#version 400

in vec3 Color;

layout (location = 0) out vec4 FragColor;

void main() {
    FragColor = vec4(Color, 1.0);
}

Within the main OpenGL code, we determine the rotation matrix and send it to the shader's uniform variable. To create our rotation matrix, we'll use the GLM library (see: Using the GLM library for mathematics in this chapter). Within the main OpenGL code, add the following include statements:

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

#include <glm/gtc/matrix_transform.hpp>

We'll also assume that code has been written to compile and link the shaders, and to create the vertex array object for the color triangle. We'll assume that the handle to the vertex array object is vaoHandle, and the handle to the program object is programHandle.

How to do it...

Within the render method, use the following code.

glClear(GL_COLOR_BUFFER_BIT);

mat4 rotationMatrix = glm::rotate(mat4(1.0f), angle, 
                                  vec3(0.0f,0.0f,1.0f));

GLuint location =glGetUniformLocation(programHandle,
                                     "RotationMatrix");

if( location >= 0 )
{
    glUniformMatrix4fv(location, 1, GL_FALSE, 
                      &rotationMatrix[0][0]);
}

glBindVertexArray(vaoHandle);
glDrawArrays(GL_TRIANGLES, 0, 3 );

How it works...

The steps involved with setting the value of a uniform variable include finding the location of the variable, and then assigning a value to that location using one of the glUniform functions.

In this example, we start by clearing the color buffer, then creating a rotation matrix using GLM. Next, we query for the location of the uniform variable by calling glGetUniformLocation. This function takes the handle to the shader program object and the name of the uniform variable, and returns its location. If the uniform variable is not an active uniform variable, the function returns -1.

We then assign a value to the uniform variable using glUniformMatrix4fv. The first argument is the uniform variable's location. The second is the number of matrices that are being assigned (the uniform variable could be an array). The third is a Boolean value indicating whether or not the matrix should be transposed when loaded into the uniform variable. With GLM matrices, a transpose is not required, so we use GL_FALSE here. If you were implementing the matrix using an array, and the data was in row-major order, you might need to use GL_TRUE for this argument. The last argument is a pointer to the data for the uniform variable.

There's more...

Of course uniform variables can be any valid GLSL type including complex types such as arrays or structures. OpenGL provides a glUniform function with the usual suffixes, appropriate for each type. For example, to assign to a variable of type vec3, one would use glUniform3f or glUniform3fv.

For arrays, we can use the functions ending in "v" to initialize multiple values within the array. Note that if it is desired, we can query for the location of a particular element of the uniform array using the [] operator. For example, to query for the location of the second element of MyArray we will query in the following way:

 GLuint location = 
     glGetUniformLocation( programHandle, "MyArray[1]" );

For structures, the members of the structure must be initialized individually. As with arrays, one can query for the location of a member of a structure using something like the following:

GLuint location = 
     glGetUniformLocation( programHandle, 
                           "MyMatrices.Rotation" );

Where the structure variable is MyMatrices and the member of the structure is Rotation.

See also

  • Compiling a shader

  • Linking a shader program

  • Sending data to a shader using per-vertex attributes and vertex buffer objects

You have been reading a chapter from
OpenGL 4.0 Shading Language Cookbook
Published in: Jul 2011
Publisher: Packt
ISBN-13: 9781849514767
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