Last active
August 24, 2020 09:21
-
-
Save GoToLoop/193d951ef13f212f09e2b2809efc4de0 to your computer and use it in GitHub Desktop.
Push/Pop Limit Workaround (Python Mode)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
* Push/Pop Limit Workaround (v1.0.5) | |
* GoToLoop (2020/Aug/22) | |
* | |
* Discourse.Processing.org/t/too-many-calls-to-pushmatrix/23411/9 | |
* Gist.GitHub.com/GoToLoop/193d951ef13f212f09e2b2809efc4de0 | |
""" | |
from processing.opengl import PGraphicsOpenGL | |
class MatrixFrameGL: | |
camField = PGraphicsOpenGL.getField('camera') | |
def __init__(self, pg = None): | |
self.modelview = self.modelviewInv = None | |
self.camera = self.cameraInv = None | |
pg and self.pushMatrix(pg) | |
def pushMatrix(self, pg): | |
self.modelview = pg.modelview.get() | |
self.modelviewInv = pg.modelviewInv.get() | |
self.camera = self.camField.get(pg).get() | |
self.cameraInv = pg.cameraInv.get() | |
return self | |
def popMatrix(self, pg): | |
pg.modelview.set(self.modelview) | |
pg.modelviewInv.set(self.modelviewInv) | |
self.camField.get(pg).set(self.camera) | |
pg.cameraInv.set(self.cameraInv) | |
return self | |
def clone(self): | |
frame = MatrixFrameGL() | |
frame.modelview = self.modelview.get() | |
frame.modelviewInv = self.modelviewInv.get() | |
frame.camera = self.camera.get() | |
frame.cameraInv = self.cameraInv.get() | |
return frame |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
* Push/Pop Limit Workaround (v1.0.5) | |
* GoToLoop (2020/Aug/22) | |
* | |
* Discourse.Processing.org/t/too-many-calls-to-pushmatrix/23411/9 | |
* Gist.GitHub.com/GoToLoop/193d951ef13f212f09e2b2809efc4de0 | |
""" | |
from MatrixFrameGL import MatrixFrameGL | |
from collections import deque | |
BOXES, SIZE = 32, 6 | |
MOV, ROT = SIZE << 2, TAU / BOXES | |
frames = deque() | |
def setup(): | |
size(1200, 200, P3D) | |
noLoop() | |
stroke(0xffFFFF00) # yellow | |
def draw(BOXES2 = BOXES * 3 >> 1): | |
clear() | |
translate(0, height >> 2) | |
fill(0xffFF0000); # red | |
recursiveBoxes() # 32 boxes | |
translate(0, height >> 1) | |
fill(0xff0000FF); # blue | |
recursiveBoxes(BOXES2, pg = g, frames = frames) # 48 boxes | |
def recursiveBoxes( | |
qty = BOXES, size = SIZE, moveX = MOV, rot = ROT, | |
pg = this, frames = None): | |
if this.parseInt(qty) <= 0: return | |
pg.pushMatrix() if frames is None else frames.append(MatrixFrameGL(pg)) | |
pg.translate(moveX, 0) | |
pg.rotateX(rot) | |
pg.box(size) | |
recursiveBoxes(qty - 1, size, moveX, rot, pg, frames) | |
pg.popMatrix() if frames is None else frames.pop().popMatrix(pg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment