javascript - 谁能解释一下glMatrix中的几个函数。
迷茫
迷茫 2017-04-11 10:48:28
0
0
309

在学习webgl的时候涉及到坐标转换的问题,我们都知道顶点最后渲染出来要进行的坐标转换步骤是模型坐标转换,视图坐标转化,投影坐标转换,应该用顶点的向量乘以mvp这三个矩阵。webgl使用的是列向量,所以要变成pvm乘以顶点坐标,也就是说我们获得p,v,m三个矩阵以后是要按照pvm的顺序进行相乘。现在问题来了,我在使用glMatrix这个库的时候,函数mat4.multiply = function(mat, mat2, dest)中,虽然传入了mat mat2,实际上内部是mat2乘以mat,这样就奇怪了,如果我调用mat4.multiply(p, v),实际上是v * p,不知道为什么结果确实对的,求解答。mat4.multiply中的实现是这样的。

mat4.multiply = function(mat, mat2, dest) { if(!dest) { dest = mat } // Cache the matrix values (makes for huge speed increases!) var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3]; var a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7]; var a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11]; var a30 = mat[12], a31 = mat[13], a32 = mat[14], a33 = mat[15]; var b00 = mat2[0], b01 = mat2[1], b02 = mat2[2], b03 = mat2[3]; var b10 = mat2[4], b11 = mat2[5], b12 = mat2[6], b13 = mat2[7]; var b20 = mat2[8], b21 = mat2[9], b22 = mat2[10], b23 = mat2[11]; var b30 = mat2[12], b31 = mat2[13], b32 = mat2[14], b33 = mat2[15]; dest[0] = b00*a00 + b01*a10 + b02*a20 + b03*a30; dest[1] = b00*a01 + b01*a11 + b02*a21 + b03*a31; dest[2] = b00*a02 + b01*a12 + b02*a22 + b03*a32; dest[3] = b00*a03 + b01*a13 + b02*a23 + b03*a33; dest[4] = b10*a00 + b11*a10 + b12*a20 + b13*a30; dest[5] = b10*a01 + b11*a11 + b12*a21 + b13*a31; dest[6] = b10*a02 + b11*a12 + b12*a22 + b13*a32; dest[7] = b10*a03 + b11*a13 + b12*a23 + b13*a33; dest[8] = b20*a00 + b21*a10 + b22*a20 + b23*a30; dest[9] = b20*a01 + b21*a11 + b22*a21 + b23*a31; dest[10] = b20*a02 + b21*a12 + b22*a22 + b23*a32; dest[11] = b20*a03 + b21*a13 + b22*a23 + b23*a33; dest[12] = b30*a00 + b31*a10 + b32*a20 + b33*a30; dest[13] = b30*a01 + b31*a11 + b32*a21 + b33*a31; dest[14] = b30*a02 + b31*a12 + b32*a22 + b33*a32; dest[15] = b30*a03 + b31*a13 + b32*a23 + b33*a33; return dest; };

第二个问题是glMatrix的源码中,mat4.translate的实现如下,不知道为什么这么实现,这个意思使用mat * vec矩阵么,求解答,谢谢各位啦。

mat4.translate = function(mat, vec, dest) { var x = vec[0], y = vec[1], z = vec[2]; if(!dest || mat == dest) { mat[12] = mat[0]*x + mat[4]*y + mat[8]*z + mat[12]; mat[13] = mat[1]*x + mat[5]*y + mat[9]*z + mat[13]; mat[14] = mat[2]*x + mat[6]*y + mat[10]*z + mat[14]; mat[15] = mat[3]*x + mat[7]*y + mat[11]*z + mat[15]; return mat; } var a00 = mat[0], a01 = mat[1], a02 = mat[2], a03 = mat[3]; var a10 = mat[4], a11 = mat[5], a12 = mat[6], a13 = mat[7]; var a20 = mat[8], a21 = mat[9], a22 = mat[10], a23 = mat[11]; dest[0] = a00; dest[1] = a01; dest[2] = a02; dest[3] = a03; dest[4] = a10; dest[5] = a11; dest[6] = a12; dest[7] = a13; dest[8] = a20; dest[9] = a21; dest[10] = a22; dest[11] = a23; dest[12] = a00*x + a10*y + a20*z + mat[12]; dest[13] = a01*x + a11*y + a21*z + mat[13]; dest[14] = a02*x + a12*y + a22*z + mat[14]; dest[15] = a03*x + a13*y + a23*z + mat[15]; return dest; };
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all (0)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!