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 › 3d program: rotation, translation, projection..
Page Index Toggle Pages: 1
3d program: rotation, translation, projection.. (Read 1417 times)
3d program: rotation, translation, projection..
Apr 2nd, 2010, 6:27am
 
hello

I'm not sure whether I'm right here. It's not my mother tongue. Sorry for that.

I want to develop a 3d program. there I would like to program a coordinate system, that I can rotate, translate and project.

I have programed this code, but it doesn't work. when I project with "6" it isn't right. when I resize the window, the projection also doesn't work. I haven't got an idea how to do that. can anyone help me? sorry, I'm a beginner..

Code:
float rotx=.1, roty=.1;
int achl=600;
int a=100, offx=0, offy=0,offz=0;
int transx, transy, transz=-300;

void setup()
{
 size(1000,800, P3D);
 frame.setResizable(true); // Fenster kann manuell vom Benutzer verändert werden
 stroke(0);
 strokeWeight(1);
 strokeJoin(MITER);
 transx=width/2;
 transy=height/2;
}


void draw()
{
 background(226);
 
  pushMatrix(); // Koordinatensystem
  translate(transx,transy,transz);
  rotateX(rotx);
  rotateY(roty);
  beginShape(LINES);
   
   stroke(255,0,0);
   vertex(0,0,-achl);
   vertex(0,0,achl);
   
   stroke(0,255,0);
   vertex(0,-achl,0);
   vertex(0,achl,0);
   
   stroke(0,0,255);
   vertex(-achl,0,0);
   vertex(achl,0,0);
  endShape();
  popMatrix();

  pushMatrix(); // Quadrat zum Testen
  translate(transx,transy,transz);
  rotateX(rotx);
  rotateY(roty);
  beginShape(QUADS);
   fill(255,0,0);
   vertex(-a/2+offx,-a/2+offy,a/2+offz);
   vertex(-a/2+offx,a/2+offy,a/2+offz);
   vertex(a/2+offx,a/2+offy,a/2+offz);
   vertex(a/2+offx,-a/2+offy,a/2+offz);
   
   fill(0,255,0);
   vertex(-a/2+offx,-a/2+offy,a/2+offz);
   vertex(a/2+offx,-a/2+offy,a/2+offz);
   vertex(a/2+offx,-a/2+offy,-a/2+offz);
   vertex(-a/2+offx,-a/2+offy,-a/2+offz);
   
   fill(0,0,255);
   vertex(-a/2+offx,-a/2+offy,-a/2+offz);
   vertex(a/2+offx,-a/2+offy,-a/2+offz);
   vertex(a/2+offx,a/2+offy,-a/2+offz);
   vertex(-a/2+offx,a/2+offy,-a/2+offz);
   
   fill(255,255,0);
   vertex(-a/2+offx,a/2+offy,-a/2+offz);
   vertex(a/2+offx,a/2+offy,-a/2+offz);
   vertex(a/2+offx,a/2+offy,a/2+offz);
   vertex(-a/2+offx,a/2+offy,a/2+offz);
   
   fill(0,255,255);
   vertex(a/2+offx,-a/2+offy,a/2+offz);
   vertex(a/2+offx,-a/2+offy,-a/2+offz);
   vertex(a/2+offx,a/2+offy,-a/2+offz);
   vertex(a/2+offx,a/2+offy,a/2+offz);
   
   fill(255,0,255);
   vertex(-a/2+offx,-a/2+offy,-a/2+offz);
   vertex(-a/2+offx,a/2+offy,-a/2+offz);
   vertex(-a/2+offx,a/2+offy,a/2+offz);
   vertex(-a/2+offx,-a/2+offy,a/2+offz);
  endShape();
  popMatrix();
 
 
  punkt(200,150,150); //Funktion, die Punkt zeichnet
}



void mouseDragged() {
 float factor = 0.01;
if (key==CODED){ // Alt + Maus --> Rotation
 if(keyCode==ALT){
 rotx += (pmouseY-mouseY) * factor;
 roty += (mouseX-pmouseX) * factor;}
 
if(keyCode==CONTROL){ // Ctrl + Maus --> Translation
  transx+=(mouseX-pmouseX);
  transy+=(mouseY-pmouseY);
}

  }
}



