# Calculating normals for a torus

Hello,

This has been driving me nuts for ages. I have a routine to draw a torus(or n degrees of it) but i cant work out how to generate normals for it.

Please can someone give me some help…

oh - and please dont say - ‘just trace a ray’ because i’m not all that sure what it means

here is the code for my routine (which i’m sure could be a bit more efficient…any advice welcome)

void CGLtoralSweep::Draw(float R, float r, int numr, int numR, float Sweep, GLenum mode)
{
/* dA1 = change in ang1
* dA2 = change in ang2
*/

``````float ang1,ang2;
float dA1 = 360/(numR*(360/Sweep));
float dA2 = 360/numr;

Sweep = Sweep/dA1;
numr++;
numR++;
for(int j=0;j&lt;Sweep;j++)
{
glBegin(GL_LINES);
for(int i=0;i&lt;=numr;i++)
{
ang1 = (j*dA1) * (PI/180);
for(int p=0;p&lt;2;p++)
{
ang2 = (i*dA2) * (PI/180);

glColor3f(1.0,0.0,0.0);

float z = (R + r*cos(ang2))* cos(ang1);
float y = (R + r*cos(ang2))* sin(ang1);
float x = r*sin(ang2);

glVertex3f(x,y,z);
ang1 = ang1 + (dA1 * (PI/180));
}
}
glEnd();
}
``````

}

thanks,
kev.

Why not just use the GLU (or is it GLUT?) routine for a torus?. Does it all for ya. But its a good question and I look forward to knowing the answer.

Originally posted by Glenn Upton:
Why not just use the GLU (or is it GLUT?) routine for a torus?..

The glu/glut routine draws a complete torus - i want to draw only the first n degrees of a torus so i had write my own routine.

regards,
kev.

Ah. Well that will be kinda tricky cause you will have to save each 3 vertices and then calc the normal (but you know that). Not an OpenGl problem but a programming/logic problem. Likely you can figure out the solution within the loop but I bet first and last normals have to be calc’d outside the loop. Is this much help? Prob not. Good luck.

Well, the easiest solution, as far as I can tell…( heh heh ) would be this:

A torus is basically a circular or elliptical shape swept along a circular path.

With this idea in mind, if you take the current point on the circular path ( the origin of the shape you are sweeping ) and then subtract that from the point on the surface of the torus you’ve just calculated…then normalize that vector, that should be the normal for that point of the torus.

I’ve probably not described my idea very well, and if I could draw a picture it would be much clearer I’m sure…ah well. Hope this helps?

Yeah - thats pretty much what i ended up doing.

thanks,
kev.