javascriptlinear-algebragl-matrix

Migrating to from glMatrix 1 to glMatrix 2


I haven't been able to update these functions from an old application using glMatrix 1.2 to glMatrix 2.7:

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

And the following function of multiplying a matrix by a 4-component vector doesn't exist:

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

Solution

  • In common the normal matrix is 3*3 matrix (mat3).

    But anyway, glMatrix is well documented and according to the documentation of mat4 and vec4, your code can be ported like this:

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

    May be it is not necessary to create the vectors in the following, but I don't kow if the vectors are existing in your case:

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