0% found this document useful (0 votes)
96 views19 pages

Processing Tutorial Bab 7

The document discusses 3D rendering in Processing. It covers: - Two modes for 3D rendering: P3D and OPENGL - Using 2D and 3D primitives, like lines, boxes, and spheres - 3D transformations like translation, rotation, and scale - Basic lighting and texture mapping - Examples of drawing a rotating cube and interactive cube rotation

Uploaded by

sayurin 46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views19 pages

Processing Tutorial Bab 7

The document discusses 3D rendering in Processing. It covers: - Two modes for 3D rendering: P3D and OPENGL - Using 2D and 3D primitives, like lines, boxes, and spheres - 3D transformations like translation, rotation, and scale - Basic lighting and texture mapping - Examples of drawing a rotating cube and interactive cube rotation

Uploaded by

sayurin 46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Computer Graphics

3D with processing

Jordi Linares i Pellicer


Escola Politècnica Superior d’Alcoi
Dep. de Sistemes Informàtics i Computació
[email protected]
http://www.dsic.upv.es/~jlinares
3D with processing
• processing offers two 3D rendering modes: P3D and OPENGL
• P3D is based on software, OPENGL is implemented using
OpenGL (hardware). With some exceptions, both allow the
same set of primitives and functions.
• 2D primitives and functions can be used in 3D (some
exceptions might exist)
• Some of the primitives, such as lines (line), points (point),
curves and shapes (vertex primitive), can define 3
coordinates: x, y, z
• The rest of the 2D primitives can also be used (with an implicit
z=0)
• Stroke, fill, text and image (textures) functions can also be used
in 3D
3D with processing
3D geometric transformations

Translation Rotation x axis

Rotation y axis
Scale

Rotation z axis
3D with processing
3D geometric transformations

Translation
translate(tx, ty, tz)

Scale
scale(sx, sy, sz)

Rotation around an axis


rotateX(), rotateY(), rotateZ()
3D with processing
• By default, a perpective projection is applied
• (0,0,0) is placed in the left-top corner
• -z to move away
-z

(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)

• Draws a cuboid, a right prism, centered in (0,0,0) of width (x),


height (y) and depth (z) as arguments

sphere(radius)

• Draws a sphere centered in (0,0,0) with the specified radius


• The level of detail in which the sphere will be drawn can be
adjusted with the sphereDetail(n) function, where n
implies that vertices will be generated every 360º/n (by
default, n = 30)
3D with processing
// A cube rotating
// around the three axes

// 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

// Basic lighting version

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 draw(){ void mouseReleased()


background(0); {
rotX += distY;
lights(); rotY += distX;
distX = distY = 0.0;
translate(width/2, height/2); }
rotateX(rotX + distY);
rotateY(rotY + distX);

box(200, 200, 200);


}
Practice 7-1
• Modify the previous program to allow zoom, by moving the
cube through the z axis
• For that it will be needed to add a z component to the
translation
• Initially this displacement will be 0 and will vary from 0 to -500
with steps of 10
• Use the UP and DOWN keys catching the keyPressed()
event to change this zoom
3D with processing
// Let’s do it with OpenGL // We provide the vertices of
// The import is compulsory // each face of the cube. // -X "left" face
import processing.opengl.*; // The last two values vertex(-1, -1, -1, 0, 0);
// are the texture coordinates vertex(-1, -1, 1, 1, 0);
float rotX = 0.0, rotY = 0.0; // that correspond to the vertex(-1, 1, 1, 1, 1);
int lastX, lastY; // vertex vertex(-1, 1, -1, 0, 1);
float distX = 0.0, distY = 0.0;
// +Z "front" face endShape();
// Texture vertex(-1, -1, 1, 0, 0);
PImage foto; vertex( 1, -1, 1, 1, 0); }
vertex( 1, 1, 1, 1, 1);
void setup(){ vertex(-1, 1, 1, 0, 1); void mousePressed()
size(400, 400, OPENGL); {
noStroke(); // -Z "back" face lastX = mouseX;
foto = loadImage("foto.jpg"); vertex( 1, -1, -1, 0, 0); lastY = mouseY;
vertex(-1, -1, -1, 1, 0); }
// We want to work with texture vertex(-1, 1, -1, 1, 1); void mouseDragged()
// coordinates from (0,0) to (1,1) vertex( 1, 1, -1, 0, 1); {
textureMode(NORMALIZED); distX = radians(mouseX - lastX);
} // +Y "bottom" face distY = radians(lastY - mouseY);
vertex(-1, 1, 1, 0, 0); }
void draw(){ vertex( 1, 1, 1, 1, 0);
background(0); vertex( 1, 1, -1, 1, 1); void mouseReleased()
vertex(-1, 1, -1, 0, 1); {
translate(width/2, height/2); rotX += distY;
rotateX(rotX + distY); // -Y "top" face rotY += distX;
rotateY(rotY + distX); vertex(-1, -1, -1, 0, 0); distX = distY = 0.0;
vertex( 1, -1, -1, 1, 0); }
// We want the cube 200 x 200 x 200 vertex( 1, -1, 1, 1, 1);
// We draw it from -1 to 1 vertex(-1, -1, 1, 0, 1);
scale(100, 100, 100);
// +X "right" face
beginShape(QUADS); vertex( 1, -1, 1, 0, 0);
texture(foto); vertex( 1, -1, -1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex( 1, 1, 1, 0, 1);
3D with processing
3D with processing
import processing.opengl.*; void draw() void drawFunction()
{ {
// Drawing a 3D function background(0); float x, y, z;
float rotX = 0.0, rotY = 0.0; int i = 0, j = 0;
int lastX, lastY; // We center the results on window float in_steps = 1.0 / steps;
float distX = 0.0, distY = 0.0; translate(gX/2, gY/2, zoomZ);
float[][] matrix = new float[steps+1][steps+1];
// Function steps // Rotation
int steps = 50; rotateY(rotY + distX); for (y = 0.0, j = 0; y <= 1.0; y+=in_steps, j++)
rotateX(rotX + distY); for (x = 0.0, i = 0; x <= 1.0; x+=in_steps, i++)
// z scale matrix[i][j] = function(x, y);
float scaleZ = 200.0; // Centering around (0, 0);
translate(-gX/2, -gY/2); for (j = 0, y = 0.0; j < steps; j++, y+=in_steps) {
// z zoom beginShape(QUAD_STRIP);
float zoomZ = -300.0; // Function covers for (i = 0, x = 0.0; i <= steps; i++, x+=in_steps) {
// 400 x 400 x scaleZ vertex(x, y, matrix[i][j]);
// Graphic size scale(gX, gY, scaleZ); vertex(x, y + in_steps, matrix[i][j+1]);
float gX = 500.0, gY = 500.0; }
// Drawing the function endShape();
void setup() stroke(255); }
{ drawFunction(); }
size(500, 500, OPENGL); void mousePressed()
noFill(); // Drawing axes {
} stroke(255, 0, 0); lastX = mouseX;
line(0,0,0,2000,0,0); lastY = mouseY;
float function(float x, float y) stroke(0,255,0); }
{ line(0,0,0,0,2000,0); void mouseDragged()
return x*x*x + y*y*y; stroke(0,0,255); {
} line(0,0,0,0,0,2000); distX = radians(mouseX - lastX);
} 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

You might also like