void keyPressed()
{
 if(key!=CODED){
if (key=='1'){rotx=0;roty=0; transx=width/2;transy=height/2;transz=-300;}
if (key=='3'){rotx=0;roty=radians(-90);}
if (key=='7'){rotx=radians(-90);roty=0;}  
}

  if (key == CODED) {
if(keyCode==CONTROL){
  if(key=='1'){rotx=0;roty=PI;}
     if(key=='3'){rotx=0;roty=radians(90);}
      if(key=='7'){rotx=radians(90);roty=0;}
}
}
   
}



void punkt(int px, int py, int pz){
pushMatrix();
translate(transx,transy,transz);
 rotateX(rotx);
 rotateY(roty);
beginShape(POINTS);
vertex(px,py,pz);
//  vertex(px+20,py+20);
endShape();
popMatrix();

}

Re: 3d program: rotation, translation, projection..
Reply #1 - Apr 2nd, 2010, 11:13am
 
Quote:
I have programed this code, but it doesn't work. when I project with "6" it isn't right.

Your program does not respond to key "6" being pressed so not sure what to do here.

Quote:
when I resize the window, the projection also doesn't work.

You need to recalculate the transx and transy values based on the new window size. I have added a method pre() that will see if the window has been resized and do the calculation for you. NOTE the registerPre() in setup is required to ensure the pre() method is called before each call to draw(). Do not change the method name from pre.

I also noticed you had redundant code in draw() to perform the translation and rotation, I have got rid of the redundancy. Also the punkt method did not display a point because you had not set the stroke weight and colour, I have fixed that.

Finally I have removed some of the code in draw() to other functions to make the overall coding clearer.

Hope this all makes sense.
Smiley

Code:

float rotx=.1, roty=.1;
int achl=600;
int a=100, offx=0, offy=0,offz=0;
int transx, transy, transz=-300;

int lastWidth, lastHeight;

void setup()
{
 size(800,600, P3D);
 // Remember the starting width and height
 lastWidth = width;
 lastHeight = height;
 frame.setResizable(true); // Fenster kann manuell vom Benutzer verändert werden
 stroke(0);
 strokeWeight(1);
 strokeJoin(MITER);
 transx=width/2;
 transy=height/2;

 // Add the pre() method to the execution loop
 registerPre(this);
}

// Recalculate the amount of translation needed if the
// window size has changed
void pre(){
 if(width != lastWidth || height != lastHeight){
   int deltaX = transx - lastWidth/2;
   int deltaY = transy - lastHeight/2;
   transx = deltaX + width/2;
   transy = deltaY + height/2;
   lastWidth = width;
   lastHeight = height;
 }
}

void draw()
{
 background(226);
 //  camera();
 //  perspective();
 pushMatrix(); // Koordinatensystem
 // Perform translation and rotation
 translate(transx,transy,transz);
 rotateX(rotx);
 rotateY(roty);

 drawAxis();
 drawCubeAtOrigin();

 punkt(150,150,150); //Funktion, die Punkt zeichnet

 popMatrix(); // Restore matrix
 println(width+" x "+height);
}

void drawCubeAtOrigin(){
 beginShape(QUADS);
 noStroke();
 fill(255,0,0);
 vertex(-a/2+offx,-a/2+offy,a/2+offz);
 vertex(-a/2+offx,a/2+offy,a/2+offz);
 vertex(a/2+offx,a/2+offy,a/2+offz);
 vertex(a/2+offx,-a/2+offy,a/2+offz);

 fill(0,255,0);
 vertex(-a/2+offx,-a/2+offy,a/2+offz);
 vertex(a/2+offx,-a/2+offy,a/2+offz);
 vertex(a/2+offx,-a/2+offy,-a/2+offz);
 vertex(-a/2+offx,-a/2+offy,-a/2+offz);

 fill(0,0,255);
 vertex(-a/2+offx,-a/2+offy,-a/2+offz);
 vertex(a/2+offx,-a/2+offy,-a/2+offz);
 vertex(a/2+offx,a/2+offy,-a/2+offz);
 vertex(-a/2+offx,a/2+offy,-a/2+offz);

 fill(255,255,0);
 vertex(-a/2+offx,a/2+offy,-a/2+offz);
 vertex(a/2+offx,a/2+offy,-a/2+offz);
 vertex(a/2+offx,a/2+offy,a/2+offz);
 vertex(-a/2+offx,a/2+offy,a/2+offz);

 fill(0,255,255);
 vertex(a/2+offx,-a/2+offy,a/2+offz);
 vertex(a/2+offx,-a/2+offy,-a/2+offz);
 vertex(a/2+offx,a/2+offy,-a/2+offz);
 vertex(a/2+offx,a/2+offy,a/2+offz);

 fill(255,0,255);
 vertex(-a/2+offx,-a/2+offy,-a/2+offz);
 vertex(-a/2+offx,a/2+offy,-a/2+offz);
 vertex(-a/2+offx,a/2+offy,a/2+offz);
 vertex(-a/2+offx,-a/2+offy,a/2+offz);
 endShape();
}

