# Adding triangles to my torus to make a surface.

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

## Recommended Posts

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)
{
glVertex3f(0.0,
}

glEnd ( ) ;

}

static void drawShapes(void)
{

float radius = 100;

glColor3f(1.0f,0.0f,0.0f);

for (int i = 0; i < 40; i++)
{
glPushMatrix();
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

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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
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'.

##### Share on other sites
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;		 }	}

##### Share on other sites
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?

##### Share on other sites
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[i * 20 + j] = innerRing + c * u + s * v;         }  }    // ... The rest not entered yet.}

I think what I have done so far is correct, but i need help converting this:

Vector u = Vector::Normalize(-innerRing) * 10;
Vector v = Vector::Z_AXIS * 10;

My vector class is just this at the moment:

#include <GL/gl.h>		#include <GL/glu.h>class Vector  {  public:      GLfloat i;    GLfloat j;    GLfloat k;        void operator *=(GLfloat scalar);      Vector(void);   ~Vector(void);	};

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?

Thx.

##### Share on other sites
Hi,

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

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633296
• Total Posts
3011245
• ### Who's Online (See full list)

There are no registered users currently online

×