Search code examples
pythonmatrixprocessing

How do I add a custom matrix to the matrix transformation stack in Python Processing?


I am working with Python Processing. I have a custom matrix that I'd like to add to the current matrix transformation stack without having to first bother to factor the matrix into rotateX() rotateY() etc.

How can I specify my own matrix and apply it?


Solution

  • The best way to answer questions like this is to look in the reference.

    Sounds like you're looking for the applyMatrix() function:

    size(100, 100, P3D);
    noFill();
    translate(50, 50, 0);
    rotateY(PI/6); 
    stroke(153);
    box(35);
    // Set rotation angles
    float ct = cos(PI/9.0);
    float st = sin(PI/9.0);          
    // Matrix for rotation around the Y axis
    applyMatrix(  ct, 0.0,  st,  0.0,
                 0.0, 1.0, 0.0,  0.0,
                 -st, 0.0,  ct,  0.0,
                 0.0, 0.0, 0.0,  1.0);  
    stroke(255);
    box(50);
    

    transformed cube
    (source: processing.org)

    Multiplies the current matrix by the one specified through the parameters. This is very slow because it will try to calculate the inverse of the transform, so avoid it whenever possible. The equivalent function in OpenGL is glMultMatrix().

    The equivalent Processing.py reference is here.