Jump to content
  • Advertisement
Sign in to follow this  
endfx

cylinders from scratch

This topic is 5466 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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)

v0
x = cos(angle);
z = sin(angle);
y = 0;

v1
x = 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

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
Sorry, I forgot something.

U, V are Integer
Other are Single or GLFloat

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!