Operand of OpenGL es Shader language (Official document, chapter fifth)

Source: Internet
Author: User
Tags scalar

operand of OpenGL es Shader language (Official document, chapter fifth)5.1 Operands

The OpenGL ES shader language contains the following operators.

5.2 Array Subscript

The array element is accessed through the array subscript operator ([]). This is the only operator that operates an array, and for example, an array element is accessed:

Diffusecolor + = lightintensity[3] * NDOTL;

5.3 Function calls

If a function has a return value, the function call is usually used in an expression.

5.4 Constructors

The constructor uses the function call syntax, which is a basic type of keyword or struct name that is used in the initializer or expression. The parameter is used to initialize the value to be constructed. The constructor can convert one data scalar type to another scalar type, or from a smaller type to a larger type. or convert from a larger type to a smaller type.

5.4.1 Conversion and scalar constructors

Conversions between scalars:

INT (BOOL)         //Convert Boolean value to integer int (float)         //Convert floating-point value to Integer float (bool)         //Convert Boolean value to floating-point number float (int)         // Converts an integer value to a floating-point bool (int)         //Converts an integer value to a Boolean value bool (float)         //Converts a floating-point number to a Boolean value

When the constructor converts a float to int, the decimal portion of the floating-point number is automatically discarded.

When int and float are converted to bool, 0 and 0.0 are converted to false, and non-0 values are converted to true. When the constructor converts a bool value to int or float, false is converted to 0 or 0.0, and true is converted to 1 or 1.0.

Equivalent constructors, such as float (float), are also legal, but seldom used.

If the constructor's parameter is not a scalar, such as a vector, then the constructor takes its first element. such as the float (VEC3) constructor takes the first value in the VEC3.

5.4.2 Vector and Matrix constructors

Constructors can also be used to create vectors and matrices from scalar collections, vectors, and matrices. The vector length can be shortened as well.

If a single scalar is used to initialize the vector, all values of the vector are initialized with that value. If a single scalar is used to initialize the matrix, all elements of the diagonal of the matrix will be initialized to that value, but the other elements will be initialized to 0.0

If a vector is constructed from multiple scalar, vector, or matrix or a mixture of these, then the elements of the vector are initialized in the order of the parameter list. The constructor takes the argument from left to right in the argument list, and if the argument has multiple values, Then take the value out of this parameter in turn. The construction matrix is similar. The matrix elements are constructed in the main order of the columns. The constructor constructs the elements of the matrix by taking the parameter values from the argument list in turn. If the number of values in the argument list exceeds the number of elements in the matrix or vector, an error will be raised.

If a matrix is used to construct the matrix, then the element values in the parameter matrix are placed in the corresponding position of the new matrix.

If the base type (int, float, BOOL) is passed as a parameter to the constructor, but the type of element to construct differs from the data type passed in, the type conversion is used.

VEC3 (float)//Initializes each component of with the FLOATVEC4 (IVEC4)//Makes a VEC4 with Component-wise conversionvec2 ( float, float)//Initializes a VEC2 with 2 floatsivec3 (int, int., int)//Initializes an IVEC3 with 3 intsbvec4 (int, int, f Loat, float)//uses 4 Boolean conversionsvec2 (VEC3)//Drops the third component of a VEC3VEC3 (VEC4)//drops the fourth Component of a vec4vec3 (VEC2, float)//vec3.x = vec2.x, vec3.y = vec2.y, vec3.z = FLOATVEC3 (float, vec2)//vec3.x = Floa T, vec3.y = vec2.x, vec3.z = VEC2.YVEC4 (vec3, float) vec4 (float, VEC3) vec4 (VEC2, vec2) vec4 color = vec4 (0.0, 1.0, 0.0, 1.0 ); Vec4 Rgba = VEC4 (1.0); Sets each component to 1.0VEC3 RGB = VEC3 (color); Drop the 4th Component Mat2 (float) mat3 (float) mat4 (float) mat2 (VEC2, VEC2); MAT3 (VEC3, VEC3, VEC3); Mat4 (VEC4, VEC4, VEC4 , VEC4); Mat2 (float, float,float, float), mat3 (float, float, float,float, float, float,float, float, float), mat4 (float, float, float, float,float, float, float, float,float, float, float, float,float, float, float, float); 
5.4.3 Structure Constructor

