Book Image

OpenGL 4 Shading Language Cookbook - Second Edition

By : David Wolff, David A Wolff
Book Image

OpenGL 4 Shading Language Cookbook - Second Edition

By: David Wolff, David A Wolff

Overview of this book

OpenGL Shading Language (GLSL) is a programming language used for customizing parts of the OpenGL graphics pipeline that were formerly fixed-function, and are executed directly on the GPU. It provides programmers with unprecedented flexibility for implementing effects and optimizations utilizing the power of modern GPUs. With Version 4, the language has been further refined to provide programmers with greater power and flexibility, with new stages such as tessellation and compute. OpenGL 4 Shading Language Cookbook provides easy-to-follow examples that first walk you through the theory and background behind each technique, and then go on to provide and explain the GLSL and OpenGL code needed to implement it. Beginner level through to advanced techniques are presented including topics such as texturing, screen-space techniques, lighting, shading, tessellation shaders, geometry shaders, compute shaders, and shadows. OpenGL Shading Language 4 Cookbook is a practical guide that takes you from the fundamentals of programming with modern GLSL and OpenGL, through to advanced techniques. The recipes build upon each other and take you quickly from novice to advanced level code. You'll see essential lighting and shading techniques; examples that demonstrate how to make use of textures for a wide variety of effects and as part of other techniques; examples of screen-space techniques including HDR rendering, bloom, and blur; shadowing techniques; tessellation, geometry, and compute shaders; how to use noise effectively; and animation with particle systems. OpenGL Shading Language 4 Cookbook provides examples of modern shading techniques that can be used as a starting point for programmers to expand upon to produce modern, interactive, 3D computer graphics applications.
Table of Contents (17 chapters)
OpenGL 4 Shading Language Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Getting a list of active uniform variables


While it is a simple process to query for the location of an individual uniform variable, there may be instances where it can be useful to generate a list of all active uniform variables. For example, one might choose to create a set of variables to store the location of each uniform and assign their values after the program is linked. This would avoid the need to query for uniform locations when setting the value of the uniform variables, creating slightly more efficient code.

The process for listing uniform variables is very similar to the process for listing attributes (see the Getting a list of active vertex input attributes and locations recipe), so this recipe will refer the reader back to the previous recipe for detailed explanation.

Getting ready

Start with a basic OpenGL program that compiles and links a shader program. In the following, we'll assume that the handle to the program is in a variable named programHandle.

How to do it…

After linking and enabling the shader program, use the following code to display the list of active uniforms:

  1. Start by querying for the number of active uniform variables:

    GLint numUniforms = 0;
    glGetProgramInterfaceiv( handle, GL_UNIFORM, GL_ACTIVE_RESOURCES, &numUniforms);
  2. Loop through each uniform index and query for the length of the name, the type, the location and the block index:

    GLenum properties[] = {GL_NAME_LENGTH, GL_TYPE, GL_LOCATION, GL_BLOCK_INDEX};
    
    printf("Active uniforms:\n");
    for( int i = 0; i < numUniforms; ++i ) {
      GLint results[4];
      glGetProgramResourceiv(handle, GL_UNIFORM, i, 4, properties, 4, NULL, results);
      if( results[3] != -1 ) 
            continue;       // Skip uniforms in blocks 
      GLint nameBufSize = results[0] + 1;
      char * name = new char[nameBufSize];
      glGetProgramResourceName(handle, GL_UNIFORM, i, nameBufSize, NULL, name);
    printf("%-5d %s (%s)\n", results[2], name, getTypeString(results[1]));
      delete [] name;
    }

How it works...

The process is very similar to the process shown in the recipe Getting a list of active vertex input attributes and locations. I will focus on the main differences.

First and most obvious is that we use GL_UNIFORM instead of GL_PROGRAM_INPUT as the interface that we are querying in glGetProgramResourceiv and glGetProgramInterfaceiv. Second, we query for the block index (using GL_BLOCK_INDEX in the properties array). The reason for this is that some uniform variables are contained within a uniform block (see the recipe Using uniform blocks and uniform buffer objects). For this example, we only want information about uniforms that are not within blocks. The block index will be -1 if the uniform variable is not within a block, so we skip any uniform variables that do not have a block index of -1.

Again, we use the getTypeString function to convert the type value into a human-readable string (see example code).

When this is run on the shader program from the previous recipe, we see the following output:

Active uniforms:
0    RotationMatrix (mat4)

There's more...

As with vertex attributes, a uniform variable is not considered active unless it is determined by the GLSL linker that it will be used within the shader.

The previous code is only valid for OpenGL 4.3 and later. Alternatively, you can achieve similar results using the functions glGetProgramiv, glGetActiveUniform, glGetUniformLocation, and glGetActiveUniformName.

See also

  • The Sending data to a shader using uniform variables recipe