1 /* tube, Copyright (c) 2001, 2003 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 caps_p, 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);
74 for (z = 0; z <= 1; z++)
76 glFrontFace(z == 0 ? GL_CCW : GL_CW);
77 glNormal3f(0, (z == 0 ? -1 : 1), 0);
78 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
79 if (! wire) glVertex3f(0, z, 0);
80 for (i = 0, th = 0; i <= faces; i++)
93 unit_cone (int faces, Bool smooth, Bool cap_p, Bool wire)
96 GLfloat step = M_PI * 2 / faces;
104 glBegin(wire ? GL_LINES : GL_TRIANGLES);
112 for (i = 0; i < faces; i++)
114 glNormal3f(x0, 0, y0);
117 if (smooth) glNormal3f(x, 0, y);
126 if (smooth) glNormal3f(x, 0, y);
136 glNormal3f(0, -1, 0);
137 glBegin(wire ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
138 if (! wire) glVertex3f(0, 0, 0);
139 for (i = 0, th = 0; i <= faces; i++)
141 GLfloat x = cos (th);
142 GLfloat y = sin (th);
152 tube_1 (GLfloat x1, GLfloat y1, GLfloat z1,
153 GLfloat x2, GLfloat y2, GLfloat z2,
154 GLfloat diameter, GLfloat cap_size,
155 int faces, Bool smooth, Bool caps_p, Bool wire,
158 GLfloat length, X, Y, Z;
160 if (diameter <= 0) abort();
166 if (X == 0 && Y == 0 && Z == 0)
169 length = sqrt (X*X + Y*Y + Z*Z);
173 glTranslatef(x1, y1, z1);
174 glRotatef (-atan2 (X, Y) * (180 / M_PI), 0, 0, 1);
175 glRotatef ( atan2 (Z, sqrt(X*X + Y*Y)) * (180 / M_PI), 1, 0, 0);
176 glScalef (diameter, length, diameter);
178 /* extend the endpoints of the tube by the cap size in both directions */
181 GLfloat c = cap_size/length;
182 glTranslatef (0, -c, 0);
183 glScalef (1, 1+c+c, 1);
187 unit_cone (faces, smooth, caps_p, wire);
189 unit_tube (faces, smooth, caps_p, wire);
196 tube (GLfloat x1, GLfloat y1, GLfloat z1,
197 GLfloat x2, GLfloat y2, GLfloat z2,
198 GLfloat diameter, GLfloat cap_size,
199 int faces, Bool smooth, Bool caps_p, Bool wire)
201 tube_1 (x1, y1, z1, x2, y2, z2, diameter, cap_size,
202 faces, smooth, caps_p, wire,
208 cone (GLfloat x1, GLfloat y1, GLfloat z1,
209 GLfloat x2, GLfloat y2, GLfloat z2,
210 GLfloat diameter, GLfloat cap_size,
211 int faces, Bool smooth, Bool cap_p, Bool wire)
213 tube_1 (x1, y1, z1, x2, y2, z2, diameter, cap_size,
214 faces, smooth, cap_p, wire,