Created
February 6, 2013 06:08
-
-
Save rhulha/4720712 to your computer and use it in GitHub Desktop.
NodeInverse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CLASS | |
define(["glMatrix", "Spatial"], function(glmat, Spatial){ | |
// a Node is a Spatial without Mesh data | |
function Node(child) | |
{ | |
Spatial.call(this, false); | |
this.name =""; | |
this.type =""; | |
this.enabled = true; | |
this.mvMatrix = mat4.create(); | |
// children inheret the parent matrix for its rotation and position | |
this.children = []; | |
if( child) | |
this.children.push(child); | |
this.temp2 = mat4.create(); | |
} | |
var p = Node.prototype = Object.create(Spatial.prototype); | |
p.add = function(obj) { | |
this.children.push(obj); | |
return obj; | |
} | |
// overwrite to animate this object | |
p.animate = function(elapsed) | |
{ | |
} | |
p.draw = function(program, parentMVMatrix) | |
{ | |
mat4.set( parentMVMatrix, this.mvMatrix); // copy the mvMatrix, so we don't change the original | |
// funky stuff going on below, I don't know why it is needed, but otherwise ship rotation is wrong | |
// found this code by trial and error | |
mat4.set( this.matrix, this.temp2); | |
mat4.inverse(this.temp2); | |
mat4.copyPosition( this.matrix, this.temp2); | |
mat4.multiply( this.mvMatrix, this.temp2); | |
for (var i = 0; i < this.children.length; i++) | |
{ | |
this.children[i].draw( program, this.mvMatrix); | |
} | |
} | |
return Node; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment