Such as gl_FragColor = v1 * v2
, i can't really get how does it multiplies and it seems that the reference give the explanation of vector multiply matrix.
ps: The type of v1
and v2
are both vec4
.
GLSL only supports square matrices, so the size of two matrices must be equal to multiply them together. A vector is treated as either a row or column vector whenever it is multiplied by a matrix, whichever makes the operation correct. You do not have to transpose a vector as you would in normal matrix algebra.
Vectors can be multiplied in two different ways i.e., dot product and cross product. The results in both of these multiplications of vectors are different. Scalar multiplication of vectors or dot product gives a scalar quantity as a result whereas vector multiplication of vectors or cross product gives vector quantity.
The *
operator works component-wise for vectors like vec4
.
vec4 a = vec4(1.0, 2.0, 3.0, 4.0); vec4 b = vec4(0.1, 0.2, 0.3, 0.4); vec4 c = a * b; // vec4(0.1, 0.4, 0.9, 1.6)
The GLSL Language Specification says under section 5.10 Vector and Matrix Operations:
With a few exceptions, operations are component-wise. Usually, when an operator operates on a vector or matrix, it is operating independently on each component of the vector or matrix, in a component-wise fashion. [...] The exceptions are matrix multiplied by vector, vector multiplied by matrix, and matrix multiplied by matrix. These do not operate component-wise, but rather perform the correct linear algebraic multiply.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With