To draw a part of a torus I use the following code. The problem is that this torus looks faceted (not smoothly). Does someone know how to modify this code in order to let this torus look more natural ?
twopi := 2*PI;
for i:=0 to numc-1 do
begin
glBegin(GL_QUAD_STRIP);
j:=0;
d:=numt*morc;
while (j<=d) do
begin
d2:=OuterRadius+InnerRadius*cos((i+1)*twopi/numc);
x_norm := d2*cos((j+0.5)*twopi/numt)-OuterRadius*cos((j+0.5)*twopi/numt);
y_norm := d2*sin((j+0.5)*twopi/numt)-OuterRadius*sin((j+0.5)*twopi/numt);
z_norm := InnerRadius*sin((i+1)*twopi/numc);
norm := sqrt(x_norm*y_norm+y_norm*y_norm+z_norm*z_norm);
x_norm := x_norm/norm;
y_norm := y_norm/norm;
z_norm := z_norm/norm;
glNormal3d(x_norm,y_norm,z_norm);
for k_:=1 downto 0 do
begin
s_ := (i+k_) mod numc + 0.5;
t := j mod numt;
d2 := OuterRadius+InnerRadius*cos(s_*twopi/numc);
x1 := d2*cos(t*twopi/numt);
y1 := d2*sin(t*twopi/numt);
z1 := InnerRadius*sin(s_*twopi/numc);
glVertex3d(x1,y1,z1);
end;
inc(j);
end;
glEnd;
end;
Hooh! I’m understand noting in your code, but one advice - you need to specify diffrent normals to each vertex to obtainsmooth result (as I anderstand you draw more than one vertex with one normal). Or try to use glEnable(GL_AUTO_NORMAL) to enable automatic normal generation;
Simply put vertices into vertex array and normals into normal array. You do not have to use OpenGL’s vertex arrays if you don’t want to - you can just have your own arrays and use glNormal / glVertex.
I first trie to rewrite the code using your algorithm, without usage of vertex-arrays.
If this works fine, I will rewrite it using vertex-arrays.
The problem is that no torus is drawn but something completely different. Do you see what is wrong in the code below (maybe the position of glBegin … glEnd ?)
{*********************************************************************************************}
Procedure TCadObj.Torus2(Tx,Ty,Tz,Ax,Ay,Az,InnerRadius,OuterRadius,Angle1,Angle2,Step:double);
{*********************************************************************************************}
var PosX,PosY,PosZ : double;
NormX,NormY,NormZ : double;
CosA1,SinA1 : double;
CosA2,SinA2 : double;
a1,a2 : double;
begin
PiOver180:=PI/180;
glTranslated(Tx,Ty,Tz);
if Ax<>0 then glRotated(Ax,1,0,0);
if Ay<>0 then glRotated(Ay,0,1,0);
if Az<>0 then glRotated(Az,0,0,1);
glDisable(GL_CULL_FACE);
Angle1 := PiOver180*Angle1; {degrees to radians}
Angle2 := PiOver180*Angle2; {degrees to radians}
Step := PiOver180*Step; {degrees to radians}
a1:=0;
while a1<=Angle1 do
begin
CosA1:=cos(a1);
SinA1:=sin(a1);
a2:=0;
glBegin(GL_QUAD_STRIP);
while a2<=Angle2 do
begin
CosA2:=cos(a2);
SinA2:=sin(a2);
PosX := SinA1 * InnerRadius;
PosY := (CosA1 * InnerRadius + OuterRadius) * SinA2;
PosZ := (CosA1 * InnerRadius + OuterRadius) * CosA2;
NormX := SinA1;
NormY := CosA1 * SinA2;
NormZ := CosA1 * CosA2;
glNormal3d(NormX, NormY, NormZ);
glVertex3d(PosX, PosY, PosZ);
a2:=a2+step;
end;
glEnd;
a1:=a1+step;
end;
glEnable(GL_CULL_FACE);
end;