diff --git a/experiment-3/model.js b/experiment-3/model.js index ccd552e140dddcefa3af82e0abf4fe8cec88fcc2..861ae74485e4ee37e1b4ade70659af9cca096631 100644 --- a/experiment-3/model.js +++ b/experiment-3/model.js @@ -22,7 +22,7 @@ class Model { this.node = ObjectNode.getOrCreate(name).updateWith({ model: this, - parentName: !!parent + parent: !!parentName ? ObjectNode.getOrCreate(parentName) : undefined, }); @@ -63,9 +63,11 @@ class Model { // objectNodesList and objectNameToId is a global variable // I know, it looks dirty to call those variables here // but this is the fastest way to call it. - console.log(this.name); - if (this.node.hasParent) { - var parentNode = thihs.node.parent; + + // If the node already has parent and the model is already initialized + if (this.node.hasParent && !!this.node.parent.model) { + var parentNode = this.node.parent; + console.log('parent: ', parentNode); var parentMatrix = parentNode.model.fullTransformMatrix; this.fullTransformMatrix = m4.multiply( parentMatrix, @@ -138,7 +140,7 @@ class Model { gl.uniformMatrix4fv( gl.modelMatrixLoc, false, - flatten(this.transformationMatrix) + flatten(this.fullTransformMatrix) ); gl.drawArrays(gl.TRIANGLES, this.bufferStartIndex, this.vertexCount); } diff --git a/experiment-3/no-2.js b/experiment-3/no-2.js index 272c0b6801e3bbb060fac1105a4ef6acbb15aba9..4edbaa1ccfe885b5b114cf6be1985b230e946265 100644 --- a/experiment-3/no-2.js +++ b/experiment-3/no-2.js @@ -157,6 +157,7 @@ function initObjects() { }); if (!model.node.hasParent) rootNodes.push(model.node); }); + rootNodes.forEach((rootNode) => rootNode.updateTransformations()); } function initCanvasAndGL() { diff --git a/experiment-3/object-node.js b/experiment-3/object-node.js index fae07261bf7af050e7a83e388165e7972e956369..feb0aa3eeddd9101cb300898f30030b9101c0941 100644 --- a/experiment-3/object-node.js +++ b/experiment-3/object-node.js @@ -10,7 +10,7 @@ class ObjectNode { this.key = key; this.parent = null; this.children = []; - this.model = {}; + this.model = null; } updateWith({ model, parent }) { @@ -36,4 +36,11 @@ class ObjectNode { this.model.render(gl); this.children.forEach(child => child.render(gl)); } + + updateTransformations() { + this.model.updateMatrices(); + this.children.forEach(children => { + children.updateTransformations() + }); + } }