We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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);
}
Answers
Edit post, highlight code, press Ctrl-o to format
you create the shape at the beginning, using createShape and beginShape. then, in draw(), all you have to do is call shape()
i haven't checked the winding though, just used the old code with as little change as possible.
From https://processing.org/tutorials/pshader/
Removed the noStroke() so you can see what is is been done.
Kf
I appreciate the help and the secondary reference! I'm still fairly new to Processing and programming. Your notes helped a bunch. Thanks again!
that can has no ends though...
my version with lights()
still no ends...
and you have texture coordinates but no texture...
Is there a way to copy the cylinder made and translate/scale/rotate afterwards?
see draw()
The usual scale and rotate and translate methods work in draw for my example. And peasycam will let you move the camera, obv.
Because everything is precalculated you can't really dynamically change the geometry of the object, the number of sides, the length or radius. You could call createCylinder with new parameters every frame but you'd lose a lot of the benefits of PShape (the speed) doing that.
Thank You!
Yes. That’s what’s happening in draw(). You can change draw and add scale() or change translate and rotate there. You can also call ghe same with thing in many ways (positions, scales)
There is a transformation tutorial in the Processing website.
Kf