Shaders - jgoffeney/Cesium4Unreal GitHub Wiki

Back

References

GLSL

GLSL is C-like shader language for use with graphics.

Types

GLSL supports the C-scalar types of int, float, double, uint and bool. It also support vector and matrix types.

Vectors

The GLSL vector type is used as TvecN where T is the letter defining the data type and N is the number of elements. If T is empty then the default type is float otherwise T defines the type using the first letter of the scalar data type. N can be a value of 2, 3 or 4 to define the number of elements. A 4 element vector of booleans is bvec4 while a 2 element vector of unsigned integers is uvec2.

Components of the vector can be accessed via .x, .y, .z and .w. The same elements can be accessed via rgba or stpq which are provided for code clarity when accessing color and texture coordinate data.

Swizzling

Swizzling is a method of specifying how you want to access the vector elements together. By concatenating / reordering the vector element accessors GLSL will creating a new vector of size specified by the number of accessors and fill it based on the order of the accessors. Accessors can be repeated, as well, to get the same values.

vec4 myVector;
vec2 twoVector = myVector.xy;
vec3 threeVector = twoVector.xyx;
vec4 fourVector = threeVector.zyzx;

Constructors

Vectors can be constructed by supplying the number of element values specified by the vector size. But you can also use swizzling to initialize multiple elements of the vector or the entire vector.

vec4 myVector = vec4(1.0, 2.0, 3.0, 4.0);
vec4 otherVector = vec4(myVector.xz, 5.0, 6.0);
vec3 threeVector = myVector.xyz;
vec4 fourVector = vec4(threeVector, 7.0);

In and Out

The in and out keywords are used in the variable section of the shader to define variables as either being read as inputs or passed along as outputs. For inputs there is additionally a layout keyword which is used to set the position index of the parameter like setting each input as an array element. This position index is used in the host code to set the value for the input parameter. If layout is not used then the host code has to make additional lookups to find the automatically generated index value.

// Vertex Shader
// Expecting the host to set a three element float vector as the first input parameter.
layout(location = 0) in vec3 inputColor;

// Pass the four element vector as output to the next step in pipeline.
out vec4 outputColor;

void main(){
  //Pass a color to the fragment shader
  outputColor = vec4(inputColor, 1.0);
}

Uniforms

The uniform keyword is used to declare a variable as global within the shader space. It also does not have to be declared in the vertex shader but can be in any shader. This prevents the need to pass the variable through the shader stages until it gets to where it is needed.

// Fragment shader
uniform vec3 inputColor

out vec4 outputColor;

void main(){
  //Pass a color to the fragment shader
  outputColor = vec4(inputColor, 1.0);
}

Setting a Uniform

The function glGetUniformLocation finds the paramter's index location within the shaders. After glUseProgram is called set the value via a function of the form glUniformNT where is N is the number of vector elements and T is the letter indicating the data type.

// Host Code
int inputColorLocation = glGetUniformLocation(shaderProgram, "inputColor");
glUseProgram(shaderProgram);
glUniform3f(inputColorLocation, 1.0, 0.5, 0.2);

Uniform Matrices

Matrices can be passed as uniform as well via the function glUniformMatrixNTv where is N is the number of vector elements and T is the letter indicating the data type. As above it needs to be called after glUseProgram.

// Shader
uniform mat4 


// Host Code

glUseProgram(programID);
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);