Rotating 3D-objects in PGraphics for hires output

I’m trying to achieve something which should be fairly easy, but after checking the forums I still can’t get it to work: making 3D-objects to rotate in hires imagery in Processing (v3 in this case, v4 doesn’t seem to work) by using a PGraphics object. Everything works, except the rotating part, hence the obvious.
I tried all kinds of variations with begin/endDraw(), rotating the screen instead g, wondering why I have to declare g anyway since it’s a global variable anyway (in JAVA2D mode at least), using another PGraphics object instead of global g, using push/pop etc. I get the hires image as expected, except for the boxes which are fixed in position, not rotating at all. What am I doing wrong?

int w=3000, h=3000;
PGraphics g;

void setup() {
  g = createGraphics(w, h, P3D);
  size(1000, 1000, P3D);
  background(#ffffff);
}

void draw() {
  g.beginDraw();
  g.rotate(.01*frameCount); // this should slowly rotate the boxes, but it doesn't!
  g.fill(#aa33ff+frameCount);
  g.stroke(#ffffff);
  g.strokeWeight(5);
  g.endDraw(); // I only get hires output with end/beginDraw before drawing boxes
  g.beginDraw();
  g.translate(int(random(w)), int(random(h)));
  g.box(random(100));
  g.endDraw();
  image(g, 0, 0, width, height);
}

void keyPressed() {
  if (key==' ') {
    g.save("hires.png");
  }
}

Hello,

Try this:

  g.beginDraw();
  //g.rotate(.01*frameCount); // this should slowly rotate the boxes, but it doesn't!
  g.fill(#aa33ff+frameCount);
  g.stroke(#ffffff);
  g.strokeWeight(5);
  g.endDraw(); // I only get hires output with end/beginDraw before drawing boxes
  
  g.beginDraw();
  //g.rotate(.01*frameCount);
  g.translate(int(random(w)), int(random(h)));
  g.rotate(.01*frameCount);
  g.box(random(100));
  g.endDraw();

  image(g, 0, 0, width, height);

Try rotate before or after translate to see what happens.

Also try above removing the endDraw() / beginDraw() in the middle.

Read these to understand why:

Note: in Processing, the coordinate system is restored to its original state (origin at the upper left of the window, no rotation, and no scaling) every time that the draw() function is executed.

:)

2 Likes

I somehow completely failed to see this - in hindsight - easy solution, but I didn’t. Thanks, this obviously does exactly what is needed!

2 Likes