Once a struct is defined and given a type name, the constructor with the same name can be used.

struct Light {float intensity;vec3 position;}; Light Lightvar = Light (3.0, VEC3 (1.0, 2.0, 3.0));

The arguments passed into the constructor must have the same order and type as declared inside the struct.

A struct constructor can be used in an initializer or in an expression.

5.5 Vector components

The name of each component in the vector is represented by a single character. A component of a commonly used position, color, or texture coordinate vector is directly associated with several handy numbers. A component in an access vector can use the vector name (.) The way the component name is.

The supported component names are as follows:

The component name X,r,s is a synonym for the same component in the vector.

Note that in order to not confuse R (red) in the color vector, the third component name in the texture vector uses p.

Accessing a component that exceeds the number of vectors causes an error:

VEC2 POS;
Pos.x//IS legal
POS.Z//Is illegal

Component Selection syntax You can select more than one component at a time:

VEC4 V4;v4.rgba; is a vec4 and the same as just using V4,v4.rgb; is a vec3,v4.b; is a float,v4.xy; is a vec2,v4.xgba; Is illegal-the component names does not come from//the same set.

Different vectors can be produced by moving and replacing the components:

Vec4 pos = VEC4 (1.0, 2.0, 3.0, 4.0);
VEC4 swiz= Pos.wzyx; SWiZ = (4.0, 3.0, 2.0, 1.0)
Vec4 dup = POS.XXYY; DUP = (1.0, 1.0, 2.0, 2.0)

Component group symbols can appear in an lvalue or in the right value.

Vec4 pos = VEC4 (1.0, 2.0, 3.0, 4.0);p OS.XW = VEC2 (5.0, 6.0); pos = (5.0, 2.0, 3.0, 6.0) POS.WX = VEC2 (7.0, 8.0); pos = (8.0, 2.0, 3.0, 7.0) pos.xx = VEC2 (3.0, 4.0); Illegal-' x ' used twicepos.xy = VEC3 (1.0, 2.0, 3.0); Illegal-mismatch between VEC2 and VEC3

The array subscript index syntax also applies to vectors. So:

VEC4 POS;

Pos[2] Represents a third element, which is equivalent to using POS.Z.

5.6 Matrix Components

an Access matrix component can use the subscript index syntax of an array . Using a one-dimensional array access matrix means that you want to access the corresponding column component in the matrix, which is the vector that returns all elements of the corresponding column. A two-bit array is a specific access to a component. Because matrices are column-first, when you use arrays to index matrix elements, the first dimension of the array represents the column, and the second dimension represents the row.

MAT4 m;m[1] = VEC4 (2.0); Sets the second column to all 2.0m[0][0] = 1.0; Sets the upper left element to 1.0m[2][3] = 2.0; Sets the 4th element of the third column to 2.0

if the subscript is out of bounds, it will cause a compile-time error.

5.7 Structs and Fields

The access to the struct field is also used with the point operator (.).

There are several operations available for structs:

The Equals and assignment operators make sense only if the two operand types are the same struct. Even if the names and fields of the two structures are identical, they are not equal. The assignment and equals operation of the struct containing the matrix and sampler type is undefined.

Struct S {int x;};
S A;
{
struct S {int x;};
S b;
A = b; Error:type mismatch
}

5.8 Assigning values

An assignment expression structure is as follows:

lvalue-expression = expression;

Lvalue-expression represents an lvalue expression that assigns the value of an expression to lvalue-expression through the assignment operator "="; the expression and the lvalue have the same type to compile. All type conversions must be specified by the constructor in the display. The left value must be writable.

Operators such as + =,-=, *=,/= are supported.

Operators such as%=, <<=, >>=, |=, and ^= are reserved for subsequent revisions.

Reading a variable that is not written or initialized is legal, but its value is undefined.

5.9 Expressions

Expressions are created in the shading language in the following ways:

  • bool. int, float type constant, all vector type, all matrix type.
  • All types of constructors
  • Variable names of all types, except for the array names that do not follow the subscript
  • Array name with subscript
  • The name of the array without the subscript. It is generally used only in function parameter passing.
  • A function call with a return value.
  • The component field selector and array subscript return values.
  • Bracket expression. Any expression can be a bracket expression.
  • Binary operators +,-, *,/
  • % reserved for revisions
  • unary minus operator (-), self-add (+ +), decrement (-) operator.
  • The comparison operator. Greater than (), less than (=), greater than or equal to (>=), less than or equal to (<=). If you want to compare vectors, use the built-in function lessthan,lessthanequal,greaterthan,greaterthanequal.
  • equals (=) and does not equal (! =) operator. In addition to arrays, structures that contain arrays, samplers, and other types that contain samplers outside the body can be used. If you want components to be compared by bit, use the built-in functions equal and notequal.
  • Logical binary operator (&&, | |, ^).
  • Logical unary operation printable (! )
  • The comma operator. The comma operator returns the last result in the sequence of expressions.
  • Ternary selection operator (? :)
  • Operator (&,|,^,~,>>,<<) is reserved for revision.
5.10 Constant-expression

Constant expressions can be of the following types:

    • Character value (such as 5 or true)
    • Global or local use of const-decorated variables, except for function arguments
    • Returns a constant array, an element of a constant matrix, or a field of a constant structure body
    • Parameters are structural bodies of constant expressions
    • All parameters are built-in function types for constant expressions, except for texture lookup functions.

The following cannot be used in a constant expression:

    • User-defined Functions
    • uniform,attribute,varying Variable

An array variable cannot have a constant expression because the constant must be initialized at declaration time, but the array has no initialization mechanism

5.11 Vector and matrix operations

For matrix and vector operations, each individual build of a vector and a matrix is manipulated. As an example,

(1) The vector and a floating-point number or integer are added, and the result is that each element of the vector is added to the floating-point number

Vec3 V, u;
float F;
v = U + F;
Equivalent to:
v.x = u.x + F;
V.Y = U.y + F;
V.z = u.z + F;

(2) Adding vectors and vectors, the result is that the components of the corresponding positions of the two vectors are added separately

Vec3 V, u, W;
W = v + u;

Equivalent to:

w.x = v.x + u.x;
W.Y = V.y + u.y;
W.z = v.z + u.z;

This is a similar operation for most operators and for all integers, floating-point vectors, and matrix types. Exceptions are the multiplication of matrices and vectors, and the multiplication of matrices and matrices. Such as:

(3) Multiply the vector by the matrix, and the result is

Vec3 V, u;
MAT3 m;
u = v * m;
Equivalent to:
u.x = Dot (V, m[0]); M[0] is the left column of M
U.Y = Dot (V, m[1]); Dot (A, b) is the inner (dot) product of A and B
U.z = Dot (V, m[2]);

(4) Multiply the matrix by the vector, and the result is the multiplication of the rows and vectors of the matrix
u = m * V;
Equivalent to:
u.x = m[0].x * v.x + m[1].x * v.y + m[2].x * V.Z;
U.Y = m[0].y * v.x + m[1].y * v.y + m[2].y * V.Z;
u.z = m[0].z * v.x + m[1].z * v.y + m[2].z * V.Z;

(5) The matrix multiplied by the matrix, the rows of the previous matrix multiply the columns of a matrix later
Mat m, N, R;
r = m * N;
Equivalent to:
r[0].x = m[0].x * n[0].x + m[1].x * n[0].y + m[2].x * N[0].Z;
r[1].x = m[0].x * n[1].x + m[1].x * n[1].y + m[2].x * N[1].Z;
r[2].x = m[0].x * n[2].x + m[1].x * n[2].y + m[2].x * N[2].Z;
R[0].Y = m[0].y * n[0].x + m[1].y * n[0].y + m[2].y * N[0].Z;
R[1].Y = m[0].y * n[1].x + m[1].y * n[1].y + m[2].y * N[1].Z;
R[2].Y = m[0].y * n[2].x + m[1].y * n[2].y + m[2].y * N[2].Z;
r[0].z = m[0].z * n[0].x + m[1].z * n[0].y + m[2].z * N[0].Z;
r[1].z = m[0].z * n[1].x + m[1].z * n[1].y + m[2].z * N[1].Z;
r[2].z = m[0].z * n[2].x + m[1].z * n[2].y + m[2].z * N[2].Z;

Operand of OpenGL es Shader language (Official document, chapter fifth)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.