# Opengl create tunnel TRIANGLES and texture coords on a 3d path

I have a path, lets say a bezier, or a circle.

I d like to make a continously 3d cylinder (tunnel) on it (R=0.5f)

How can I calculate tha TRIangles (TRIANGLE_STRIP) coordinate, and the texture coords right?

## 1 Answer

You need normal and binormal for each point in your curve, as illustrated in this picture as n and b:

You can calculate the tangent t from the curve, it's the first derivative. If the derivative is not available, you can approximate it with a direction vector from previous sample point to the next one. Binormal is simply a cross product from tangent and normal. The only problem is how to define the normal. Normals can be arbitrary, but it should always be on the "same side of the curve" so that adjacent vertices in the tube are close to each other and not jumping around. Four ways to calculate the normal comes to my mind.

1. Manually define also normals for each control point in your curve and interpolate from those.
2. Define an up-vector or something similar in the world and calculate the normal from cross product of the up vector and tangent. In this case the curve must not go directly upwards. I used this myself recently, and it seemed to work well.
3. Choose the first normal arbitrarily and calculate the next normals always so that it is as close to the previous as possible.
4. If your curve lies in a plane, which is the case for a circle, you can use the plane normal directly.

When you have the normals and binormals, you can generate the tunnel vertices simply with a linear combination of those vectors.

for (i = 0; i < pointCount; i++) {
angle = 2 * PI * i / pointCount;
tunnelPoint[i] = curvePoint + (normal * cos(angle) + binormal * sin(angle)) * radius;
}


Texture coordinates are simple to calculate if your points are evenly distributed or the stretching is not a problem. If those are a problem, you need to numerically first calculate the length of the curve and then also numerically find the correct texture coordinate for any given point in the curve. This gives the u part. v part is simply i / pointCount in the code above.