How to display multiple 3D views?

edited May 2014 in How To...

Hi. Is it possible to use more than one camera in 3D? And is it possible without drawing all the shapes again for each camera?

I tried redrawing everything four times:

PGraphics[] pg;
void setup() {
  size(1000, 800, P3D);
  pg = new PGraphics[4];
  for(int i=0; i<pg.length; i++) {
    pg[i] = createGraphics(width/2, height/2, P3D);
  }
}
void draw() {
  for(int i=0; i<pg.length; i++) {
    PGraphics p = pg[i];
    p.beginDraw();
    p.background(80);
    p.lights();
    p.translate(p.width/2, p.height/2);
    p.rotateY((i+1)*frameCount*0.0011);
    p.rotateX((i+1)*frameCount*0.0013);
    p.stroke(255, 30);
    p.fill(#FF0000);
    p.box(200, 200, 10);
    p.fill(#00FF00);
    p.box(200, 10, 200);
    p.fill(#0000FF);
    p.box(10, 200, 200);
    p.endDraw();
    image(p, (i % 2) * width/2, (i/2) * height/2); 
  }
}

But the z-indexes are messed up. Last shape (the blue one) is always on top. There should be 3 crossing planes. Also I think lights() is not working.

2353

I'm on Ubuntu, old Intel graphics. Maybe it works on newer systems?

Thanks!

Tagged:

Answers

  • Answer ✓

    looks perfect on my system.

  • Ok thanks! That means it's my system.

    It would be still nice to know if it's possible to just change the camera without redrawing the objects four times. Like in most 3D programs, where you can see Top, Front, Left and Perspective views of the same scene.

  • edited May 2014

    Just to warn if any1 is using an old Processing version like me (v2.0.2), that I had to place image() in its own loop
    in order not to confuse translation positioning somehow! :|

    It's similar inconsistencies I've encountered in this old thread:
    http://forum.processing.org/two/discussion/3256/performance-issues-with-background

    // forum.processing.org/two/discussion/5054/
    // how-to-display-multiple-3d-views
    
    static final int NUM = 4;
    final PGraphics[] pg = new PGraphics[NUM];
    
    void setup() {
      size(1000, 800, P2D);
    
      for (int i=0; i!=NUM;) {
        final PGraphics p = pg[i++] = 
          createGraphics(width>>1, height>>1, P3D);
    
        p.beginDraw();
        p.stroke(-1, 30);
        p.endDraw();
      }
    }
    
    void draw() {
      background(0);
    
      for (int i=0; i!=NUM; i++) {
        final PGraphics p = pg[i];
    
        p.beginDraw();
        p.background(0100);
        p.lights();
    
        p.translate(p.width>>1, p.height>>1);
        p.rotateY((i+1)*frameCount*11e-4);
        p.rotateX((i+1)*frameCount*13e-4);
    
        p.fill(#FF0000);
        p.box(200, 200, 10);
        p.fill(#00FF00);
        p.box(200, 10, 200);
        p.fill(#0000FF);
        p.box(10, 200, 200);
    
        p.endDraw();
      }
    
      for (int i=0; i!=NUM; i++)
        image(pg[i], (i&1)*width >> 1, (i>>1)*height >> 1);
    }
    
  • Hi @GoToLoop, this is not related to the original question, but have you measured execution time improvements related to all the changes you did to the example above? (bit shifting instead of division, using not equals instead of less than, using static finals, etc?) I've often read that the current java compiler does already do those things automatically, and I could not detect improvements when I measured execution time. Those changes had a positive side effect thought. They made me smile :)

  • I've often read that the current java compiler does already do those things automatically,...

    While a program is running, Java's runtime continuously analyzes the code in order to see where it can be optimized.
    It might succeed or not though! Either way, I've already got many parts optimized for sure! ;)

    On a side node, there's no way my fixed code for Processing v2.0.2 can be any speedier than yours,
    since I'm using 2 loops in order to avoid the bug! [-(

Sign In or Register to comment.