Specifications for the use of vectors and matrices in the GLSL ES shader language

Table of contents

Vector and matrix types 

Here are examples of declaring vectors and matrices:

assignment and construction

vector constructor

matrix constructor 

Several ways to construct a matrix

access element

.operator

vector component names

[ ] operator

operator

Operators available for vectors and matrices

Vector and matrix related operations

Arithmetic with vectors and floating point numbers

Vector operations

Operations on matrices and floating point numbers

Matrix right multiplied by vector

Matrix left multiplication vector

Matrix multiplied by matrix


GLSL ES supports vector and matrix types, which are two data types that are well suited for processing computer graphics. Variables of both vector and matrix types contain multiple elements, each of which is a numeric value (integer, floating point, or Boolean). A vector arranges these elements into a column and can be used to represent vertex coordinates or color values, etc., while a matrix divides the elements into rows and columns and can be used to represent a transformation matrix. The figure below gives examples of vectors and matrices .

Vector and matrix types 

Here are examples of declaring vectors and matrices:

assignment and construction

We use the equal sign (=) to assign values ​​to vectors and matrices. Remember, the types of variables/values ​​on the left and right sides of the assignment operator must be the same, and the number of elements (vector or matrix) on the left and right sides must also be the same. For example, the following line of code will cause an error.

Here, the vec4 type variable has 4 elements, and you should pass in 4 floating point values ​​​​somehow. Usually we use the built-in constructor with the same name as the data type to generate variables. For the vec4 type, you can use the built-in vec4() function. For example, if you want to create four vec4 type variables with components of 1.0, 2.0, 3.0 and 4.0, you can call the vec4() function as follows. 

Such functions that specifically create variables of a specified type are called constructor functions . The name of the constructor function is always consistent with the type name of the variable it creates. 

vector constructor

Vectors are very important in GLSL ES, so GLSL ES provides a rich and flexible way to create vectors. for example:

In line 2, the constructor ignores the third component of v3 and creates a new variable using only its first and second components. Similarly, in the third line of code, only one parameter 1.0 is passed to the constructor, and the constructor will automatically assign this parameter value to all elements of the newly created vector. However, if the constructor receives more than one parameter, but the number of parameters is less than the number of elements of the vector, an error will occur.

Finally, you can also combine multiple vectors into one vector, such as:

The rule here is to fill in all the elements of the first parameter v2 first. If it is not filled yet, continue to fill it with the elements of the second parameter v4.

matrix constructor 

The matrix constructor is used in much the same way as the vector constructor. However, you must ensure that the elements stored in the matrix are arranged in column-major order. The following examples show different ways of using the matrix constructor.

Several ways to construct a matrix

● Pass the value of each element of the matrix into the matrix constructor to construct the matrix. Note that the order of the passed values ​​must be in column-major order.

● Pass one or more vectors into the matrix constructor, and use the element values ​​in the vectors to construct the matrix in column-major order.

● Pass vectors and values ​​into the matrix constructor, and use the element values ​​in the vector and the directly passed values ​​to construct the matrix in column-major order. 

● Pass a single value into the matrix constructor, which will generate a matrix in which the elements on the diagonal are all that value and the other elements are 0.0. 

Similar to the vector constructor, if the number of values ​​passed in is greater than 1 and the number of matrix elements is not reached, an error will occur. 

access element

In order to access elements in a vector or matrix, you can use the . or [] operator, which will be described in sections below.

.operator

By appending the dot operator (.) to the vector variable name and then the component name, you can access the elements of the vector. The component names of vectors are shown in the table below.

vector component names

Since vectors can be used to store vertex coordinates, colors, and texture coordinates, GLSL ES supports the above three component names to enhance program readability. In fact, the x, r, or s component of any vector returns the 1st component, the y, g, t components all return the 2nd component, and so on. Feel free to use them interchangeably if you wish. for example: 

As you can see, in these examples, although x, r, and s have different names, they all access the first component. If you try to access components that exceed the length of the vector, you will get an error: 

Multiple components can be extracted from a vector at the same time by placing multiple component names (of the same set) together after the dot operator. This process is called swizzling . In the following example, we use x, y, z, and w, but other sets will have the same effect: 

Aggregation component names can also be used as lvalues ​​of assignment expressions (=):

Remember, multiple component names must belong to the same set, for example, you cannot use v3.was. 

[ ] operator

In addition to the . operator, you can also use the [ ] operator to access elements of a vector or matrix through array subscripts. Note that the elements in the matrix are still read in column-major order. As in JavaScript, subscripts start at 0, so [0] can access the first column element of the matrix, [1] can access the second column element, [2] can access the third column element, And so on, as shown below:

In addition, you can access an element of a column by using two [ ] operators in succession:

Likewise, you can use the [] operator and component names together to access elements in a matrix, as follows:

There is a restriction here, that is, the index value that can only appear in [] must be a constant index value (constant index). The definition of a constant index value is as follows:

● Integer literal (such as 0 or 1).

● Global variables or local variables modified with const, excluding function parameters.

● Loop index.

● An expression consisting of terms from the three preceding items.

The following example uses a const variable as an index to access array elements:

The following example uses a const expression as an index:

Note that you cannot use a non-const variable as an index value because it is not a constant index value (unless it is a loop index):

operator

The following table shows the operations supported by vectors and matrices. The operators for matrices and vectors are very similar to the operators for primitive types (such as integers). Note that for vectors and matrices, only comparison operators == and ! can be used! =, >, <, >= and <= cannot be used. If you want to compare the sizes of vectors and matrices, you should use built-in functions like lessThan().

Operators available for vectors and matrices

Note that when an operation assignment operation is performed on a vector or matrix, an independent operation assignment is actually performed on each element of the matrix or vector component by component. 

Vector and matrix related operations

The following examples show common situations when working with vectors and matrices. We assume that, in these examples, the variables are defined as follows:

Arithmetic with vectors and floating point numbers

This example shows the + operator in action: 

For example, v3a=vec3(1.0, 2.0, 3.0) and f=1.0, then the result is v3b=(2.0, 3.0, 4.0).

Vector operations

Vector arithmetic operations occur on each component of a vector. 

For example, v3a=vec3(1.0, 2.0, 3.0) and v3b=(4.0, 5.0, 6.0), then the result is v3c=(5.0, 7.0, 9.0).

Operations on matrices and floating point numbers

Matrix operations with floating point numbers occur on each component of the matrix. 

Matrix right multiplied by vector

The result of right-multiplying a vector by a matrix is ​​a vector in which each component is the corresponding component in the original vector multiplied by the sum of the products of each element of the corresponding row of the matrix. 

Matrix left multiplication vector

Matrix multiplied by matrix

Guess you like

Origin blog.csdn.net/dabaooooq/article/details/132793809