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

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.

Sending data to a shader using uniform variables

Getting ready

We'll use the following vertex shader:

#version 430

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 via the OpenGL program. The RotationMatrix is also 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 430

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 the Using the GLM for mathematics recipe 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, 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's location using glUniformMatrix4fv. The first argument is the uniform variable's location. The second is the number of matrices that are being assigned (note that 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, one can use the functions ending in "v" to initialize multiple values within the array. Note that if it is desired, one 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:

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

  • The Compiling a shader recipe
  • The Linking a shader program recipe
  • The Sending data to a shader using vertex attributes and vertex buffer objects recipe
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 €18.99/month. Cancel anytime