Transformations such as translate(), rotate(), and scale() do not work within beginShape() -- beginShape() reference
This means if you specifically want to use the 2D TRIANGLE_STRIP, one way of doing it is by rotating around X Y and Z in the space and drawing a series of faces made out of rows of triangle strips.
void drawTriangleSheet(int cols, int rows, float w, float h){
pushMatrix();
translate(-cols*w/2,-rows*h/2);
for (int y = 0; y < rows; y++) {
beginShape(TRIANGLE_STRIP);
for (int x = 0; x <= cols; x++) {
vertex(x*w, y*h);
vertex(x*w, y*h + h);
}
endShape();
}
popMatrix();
}
For those interested in this particular problem (using TRIANGLE_STRIP or QUAD_STRIP to build cubes) I wrote a demo sketch.
It isn't efficient or compact -- rather, it is meant to walk through the logic of assembling a face out of strips, then creating pairs of faces, then building a full cube out of three pairs. It flips between quads and triangles every other second. Remove the "timedTumble" and add the peasyCam library to interact with the shape.
/**
* Shape Strip Cube
* 2016-12-14 Jeremy Douglass - Processing 3.2.3
* https:// forum.processing.org/two/discussion/18996/how-to-draw-a-cube-with-triangle-strip
**/
int sheetType;
void setup() {
size(500, 500, P3D);
stroke(0,255,0);
noFill();
}
void draw() {
background(0);
translate(width/2,height/2);
//// spin camera
timedTumble(40);
//// draw cube
sheetType = (second()%2==0) ? TRIANGLE_STRIP : QUAD_STRIP;
drawCube(10, 28);
}
void drawCube(int units, float size){
pushMatrix();
//// front and back
drawFacePair(units, size);
rotateX(HALF_PI);
//// top and bottom
drawFacePair(units, size);
rotateY(HALF_PI);
//// left and right
drawFacePair(units, size);
popMatrix();
}
void drawFacePair(int units, float size){
pushMatrix();
translate(0,0,units*size/2);
drawSheet(units, units, size, size);
translate(0,0,-units*size);
drawSheet(units, units, size, size);
popMatrix();
}
void drawSheet(int cols, int rows, float w, float h){
pushMatrix();
translate(-cols*w/2,-rows*h/2);
for (int y = 0; y < rows; y++) {
beginShape(sheetType);
for (int x = 0; x <= cols; x++) {
vertex(x*w, y*h);
vertex(x*w, y*h + h);
}
endShape();
}
popMatrix();
}
//// Timed 3D tumble, rotations cycle back to start every `duration` seconds
void timedTumble(float duration){
duration = duration*1000; // milliseconds
float spin = TWO_PI*2 * (millis()%duration)/duration;
rotateX(spin);
rotateY(spin/2);
rotateZ(spin/8);
}
This was the original reference image for the question:
Wouldn't this be better, since it would be only one Shape, and not multiple, or doesn't this work?
Edit: doesnt work correctly, but i'll leave it here, just in case someone was going to try the same.(Could still be used, but only if individual triangles arent visible);
void drawTriangleSheet(int cols, int rows, float w, float h){
*Boolean L = true;
pushMatrix();
translate(-cols*w/2,-rows*h/2);
beginShape(TRIANGLE_STRIP);
for (int y = 0; y < rows; y++) {
*if (L == true) {
for (int x = 0; x <= cols; x++) {
vertex(x*w, y*h);
vertex(x*w, y*h + h);
}
*} else {
* for (int x = cols; x > 0; x--) {
* vertex(x*w, y*h);
* vertex(x*w, y*h + h);
* }
*}
endShape();
}
popMatrix();
}
Answers
This means if you specifically want to use the 2D TRIANGLE_STRIP, one way of doing it is by rotating around X Y and Z in the space and drawing a series of faces made out of rows of triangle strips.
For those interested in this particular problem (using TRIANGLE_STRIP or QUAD_STRIP to build cubes) I wrote a demo sketch.
It isn't efficient or compact -- rather, it is meant to walk through the logic of assembling a face out of strips, then creating pairs of faces, then building a full cube out of three pairs. It flips between quads and triangles every other second. Remove the "timedTumble" and add the peasyCam library to interact with the shape.
This was the original reference image for the question:
...and this is the sketch output:
Wouldn't this be better, since it would be only one Shape, and not multiple, or doesn't this work?
Edit: doesnt work correctly, but i'll leave it here, just in case someone was going to try the same.(Could still be used, but only if individual triangles arent visible);