Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does vectors multiply act in shader language?

Tags:

opengl

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.

like image 415
Frank Cheng Avatar asked Dec 16 '12 12:12

Frank Cheng


People also ask

How do you multiply a vector in GLSL?

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.

How do vectors multiply?

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.


1 Answers

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.

like image 162
Matthias Avatar answered Sep 29 '22 13:09

Matthias