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);
}