We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Offscreen 3D contexts
Page Index Toggle Pages: 1
Offscreen 3D contexts (Read 318 times)
Offscreen 3D contexts
Apr 14th, 2008, 9:59pm
 
Hi all,
I'm totally new ( < 1 week ) to processing, and I'm having a blast so far.  I have a question, however, that I've been unable to find a clue for after a couple days of research.  

I'm working on a program that creates an algorithmic drawing, which is different each time, based on randomness.  What I would like to do is to draw the shape, and then rotate it in all three dimensions.  I think I've gleaned enough about this subject to know that I'll have to draw it into an offscreen context, and then use image() to render that context into my drawing window.  Am i correct?

If I am, then as a pedagogical step, I've been trying to make this happen with the RGBCube example.  If someone can help me with this, I think I can figure out my problem.  Thanks in advance!

Code:

/**
* RGB Cube. - altered to try to make it happen offscreen....
*
*/

float xmag, ymag = 0;
float newXmag, newYmag = 0;
PGraphics pg;

void setup()
{
 size(200, 200);
 noStroke();
 colorMode(RGB, 1);
 pg = createGraphics(200, 200, P3D);
 pg.background(0.5, 0.5, 0.45);
 pg.translate(width/2, height/2, -30);
 pg.beginShape(QUADS);
 pg.fill(0, 1, 1); pg.vertex(-1,  1,  1);
 pg.fill(1, 1, 1); pg.vertex( 1,  1,  1);
 pg.fill(1, 0, 1); pg.vertex( 1, -1,  1);
 pg.fill(0, 0, 1); pg.vertex(-1, -1,  1);

 pg.fill(1, 1, 1); pg.vertex( 1,  1,  1);
 pg.fill(1, 1, 0); pg.vertex( 1,  1, -1);
 pg.fill(1, 0, 0); pg.vertex( 1, -1, -1);
 pg.fill(1, 0, 1); pg.vertex( 1, -1,  1);

 pg.fill(1, 1, 0); pg.vertex( 1,  1, -1);
 pg.fill(0, 1, 0); pg.vertex(-1,  1, -1);
 pg.fill(0, 0, 0); pg.vertex(-1, -1, -1);
 pg.fill(1, 0, 0); pg.vertex( 1, -1, -1);

 pg.fill(0, 1, 0); pg.vertex(-1,  1, -1);
 pg.fill(0, 1, 1); pg.vertex(-1,  1,  1);
 pg.fill(0, 0, 1); pg.vertex(-1, -1,  1);
 pg.fill(0, 0, 0); pg.vertex(-1, -1, -1);

 pg.fill(0, 1, 0); pg.vertex(-1,  1, -1);
 pg.fill(1, 1, 0); pg.vertex( 1,  1, -1);
 pg.fill(1, 1, 1); pg.vertex( 1,  1,  1);
 pg.fill(0, 1, 1); pg.vertex(-1,  1,  1);

 pg.fill(0, 0, 0); pg.vertex(-1, -1, -1);
 pg.fill(1, 0, 0); pg.vertex( 1, -1, -1);
 pg.fill(1, 0, 1); pg.vertex( 1, -1,  1);
 pg.fill(0, 0, 1); pg.vertex(-1, -1,  1);

 pg.endShape();
 pg.scale(50);
}

void draw()
{
 newXmag = mouseX/float(width) * TWO_PI;
 newYmag = mouseY/float(height) * TWO_PI;
 
 float diff = xmag-newXmag;
 if (abs(diff) >  0.01) { xmag -= diff/4.0; }
 
 diff = ymag-newYmag;
 if (abs(diff) >  0.01) { ymag -= diff/4.0; }
 
 pg.rotateX(-ymag);
 pg.rotateY(-xmag);
 
 image(pg, 0, 0);
 scale(50);
 
}

Re: Offscreen 3D contexts
Reply #1 - Apr 14th, 2008, 10:25pm
 
Unfortunately, that won't do what you want it to do. I think you have some incorrect assumptions about how things work.

What your code does is create the image of a cube on a PGraphics instance. It *DOESN'T* create a 3D world which you can them view from different angles. So once you've drawn your cube on your pg object, it's just 2d pixels, and no amount of rotates or the like will change that. Once it's drawn, it's drawn.

To do what I think you're trying to do, I think you need to generate an intermediate set of data from your algorithm, and then visualise the data.

I'm not sure what you're using to draw, but if it's a series of cubes say, you can get your algorithm to populate some arrays of positions or the like, and then draw them interactively. That way your algorithm can be random, but you store the result in a way you can view from different angles.
Re: Offscreen 3D contexts
Reply #2 - Apr 14th, 2008, 10:51pm
 
Gotcha, John, thanks so much for your reply.  You describe my misconception perfectly.  I think I had figured as much... but no harm in asking.  I will do exactly what you describe, making a descriptive data set as the drawing is made so that it can be regenerated as the perspective is rotated.

Thanks again!
Page Index Toggle Pages: 1