I want to draw a tube along a path defined by a list of points.
What I figured is that I could basically draw circles around each of my points, and then connect the circles (or, rather, points on the circles) together using GL_QUAD_STRIP.
So, my code for creating the points on the circles is something like:
point_of_circle[0] = pathVertices[i][0] + TUBE_RADIUS * cos(j/TUBE_SEGMENTS * 2*M_PI));
point_of_circle[1] = pathVertices[i][1] + TUBE_RADIUS * sin(j/TUBE_SEGMENTS * 2*M_PI));
point_of_circle[2] = pathVertices[i][2];
where i
is the index of the path vertex (i.e. one of the original points along which I want the tube to be drawn), and j
is the index of the current point of the circle being created (it goes from 0 to TUBE_SEGMENTS
).
Now, the problem is that if I create the circles in this way, they are always oriented in the same way (the circles are all "parallel" to each other, seeing as I always do point_of_circle[2] = pathVertices[i][2]
), which is not what I want because then the structure doesn't look like a proper tube.
I figured what I could do is compute vectors that would be tangent to the path I'm drawing. Then, I'd need to somehow make the circles face the correct direction depending on the tangent computed.
For instance, if the tangent computed is 0,0,1
, the code should stay as is. However, if the tangent is computed as anything else, the circle should be rotated.
For example, if the tangent computed is 0,1,0
, the y coordinate should be the same for the entire circle, and if you are looking along the z axis, you should just see a horizontal segment for the circle.
If the tangent was 1,0,0
, you should see a vertical segment for the circle if looking along the z axis.
How can I write code that would create the circles properly depending on the tangent computed? I hope my explanation was clear enough, I'm a beginner in OpenGL and most of 3D math.