Skip to main content
gamedev.net
🔒 Locked

Torus, how?

Started by Venerable Vampire 5 replies 1.2k views
Original Post
Venerable Vampire
Venerable Vampire
Just out of curiosity, what is an easy way to generate the triangle mesh for a torus of specified inner and outer radi and the number of splits around the edges? I''v been playing around in OpenGL demos and wrote a sphere generation thingy, but toruses (tori?) seem kinda fun too. Thanks in advance. Life would be so much easier if we could just get the source code.
--------------------------------------------------------Life would be so much easier if we could just get the source code.
C-Junkie
C-Junkie
One of NeHe's tutorials does this.

[edited by - C-Junkie on May 2, 2004 8:19:59 PM]
benjamin bunny
benjamin bunny
Well a torus is just a circle extruded along another circle, so if you know the radius of both circles then you can describe a point on a torus by just 2 values: the angles about the first and the second circle. ie, [x y z]T=T(a1,a2). That should help clarify the problem a bit more. You can fill in the details yourself .

[edited by - benjamin bunny on May 3, 2004 8:49:54 AM]
cow_in_the_well
cow_in_the_well
This page should help:
http://mathworld.wolfram.com/Torus.html

I used the parametric equation on this page to geberate my torus mesh. I don''t have the source on this machine, but if you want it I can post it when I get home .

- Thomas
Venerable Vampire
Venerable Vampire
Thanks a bunch people! I have to go to school now but this should let me code up something when I get back.

Life would be so much easier if we could just get the source code.
--------------------------------------------------------Life would be so much easier if we could just get the source code.
cow_in_the_well
cow_in_the_well
Well, in response to your sig, here's the source code . Its from an 3D app i am writing so you'll have to paraphrase it into your own program -_^.


// http://mathworld.wolfram.com/Torus.html

MBoolean MCmdCreateTorus::Execute (MAttributeArray &params)
{
m_pTheMesh = (MMesh*)MGetScene()->CreateObject ("MMesh", NULL);
if (!m_pTheMesh)
return NULL;

MDynamicArray<MVector3D> FacePoints;

MFloat fOuterRadius = params[0].GetFloat();
MFloat fTubeRadius = params[1].GetFloat();
MInt nVertSections = params[2].GetInteger();
MInt nHorzSections = params[3].GetInteger();

if (nVertSections < 2) nVertSections = 2;
if (nHorzSections < 2) nHorzSections = 2;

MFloat fDU = (MMath::PI) / (MFloat)nHorzSections;
MFloat fDV = (MMath::PI) / (MFloat)nVertSections;

MFloat c = fOuterRadius-(fTubeRadius/2);
MFloat a = fTubeRadius;

for (MFloat fV = 0; fV < 2*MMath::PI; fV += fDV)
{
for (MFloat fU = 0; fU < 2*MMath::PI; fU += fDU)
{
FacePoints.Clear();

FacePoints.InsertBack (MVector3D ( (c + a*cosf(fV))*cosf(fU+fDU),
(a*sinf(fV)),
(c + a*cosf(fV))*sinf(fU+fDU) ) );
FacePoints.InsertBack (MVector3D ( (c + a*cosf(fV))*cosf(fU),
(a*sinf(fV)),
(c + a*cosf(fV))*sinf(fU) ) );
FacePoints.InsertBack (MVector3D ( (c + a*cosf(fV+fDV))*cosf(fU),
(a*sinf(fV+fDV)),
(c + a*cosf(fV+fDV))*sinf(fU) ) );
FacePoints.InsertBack (MVector3D ( (c + a*cosf(fV+fDV))*cosf(fU+fDU),
(a*sinf(fV+fDV)),
(c + a*cosf(fV+fDV))*sinf(fU+fDU) ) );

m_pTheMesh->AddFace (FacePoints, TRUE, MMath::EPSILON*2);
}
}



m_pTheMesh->SetName ("Torus");
MGetSystem ()->RedrawViews ();

return TRUE;
}


[edited by - cow_in_the_well on May 3, 2004 11:03:09 AM]
Venerable Vampire
Venerable Vampire
I just got back from school and haven''t actually started coding yet, but thanks for the code (and for the comment on the sig)!

Life would be so much easier if we could just get the source code.
--------------------------------------------------------Life would be so much easier if we could just get the source code.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.