#### Archived

This topic is now archived and is closed to further replies.

# Torus coordinates

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

## Recommended Posts

Does anyone have a list of coordinates to generate a relatively smooth torus (12 stacks and slices??) or an algorithm to generate these? I cannot use glutSolidTorus since I need access to the vertex coordinates of each triangle, to produce a tangent vector for each face. Thanks Paul

##### Share on other sites
Ok isn''t GLUT open source?... I''m pretty sure it is...... Couldn''t you just rip the code out of the library?...

##### Share on other sites
Please promise me that you will look into the code, and won't just plainly copy/paste it somewhere.

  static int _glPrecision = 12;void draw_one_vertex(int i, int idec, int j, float center, float radius) const{ float theta, phi; phi = (float)(j) * 2 * M_PI / (float)_glPrecision; theta = (float)(i+idec) * 2 * M_PI / (float)_glPrecision + phi / (float)_glPrecision; glTexCoord2f(theta / (2 * M_PI), phi / (2 * M_PI) + (float)(i)); glNormal3f(cos(phi)*cos(theta), sin(phi)*cos(theta), sin(theta)); glVertex3f( center*cos(phi)+radius*cos(phi)*cos(theta), center*sin(phi)+radius*sin(phi)*cos(theta), radius*sin(theta) );}void draw_torus(void){ float inner = ...; // set here your inner radius float outer = ...; // set here your outer radius float center = (inner + outer) / 2; float radius = outer - center; if (inner < outer) // choose the right polygon orientation for backface culling. { glBegin(GL_TRIANGLE_STRIP); for (int i = 0 ; i < _glPrecision ; i++) for (int j = 0 ; j < _glPrecision ; j++) { draw_one_vertex(i, 1, j, center, radius); draw_one_vertex(i, 0, j, center, radius); } draw_one_vertex(_glPrecision, 1, 0, center, radius); draw_one_vertex(_glPrecision, 0, 0, center, radius); glEnd(); } else { glBegin(GL_TRIANGLE_STRIP); for (int i = 0 ; i < _glPrecision ; i++) for (int j = 0 ; j < _glPrecision ; j++) { draw_one_vertex(i, 0, j, center, radius); draw_one_vertex(i, 1, j, center, radius); } draw_one_vertex(_glPrecision, 0, 0, center, radius); draw_one_vertex(_glPrecision, 1, 0, center, radius); glEnd(); }}

Notes :

- You can set an inner radius which is greater than an outer radius : in that case, polygons will be drawn to cull frontward faces instead of backward ones.

- You may invert the test (inner < outer) if the backface culling gives the inverted result you look for.

- Your texture have to be repeated (not clamped).

- The number of triangles is _glPrecision², eg 144.

- Instead of calling glVertex3f (in the draw_one_vertex function), you can get the coordinates and store them whereever you want for future use.

Hope that helps...
And also hope that no one will just plainly copy the code. If you don't understand why I did this or that, feel free to ask it . Thank you in advance

Edited by - vincoof on January 15, 2002 10:40:15 AM

##### Share on other sites
Sorry for getting this old topic op(or how do you call that :D)

But I tried to compile the code but I''m getting a few errors.

--------------------Configuration: lesson1 - Win32 Debug--------------------
Compiling...
Lesson1.cpp
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(87) : warning C4244: ''='' : conversion from ''double'' to ''float'', possible loss of data
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(87) : error C2143: syntax error : missing '';'' before ''/''
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(88) : warning C4244: ''='' : conversion from ''double'' to ''float'', possible loss of data
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(88) : error C2143: syntax error : missing '';'' before ''/''
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(89) : error C2143: syntax error : missing '')'' before '';''
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(89) : error C2143: syntax error : missing '')'' before '';''
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(89) : error C2660: ''glTexCoord2f'' : function does not take 1 parameters
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(89) : error C2059: syntax error : '')''
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(89) : error C2143: syntax error : missing '';'' before '',''
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(89) : error C2143: syntax error : missing '';'' before '')''
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(89) : error C2059: syntax error : '')''
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(90) : warning C4244: ''argument'' : conversion from ''double'' to ''float'', possible loss of data
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(90) : warning C4244: ''argument'' : conversion from ''double'' to ''float'', possible loss of data
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(90) : warning C4244: ''argument'' : conversion from ''double'' to ''float'', possible loss of data
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(91) : warning C4244: ''argument'' : conversion from ''double'' to ''float'', possible loss of data
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(91) : warning C4244: ''argument'' : conversion from ''double'' to ''float'', possible loss of data
D:\Programmeren\OpenGL\Tutorials\NeHe\Lesson01\Lesson1.cpp(91) : warning C4244: ''argument'' : conversion from ''double'' to ''float'', possible loss of data
Error executing cl.exe.

lesson1.exe - 9 error(s), 8 warning(s)

Anyone got an idea?

##### Share on other sites
I copy''n''pasted this code and it works.
IMO you don''t have M_PI defined.
Try to insert this at the beginning of the file :

#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif

##### Share on other sites
One q : How the heck did you(or whoever did), come up with this code?!?

-----------------------------
"Q: How many Microsoft engineers does it take to change a light bulb?
A: None. Bill Gates will just redefine Darkness(TM) as the new industry standard. "
~wUn LoVe tO aLl ThE mAlUs OuT tHeRe!~

##### Share on other sites
Lord of the Rings contest... Torus... I''m beginning to see a connection here

_________
"Maybe this world is another planet''''s hell." -- Aldous Huxley

##### Share on other sites
ring is NOT a torus... you have to modify it a bit...

vincoof : nice code, but it could be much more optimimized...

There are more worlds than the one that you hold in your hand...

##### Share on other sites
how did we come up with come up with this code ?
what would you like on top of that ?

_DarkWIng_:

> ring is NOT a torus... you have to modify it a bit...

Yes I know. That''s why I recommend using such algorithm instead of glut/glaux''s torus because I don''t think neither glut nor glaux can do it.

> nice code, but it could be much more optimimized

Much more optimized ? how the hell ?!!
There''s just _one_ triangle strip !
How can you make anything faster !?
I know that the computations are a bit heavy (I mean: sin and cos are computed twice, and without any cos table), but assuming you use a display list then the rendering is optimal. Isn''t it ?

• 9
• 13
• 41
• 15
• 14