void drawAxis(){
 strokeWeight(2);
 beginShape(LINES);

 stroke(255,0,0);
 vertex(0,0,-achl);
 vertex(0,0,achl);

 stroke(0,255,0);
 vertex(0,-achl,0);
 vertex(0,achl,0);

 stroke(0,0,255);
 vertex(-achl,0,0);
 vertex(achl,0,0);
 endShape();
}

void mouseDragged() {
 float factor = 0.01;
 if (key==CODED){ // Alt + Maus --> Rotation
   if(keyCode==ALT){
     rotx += (pmouseY-mouseY) * factor;
     roty += (mouseX-pmouseX) * factor;
   }

   if(keyCode==CONTROL){ // Ctrl + Maus --> Translation
     transx+=(mouseX-pmouseX);
     transy+=(mouseY-pmouseY);
   }
 }
}

void keyPressed()
{
 if(key!=CODED){
   if (key=='1'){
     rotx=0;
     roty=0;
     // The next 2 lines center the axis on the screen, if this
     // what you want they need to be included for '3' and '7'
     transx=width/2;
     transy=height/2;
     transz=-300;
   }
   if (key=='3'){
     rotx=0;
     roty=radians(-90);
     transx=width/2;
     transy=height/2;
     transz=-300;
   }
   if (key=='7'){
     rotx=radians(-90);
     roty=0;
     transx=width/2;
     transy=height/2;
     transz=-300;
   }  
 }

 if (key == CODED) {
   if(keyCode==CONTROL){
     if(key=='1'){
       rotx=0;
       roty=PI;
     }
     if(key=='3'){
       rotx=0;
       roty=radians(90);
     }
     if(key=='7'){
       rotx=radians(90);
       roty=0;
     }
   }
 }
}

void punkt(int px, int py, int pz){
 beginShape(POINTS);
 strokeWeight(6);
 stroke(0);
 vertex(px,py,pz);
 //  vertex(px+20,py+20);
 endShape();
}

Re: 3d program: rotation, translation, projection..
Reply #2 - Apr 2nd, 2010, 12:11pm
 
hey..
thank you for your work. I meant 7 or something like that, that is in the code Smiley

it's better now. but my problem is that when I translate I only would like to go along the screen. But it changes the perspective. when I push the ctrl button I would only like to "drive" along the screen, at the same place. but when I do that aftwerwords I can see the right or the left side.
When I project it with the key "1" and then translate with the key "ctrl" I still want to see just this projection and not a left or a right side.


and with the rotation it's the problem that when I rotate when the under side is up, it's inverted...


could you help me with this? I just can't understand...
Re: 3d program: rotation, translation, projection..
Reply #3 - Apr 4th, 2010, 5:06am
 
Quote:
it's better now. but my problem is that when I translate I only would like to go along the screen. But it changes the perspective. when I push the ctrl button I would only like to "drive" along the screen, at the same place. but when I do that aftwerwords I can see the right or the left side.
When I project it with the key "1" and then translate with the key "ctrl" I still want to see just this projection and not a left or a right side.

and with the rotation it's the problem that when I rotate when the under side is up, it's inverted...


The program is working as it should but obviously not as you want/expect.

Try this experiment.
Get a small box or dice and hold it at arms length so you can see just the one side (i.e. the front). Now without moving your head or the orientation of the box (i.e. translation) swing your arm to one side follow the box with your eyes. You will eventually see both the front and the side of the box. This is what is happening when you use the ctrl key in your program, it is supposed to work this way, it is the world of 3D.
Re: 3d program: rotation, translation, projection..
Reply #4 - Apr 6th, 2010, 2:54am
 
but I think the y-axis should be parallel to the window. But it isn't. is this only because of the translation and the view? but I don't want that it is like this, I want it to be parallel...
?? I have no idea..
Re: 3d program: rotation, translation, projection..
Reply #5 - Apr 12th, 2010, 3:12pm
 
and another problem. when I maximize the window there's no problem. it works. but when I change the window size (not the start height, width and not the maximized size) it doesn't work when I project the coordinate system... why?
Page Index Toggle Pages: 1