Game Development Reference
In-Depth Information
In subsequent chapters, when we will be dealing with complex geometries with
multiple objects, we will use matrix stacks to store the parent object's transformations
before applying transformations to child objects. This way, the child objects will
be rendered with respect to their parent object. The following code implements the
matrix stack:
var matrixStack = [];
function pushMatrix() {
var copy = mat4.create();
mat4.copy(copy,mvMatrix);
matrixStack.push(copy);
}
function popMatrix() {
if (matrixStack.length == 0) {
throw"Invalid popMatrix!";
}
mvMatrix = matrixStack.pop();
}
Also, let's look at how we use this stack in our drawScene function:
function drawScene() {
...........................
for(var i=0;i<stage.stageObjects.length;++i){
..........................
pushMatrix();
mat4.translate(mvMatrix,mvMatrix,
stage.stageObjects[i].location);
mat4.rotateX(mvMatrix,mvMatrix,
stage.stageObjects[i].rotationX);
mat4.rotateY(mvMatrix,mvMatrix,
stage.stageObjects[i].rotationY);
mat4.rotateZ(mvMatrix,mvMatrix,
stage.stageObjects[i].rotationZ);
....//Rendering code(Buffer Activation and drawElements
call................
popMatrix();
}
}
 
Search WWH ::




Custom Search