diff --git a/src/game-lib-api-matrix4.js b/src/game-lib-api-matrix4.js index 617b5fd..b2a0a98 100644 --- a/src/game-lib-api-matrix4.js +++ b/src/game-lib-api-matrix4.js @@ -1,9 +1,9 @@ /** * Api Matrix 4 - * @param row0 GameLib.API.Quaternion - * @param row1 GameLib.API.Quaternion - * @param row2 GameLib.API.Quaternion - * @param row3 GameLib.API.Quaternion + * @param row0 GameLib.API.Vector4 + * @param row1 GameLib.API.Vector4 + * @param row2 GameLib.API.Vector4 + * @param row3 GameLib.API.Vector4 * @constructor */ GameLib.API.Matrix4 = function ApiMatrix4( @@ -15,42 +15,60 @@ GameLib.API.Matrix4 = function ApiMatrix4( this.rows = []; if (GameLib.Utils.UndefinedOrNull(row0)) { - row0 = new GameLib.API.Quaternion(1, 0, 0, 0); + row0 = new GameLib.API.Vector4(1, 0, 0, 0); } this.rows[0] = row0; if (GameLib.Utils.UndefinedOrNull(row1)) { - row1 = new GameLib.API.Quaternion(0, 1, 0, 0); + row1 = new GameLib.API.Vector4(0, 1, 0, 0); } this.rows[1] = row1; if (GameLib.Utils.UndefinedOrNull(row2)) { - row2 = new GameLib.API.Quaternion(0, 0, 1, 0); + row2 = new GameLib.API.Vector4(0, 0, 1, 0); } this.rows[2] = row2; if (GameLib.Utils.UndefinedOrNull(row3)) { - row3 = new GameLib.API.Quaternion(0, 0, 0, 1); + row3 = new GameLib.API.Vector4(0, 0, 0, 1); } this.rows[3] = row3; + + this.temp = []; + this.temp.push( + new GameLib.API.Vector4() + ); + + this.temp.push( + new GameLib.API.Vector4() + ); + + this.temp.push( + new GameLib.API.Vector4() + ); + + this.temp.push( + new GameLib.API.Vector4() + ); + }; GameLib.API.Matrix4.prototype.rotationMatrixX = function (radians) { this.identity(); - this.rows[1] = new GameLib.API.Quaternion(0, Math.cos(radians), -1 * Math.sin(radians), 0); - this.rows[2] = new GameLib.API.Quaternion(0, Math.sin(radians), Math.cos(radians), 0); + this.rows[1] = new GameLib.API.Vector4(0, Math.cos(radians), -1 * Math.sin(radians), 0); + this.rows[2] = new GameLib.API.Vector4(0, Math.sin(radians), Math.cos(radians), 0); return this; }; GameLib.API.Matrix4.prototype.rotationMatrixY = function (radians) { this.identity(); - this.rows[0] = new GameLib.API.Quaternion( + this.rows[0] = new GameLib.API.Vector4( Math.cos(radians), 0, Math.sin(radians), 0 ); - this.rows[2] = new GameLib.API.Quaternion( + this.rows[2] = new GameLib.API.Vector4( -1 * Math.sin(radians), 0, Math.cos(radians), @@ -61,8 +79,8 @@ GameLib.API.Matrix4.prototype.rotationMatrixY = function (radians) { GameLib.API.Matrix4.prototype.rotationMatrixZ = function (radians) { this.identity(); - this.rows[0] = new GameLib.API.Quaternion(Math.cos(radians), -1 * Math.sin(radians), 0, 0); - this.rows[1] = new GameLib.API.Quaternion(Math.sin(radians), Math.cos(radians), 0, 0); + this.rows[0] = new GameLib.API.Vector4(Math.cos(radians), -1 * Math.sin(radians), 0, 0); + this.rows[1] = new GameLib.API.Vector4(Math.sin(radians), Math.cos(radians), 0, 0); return this; }; @@ -85,7 +103,7 @@ GameLib.API.Matrix4.prototype.rotateZ = function (radians, point) { }; GameLib.API.Matrix4.prototype.multiply = function (mvp) { - if (mvp instanceof GameLib.API.Quaternion) { + if (mvp instanceof GameLib.API.Quaternion || mvp instanceof GameLib.API.Vector4) { return new GameLib.API.Quaternion( this.rows[0].x * mvp.x + this.rows[0].y * mvp.y + this.rows[0].z * mvp.z + this.rows[0].w * mvp.w, this.rows[1].x * mvp.x + this.rows[1].y * mvp.y + this.rows[1].z * mvp.z + this.rows[1].w * mvp.w, @@ -103,10 +121,10 @@ GameLib.API.Matrix4.prototype.multiply = function (mvp) { GameLib.API.Matrix4.prototype.identity = function () { this.rows = [ - new GameLib.API.Quaternion(1, 0, 0, 0), - new GameLib.API.Quaternion(0, 1, 0, 0), - new GameLib.API.Quaternion(0, 0, 1, 0), - new GameLib.API.Quaternion(0, 0, 0, 1) + new GameLib.API.Vector4(1, 0, 0, 0), + new GameLib.API.Vector4(0, 1, 0, 0), + new GameLib.API.Vector4(0, 0, 1, 0), + new GameLib.API.Vector4(0, 0, 0, 1) ]; }; diff --git a/src/game-lib-component-a.js b/src/game-lib-component-a.js index c6ca7a0..5eb593f 100644 --- a/src/game-lib-component-a.js +++ b/src/game-lib-component-a.js @@ -1,6 +1,14 @@ +/** + * Component Interface + * @param componentType + * @param linkedObjects + * @param loaded (indicates whether the linked Objects for this component has been loaded) + * @constructor + */ GameLib.Component = function( componentType, - linkedObjects + linkedObjects, + loaded ) { this.componentType = componentType; @@ -8,6 +16,11 @@ GameLib.Component = function( linkedObjects = {}; } this.linkedObjects = linkedObjects; + + if (GameLib.Utils.UndefinedOrNull(loaded)) { + loaded = false; + } + this.loaded = loaded; }; GameLib.Component.COMPONENT_TYPE_PATH_FOLLOWING = 0x1; @@ -18,6 +31,7 @@ GameLib.Component.COMPONENT_LOOK_AT = 0x5; GameLib.Component.COMPONENT_CAMERA = 0x6; GameLib.Component.COMPONENT_FOLLOW = 0x7; GameLib.Component.COMPONENT_MESH = 0x8; +GameLib.Component.COMPONENT_SPLINE = 0x9; GameLib.Component.prototype.toApiComponent = function() { return this.id; diff --git a/src/game-lib-d3-api-mesh.js b/src/game-lib-d3-api-mesh.js index 7e15896..65a5ac7 100644 --- a/src/game-lib-d3-api-mesh.js +++ b/src/game-lib-d3-api-mesh.js @@ -19,6 +19,7 @@ * @param localRotation GameLib.API.Vector3 * @param localScale GameLib.API.Vector3 * @param up + * @param modelMatrix GameLib.API.Matrix4 * @constructor */ GameLib.D3.API.Mesh = function( @@ -40,7 +41,8 @@ GameLib.D3.API.Mesh = function( localPosition, localRotation, localScale, - up + up, + modelMatrix ) { GameLib.Component.call( this, @@ -145,6 +147,11 @@ GameLib.D3.API.Mesh = function( up = new GameLib.API.Vector3(0,1,0); } this.up = up; + + if (GameLib.Utils.UndefinedOrNull(modelMatrix)) { + modelMatrix = new GameLib.API.Matrix4(); + } + this.modelMatrix = modelMatrix; }; GameLib.D3.API.Mesh.prototype = Object.create(GameLib.Component.prototype); diff --git a/src/game-lib-d3-api-scene.js b/src/game-lib-d3-api-scene.js index 86afea1..55dd042 100644 --- a/src/game-lib-d3-api-scene.js +++ b/src/game-lib-d3-api-scene.js @@ -14,7 +14,6 @@ * @param shapes GameLib.D3.API.Shape[] * @param cameras * @param activeCameraIndex - * @param splines GameLib.D3.API.Spline[] * @constructor */ GameLib.D3.API.Scene = function( @@ -31,8 +30,7 @@ GameLib.D3.API.Scene = function( entityManager, shapes, cameras, - activeCameraIndex, - splines + activeCameraIndex ) { if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); @@ -103,9 +101,4 @@ GameLib.D3.API.Scene = function( activeCameraIndex = 0; } this.activeCameraIndex = activeCameraIndex; - - if (GameLib.Utils.UndefinedOrNull(splines)) { - splines = []; - } - this.splines = splines; }; diff --git a/src/game-lib-d3-api-spline.js b/src/game-lib-d3-api-spline.js index 3b786d2..a1fa0c5 100644 --- a/src/game-lib-d3-api-spline.js +++ b/src/game-lib-d3-api-spline.js @@ -2,7 +2,7 @@ * API Spline * @param id String * @param name String - * @param vertices GameLib.API.Vector3[] + * @param vertices GameLib.API.Vertex[] * @constructor */ GameLib.D3.API.Spline = function( @@ -10,6 +10,11 @@ GameLib.D3.API.Spline = function( name, vertices ) { + GameLib.Component.call( + this, + GameLib.Component.COMPONENT_SPLINE + ); + if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); } @@ -24,4 +29,7 @@ GameLib.D3.API.Spline = function( vertices = []; } this.vertices = vertices; -}; \ No newline at end of file +}; + +GameLib.D3.API.Spline.prototype = Object.create(GameLib.Component.prototype); +GameLib.D3.API.Spline.prototype.constructor = GameLib.D3.API.Spline; \ No newline at end of file diff --git a/src/game-lib-d3-api-vertex.js b/src/game-lib-d3-api-vertex.js new file mode 100644 index 0000000..1d90746 --- /dev/null +++ b/src/game-lib-d3-api-vertex.js @@ -0,0 +1,21 @@ +/** + * API Vertex + * @param position GameLib.API.Vector3 + * @param boneWeights GameLib.API.BoneWeight[] + * @constructor + */ +GameLib.D3.API.Vertex = function( + position, + boneWeights +) { + + if (GameLib.Utils.UndefinedOrNull(position)) { + position = new GameLib.API.Vector3(); + } + this.position = position; + + if (GameLib.Utils.UndefinedOrNull(boneWeights)) { + boneWeights = []; + } + this.boneWeights = boneWeights; +}; diff --git a/src/game-lib-d3-mesh.js b/src/game-lib-d3-mesh.js index db69397..57dbc6e 100644 --- a/src/game-lib-d3-mesh.js +++ b/src/game-lib-d3-mesh.js @@ -81,8 +81,14 @@ GameLib.D3.Mesh = function RuntimeMesh( this.localScale ); + this.modelMatrix = new GameLib.Matrix4( + graphics, + this, + this.modelMatrix + ); + this.computeNormals = computeNormals; - + this.instance = this.createInstance(false); }; @@ -343,10 +349,25 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { instance.gameLibObject = this; - if (this.parentMesh && this.parentMesh.position) { - this.position.x = this.parentMesh.position.x; - this.position.y = this.parentMesh.position.y; - this.position.z = this.parentMesh.position.z; + if (this.parentMesh && this.parentMesh.loaded) { + + instance.parent = this.parentMesh.instance; + + instance.position.x = this.localPosition.x; + instance.position.y = this.localPosition.y; + instance.position.z = this.localPosition.z; + + instance.rotation.x = this.localRotation.x; + instance.rotation.y = this.localRotation.y; + instance.rotation.z = this.localRotation.z; + + instance.scale.x = this.localScale.x; + instance.scale.y = this.localScale.y; + instance.scale.z = this.localScale.z; + + // this.position.x = this.parentMesh.position.x; + // this.position.y = this.parentMesh.position.y; + // this.position.z = this.parentMesh.position.z; // var euler = new THREE.Euler(); // @@ -363,26 +384,24 @@ GameLib.D3.Mesh.prototype.createInstance = function(update) { instance.quaternion.y = this.quaternion.y; instance.quaternion.z = this.quaternion.z; instance.quaternion.w = this.quaternion.w; + + instance.position.x = this.position.x + this.localPosition.x; + instance.position.y = this.position.y + this.localPosition.y; + instance.position.z = this.position.z + this.localPosition.z; + + instance.scale.x = this.scale.x * this.localScale.x; + instance.scale.y = this.scale.y * this.localScale.y; + instance.scale.z = this.scale.z * this.localScale.z; + + instance.up.x = this.up.x; + instance.up.y = this.up.y; + instance.up.z = this.up.z; + + instance.rotateX(this.localRotation.x); + instance.rotateY(this.localRotation.y); + instance.rotateZ(this.localRotation.z); } - instance.position.x = this.position.x + this.localPosition.x; - instance.position.y = this.position.y + this.localPosition.y; - instance.position.z = this.position.z + this.localPosition.z; - - instance.scale.x = this.scale.x * this.localScale.x; - instance.scale.y = this.scale.y * this.localScale.y; - instance.scale.z = this.scale.z * this.localScale.z; - - instance.up.x = this.up.x; - instance.up.y = this.up.y; - instance.up.z = this.up.z; - - - - instance.rotateX(this.localRotation.x); - instance.rotateY(this.localRotation.y); - instance.rotateZ(this.localRotation.z); - return instance; }; @@ -411,7 +430,11 @@ GameLib.D3.Mesh.prototype.toApiMesh = function() { this.id, this.meshType, this.name, - this.vertices, + this.vertices.map( + function (vertex) { + return vertex.toApiVertex(); + } + ), this.faces, this.faceVertexUvs, this.materials.map(function(material){return material.toApiMaterial()}), @@ -446,20 +469,9 @@ GameLib.D3.Mesh.FromObjectMesh = function(graphics, objectMesh, computeNormals, objectMesh.name, objectMesh.vertices.map( function (objectVertex) { - return new GameLib.D3.Vertex( - new GameLib.API.Vector3( - objectVertex.position.x, - objectVertex.position.y, - objectVertex.position.z - ), - objectVertex.boneWeights.map( - function (boneWeight) { - return new GameLib.D3.API.BoneWeight( - boneWeight.boneIndex, - boneWeight.weight - ) - } - ) + return GameLib.D3.Vertex.FromObjectVertex( + graphics, + objectVertex ); } ), diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index 2168ebc..6424ce8 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -31,8 +31,7 @@ GameLib.D3.Scene = function Scene( apiScene.entityManager, apiScene.shapes, apiScene.cameras, - apiScene.activeCameraIndex, - apiScene.splines + apiScene.activeCameraIndex ); this.position = new GameLib.Vector3( @@ -67,8 +66,7 @@ GameLib.D3.Scene = function Scene( this.interestingProperties = [ "cameras", "meshes", - "lights", - "splines" + "lights" ]; this.idToObject = {}; @@ -162,12 +160,6 @@ GameLib.D3.Scene.prototype.toApiScene = function() { } ); - var apiSplines = this.splines.map( - function(spline) { - return spline.toApiSpline(); - } - ); - var apiWorlds = this.worlds.map( function(world) { return world.toApiWorld(); @@ -289,15 +281,7 @@ GameLib.D3.Scene.FromObjectScene = function( ); } ), - objectScene.activeCameraIndex, - objectScene.splines.map( - function (objectSpline) { - return GameLib.D3.Spline.FromObjectSpline( - graphics, - objectSpline - ) - } - ) + objectScene.activeCameraIndex ); return new GameLib.D3.Scene( diff --git a/src/game-lib-d3-spline.js b/src/game-lib-d3-spline.js index 79acddd..004ad42 100644 --- a/src/game-lib-d3-spline.js +++ b/src/game-lib-d3-spline.js @@ -4,40 +4,43 @@ * @param apiSpline GameLib.D3.API.Spline * @constructor */ -GameLib.D3.Spline = function Spline( +GameLib.D3.Spline = function RuntimeSpline( graphics, apiSpline ) { - - for (var property in apiSpline) { - if (apiSpline.hasOwnProperty(property)) { - this[property] = apiSpline[property]; - } - } - this.graphics = graphics; this.graphics.isNotThreeThrow(); + GameLib.D3.API.Spline.call( + this, + apiSpline.id, + apiSpline.name, + apiSpline.vertices + ); + this.instance = this.createInstance(); }; +GameLib.D3.Spline.prototype = Object.create(GameLib.D3.API.Spline.prototype); +GameLib.D3.Spline.prototype.constructor = GameLib.D3.Spline; + /** * Creates an instance spline * @param update boolean */ GameLib.D3.Spline.prototype.createInstance = function(update) { - var vertices = []; - - for (var v = 0; v < this.vertices.length; v++) { - vertices.push(new THREE.Vector3( - this.vertices[v].x, - this.vertices[v].y, - this.vertices[v].z - )); + if (update) { + return this.instance; } - return new this.graphics.instance.CatmullRomCurve3(vertices); + var vertices = this.vertices.map( + function (vertex) { + return vertex.instance; + } + ); + + return new THREE.CatmullRomCurve3(vertices); }; /** @@ -66,12 +69,16 @@ GameLib.D3.Spline.prototype.getPointAt = function(proper) { * Converts a GameLib.D3.Spline to GameLib.D3.API.Spline * @returns {GameLib.D3.API.Spline} */ -GameLib.D3.Spline.prototype.toApiSpline = function() { +GameLib.D3.Spline.prototype.toApiComponent = function() { return new GameLib.D3.API.Spline( this.id, this.name, - this.vertices + this.vertices.map( + function (vertex) { + return vertex.toApiVertex() + } + ) ); }; @@ -79,22 +86,26 @@ GameLib.D3.Spline.prototype.toApiSpline = function() { /** * Returns a GameLib.D3.Spline from a spline Object * @param graphics GameLib.D3.Graphics - * @param objectSpline Object + * @param objectComponent Object * @returns {GameLib.D3.Spline} * @constructor */ -GameLib.D3.Spline.FromObjectSpline = function( +GameLib.D3.Spline.FromObjectComponent = function( graphics, - objectSpline + objectComponent ) { - - return new GameLib.D3.Spline( - graphics, - new GameLib.D3.API.Spline( - objectSpline.id, - objectSpline.name, - objectSpline.vertices + var apiSpline = new GameLib.D3.API.Spline( + objectComponent.id, + objectComponent.name, + objectComponent.vertices.map( + function (objectVertex) { + return GameLib.D3.Vertex.FromObjectVertex(graphics, objectVertex); + } ) ); + return new GameLib.D3.Spline( + graphics, + apiSpline + ); }; diff --git a/src/game-lib-d3-vertex.js b/src/game-lib-d3-vertex.js index 68479df..6580deb 100644 --- a/src/game-lib-d3-vertex.js +++ b/src/game-lib-d3-vertex.js @@ -1,13 +1,99 @@ /** - * The normal gets assigned when the face calculates its normal - * @param position - * @param boneWeights GameLib.D3.API.BoneWeight[] + * Runtime Vertex * @constructor + * @param graphics + * @param apiVertex */ GameLib.D3.Vertex = function Vertex( - position, - boneWeights + graphics, + apiVertex ) { - this.position = position; - this.boneWeights = boneWeights; -}; \ No newline at end of file + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + GameLib.D3.API.Vertex.call( + this, + apiVertex.position, + apiVertex.boneWeights + ); + + // this.position = new GameLib.Vector3( + // this.graphics, + // null, + // this.position + // ); + + //TODO: GameLib.D3.BoneWeight implementation + + this.instance = this.createInstance(); +}; + +GameLib.D3.Vertex.prototype = Object.create(GameLib.D3.API.Vertex.prototype); +GameLib.D3.Vertex.prototype.constructor = GameLib.D3.Vertex; + +/** + * Creates an instance vertex + * @param update boolean + */ +GameLib.D3.Vertex.prototype.createInstance = function(update) { + + var instance = null; + + if (update) { + instance = this.instance; + } else { + instance = new THREE.Vector3(); + } + + instance.x = this.position.x; + instance.y = this.position.y; + instance.z = this.position.z; + + return instance; +}; + +/** + * Updates the instance + */ +GameLib.D3.Vertex.prototype.updateInstance = function() { + this.instance = this.createInstance(true); +}; + +/** + * Converts a GameLib.D3.Vertex to GameLib.D3.API.Vertex + * @returns {GameLib.D3.API.Vertex} + */ +GameLib.D3.Vertex.prototype.toApiVertex = function() { + + return new GameLib.D3.API.Vertex( + this.position.toApiVector(), + this.boneWeights + ); + +}; + +/** + * Returns a GameLib.D3.Vertex from a vertex Object + * @param graphics GameLib.D3.Graphics + * @param objectVertex Object + * @returns {GameLib.D3.Vertex} + * @constructor + */ +GameLib.D3.Vertex.FromObjectVertex = function( + graphics, + objectVertex +) { + var apiVertex = new GameLib.D3.API.Vertex( + new GameLib.API.Vector3( + objectVertex.position.x, + objectVertex.position.y, + objectVertex.position.z + ), + objectVertex.boneWeights + ); + + return new GameLib.D3.Vertex( + graphics, + apiVertex + ); +}; diff --git a/src/game-lib-entity-manager.js b/src/game-lib-entity-manager.js index fedb0d9..c737a3c 100644 --- a/src/game-lib-entity-manager.js +++ b/src/game-lib-entity-manager.js @@ -185,6 +185,8 @@ GameLib.EntityManager.FromObjectEntityManager = function(graphics, objectEntitie return GameLib.D3.Follow.FromObjectComponent(graphics, component); } else if (component.componentType === GameLib.Component.COMPONENT_MESH) { return GameLib.D3.Mesh.FromObjectComponent(graphics, component); + } else if (component.componentType === GameLib.Component.COMPONENT_SPLINE) { + return GameLib.D3.Spline.FromObjectComponent(graphics, component); } else { console.warn('no component was associated with this object'); throw new Error('no component was associated with this object'); @@ -245,7 +247,9 @@ GameLib.EntityManager.prototype.linkObjects = function(idToObject) { function (propertyName) { array[index][propertyName] = idToObject[array[index][propertyName]]; } - ) + ); + + array[index].loaded = true; } ) } diff --git a/src/game-lib-matrix-4.js b/src/game-lib-matrix-4.js index d9de041..55756b5 100644 --- a/src/game-lib-matrix-4.js +++ b/src/game-lib-matrix-4.js @@ -13,13 +13,13 @@ GameLib.Matrix4 = function( grain ) { - for (var property in apiMatrix4) { - if (apiMatrix4.hasOwnProperty(property)) { - this[property] = apiMatrix4[property]; - } - } - - GameLib.Utils.Extend(GameLib.Matrix4, GameLib.API.Matrix4); + GameLib.API.Matrix4.call( + this, + apiMatrix4.rows[0], + apiMatrix4.rows[1], + apiMatrix4.rows[2], + apiMatrix4.rows[3] + ); this.graphics = graphics; @@ -32,28 +32,28 @@ GameLib.Matrix4 = function( } this.grain = grain; - this.rows[0] = new GameLib.Quaternion( + this.rows[0] = new GameLib.Vector4( this.graphics, this, this.rows[0], grain ); - this.rows[1] = new GameLib.Quaternion( + this.rows[1] = new GameLib.Vector4( this.graphics, this, this.rows[1], grain ); - this.rows[2] = new GameLib.Quaternion( + this.rows[2] = new GameLib.Vector4( this.graphics, this, this.rows[2], grain ); - this.rows[3] = new GameLib.Quaternion( + this.rows[3] = new GameLib.Vector4( this.graphics, this, this.rows[3], @@ -63,6 +63,9 @@ GameLib.Matrix4 = function( this.instance = this.createInstance(); }; +GameLib.Matrix4.prototype = Object.create(GameLib.API.Matrix4.prototype); +GameLib.Matrix4.prototype.constructor = GameLib.Matrix4; + /** * Creates a matrix 4 instance (currently from graphics lib) * @param update @@ -79,22 +82,26 @@ GameLib.Matrix4.prototype.createInstance = function(update) { instance = new THREE.Matrix4(); } + /** + * We transpose our matrix when we send it to three since we use a different ordering system + * They say they use + */ instance.set( this.rows[0].x, - this.rows[0].y, - this.rows[0].z, - this.rows[0].w, this.rows[1].x, - this.rows[1].y, - this.rows[1].z, - this.rows[1].w, this.rows[2].x, - this.rows[2].y, - this.rows[2].z, - this.rows[2].w, this.rows[3].x, + this.rows[0].y, + this.rows[1].y, + this.rows[2].y, this.rows[3].y, + this.rows[0].z, + this.rows[1].z, + this.rows[2].z, this.rows[3].z, + this.rows[0].w, + this.rows[1].w, + this.rows[2].w, this.rows[3].w ); @@ -115,42 +122,16 @@ GameLib.Matrix4.prototype.updateInstance = function() { } }; -// GameLib.Matrix4.prototype.lookAt = function (position, target, up) { -// -// this.instance.lookAt(position.instance, target.instance, up.instance); -// -// this.rows[0].x = this.instance.elements[0]; -// this.rows[0].y = this.instance.elements[1]; -// this.rows[0].z = this.instance.elements[2]; -// this.rows[0].w = this.instance.elements[3]; -// -// this.rows[1].x = this.instance.elements[4]; -// this.rows[1].y = this.instance.elements[5]; -// this.rows[1].z = this.instance.elements[6]; -// this.rows[1].w = this.instance.elements[7]; -// -// this.rows[2].x = this.instance.elements[8]; -// this.rows[2].y = this.instance.elements[9]; -// this.rows[2].z = this.instance.elements[10]; -// this.rows[2].w = this.instance.elements[11]; -// -// this.rows[3].x = this.instance.elements[12]; -// this.rows[3].y = this.instance.elements[13]; -// this.rows[3].z = this.instance.elements[14]; -// this.rows[3].w = this.instance.elements[15]; -// -// }; - /** * GameLib.Matrix4 to GameLib.API.Matrix4 * @returns {*} */ GameLib.Matrix4.prototype.toApiMatrix = function () { return new GameLib.API.Matrix4( - this.rows[0].toApiQuaternion(), - this.rows[1].toApiQuaternion(), - this.rows[2].toApiQuaternion(), - this.rows[3].toApiQuaternion() + this.rows[0].toApiVector(), + this.rows[1].toApiVector(), + this.rows[2].toApiVector(), + this.rows[3].toApiVector() ) }; @@ -164,25 +145,25 @@ GameLib.Matrix4.prototype.toApiMatrix = function () { */ GameLib.Matrix4.FromObjectMatrix = function(graphics, objectMatrix, parentObject) { var apiMatrix = new GameLib.API.Matrix4( - new GameLib.API.Quaternion( + new GameLib.API.Vector4( objectMatrix[0], objectMatrix[1], objectMatrix[2], objectMatrix[3] ), - new GameLib.API.Quaternion( + new GameLib.API.Vector4( objectMatrix[4], objectMatrix[5], objectMatrix[6], objectMatrix[7] ), - new GameLib.API.Quaternion( + new GameLib.API.Vector4( objectMatrix[8], objectMatrix[9], objectMatrix[10], objectMatrix[11] ), - new GameLib.API.Quaternion( + new GameLib.API.Vector4( objectMatrix[12], objectMatrix[13], objectMatrix[14], @@ -199,59 +180,110 @@ GameLib.Matrix4.FromObjectMatrix = function(graphics, objectMatrix, parentObject GameLib.Matrix4.prototype.lookAt = function (position, target, up) { - this.instance.lookAt(position.instance, target.instance, up.instance); + var pv = new GameLib.API.Vector3(position.x, position.y, position.z); - this.rows[0].x = this.instance.elements[0]; - this.rows[0].y = this.instance.elements[1]; - this.rows[0].z = this.instance.elements[2]; - this.rows[0].w = this.instance.elements[3]; + var z = pv.subtract(target).normalize(); - this.rows[1].x = this.instance.elements[4]; - this.rows[1].y = this.instance.elements[5]; - this.rows[1].z = this.instance.elements[6]; - this.rows[1].w = this.instance.elements[7]; + if (z.squared() === 0) { + z.z = 1; + } - this.rows[2].x = this.instance.elements[8]; - this.rows[2].y = this.instance.elements[9]; - this.rows[2].z = this.instance.elements[10]; - this.rows[2].w = this.instance.elements[11]; + var x = up.cross(z).normalize(); - this.rows[3].x = this.instance.elements[12]; - this.rows[3].y = this.instance.elements[13]; - this.rows[3].z = this.instance.elements[14]; - this.rows[3].w = this.instance.elements[15]; + if (x.squared() === 0) { + z.x += 0.0001; + x = up.cross(z).normalize(); + } + var y = z.cross(x); - // var pv = new GameLib.API.Vector3(position.x, position.y, position.z); - // - // var z = pv.subtract(target).normalize(); - // - // if (z.squared() === 0) { - // z.z = 1; - // } - // - // var x = up.cross(z).normalize(); - // - // if (x.squared() === 0) { - // z.x += 0.0001; - // x = up.cross(z).normalize(); - // } - // - // var y = z.cross(x); - // - // this.rows[0].x = x.x; - // this.rows[0].y = x.y; - // this.rows[0].z = x.z; - // - // this.rows[1].x = y.x; - // this.rows[1].y = y.y; - // this.rows[1].z = y.z; - // - // this.rows[2].x = z.x; - // this.rows[2].y = z.y; - // this.rows[2].z = z.z; - // - // this.updateInstance(); + this.rows[0].x = x.x; + this.rows[0].y = x.y; + this.rows[0].z = x.z; + + this.rows[1].x = y.x; + this.rows[1].y = y.y; + this.rows[1].z = y.z; + + this.rows[2].x = z.x; + this.rows[2].y = z.y; + this.rows[2].z = z.z; + + this.updateInstance(); + + return this; +}; + +GameLib.Matrix4.prototype.identity = function () { + this.rows = [ + new GameLib.Vector4( + this.graphics, + this, + new GameLib.API.Vector4(1,0,0,0), + this.grain + ), + new GameLib.Vector4( + this.graphics, + this, + new GameLib.API.Vector4(0,1,0,0), + this.grain + ), + new GameLib.Vector4( + this.graphics, + this, + new GameLib.API.Vector4(0,0,1,0), + this.grain + ), + new GameLib.Vector4( + this.graphics, + this, + new GameLib.API.Vector4(0,0,0,1), + this.grain + ) + ]; +}; + +GameLib.Matrix4.prototype.transpose = function () { + + this.temp[0].x = this.rows[0].x; + this.temp[0].y = this.rows[1].x; + this.temp[0].z = this.rows[2].x; + this.temp[0].w = this.rows[3].x; + + this.temp[1].x = this.rows[0].y; + this.temp[1].y = this.rows[1].y; + this.temp[1].z = this.rows[2].y; + this.temp[1].w = this.rows[3].y; + + this.temp[2].x = this.rows[0].z; + this.temp[2].y = this.rows[1].z; + this.temp[2].z = this.rows[2].z; + this.temp[2].w = this.rows[3].z; + + this.temp[3].x = this.rows[0].w; + this.temp[3].y = this.rows[1].w; + this.temp[3].z = this.rows[2].w; + this.temp[3].w = this.rows[3].w; + + this.rows[0].x = this.temp[0].x; + this.rows[0].y = this.temp[0].y; + this.rows[0].z = this.temp[0].z; + this.rows[0].w = this.temp[0].w; + + this.rows[1].x = this.temp[1].x; + this.rows[1].y = this.temp[1].y; + this.rows[1].z = this.temp[1].z; + this.rows[1].w = this.temp[1].w; + + this.rows[2].x = this.temp[2].x; + this.rows[2].y = this.temp[2].y; + this.rows[2].z = this.temp[2].z; + this.rows[2].w = this.temp[2].w; + + this.rows[3].x = this.temp[3].x; + this.rows[3].y = this.temp[3].y; + this.rows[3].z = this.temp[3].z; + this.rows[3].w = this.temp[3].w; return this; };