We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Creating a cylinder object
Page Index Toggle Pages: 1
Creating a cylinder object (Read 2251 times)
Creating a cylinder object
Nov 22nd, 2005, 2:00am
 
Hi, I'm new to Processing and I need to create a 3d cylinder object, I have no idea how to create one of these, I looked through the reference but cant see how to create one, thank you in advance.

p.s. Is it possible to create a cylinder object with a hole in the middle too?

[edit]I also have a problem that one of my polygons is transparent when it shouldnt be, I've called the fill() method, but it wont fill that polygon
Re: Creating a cylinder object
Reply #1 - Nov 22nd, 2005, 2:09am
 
While it's definitely possible, it takes a bit of technical brain tweaking to make one.


First, you have to know how a geometric cylinder is defined. The usual way to create a cylinder is as follows:

Starting from a point, move out a certain distance as the radius of a circle. Sweep this radius to make points along the way (how many points you make describe the detail of this cylinder). Connect all these points to the center point, and create triangles from each of these. Now you have a flat polygon circle.

Duplicate this process with the other cap end of the cylinder at a certain distance away.

Next, vertically connect each corresponding point on the circles with a polygon. Generally, it would be ideal to use each four point (two from the top circle, two from the bottom) as the vertices of these side polygons.


What I have just done is describe to you the process in which to create a cylinder, procedurally. Now if someone will take over from here, generously produce code, or have you yourself figure it out... :]


This gives me an idea. Perhaps we should have a library to create additional parameterized primitives, a'la a 3D software package? It seems appropriate to have cylinder, torus, geosphere, and maybe teapot...
Re: Creating a cylinder object
Reply #2 - Nov 22nd, 2005, 12:04pm
 
that's already done mflux Wink

http://toxi.co.uk/p5/cylinder/

this features a generic cylinder/cone generator with optional caps and flexible vertex resolution.

demo controls: horizontal mouse pos = change resolution, left click = show caps
Re: Creating a cylinder object
Reply #3 - Nov 22nd, 2005, 7:36pm
 
AHH! I knew it!
Damn you Toxi and your polyness. Good stuff.
Re: Creating a cylinder object
Reply #4 - Nov 22nd, 2005, 9:48pm
 
Thats a good example but I need a more solid open cylinder (Like a car tyre), can anyone help me with the edited problem, some of my polygons are appearing translucent instead of the fill colour.

I could make do with an ordinary shaped cylinder (Like a jar of something).

Thanks for the help so far
Re: Creating a cylinder object
Reply #5 - Nov 23rd, 2005, 11:46am
 
There is a bug with P3D and fill() in certain circumstances, not sure if it's the same as what you're seeing though. See:

http://dev.processing.org/bugs/show_bug.cgi?id=169
Re: Creating a cylinder object
Reply #6 - Nov 23rd, 2005, 6:20pm
 
The polygon problem is sorted, I changed it from polygon to quads and triangles.

Still cant build a solid cylinder though like the one here:
http://www.econym.demon.co.uk/isotut/cylinder.jpg
Re: Creating a cylinder object
Reply #7 - Nov 23rd, 2005, 9:29pm
 
Quote:
Still cant build a solid cylinder though like the one here:
http://www.econym.demon.co.uk/isotut/cylinder.jpg


Can you explain why not?

I think toxi's example is already there. Sure, it never goes up to that many polys to make a smooth surface and it's not using smooth shading (such as lambert) but instead it's faceted. Also as mentioned there are problems with processing's poly renders.

Beyond that, I can't imagine what sort of cylinder you might be describing that toxi's routine isn't doing already...
Re: Creating a cylinder object
Reply #8 - Nov 27th, 2005, 7:14pm
 
For whatever it's worth, here's a simple cylinder routine that renders solid and smooth; works with P3D and OPENGL.  Obviously, just up the point count and you'll increase smoothness (with a performance hit of course.)

/*
Simple Cylinder
Ira Greenberg, November 27, 2005
*/
import processing.opengl.*;

float radius = 100;
float ang = 0, ang2 = 0;
int pts = 120;
float depth = 200;

void setup(){
 size(400, 400, OPENGL);
 //size(400, 400, P3D);
 background(50);
 fill(150);
 smooth();  // comment out with P3D renderer
 noStroke();
 framerate(30);
}

void draw(){
 background(50);
 directionalLight(166, 166, 196, -60, -60, -60);
 ambientLight(105, 105, 130);
 translate(width/2, height/2, -200);
 rotateY(ang2);
 rotateX(ang2*2);
 rotateZ(ang2);
 
 //body
 beginShape(QUAD_STRIP);
 for (int i=0; i<=pts; i++){
   float  px = cos(radians(ang))*radius;
   float  py = sin(radians(ang))*radius;
   vertex(px, py, depth);
   vertex(px, py, -depth);
   ang+=360/pts;
 }
 endShape();
 
 //cap 1
 beginShape(POLYGON);
 for (int i=0; i<=pts; i++){
   float  px = cos(radians(ang))*radius;
   float  py = sin(radians(ang))*radius;
   vertex(px, py, depth);
   ang+=360/pts;
 }
 endShape();

 //cap2
 beginShape(POLYGON);
 for (int i=0; i<=pts; i++){
   float  px = cos(radians(ang))*radius;
   float  py = sin(radians(ang))*radius;
   vertex(px, py, -depth);
   ang+=360/pts;
 }
 endShape();
 
 ang2+=PI/120; //for rotation
}

Page Index Toggle Pages: 1