Overv / WebCraft

Minecraft clone written in Javascript.
zlib License
385 stars 183 forks source link

use `mat4.fromRotationTranslation` in `Renderer.prototype.setCamera` #7

Closed 525c1e21-bd67-4735-ac99-b4b0e5262290 closed 4 years ago

525c1e21-bd67-4735-ac99-b4b0e5262290 commented 12 years ago

Currently the code is...

Renderer.prototype.setCamera = function( pos, ang )
{
    var gl = this.gl;

    this.camPos = pos;

    mat4.identity( this.viewMatrix );

    mat4.rotate( this.viewMatrix, -ang[0] - Math.PI / 2, [ 1, 0, 0 ], this.viewMatrix );
    mat4.rotate( this.viewMatrix, ang[1], [ 0, 0, 1 ], this.viewMatrix );
    mat4.rotate( this.viewMatrix, -ang[2], [ 0, 1, 0 ], this.viewMatrix );

    mat4.translate( this.viewMatrix, [ -pos[0], -pos[1], -pos[2] ], this.viewMatrix );

    gl.uniformMatrix4fv( this.uViewMat, false, this.viewMatrix );
}

...although this can be greatly simplified and optimised with mat4.fromRotationTranslation.

Here's the docs from gl-matrix source code FYI

/**
 * Creates a matrix from a quaternion rotation and vector translation
 * This is equivalent to (but much faster than):
 *
 *     mat4.identity(dest);
 *     mat4.translate(dest, vec);
 *     var quatMat = mat4.create();
 *     quat4.toMat4(quat, quatMat);
 *     mat4.multiply(dest, quatMat);
 *
 * @param {quat4} quat Rotation quaternion
 * @param {vec3} vec Translation vector
 * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to a new mat4
 *
 * @returns {mat4} dest if specified, a new mat4 otherwise
 */
mat4.fromRotationTranslation = function (quat, vec, dest) {