Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
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

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.

Getting ready

We'll start with a basic OpenGL program that compiles and links a shader program. You could use the shaders from the recipe Sending data to a shader using per-vertex attributes and vertex buffer objects. In the following example, we'll assume that the handle to the program is in a variable named programHandle.

How to do it...

After linking the shader program, use the following steps to print information about the active uniform variables:

  1. Retrieve the maximum length of the names of all of the active uniforms and the number of active uniforms using glGetProgramiv.

    GLint nUniforms, maxLen;
    
    glGetProgramiv( programHandle, GL_ACTIVE_UNIFORM_MAX_LENGTH, 
                  &maxLen);
    glGetProgramiv( programHandle, GL_ACTIVE_UNIFORMS,
                  &nUniforms);
  2. Allocate space to store each uniform variable's name.

    GLchar * name = (GLchar *) malloc( maxLen );
  3. Retrieve and print information about each active uniform using glGetActiveUniform and glGetUniformLocation.

    GLint size, location;
    GLsizei written;
    GLenum type;
    printf(" Location | Name\n");
    printf("------------------------------------------------\n");
    for( int i = 0; i < nUniforms; ++i ) {
        glGetActiveUniform( programHandle, i, maxLen, &written, 
                          &size, &type, name );
        location = glGetUniformLocation(programHandle, name);
        printf(" %-8d | %s\n", location, name);
    }
    
    free(name);

How it works...

In step one above, we call the function glGetProgramiv to query for the maximum length of the uniform variable names (GL_ACTIVE_UNIFORM_MAX_LENGTH), and the number of active uniforms (GL_ACTIVE_UNIFORMS). The maximum length value includes the null terminating character, so in step 2 we allocate enough space to store a name of that length.

Next, we loop from zero to the number of uniforms minus one, and call glGetActiveUniform to retrieve information about each variable. Similar to glGetActiveAttrib, this function provides several pieces of information about the variable including its size, type, and name. We then query for the location of that uniform variable by calling glGetUniformLocation. It is quite often the case that the index used in the call to glGetActiveUniform is the same as the uniform's location, but we make the call just to be sure.

Finally, we print the name and location of the variable to standard out.

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.

Note that one could also use the function glGetActiveUniformName instead of glGetActiveUniform. The former only provides the name, while the latter also provides the size and type.

See also

  • Sending data to a shader using uniform variables

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