Processing Month, Day 27 - Mesh Basics
Posted on 2011-05-27 by Jan Vantomme
Tags:
processing, tutorial
Today we'll take a look at how we can use the QUAD_STRIP
and TRIANGLE_STRIP
modes with beginShape()
to draw a mesh to the screen. This will be useful when you want to get started with drawing 3D shapes with Processing. This first example will draw a strip made from rectangles.
beginShape(QUAD_STRIP);
for (int i = 0; i < 11; i++) {
vertex( i * 40, 0 );
vertex( i * 40, 60 );
}
endShape();
The second example uses the TRIANGLE_STRIP
mode and draws the same thing to the screen, only with triangles now.
beginShape(TRIANGLE_STRIP);
for (int i = 0; i < 11; i++) {
vertex( i * 40, 0 );
vertex( i * 40, 60 );
}
endShape();
If we want to draw a grid, we need nested for loops. The code is almost the same as in the first example. The y-coordinate of each vertex gets calculated now and we wrapped an extra for loop around the code. Note that the outer for-loop only runs to 10, not to 11. If it would run to 11, we would render a grid of 11 x 10 rectangles.
for (int j = 0; j < 10; j ++) {
beginShape(QUAD_STRIP);
for (int i = 0; i < 11; i++) {
vertex( i * 40, j * 40 );
vertex( i * 40, (j+1) * 40 );
}
endShape();
}
If you want to render the grid with triangles, replace QUAD_STRIP
in the previous example to TRIANGLE_STRIP
to get the result in the image below.
Download
Download all Processing examples for Mesh Basics.