cylinders from scratch

Started by
4 comments, last by Peter2004 19 years, 6 months ago
Hi, I'm trying to draw a cylinder from scratch. I can't seem to find anywhere on the net that will tell me howto do this. Can anybody point me to a good tutorial on howto build cylinders and other round objects from scratch?? Thanks!
Advertisement
x=cos(angle);
y=sin(angle);
Just loop the angle through 0 to 2pi incrementing by whatever ammount of detail you want to get each point on the circle. Then erm join em & stuff ;]

(I should probally actually explain more, but the lack of sleep gives me a headache)
_______________________________ ________ _____ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
Quote:Original post by endfx
Hi,

I'm trying to draw a cylinder from scratch. I can't seem to find anywhere on the net that will tell me howto do this. Can anybody point me to a good tutorial on howto build cylinders and other round objects from scratch??

Thanks!


you could use glu, i'm pretty sure there is a simple call to do shapes and it includes cylinders. Forgot the call tho.

You could use triangle strips and do it,

something like (loop this, set the angle delta to the detail u want)
v0x = cos(angle);z = sin(angle);y = 0;v1x = cos(angle);z = sin(angle);y = 10; // height


this would get you the sides of it, to do the top and bottom you could use a triangle fan with a vert at the center and basically the same angle loop as above.


HTH
you can pretty much do whatever you want w/ parametric equations.
I don't know can I help you with this code. If yes, I'll be glad.

The routine below allow you to build straight and bend cylinders from scratch. The cylinder build it along the Y axis. So its bottoms lay on the x-Z plane. It is similar to gluCylinder() func.

To use the routine you must set several parameters:
TopRadius, BottomRadius - the radiuses of cylinder bottoms
U - the number of slices
V - the number of vertical segments (not stacks!)
Bend - the bend angel
Hgt - the height of the cylinder

The code is not clear and there is a trouble with different bottoms, but the additional functionality (bending) are very useful.
In you draw function call glCallList(1) to draw a cylinder.

procedure TForm1.Eval;
var
BendAng, SegHgt, SegBend, Ang, R, X, Y, Z: Single;
CenterTop: TGLArrayf3;
mR, RR, cang: Single;
I, J: Integer;
begin
// The cylinder builds along Y axis. The bottoms are placed in the X-Z plane

// Radius from center of bottom circle to the imaginary line of bending
RR := Hgt / (Bend * PI / 180);
SegHgt := Hgt / V; // The segment height
SegBend := Bend / V; // The segment bend angle
BendAng := 360 / U; // The bottoms angle

for I := 0 to U - 1 do begin
Ang := DegToRad(I * BendAng); // Angle
for J := 0 to V do begin
R := TopRadius + (BottomRadius - TopRadius) * J;

// Z coord of the circle
Z := R * Sin(Ang); // Z = const

if Bend = 0 then begin
// when no bending
X := R * Cos(Ang);
Y := J * SegHgt;
end
else begin
// when bending present
cang := DegToRad(J * SegBend); // Bend angle for current segment
{ Radius from the point to the imaginary line of bending including
rotation around Y axis (X coord of the circle) }
mR := RR - R * Cos(Ang);
// Rotate point along imaginary line
X := mR * Cos(cang) - RR;
Y := mR * Sin(cang);
end;

// Stores in array
A[I, J, 0] := X;
A[I, J, 1] := Y;
A[I, J, 2] := Z;
end;
end;

// Find tops center
if Bend = 0 then begin
// when no bending
CenterTop[0] := 0.0;
CenterTop[1] := Hgt;
end
else begin
// when bending present
cang := DegToRad(V * SegBend); // Bend angle for top segment
CenterTop[0] := RR * Cos(cang) - RR;
CenterTop[1] := RR * Sin(cang);
end;
CenterTop[2] := 0.0;

// Draws a cylinder

glNewList(1, GL_COMPILE);

// Bottom circle
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0, 0.0, 0.0);
for I := 0 to U - 1 do begin
glVertex3fv(@A[I, 0]);
end;
glVertex3fv(@A[0, 0]);
glEnd;

// Side
glBegin(GL_TRIANGLE_STRIP);
for I := 1 to V do begin
for J := 0 to U - 1 do begin
glVertex3fv(@A[J, I - 1]);
glVertex3fv(@A[J, I]);
end;
// Two last vertices (the first two)
glVertex3fv(@A[0, I - 1]);
glVertex3fv(@A[0, I]);
end;
glEnd;

// Top circle
glBegin(GL_TRIANGLE_FAN);
glVertex3fv(@CenterTop);
for I := 0 to U - 1 do begin
glVertex3fv(@A[I, V]);
end;
glVertex3fv(@A[0, V]);
glEnd;

glEndList;
end;
Sorry, I forgot something.

U, V are Integer
Other are Single or GLFloat

There is no normals calculation in this code. Do it yourself.

This topic is closed to new replies.

Advertisement