Results 1 to 2 of 2

Thread: confused about push/pop/uniform

  1. #1
    Junior Member
    Join Date
    Aug 2011

    confused about push/pop/uniform

    I am new here so hello everybody!
    I try to describe my problem even if my english skills are moderate. I am a bit confused about the matrix-functions in my project. I use mvPushMatrix, mvPopMatrix and setMatrixUniforms like seen in the "learning WebGL" lessons.
    Code :
    var mvMatrix = mat4.create();
    var pMatrix = mat4.create();
    var mvMatrixStack = [];
    function mvPushMatrix() {
    	var copy = mat4.create();
    	mat4.set(mvMatrix, copy);
    function mvPopMatrix() {
    	if(mvMatrixStack.length == 0) {
    		throw "Invalid popMatrix!";
    	mvMatrix = mvMatrixStack.pop();
    function setMatrixUniforms() {
    	gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
    	gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);

    I like to draw a simple car and some tires. At first I draw the carriage of the car.
    Then I translate the modelview-matrix to positioning the tires. As you can see in my example,
    I dont realy understand how to call push, pop or uniform. Thats because I am not too familiar with these
    methods. In my understanding I have to push a matrix for every tire, uniform it, translate it, draw it and pop it back.
    Maybe I am wrong.

    Code :
    this.draw = function(){
    	// draw carriage
    	mat4.translate(mvMatrix, [0,0,0]);, this.carPositionBuffer);, this.carPositionBuffer.itemSize,, false, 0, 0);
    	gl.bindBuffer(gl.ARRAY_BUFFER, this.carColorBuffer);, this.carColorBuffer.itemSize,, false, 0, 0);, 0, this.carPositionBuffer.numItems);
    	// draw tires
    	var idx;
    	for(idx = 0; idx<4; idx++){
    		if(idx==0) mat4.translate(mvMatrix, [1.4,	0,	1]); 
    		if(idx==1) mat4.translate(mvMatrix, [2,	0,	2	]);
    		if(idx==2) mat4.translate(mvMatrix, [3,	0,	3	]);
    		if(idx==3) mat4.translate(mvMatrix, [4,	0,	4	]);, this.tirePositionBuffer);, this.tirePositionBuffer.itemSize,, false, 0, 0);
    		gl.bindBuffer(gl.ARRAY_BUFFER, this.tireColorBuffer);, this.tireColorBuffer.itemSize,, false, 0, 0);, 0, this.tirePositionBuffer.numItems);

    Here is a link to my project:
    Could anybody try to explain what my missunderstanding is? Or does anybody have can tell me where I can find a tutorial about push/pop/uniform?
    That would be to nice! Thank you!

  2. #2
    Junior Member
    Join Date
    Dec 2011
    Kingston, Ontario, Canada

    Re: confused about push/pop/uniform

    You do not need to push and pop the Matrix unless you need to return back to the previous matrix (position).

    Consider your car example: You have the body and 4 tires. You could draw this by rendering the body then (without a push) moving to the first tire, rendering the first tire, moving from the first tire to the second tire, rendering the second tire, and so on through the third and fourth tire. If you do this then your tires are "linked" to each other. If you adjust the position of the first tire it will cause the other tires to also adjust. This may be fine for the tires but, for example, if your car spoiler was an individually rendered part then you would not want it linked to the tires because if the tires are moved then you don't necessarily want to move the spoiler. In that case you can use the Push and Pop matrix command. You would draw your car body, then push the matrix to basically store the position that you were at, then you would translate (move) to the position of the first tire and render it. Next, instead of continuing to the next tire, you Pop the matrix to return back to the position before you drew the first tire. Now you Push the matrix again (to store the position) and translate to the tire 2 position, render it and then Pop the matrix to return back to your default position. By doing this for each part the components (e.g. tires, spoiler, etc) become independent. If you now change the position of one tire (e.g change the translate amount before rendering the first tire) it will have no effect on the rest of the components.

    Basically the Pop matrix allows you to remember the current position & rotation so that you can return back to it without having to issue reverse translations and reverse rotations. As such there is no hard fast rule on if you should use them or depends on your implementation and if you want components to be independent of each other or not.

    Another scenario with your car example. Say you are going to animate your car by spinning the tires. This will require a rotate command before each tire. If you use a push before each tire and a pop after each tire then all the tires will be independent so you can rotate each at will. If however you just translated between the 4 tires then rotating one tire would cause the other 3 tires to rotate out of place because they would be all rotating around the first tire axis. This, in some cases, may be desirable. I have a nice WebGL example on my webpage ( which uses my Joint And Object Framework (basically some Javascript code to make loading object and bending them easier) to display a human figure. In such a case, when someone moves, for example, the shoulder joint then you want the rest of the arm to also rotate (and thus there would be no Pop matrix command to return back to the previous settings which would make the objects independent).

    So you can see that depending on what you are trying to do, you either need to use the Push and Pop commands or you don't.
    "We often compare ourselves to the U.S.
    and often they come out the best,
    but they only have the right to bear arms
    while we have the right to bare breasts"
    Song "Busting The Breast" by Bowser & Blue

Similar Threads

  1. Confused about when OMX_AllocateBuffer should be called?
    By guoyin.chen in forum OpenMAX IL - Discussion and questions about OpenMAX IL interface
    Replies: 3
    Last Post: 10-24-2008, 08:59 AM
  2. ok now i'm confused ...
    By b2tehrizzo in forum OpenVG and VGU
    Replies: 3
    Last Post: 03-03-2007, 03:49 AM
  3. Confused about when OMX_AllocateBuffer should be called?
    By guoyin.chen in forum OpenMAX IL - Discussion and questions about OpenMAX IL interface
    Replies: 3
    Last Post: 01-01-1970, 12:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts