PDA

View Full Version : torus knot problem

zukko
10-26-2004, 02:40 PM
im using the following code to draw a torus knot:

float r,x,y,z;
int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTranslatef(0,0,-5.0f);

glRotatef(90,1.0f,0.0f,0.0f);
glBegin(GL_TRIANGLE_STRIP);

for(double i = 0; i < 2 * PI; i += PI / 720)
{
r=0.5f*(2+sin(8*i));
x=r*cos(3*i);
y=r*cos(8*i);
z=r*sin(3*i);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(x,y,z);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(x+0.1f,y+0.1f,z+0.1f);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(x+0.2f,y+0.2f,z+0.2f);
}
glEnd();

return TRUE;
}
i have 2 questions:
1) how do i make the camera follow the knot or make the knot 'move'
2) how do i make the triangles be conected not just a secuence of triangles ??

plasmonster
10-27-2004, 11:38 PM
2) how do i make the triangles be conected not just a secuence of triangles ?Dust off the old calculus book and and lookup the chapter on vector valued functions. You can differentiate each component with respect to the time variable. The first derivative is velocity, the second is acceleration. The cross product of these 2 vectors defines a vector, B, that's orthogonal to both. The cross product of B and T (velocity), defines a third vector, N. These 3 vectors collectively define what's sometimes called a TNB triad. With a TNB, you can wind a circle around a point on the knot using N as the x-axis and B as the y-axis, for example. Create one of these "rings" at each time step. Once you have all the rings along the length of the knot, you could render the entire knot as a single tri-strip (like a string of little cylinders, so to speak).

1) how do i make the camera follow the knot or make the knot 'move'You can use the TNB you created above as a basis for the camera. Just stuff the TNB vectors into the columns of your camera matrix.

Note that there will be a twist in the orientation with this approach, since the N vector always points in the direction of curve concavity, so you might experiment with various splines instead if you don't care for that behavior.

zukko
10-28-2004, 04:04 PM
hi, thanks for your help, i have a new question, i have now T and N, what's the cross product, could you help me with some code plz thanks!

plasmonster
10-29-2004, 01:26 AM
I put together a lttle glut-based demo of this. You can copy the source and paste it into a console project. The code should build as is.

http://www.geocities.com/chalkboard9/knot.html

It might be cool to try and animate the knot over time; perform a morph of sorts. The camera movement code needs a good time function, but I opted to leave it out in the interest of brevity.

zukko
10-29-2004, 08:20 AM
thanks a lot !!