矩阵乘法与向量在GLSL中的应用

问题描述 投票:1回答:1

参考文献 http:/webglfundamentals.orgwebgllessonswebgl-3d-orthographic.html。在矢量着色器中,有乘以 mat4vec4.

attribute vec4 a_position;

uniform mat4 u_matrix;

void main() {

  // Multiply the position by the matrix.

  gl_Position = u_matrix * a_position;

}

4*4矩阵与1*4矩阵怎么可能相乘呢,是不是应该是 gl_Position = a_position * u_matrix;

谁能解释一下?

webgl
1个回答
4
投票

从GLSL规范1.017来看

5.11 向量和矩阵运算

除了少数例外,操作是分量式的。当一个运算符对一个向量或矩阵进行运算时,它是以分量的方式对向量或矩阵的每一个分量进行独立的运算。

...矩阵乘以向量、向量乘以矩阵和矩阵乘以矩阵。这些并不是以分量的方式操作,而是执行正确的线性代数乘法。它们要求操作数的大小相匹配。

vec3 v, u;
mat3 m;

u = v * m;

相当于

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]);

还有

u = m * v;

相当于

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;
© www.soinside.com 2019 - 2024. All rights reserved.