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

by lacas   Last Updated August 09, 2018 04:13 AM

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?

Tags : opengl 3d-meshes

Answers 1

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

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.

March 15, 2012 07:33 AM

Related Questions

Missing triangles in model

Updated August 08, 2018 23:13 PM

Draw edges of a complex 3D shape

Updated December 30, 2017 21:13 PM

Efficiently drawing a batch of meshes?

Updated September 03, 2018 22:13 PM