Processing Tutorial Bab 7
Processing Tutorial Bab 7
3D with processing
Rotation y axis
Scale
Rotation z axis
3D with processing
3D geometric transformations
Translation
translate(tx, ty, tz)
Scale
scale(sx, sy, sz)
(0,0,0)
x
+z
y
3D with processing
// An ellipse rotating
// around the y axis
float ang = 0.0;
void setup()
{
size(400, 400, P3D);
stroke(255, 0, 0);
noFill();
}
void draw()
{
background(0);
// Drawing centered
// (0,0,0)
translate(width/2, height/2);
rotateY(ang += 0.1);
ellipse(0, 0, 300, 200);
}
3D with processing
box(width, height, depth)
sphere(radius)
// Wireframe version
void setup()
{
size(400, 400, P3D);
stroke(255, 0, 0);
noFill();
}
void draw()
{
background(0);
// Drawing centered
// in (0,0,0)
translate(width/2, height/2);
rotateX(frameCount*PI/60.0);
rotateY(frameCount*PI/120.0);
rotateZ(frameCount*PI/180.0);
box(200, 200, 200);
}
3D with processing
// A cube rotating
// around the three axes
// Solid version
void setup()
{
size(400, 400, P3D);
fill(255, 0, 0);
}
void draw()
{
background(0);
// Drawing centered
// in (0,0,0)
translate(width/2, height/2);
rotateX(frameCount*PI/60.0);
rotateY(frameCount*PI/120.0);
rotateZ(frameCount*PI/180.0);
box(200, 200, 200);
}
3D with processing
// A cube rotating
// around the three axes
void setup()
{
size(400, 400, P3D);
fill(255, 0, 0);
noStroke();
}
void draw()
{
background(0);
// Iluminación básica
lights();
// Drawing centered
// in (0,0,0)
translate(width/2, height/2);
rotateX(frameCount*PI/60.0);
rotateY(frameCount*PI/120.0);
rotateZ(frameCount*PI/180.0);
box(200, 200, 200);
}
3D with processing
// Interactive cube void mousePressed()
float rotX = 0.0, rotY = 0.0; {
int lastX, lastY; lastX = mouseX;
float distX = 0.0, distY = 0.0; lastY = mouseY;
}
void setup(){ void mouseDragged()
size(400, 400, P3D); {
noStroke(); distX = radians(mouseX - lastX);
fill(255, 0, 0); distY = radians(lastY - mouseY);
} }
void mouseReleased()
{
rotX += distY;
rotY += distX;
distX = distY = 0.0;
}
3D with processing
Practice 7-2
• Modify the previous program in order to draw the surface in
solid mode using the basic lighting (lights() and a unique
fill())
Practice 7-3
• Modify the previous program to draw the surface with a two-
color gradient (red for low values of z and yellow for high
values, for instance)
• For that, use a fill() call before each vertex()
Practice 7-4
• Modify again the previous program to, instead of drawing a
function, loads an image and, after grayscaling it, uses these
values for the z values of the quadrilateral strips