javascript - 从 glMatrix 1 迁移到 glMatrix 2

标签 javascript linear-algebra gl-matrix

我无法将这些函数从使用 glMatrix 1.2 的旧应用程序更新到 glMatrix 2.7:

calculateNormal() {
    mat4.identity(this.normalMatrix);
    mat4.set(this.modelViewMatrix, this.normalMatrix);
    mat4.inverse(this.normalMatrix);
    mat4.transpose(this.normalMatrix);
}

并且不存在以下将矩阵乘以 4 分量向量的函数:

calculateOrientation() {
    mat4.multiplyVec4(this.matrix, [1, 0, 0, 0], this.right);
    mat4.multiplyVec4(this.matrix, [0, 1, 0, 0], this.up);
    mat4.multiplyVec4(this.matrix, [0, 0, 1, 0], this.normal);
}

最佳答案

普通矩阵通常是 3*3 矩阵 ( mat3 )。

但无论如何,glMatrix有据可查,并根据 mat4 的文档和 vec4 ,您的代码可以这样移植:

calculateNormal() {
    this.normalMatrix = mat4.clone(this.modelViewMatrix);
    mat4.invert(this.normalMatrix, this.normalMatrix);
    mat4.transpose(this.normalMatrix, this.normalMatrix);
}

可能没有必要创建下面的向量,但我不知道你的情况下是否存在向量:

calculateOrientation() {

    this.right = vec4.create();
    vec4.set( this.right, 1, 0, 0, 0 );
    vec4.transformMat4( this.right, this.right, this.matrix );

    this.up = vec4.create();
    vec4.set( this.up, 0, 1, 0, 0 );
    vec4.transformMat4( this.up, this.up, this.matrix );

    this.normal = vec4.create();
    vec4.set( this.normal, 0, 0, 1, 0 );
    vec4.transformMat4( this.normal, this.normal, this.matrix );
}

关于javascript - 从 glMatrix 1 迁移到 glMatrix 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52901576/

相关文章:

JavaScript - 将日期字符串 yyyyMMddHHmmss 转换为日期对象 yyyy-MM-dd HH :mm:ss

javascript - 如何将物联网数据发送到Oracle Iot云服务?

python - 有没有办法防止 numpy.linalg.svd 内存不足?

javascript - WebGL,gl 矩阵。如何从相机 View 和投影矩阵中获取平截头体顶点?

c++ - 使用 SDL2 和 OpenGL 旋转相机和三角形绘制不会显示任何内容?

php - jquery POST方法回调函数查询

javascript - 在 angularJS 中使用 html ID

multidimensional-array - 如何使用矢量化代码求解许多超定线性方程组?

graphics - 确定点是否在 3D 三角形内部

matrix - 对象旋转时法线旋转错误