Is it possible to reset matrix in draw loop?

edited March 2017 in Questions about Code

I keep getting the "too many call to popMatrix" message even tough im pretty sure i paired them well and no multithreading involved, also in different executions i either get the error right off the bat or never, im also using awt components mixed in the PApplet.

Its there some way to ensure a clean state of the matrix right at the beginning of draw loop?


            father.clip(posX, posY, w, h);

    for(int i=0;i<childs.size();i++){
        XYEntity child=childs.get(i); 

        father.translate(sliderX, sliderY); 






@ Override
public void render(Container view) {
    father.rect(posX, posY, w, h);
    String fullChat="";

    for(int i=chat.size()>maxMsg?chat.size()-maxMsg:0;i<chat.size();i++){
    father.text(fullChat, posX+margin, posY+margin+size/2,w,h);

I put in the "faulty" code, i see a pair of clip,noClip and a pair of push and pop, i cant understand how possibly i could get an error on the pop, am i missing some implicit pop in my code?


  • Wait a sec, you say "clean state of matrix at the start of draw". But Processing resets the matrix at the start of draw anyway, so it won't matter. You can use ctrl + f dialog box to find all the pushMatrix() calls that you have, then check for popMatrix() calls and see if they match up. If you're using loops, it becomes even more important to check correctly.

  • edited March 2017

    You can reset the matrix but it won't clear the matrix stack. I think the limit on the matrix stack is about 32 but if you exceed that you who hold get too many call to pushMatrix. There is no real solution except to ensure that the number of pushs equals the number of pops.

    When using push and pop matrix I recommend that the 'pair' are in the same method and that there is no possibility of jumping outside the pair scope. Avoid having the push or pop inside a conditional statement or loop unless they are both inside the same scope.

  • edited March 2017

    Besides, like @quark says, if the problem was indeed just filling the stack to the brim, then you'd get "too many calls to pushMatrix". The problem is simply that you've called popMatrix() more times than you've called pushMatrix, hence there's nothing left in the stack to pop out.

  • to pop out ;)

  • edited March 2017

    (Fixed, just a typo due to spellcheck. Thanks)

  • Hi i added the code in a edit

  • I dont get it i keep getting 50% correct runs and 50% fails... is there a way to clear the matrix stack? so i could clear=>push 2/3 times at the beginning of draw loop?

  • actually, which of these two error messages are you getting:

    static final protected String ERROR_PUSHMATRIX_OVERFLOW =
      "Too many calls to pushMatrix().";
    static final protected String ERROR_PUSHMATRIX_UNDERFLOW =
      "Too many calls to popMatrix(), and not enough to pushMatrix().";

    it pays to be EXACT with error messages...

  • If you are getting "too many call to popMatrix" errors then the matrix stack is clear, there is nothing in it to clear, it is empty!!! Try to do some pushes at the beginning of draw will not solve the problem.

    There is NO good solution to this problem except to make sure that the number of pushs == the number of pops.

    The problem might be caused by some conflict between AWT and Processing event handling.

    What is the classes of father and 'child'? The render method suggests that father is an AWT conponent.

  • i too think it may be a problem with openGL, it would be nice to have some way to detect the current matrix depth or to manage it so that the draw loop doesnt cause application crash.

  • father its an instance of PApplet, child its a component that implements render and draws throught the main PApplet instance

  • edited March 2017

    In order to debug your code for Thread concurrency when dealing w/ the main canvas, place println(Thread.currentThread()); at the top of each function which got pushMatrix() or popMatrix() in it.

    If it println() anything other than exactly "Thread[Animation Thread,5,main]", you're mutating the canvas outside the "Animation" Thread! :-O

  • What version of Processing are you using? What IDE are you using to develop the program?

  • Answer ✓

    i too think it may be a problem with openGL

    I didn't say that I thought it was OpenGL.

    PApplet has its own event handling thread which will be separate from the AWT event handling thread. If you try to draw to the PApplet canvas from the AWT event thread then you are likely to have problems.

  • Perhaps, just post the complete sketch.

  • "If you try to draw to the PApplet canvas from the AWT event thread then you are likely to have problems." this is probably it, im using processing 2.2.1, im visualizing PApplet inside an awt Form, is there some way to switch to another standalone solution?

  • @ GoToLoop im getting Thread[Animation Thread,5,main] on all push/pop

Sign In or Register to comment.