Graphics Reference
In-Depth Information
is that vectors are used interchangeably to represent mathematical vectors,
colors, and texture coordinates. The x , r , or s component will always refer
to the first element of a vector. The different naming conventions are just
provided as a convenience. That said, you cannot mix naming conventions
when accessing a vector (in other words, you cannot do something like
.xgr , as you can use only one naming convention at a time). When using
the “.” operator, it is also possible to reorder components of a vector in an
operation. The following examples show how this can be done.
vec3 myVec3 = vec3(0.0, 1.0, 2.0); // myVec3 = {0.0, 1.0, 2.0}
vec3 temp;
temp = myVec3.xyz; // temp = {0.0, 1.0, 2.0}
temp = myVec3.xxx; // temp = {0.0, 0.0, 0.0}
temp = myVec3.zyx; // temp = {2.0, 1.0, 0.0}
In addition to the “.” operator, vectors can be accessed using the array
subscript “ [] ” operator. In array subscripting, element [0] corresponds to
x , element [1] corresponds to y , and so forth. Matrices are treated as being
composed of a number of vectors. For example, a mat2 can be thought
of as two vec2 s, a mat3 as three vec3 s, and so forth. For matrices, the
individual column is selected using the array subscript operator “ [] ”, and
then each vector can be accessed using the vector access behavior. The
following shows some examples of accessing matrices:
mat4 myMat4 = mat4(1.0); // Initialize diagonal to 1.0
(identity)
vec4 colO = myMat4[0]; // Get colO vector out of the matrix
float ml_l = myMat4[1][1]; // Get element at [1][1] in matrix
float m2_2 = myMat4[2].z; // Get element at [2][2] in matrix
Constants
It is possible to declare any of the basic types as being constant variables.
Constant variables are those whose values do not change within the shader.
To declare a constant, you add the const qualifier to the declaration.
Constant variables must be initialized at declaration time. Some examples
of const declarations follow:
const float zero = 0.0;
const float pi = 3.14159;
const vec4 red = vec4(1.0, 0.0, 0.0, 1.0);
const mat4 identity = mat4(1.0);
 
 
Search WWH ::




Custom Search