+
+static void
+unit_tube (Bool wire)
+{
+ int i;
+ int faces = TUBE_FACES;
+ GLfloat step = M_PI * 2 / faces;
+ GLfloat th;
+ int z = 0;
+
+ /* side walls
+ */
+ glFrontFace(GL_CCW);
+
+# ifdef SMOOTH_TUBE
+ glBegin(wire ? GL_LINES : GL_QUAD_STRIP);
+# else
+ glBegin(wire ? GL_LINES : GL_QUADS);
+# endif
+
+ for (i = 0, th = 0; i <= faces; i++)
+ {
+ GLfloat x = cos (th);
+ GLfloat y = sin (th);
+ glNormal3f(x, 0, y);
+ glVertex3f(x, 0.0, y);
+ glVertex3f(x, 1.0, y);
+ th += step;
+
+# ifndef SMOOTH_TUBE
+ x = cos (th);
+ y = sin (th);
+ glVertex3f(x, 1.0, y);
+ glVertex3f(x, 0.0, y);
+# endif
+ }
+ glEnd();
+
+ /* End caps
+ */
+ for (z = 0; z <= 1; z++)
+ {
+ glFrontFace(z == 0 ? GL_CCW : GL_CW);
+ glNormal3f(0, (z == 0 ? -1 : 1), 0);
+ glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
+ if (! wire) glVertex3f(0, z, 0);
+ for (i = 0, th = 0; i <= faces; i++)
+ {
+ GLfloat x = cos (th);
+ GLfloat y = sin (th);
+ glVertex3f(x, z, y);
+ th += step;
+ }
+ glEnd();
+ }
+}
+
+
+static void
+tube (GLfloat x1, GLfloat y1, GLfloat z1,
+ GLfloat x2, GLfloat y2, GLfloat z2,
+ GLfloat diameter, GLfloat cap_size,
+ Bool wire)
+{
+ GLfloat length, angle, a, b, c;
+
+ if (diameter <= 0) abort();
+
+ a = (x2 - x1);
+ b = (y2 - y1);
+ c = (z2 - z1);
+
+ length = sqrt (a*a + b*b + c*c);
+ angle = acos (a / length);
+
+ glPushMatrix();
+ glTranslatef(x1, y1, z1);
+ glScalef (length, length, length);
+
+ if (c == 0 && b == 0)
+ glRotatef (angle / (M_PI / 180), 0, 1, 0);
+ else
+ glRotatef (angle / (M_PI / 180), 0, -c, b);
+
+ glRotatef (-90, 0, 0, 1);
+ glScalef (diameter/length, 1, diameter/length);
+
+ /* extend the endpoints of the tube by the cap size in both directions */
+ if (cap_size != 0)
+ {
+ GLfloat c = cap_size/length;
+ glTranslatef (0, -c, 0);
+ glScalef (1, 1+c+c, 1);
+ }
+
+ unit_tube (wire);
+ glPopMatrix();
+}
+
+
+static void
+ctube (GLfloat diameter, GLfloat width, Bool wire)
+{
+ tube (0, 0, width/2,
+ 0, 0, -width/2,
+ diameter, 0, wire);
+}
+
+static void
+arm(GLfloat length,
+ GLfloat width1, GLfloat height1,
+ GLfloat width2, GLfloat height2,
+ Bool wire)
+{
+ glShadeModel(GL_FLAT);
+
+#if 0 /* don't need these - they're embedded in other objects */
+ /* draw end 1 */
+ glFrontFace(GL_CW);
+ glNormal3f(-1, 0, 0);
+ glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
+ glVertex3f(-length/2, -width1/2, -height1/2);
+ glVertex3f(-length/2, width1/2, -height1/2);
+ glVertex3f(-length/2, width1/2, height1/2);
+ glVertex3f(-length/2, -width1/2, height1/2);
+ glEnd();
+
+ /* draw end 2 */
+ glFrontFace(GL_CCW);
+ glNormal3f(1, 0, 0);
+ glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
+ glVertex3f(length/2, -width2/2, -height2/2);
+ glVertex3f(length/2, width2/2, -height2/2);
+ glVertex3f(length/2, width2/2, height2/2);
+ glVertex3f(length/2, -width2/2, height2/2);
+ glEnd();
+#endif
+
+ /* draw top */
+ glFrontFace(GL_CCW);
+ glNormal3f(0, 0, -1);
+ glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
+ glVertex3f(-length/2, -width1/2, -height1/2);
+ glVertex3f(-length/2, width1/2, -height1/2);
+ glVertex3f( length/2, width2/2, -height2/2);
+ glVertex3f( length/2, -width2/2, -height2/2);
+ glEnd();
+
+ /* draw bottom */
+ glFrontFace(GL_CW);
+ glNormal3f(0, 0, 1);
+ glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
+ glVertex3f(-length/2, -width1/2, height1/2);
+ glVertex3f(-length/2, width1/2, height1/2);
+ glVertex3f( length/2, width2/2, height2/2);
+ glVertex3f( length/2, -width2/2, height2/2);
+ glEnd();
+
+ /* draw left */
+ glFrontFace(GL_CW);
+ glNormal3f(0, -1, 0);
+ glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
+ glVertex3f(-length/2, -width1/2, -height1/2);
+ glVertex3f(-length/2, -width1/2, height1/2);
+ glVertex3f( length/2, -width2/2, height2/2);
+ glVertex3f( length/2, -width2/2, -height2/2);
+ glEnd();
+
+ /* draw right */
+ glFrontFace(GL_CCW);
+ glNormal3f(0, 1, 0);
+ glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
+ glVertex3f(-length/2, width1/2, -height1/2);
+ glVertex3f(-length/2, width1/2, height1/2);
+ glVertex3f( length/2, width2/2, height2/2);
+ glVertex3f( length/2, width2/2, -height2/2);
+ glEnd();
+
+ glFrontFace(GL_CCW);
+}
+
+