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 & HelpSyntax Questions › Pgraphics 3d rotate
Page Index Toggle Pages: 1
Pgraphics 3d rotate (Read 619 times)
Pgraphics 3d rotate
Dec 1st, 2008, 1:13pm
 
Hi,
I just started with Processing and I have a very simple problem but can't find the solution.

I need to apply a 3d rotate of a PGraphics object but It seems I am missing something. Where's the error?

Here's the code:


PGraphics pg;

int hSize = 500;
int wSize = 500;

void setup() {
 size(wSize, hSize);

 pg = createGraphics(wSize, hSize, P3D);
 pg.background(0);
 
 //Initialize random points on PGraphics object
 pg.beginDraw();
 
 for(int i=0; i<1000; i++) {
   float r = random(255);
   color c1 = color(random(255), random(255), 0);
   pg.stroke(c1);
   pg.point(50+int(random(wSize-100)), 50+int(random(hSize-100)),0);
 }
 pg.endDraw();
}

void draw() {
 background(0);

 pushMatrix();
   pg.rotateX(frameCount);
   pg.rotateY(frameCount);
   pg.rotateZ(frameCount);
   image(pg, 0, 0);
 popMatrix();
}
Re: Pgraphics 3d rotate
Reply #1 - Dec 1st, 2008, 2:32pm
 
try something like that, you have forgot the  beginDraw() and endDraw() methods

 pushMatrix();
>>> pg.beginDraw();
   pg.rotateX(frameCount);
   pg.rotateY(frameCount);
   pg.rotateZ(frameCount);
>>> pg.endDraw();
   image(pg, 0, 0);
 popMatrix();
Re: Pgraphics 3d rotate
Reply #2 - Dec 1st, 2008, 3:11pm
 
Thanks,
I tried but with no success..
I also tried with the translate but it doesn't work.

    pg.translate(frameCount,frameCount,0);

Any more ideas?
Re: Pgraphics 3d rotate
Reply #3 - Dec 1st, 2008, 3:22pm
 
two things.

first of all to see something just have at least to draw it!(don't worry evrybody usually forget this kind of things) Cheesy

second thing you use the background not on pg but on the main canvas.


try this

 for(int i=0; i<1000; i++) {
   float r = random(255);
   color c1 = color(random(255), random(255), 0);
   pg.stroke(c1);
   pg.point(50+int(random(wSize-100)), 50+int(random(hSize-100)),0);
 }
Re: Pgraphics 3d rotate
Reply #4 - Dec 1st, 2008, 4:04pm
 
Thanks again q.
However the PGraphic pg object should be already populated with random colored dots in the setup().
Then I assume that I must have colored object available to traslate or rotate keeping the intially dots drawn on it.

I.e. if I change, in the draw() section, the line 31:
     image(pg, 0, 0);

with
     image(pg, 0, 0,int(500-(frameCount*10)), int(500-(frameCount*10)));

I get a resizing square with the initial dots. Then I think that the PGraphics pg object is like an image where i can draw (in this case in the setup() section). And my problem is that I cannot find the way to apply rotations to it.
Re: Pgraphics 3d rotate
Reply #5 - Dec 1st, 2008, 4:17pm
 
maybe i wrong but i think it reset your object everytime you call draw(), take a look around because i'm not sure, i'm learning processing as well.
Re: Pgraphics 3d rotate
Reply #6 - Dec 1st, 2008, 4:39pm
 
Thanks anyway q,
I will try to find a way out.
Re: Pgraphics 3d rotate
Reply #7 - Dec 1st, 2008, 5:44pm
 
I am not too familiar with 3D in Processing, but I think that when you do pg.rotate*, you move the PGraphics for further drawings, but you don't do them.

I think your should rotate the current graphics canvas before drawing.
I tried with:
Code:

PGraphics pg;
float angle = 0;

void setup() {
 // Should not use parameters there, see reference
 size(500, 500, P3D);

 pg = createGraphics(width, height, JAVA2D);
 pg.background(0);
 
 //Initialize random points on PGraphics object
 pg.beginDraw();
 
 for(int i=0; i<1000; i++) {
   float r = random(255);
   color c1 = color(random(255), random(255), 0);
   pg.stroke(c1);
   pg.point(50+int(random(width-100)), 50+int(random(height-100)));
 }
 pg.endDraw();
}

void draw() {
 background(0);
 angle += PI/60;

 pushMatrix();
   rotateX(angle);
   rotateY(angle);
   rotateZ(angle);
   image(pg, 0, 0);
 popMatrix();
}

I see movement, but it might be wrong (not what you want) because I draw the image on the rotated canvas, making a projection. Or something like that, I am not too sure... Smiley
The correct way of doing that would probably to do a 3D square, apply your random texture to it, and rotate that shape.
Re: Pgraphics 3d rotate
Reply #8 - Dec 1st, 2008, 7:11pm
 
Thanks a lot.
Now I got it. I slightly modified the code so that clicking and dragging with the mouse, the initial surface rotates with the initially randomly plotted dots.

Here's the example code:

Code:

PGraphics pg;

float angleX = 0;
float angleY = 0;

float xcoor=0;
float ycoor=0;

boolean bMouseDown = false;

void setup() {
// Should not use parameters there, see reference
size(500, 500, P3D);

pg = createGraphics(width, height, P3D);
pg.background(0);

//Initialize random points on PGraphics object
pg.beginDraw();

for(int i=0; i<10000; i++) {
float r = random(255);
color c1 = color(random(255), random(255), 0);
pg.stroke(c1);
pg.point(50+int(random(width-100)), 50+int(random(height-100)));
}
pg.endDraw();

}

void draw() {
background(0);

if(bMouseDown){
angleX=angleX+((mouseX-xcoor)*(PI/10000));
angleY=angleY+((mouseY-ycoor)*(PI/10000));
}

pushMatrix();
translate(pg.width/2,pg.height/2);
rotateX(angleY);
rotateZ(angleX);
translate(-pg.width/2,-pg.height/2);
image(pg, 0, 0);
popMatrix();
}

void mousePressed(){
bMouseDown = true;
xcoor=mouseX;
ycoor=mouseY;
}

void mouseReleased(){
bMouseDown = false;
}

Page Index Toggle Pages: 1