title: OpenGL Tube Along a Path
body: <p>So I'm playing around with OpenGL and trying to figure out how to draw some fun shapes.</p>
  
  <p>Right now, I'm working on a tube. I can draw a straight tube just fine with:</p>
  
  <pre><code>void tube(GLfloat radius, GLfloat segment_length) {
      glPolygonMode(GL_BACK, GL_NONE);
      glPolygonMode(GL_FRONT, GL_FILL);
  
      glPushMatrix(); {
          GLfloat z1 = 0.0;
          GLfloat z2 = segment_length;
  
          GLfloat y_offset = 0.0;
          GLfloat y_change = 0.00;
  
          int i = 0;
          int j = 0;
          for (j = 0; j &lt; 20; j++) {
              glPushMatrix(); {
                  glBegin(GL_TRIANGLE_STRIP); {
                      for (i = 360; i &gt;= 0; i--) {
                          GLfloat theta = i * pi/180;
                          GLfloat x = radius * cos(theta);
                          GLfloat y = radius * sin(theta) + y_offset;
  
                          glVertex3f(x, y, z1);
                          glVertex3f(x, y, z2);
                      }
                  } glEnd();
              } glPopMatrix();
  
              // attach the front of the next segment to the back of the previous
              z1 = z2;
              z2 += segment_length;
  
              // make some other adjustments
              y_offset += y_change;
          }
      } glPopMatrix();
  }
  </code></pre>
  
  <p>However, I haven't figured out how to make the tube follow any predefined path like a spiral, or even a simple line. If you change y_change to something like 0.01, it will draw each tube segment offset an additional 0.01 in the y direction. That great, but how can I make each segment point in that direction? In other words, right now, each segment is drawn so that they all face the same direction, and the direction is not the direction of the tube (since with y_change = 0.01, the direction is slightly "up").</p>
  
  <p>I'm not sure how to proceed. I've played with vectors by getting the vector between the previous segment and the current one, but I'm not really sure what to do with it past that.</p>