#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How to create a faceted box with beginShape()?

edited November 2015

Hi all,

I'm trying to create a box where every face is subdivided into small triangular faces. I tried to use the beginShape() function to create all the triangles but I cannot find a good way of organizing the vertices position.

Can anyone give me any suggestion? thanks, J_D

Tagged:

## Answers

• you wrote

I'm trying to create a box where every face is subdivided into small triangular faces.

does this mean 2 triangles per face?

could you show your code please?

https://www.processing.org/examples/texturecube.html

• edited December 2015

Hi Chrisir. This is my actual code, I managed to create a box but I'm not sure this is the correct way of doing it (i.e. I'm not sure about the orientation of the faces, should I follow some rule to obtain the normal of the each triangles directed to outside?) And also, The next step would be to move the vertices along vectors. If I create the box in this way, is it still possible to achieve the next step? or do I need to rethink the overall code? Thank you.

``````import peasy.*;

PeasyCam cam;
int resX = 100;
int resY = 100;
int resZ = 100;
int xs = 10;
int ys = 5;
int zs = 5;

void setup() {
size(1000, 500, P3D);
smooth();
cam = new PeasyCam(this, width);
}

void draw() {
background(0);
stroke(255, 0, 0);
for (int i = 0; i < xs; i++) {
for (int j = 0; j < ys; j++) {
beginShape(TRIANGLE);
vertex(resX*i, resY*j, 0);
vertex(resX*(i+1), resY*j, 0);
vertex(resX*i, resY*(j+1), 0);
vertex(resX*(i+1), resY*j, 0);
vertex(resX*(i+1), resY*(j+1), 0);
vertex(resX*i, resY*(j+1), 0);
endShape();
}
}

for (int i = 0; i < xs; i++) {
for (int j = 0; j < ys; j++) {
beginShape(TRIANGLE);
vertex(resX*i, resY*j, zs*resZ);
vertex(resX*(i+1), resY*j, zs*resZ);
vertex(resX*i, resY*(j+1), zs*resZ);
vertex(resX*(i+1), resY*j, zs*resZ);
vertex(resX*(i+1), resY*(j+1), zs*resZ);
vertex(resX*i, resY*(j+1), zs*resZ);
endShape();
}
}

for (int i = 0; i < xs; i++) {
for (int j = 0; j < zs; j++) {
beginShape(TRIANGLE);
vertex(resX*i, 0, resZ*j);
vertex(resX*(i+1), 0, resZ*j);
vertex(resX*(i+1), 0, resZ*(j+1));
vertex(resX*i, 0, resZ*(j+1));
vertex(resX*i, 0, resZ*j);
vertex(resX*(i+1), 0, resZ*(j+1));
endShape();
}
}

for (int i = 0; i < xs; i++) {
for (int j = 0; j < zs; j++) {
beginShape(TRIANGLE);
vertex(resX*i, ys*resY, resZ*j);
vertex(resX*(i+1), ys*resY, resZ*j);
vertex(resX*(i+1), ys*resY, resZ*(j+1));
vertex(resX*i, ys*resY, resZ*(j+1));
vertex(resX*i, ys*resY, resZ*j);
vertex(resX*(i+1), ys*resY, resZ*(j+1));
endShape();
}
}

for (int i = 0; i < ys; i++) {
for (int j = 0; j < zs; j++) {
beginShape(TRIANGLE);
vertex(0, resY*i, resZ*j);
vertex(0, resY*(i+1), resZ*j);
vertex(0, resY*(i+1), resZ*(j+1));
vertex(0, resY*i, resZ*(j+1));
vertex(0, resY*i, resZ*j);
vertex(0, resY*(i+1), resZ*(j+1));
endShape();
}
}

for (int i = 0; i < ys; i++) {
for (int j = 0; j < zs; j++) {
beginShape(TRIANGLE);
vertex(xs*resX, resY*i, resZ*j);
vertex(xs*resX, resY*(i+1), resZ*j);
vertex(xs*resX, resY*(i+1), resZ*(j+1));
vertex(xs*resX, resY*i, resZ*(j+1));
vertex(xs*resX, resY*i, resZ*j);
vertex(xs*resX, resY*(i+1), resZ*(j+1));
endShape();
}
}

}
``````
Sign In or Register to comment.