1 /* tube, Copyright (c) 2001 Jamie Zawinski <jwz@jwz.org>
2 * Utility functions to create tubes and cones in GL.
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
20 unit_tube (int faces, Bool smooth, Bool wire)
23 GLfloat step = M_PI * 2 / faces;
26 GLfloat x, y, x0=0, y0=0;
32 glBegin (wire ? GL_LINES : (smooth ? GL_QUAD_STRIP : GL_QUADS));
46 for (i = 0; i < faces; i++)
51 glNormal3f(x0, 0, y0);
73 for (z = 0; z <= 1; z++)
75 glFrontFace(z == 0 ? GL_CCW : GL_CW);
76 glNormal3f(0, (z == 0 ? -1 : 1), 0);
77 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
78 if (! wire) glVertex3f(0, z, 0);
79 for (i = 0, th = 0; i <= faces; i++)
92 unit_cone (int faces, Bool smooth, Bool wire)
95 GLfloat step = M_PI * 2 / faces;
103 glBegin(wire ? GL_LINES : GL_TRIANGLES);
111 for (i = 0; i < faces; i++)
113 glNormal3f(x0, 0, y0);
116 if (smooth) glNormal3f(x, 0, y);
125 if (smooth) glNormal3f(x, 0, y);
133 glNormal3f(0, -1, 0);
134 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
135 if (! wire) glVertex3f(0, 0, 0);
136 for (i = 0, th = 0; i <= faces; i++)
138 GLfloat x = cos (th);
139 GLfloat y = sin (th);
148 tube_1 (GLfloat x1, GLfloat y1, GLfloat z1,
149 GLfloat x2, GLfloat y2, GLfloat z2,
150 GLfloat diameter, GLfloat cap_size,
151 int faces, Bool smooth, Bool wire,
154 GLfloat length, X, Y, Z;
156 if (diameter <= 0) abort();
162 if (X == 0 && Y == 0 && Z == 0)
165 length = sqrt (X*X + Y*Y + Z*Z);
169 glTranslatef(x1, y1, z1);
170 glRotatef (-atan2 (X, Y) * (180 / M_PI), 0, 0, 1);
171 glRotatef ( atan2 (Z, sqrt(X*X + Y*Y)) * (180 / M_PI), 1, 0, 0);
172 glScalef (diameter, length, diameter);
174 /* extend the endpoints of the tube by the cap size in both directions */
177 GLfloat c = cap_size/length;
178 glTranslatef (0, -c, 0);
179 glScalef (1, 1+c+c, 1);
183 unit_cone (faces, smooth, wire);
185 unit_tube (faces, smooth, wire);
192 tube (GLfloat x1, GLfloat y1, GLfloat z1,
193 GLfloat x2, GLfloat y2, GLfloat z2,
194 GLfloat diameter, GLfloat cap_size,
195 int faces, Bool smooth, Bool wire)
197 tube_1 (x1, y1, z1, x2, y2, z2, diameter, cap_size, faces, smooth, wire,
203 cone (GLfloat x1, GLfloat y1, GLfloat z1,
204 GLfloat x2, GLfloat y2, GLfloat z2,
205 GLfloat diameter, GLfloat cap_size,
206 int faces, Bool smooth, Bool wire)
208 tube_1 (x1, y1, z1, x2, y2, z2, diameter, cap_size, faces, smooth, wire,