Hi, I’m working on a homework project, part of which requires me to draw a cube made out of triangles. I have drawn a frame and started to fill it in, so that each triangle is a different color. I’ve started writing the code, but I am not getting what I had expected:
<
import controlP5.*;
ControlP5 cp5;
float positionX = 15;
float positionY = 15;
float positionZ = 15;
float cameraBounds = 30;
void setup() {
size(1600, 1000, P3D);
stroke(255);
strokeWeight(2);
perspective(radians(50.0f), width/(float)height, 0.1, 1000);
cp5 = new ControlP5(this);
cp5.addSlider(“positionX”, -30, 30).setPosition(20, 30);
cp5.addSlider(“positionY”, -30, 30).setPosition(20, 50);
cp5.addSlider(“positionZ”, -30, 30).setPosition(20, 70);
}
void draw() {
background(200);
perspective(radians(60), (float)width/height, 0.1, 100);
camera(positionX, positionY, positionZ, 0, 0, 0, 0, 1, 0);
beginShape();
fill(255,0,0);
vertex(0, 0, 0);
vertex(0, 1, 0);
vertex(0, 1, 1);
vertex(0,0,0);
//end of first triangle
vertex(0, 0, 1);
vertex(0, 1, 1);
vertex(0, 0, 0);
vertex(0, 0, 1);
vertex(1, 0, 0);
vertex(0, 0, 0);
vertex(1, 0, 0);
vertex(0, 1, 0);
vertex(1, 1, 1);
vertex(1, 1, 0);
vertex(0, 1, 0);
vertex(1, 1, 1);
vertex(0, 1, 1);
vertex(0, 1, 0);
vertex(1, 1, 1);
vertex(0, 1, 1);
vertex(1, 0, 1);
vertex(1, 1, 1);
vertex(1, 0, 1);
vertex(1, 1, 0);
vertex(1, 0, 0);
vertex(1, 0, 1);
vertex(1, 1, 0);
vertex(1, 0, 0);
vertex(1, 0, 1);
vertex(0, 0, 1);
vertex(1, 0, 0);
vertex(1, 1, 0);
vertex(0, 1, 0);
vertex(0, 0, 1);
vertex(0, 1, 1);
vertex(1, 0, 1);
vertex(0,0,0);
endShape();
//Reset the perspective and camera matrices so Controlp5 will render correctly
perspective();
camera();
}/>
My first guess was that I was not closing the shape, but adding an extra vertex to just the first triangle didn’t seem to help. Now I am thinking that I have the vertexes out of order. I can try to rearrange them, but even so, this approach seems inneficient.
My question is 1) is there a better way to do this? Like a built in function, or code someone else has written? 2) If not, what mistake am I making with this code?