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