Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

How to create a 3d cylinder using PShape and vertex (x,y,z)?

$
0
0

The code I have is done using a different method I believe, but I am unfamiliar using the PShape and vertex(x,y,z) method. Please help me understand what I need to do to convert.

This is the code I have: import peasy.*; import peasy.org.apache.commons.math.*; import peasy.org.apache.commons.math.geometry.*;

PeasyCam cam;

void setup() { size(600, 600, P3D);
lights(); fill(100, 200, 0); cam = new PeasyCam(this, 600); }

void draw() { background(255);

pushMatrix();    
rotateX(PI/4);
drawCylinder( 30, 100, 300 );
popMatrix();

}

void drawCylinder( int sides, float r, float h) { float angle = 360 / sides; float halfHeight = h / 2;

// draw top of the tube
beginShape();
for (int i = 0; i < sides; i++) {
    float x = cos( radians( i * angle ) ) * r;
    float y = sin( radians( i * angle ) ) * r;
    vertex( x, y, -halfHeight);
}
endShape(CLOSE);

// draw bottom of the tube
beginShape();
for (int i = 0; i < sides; i++) {
    float x = cos( radians( i * angle ) ) * r;
    float y = sin( radians( i * angle ) ) * r;
    vertex( x, y, halfHeight);
}
endShape(CLOSE);

// draw sides
beginShape(TRIANGLE_STRIP);
for (int i = 0; i < sides + 1; i++) {
    float x = cos( radians( i * angle ) ) * r;
    float y = sin( radians( i * angle ) ) * r;
    vertex( x, y, halfHeight);
    vertex( x, y, -halfHeight);    
}
endShape(CLOSE);

}


Viewing all articles
Browse latest Browse all 2896

Trending Articles