Fakultas Ilmu Komputer UI

Commit 95ec11b5 authored by Albertus Angga's avatar Albertus Angga
Browse files

Fix parent child bugs and connect parent-child matrix

parent 97caa2f9
......@@ -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);
}
......
......@@ -157,6 +157,7 @@ function initObjects() {
});
if (!model.node.hasParent) rootNodes.push(model.node);
});
rootNodes.forEach((rootNode) => rootNode.updateTransformations());
}
function initCanvasAndGL() {
......
......@@ -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()
});
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment