Book Image

OpenGL ES 3.0 Cookbook

By : Parminder Singh
Book Image

OpenGL ES 3.0 Cookbook

By: Parminder Singh

Overview of this book

<p>"Write once, use anywhere" is truly the power behind OpenGL ES and has made it an embedded industry standard. The library provides cutting-edge, easy-to-use features to build a wide range of applications in the gaming, simulation, augmented-reality, image-processing, and geospatial domains.</p> <p>The book starts by providing you with all the necessary OpenGL ES 3.0 setup guidelines on iOS and Android platforms. You'll go on to master the fundamentals of modern 3D graphics, such as drawing APIs, transformations, buffer objects, the model-view-project analogy, and much more. The book goes on to deal with advanced topics and offers a wide range of recipes on the light shading, real-time rendering techniques with static and procedure textures to create stunning visualizations and runtime effects.</p>
Table of Contents (21 chapters)
OpenGL ES 3.0 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Using uniform variables to send data to a shader


The uniform variables contain the data values that are global. They are shared by all vertices and fragments in the vertex and fragment shaders. Generally, some information that is not specific to the per-vertex is treated in the form of uniform variables. The uniform variable could exist in both the vertex and fragment shaders.

Getting ready

The vertex shader we programmed in the Programming shaders in OpenGL ES shading language 3.0 recipe contains a uniform variable RadianAngle. This variable is used to rotate the rendered triangle:

// Uniform variable for rotating triangle
uniform float  RadianAngle;

This variable will be updated on the client side (CPU) and send to the shader at server side (GPU) using special OpenGL ES 3.0 APIs. Similar to per-vertex attributes for uniform variables, we need to query and bind data in order to make it available in the shader.

How to do it...

Follow these steps to send data to a shader using uniform variables:

  1. Declare a global variable in NativeTemplate.cpp to store the queried attribute location IDs of radianAngle:

    GLuint radianAngle;
  2. Query the uniform variable location using the glGetUniformLocation API:

    radianAngle=glGetUniformLocation(programID,"RadianAngle");

    This API will return a value greater than or equal to 0 to ensure that a uniform variable with the given name exists.

    • Syntax:

      GLint glGetUniformLocation(GLuint program,const GLchar *name)

      Variable

      Description

      program

      This is the handle of a successfully linked OpenGL ES program

      name

      This is the name of the uniform variable in the shader source program

  3. Send the updated radian value to the shader using the glUniform1f API:

    float degree = 0; // Global degree variable
    float radian;     // Global radian variable
    
    // Update angle and convert it into radian
    radian = degree++/57.2957795; 
    // Send updated data in the vertex shader uniform 
    glUniform1f(radianAngle, radian);

    There are many variants of the glUniform API.

    • Syntax:

      void glUniform1f(GLint location, GLfloat v0);

      Variable

      Description

      location

      This is the index of the uniform variable in the shader

      v0

      This is the data value of type float that needs to be sent

    Note

    For more information on other variants, refer to OpenGL ES 3.0 Reference Pages at http://www.khronos.org/opengles/sdk/docs/man3/.

  4. Use a general form of 2D rotation to apply on the entire incoming vertex coordinates:

    . . . . 
    uniform float  RadianAngle;
    mat2 rotation = mat2(cos(RadianAngle),sin(RadianAngle),
                        -sin(RadianAngle),cos(RadianAngle));
    void main() {
      gl_Position = mat4(rotation)*VertexPosition;
      . . . . .
    }

How it works...

The uniform variable RadianAngle defined in the vertex shader is used to apply rotation transformation on the incoming per-vertex attribute VertexPosition. On the client side, this uniform variable is queried using glGetUniformLocation. This API returns the index of the uniform variable and stores it in radianAngle. This index will be used to bind the updated data information that is stored the radian with the glUniform1f OpenGL ES 3.0 API. Finally, the updated data reaches the vertex shader executable, where the general form of the Euler rotation is calculated:

mat2 rotation = mat2(cos(RadianAngle),sin(RadianAngle),
              -sin(RadianAngle),cos(RadianAngle));

The rotation transformation is calculated in the form of 2 x 2 matrix rotation, which is later promoted to a 4 x 4 matrix when multiplied by VertexPosition. The resultant vertices cause to rotate the triangle in a 2D space.

See also

  • Refer to the Grouping uniforms and creating buffer objects recipe in Chapter 3, New Features of OpenGL ES 3.0