Adding triangles to my torus to make a surface.

Started by
11 comments, last by dawberj3 18 years, 8 months ago
The glutWireTorus(); draws a torus with triangles to make a surface, I want to achieve something similar. It will be to draw "pipes" so im just building the basic functionality atm. At the moment i can draw circles like this: but i want to do this: so i can add a surface to it: Here is my code that I have so far:

static void drawCircle(float radius)
{

  glBegin(GL_POINTS);
  
  for (int i = 0; i < 360; i=i+27)
  {
    float degInRad = i * DEG_TO_RAD;
    glVertex3f(0.0,
               cos(degInRad)*radius,
               sin(degInRad)*radius);
  }

  glEnd ( ) ;

}


static void drawShapes(void)
{

  float radius = 100;
  
  glColor3f(1.0f,0.0f,0.0f);
 
  for (int i = 0; i < 40; i++)
  {
    glPushMatrix();      
      glTranslatef(sin((9*i)*DEG_TO_RAD)*radius,0.0,cos((9*i)*DEG_TO_RAD)*radius);
      glRotatef(i*9,0,1,0);
      drawCircle(10);
    glPopMatrix();     
     
  }

}

Now i want to add lines to makes triangles so i can add a smooth surface and colour it. Can anyone help? Thanks
Advertisement
Here is a thread from a while back where we discussed mesh construction for various shapes, including tori. I think I posted some code there as well.

'Hope that helps.
Im having a bit of trouble to creating the GL_QUADS bit to connect vertices together along the curve. Whats the best way to construct them?
Quote:Im having a bit of trouble to creating the GL_QUADS bit to connect vertices together along the curve. Whats the best way to construct them?
Did you look at the code from the earlier thread? (A sincere question - not trying to be flippant.)

I don't know if there's a single best way. The version of my mesh class that I posted is just a sketch of sorts and doesn't have variable level of detail, so it just constructs one set of triangles for the mesh, and that's it. Usually mesh construction of this sort just comes down to bookkeeping - so many vertices for the inner ring, so many vertices for the outer rings, finding the correct offset to get from one ring to the next, that sort of thing. You just kind of have to figure it out.

How you triangulate will of course depend on in what order you created the vertices. That's about as specific as I can be without being familiar with your code.
Im reading through your code now and Im trying to get used to some syntax you have used that I havnt before, can you please explain what the following means and why its there:

namespace jyk {

template <class T = float> (You use this <T> business alot)

Also can you tell me how you used this code to draw whatever you want to draw. Thx

What Im trying to achieve is, if you have a bezier curve in 3D space, i want to construct a mesh to make a pipe that follows this curve. Every 10 units or so it creates another set of vertices to make the pipe 'curvy'.

Thx :D

Quote:Im reading through your code now and Im trying to get used to some syntax you have used that I havnt before, can you please explain what the following means and why its there:

namespace jyk {

template <class T = float>
Those things don't really affect the triangulation code. But, a namespace is just a way to group similar code together and prevent name clashes between libraries. The template parameter is so you can create objects of the class using different real number types, such as floats or doubles. For the sake of example, just replace all the T's with 'float'.
Quote:Also can you tell me how you used this code to draw whatever you want to draw.
I just used vertex arrays, but you can do it however you want (if that's what you're asking).
Quote:What Im trying to achieve is, if you have a bezier curve in 3D space, i want to construct a mesh to make a pipe that follows this curve. Every 10 units or so it creates another set of vertices to make the pipe 'curvy'.
You should be able to use the torus or torus knot code as a basis for this. In each case the function follows a curve (circle for the torus, torus knot for the torus knot), and creates rings of vertices around the curve at regular intervals. The trick is finding an orthonormal basis at each step to build the rings with. For Bezier, you could just fake it by building a basis using the vector from the current point to the next, or, you might google 'Frenet frame'.
I assume you are reading blackpawn's PQ torus knot tutorial. Basically what I did was apply a lathing algo.

For the PQ torus here's an example that travels it's inside:

jejunum
Another viewed from afar:

Peristalsis



As for the lathing part:

	//lathing	unsigned int s, u, slice, maxvert;	maxvert = max_point;    int i = 0;	for (s = 0; s < (rings); s++)	{		 slice = s * bands;		 for (u = 0; u <= bands; u++)	  //duplicate texture ( not bands - 1)		 {			 poly.v1=(u+bands+1+(slice)) % maxvert;			 poly.v2=(u+bands+(slice)) % maxvert;			 poly.v3=(u+(slice)) % maxvert;			 poly[i+1].v1=(u+(slice)) % maxvert;			 poly[i+1].v2=(u+1+(slice)) % maxvert;			 poly[i+1].v3=(u+bands+1+(slice)) % maxvert;			 i += 2;		 }	}
Hi.
jyk thanks for all your help, Ill get back to you when I've had a little play around with some things.

The inner ring is points along the curve and the outring is the vertices around that?
Im half way converting your code for the drawTorus, this is what i have so far:

static void drawTorus(void){  Vector m_verts[400];    Vector innerRing[20];    for (int i = 0; i < 20; ++i)  {      float angle = (9*i)*DEG_TO_RAD;      float x = cos(angle) * 10;    float y = sin(angle) * 10;        innerRing.i = x;    innerRing.j = y;    innerRing.k = 0.0;      }      // Now for the actual verts, we create a ring around each point in the inner ring      for (int i = 0; i < 20; ++i)  {    // Basis vectors for this ring are the z axis and the normalized vector    // from the point to the origin    Vector u = Vector3<T>::Normalize(-p) * r2; // Could be p also            Vector v = Vector3<T>::Z_AXIS * r2;    // Create the verts        for (int j = 0; j < 20; ++j)    {          float angle = (9*j)*DEG_TO_RAD;          float c = cos(angle);      float s = sin(angle);            m_verts = innerRing<span style="font-weight:bold;"> + c * u + s * v;<br>     <br>    }<br>  }<br>  <br>  <span class="cpp-comment">// … The rest not entered yet.</span><br><br>}<br><br><br><br></pre></div><!–ENDSCRIPT–><br><br>I think what I have done so far is correct, but i need help converting this:<br><br>Vector u = Vector::Normalize(-innerRing<span style="font-weight:bold;">) * 10;        <br>Vector v = Vector::Z_AXIS * 10;<br><br>My vector class is just this at the moment:<br><br><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre><br><span class="cpp-directive">#include</span> &lt;GL/gl.h&gt;		<br><span class="cpp-directive">#include</span> &lt;GL/glu.h&gt;<br><br><span class="cpp-keyword">class</span> Vector  <br>{<br>  <span class="cpp-keyword">public</span>:<br>  <br>    GLfloat i;<br>    GLfloat j;<br>    GLfloat k;<br>    <br>    <span class="cpp-keyword">void</span> <span class="cpp-keyword">operator</span> *=(GLfloat scalar);<br>  <br>    Vector(<span class="cpp-keyword">void</span>);<br>   ~Vector(<span class="cpp-keyword">void</span>);<br>	<br>};<br><br><br><br></pre></div><!–ENDSCRIPT–><br><br>Can you help me to convert the rest of the code? Im not sure what your indices array is (Cant actually remember what an indice is lol :p ) and can you explain the triangulate bit?<br><br>Thx.
Hi,

I'm just home for lunch at the moment, but I should be able to answer your questions later this evening.

This topic is closed to new replies.

Advertisement