Archived

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

bakery2k1

Torus coordinates

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
mAdMaLuDaWg:
how did we come up with come up with this code ?
huh. bakery asked. I answered.
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 ?

Share this post


Link to post
Share on other sites
vincoof : heheh... just stunned by the code... I guess when you become an expert in OpenGL, code like this is easy to come up with.
BTW, I''m dying trying to figure out a problem with a Bezier curve program I created... mind checking it out... it''s under a post I made a couple of days ago.
Thanks.

-----------------------------
"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 this post


Link to post
Share on other sites
Shannara: That link is awesome. Thank you for posting it !
''center'' (in this topic) represents ''r0'' (in the link), and ''radius'' represents ''r1''.


mAdMaLuDaWg:
thank you. but that''s not the work of an expert.
I wrote that when I was a student, and since it is a pretty good algorithm I kept it.
what does make you stunned ?
the ability to play with math functions (such as sin and cos) to serve _the_ 3D ?
the lisibility of the code ?
the possibility to use one triangle strip only (instead of multiple quadstrips) ?

Share this post


Link to post
Share on other sites
He thnx for the link I''m going to check it out.


And about the code.
Well the way you use the ''math functions (such as sin and cos) to serve _the_ 3D'' make me stunned

But I shall search on google if I can find a good explanation about the sin and cos functions

Share this post


Link to post
Share on other sites
vincoof : I was refereing to math calculation. Those sin/cos are a BIG hit (specialy if you use original functions) and NEVER use (float)i conversion when performance matters.

from your code:
theta / (2 * M_PI)
calculte someVal = 0.5f / M_PI; onece then use theta * someVal;

...

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

Share this post


Link to post
Share on other sites
Ok I see the optimizations you''re talking about.
I know I could win a lot.
But I don''t really care about that because the model is compiled in the display list. So, only the list compilation is slow. The list call is not slower, which is the most important.
I prefer keeping the code lisible, so I can modify it more easily if I need to.

Anyway, if the torus had to be drawn without display lists, I agree that some optimizations would be welcomed. But I assume that display lists are available.

Share this post


Link to post
Share on other sites
good point.. with display list this realy doesn''t matter. But I dont use them becouse I need to change details while running (and compiling lists at frame to frame basis is a big no-no)

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

Share this post


Link to post
Share on other sites
eh eh of course this needs some optims for drawing without display lists, and of course you gotta not use display lists for a dynamic model (unless the rendering fits some particular puposes).

Animated torus, eh ?
You could probably have two vertex arrays and then just morph between the two.
This way you''d *just* need to initialize the ''start'' and ''end'' vertex arrays, and rendering would *just* need an interpolation (which can be done very fast). Such a method could use ''slow'' initializations like I did above, because they''re just inits.

Share this post


Link to post
Share on other sites