1
/* tube, Copyright (c) 2001, 2003, 2007 Jamie Zawinski <jwz@jwz.org>
1
/* tube, Copyright (c) 2001-2011 Jamie Zawinski <jwz@jwz.org>
2
2
* Utility functions to create tubes and cones in GL.
4
4
* Permission to use, copy, modify, distribute, and sell this software and its
37
40
GLfloat x, y, x0=0, y0=0;
44
struct { XYZ p; XYZ n; GLfloat s, t; } *array;
46
arraysize = (faces+1) * 6;
47
array = (void *) calloc (arraysize, sizeof(*array));
52
/* #### texture coords are currently not being computed */
43
glBegin (wire_p ? GL_LINES : (smooth ? GL_QUAD_STRIP : GL_QUADS));
57
69
for (i = 0; i < faces; i++)
71
array[out].p.x = x; /* bottom point A */
76
array[out].n = array[out].p; /* its own normal */
62
glNormal3f(x0, 0, y0);
79
array[out].n.x = x0; /* mid-plane normal */
86
array[out].p.x = x; /* top point A */
89
array[out].n = array[out-1].n; /* same normal */
73
99
x0 = cos (th + s2);
74
100
y0 = sin (th + s2);
102
array[out].p.x = x; /* top point B */
105
array[out].n = array[out-1].n; /* same normal */
109
array[out] = array[out-3]; /* bottom point A */
112
array[out] = array[out-2]; /* top point B */
115
array[out].p.x = x; /* bottom point B */
118
array[out].n = array[out-1].n; /* same normal */
125
if (out >= arraysize) abort();
128
glEnableClientState (GL_VERTEX_ARRAY);
129
glEnableClientState (GL_NORMAL_ARRAY);
130
glEnableClientState (GL_TEXTURE_COORD_ARRAY);
132
glVertexPointer (3, GL_FLOAT, sizeof(*array), &array[0].p);
133
glNormalPointer ( GL_FLOAT, sizeof(*array), &array[0].n);
134
glTexCoordPointer (2, GL_FLOAT, sizeof(*array), &array[0].s);
136
glDrawArrays ((wire_p ? GL_LINES :
137
(smooth ? GL_TRIANGLE_STRIP : GL_TRIANGLES)),
86
144
for (z = 0; z <= 1; z++)
88
glFrontFace(z == 0 ? GL_CCW : GL_CW);
89
glNormal3f(0, (z == 0 ? -1 : 1), 0);
90
glBegin(wire_p ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
91
if (! wire_p) glVertex3f(0, z, 0);
92
for (i = 0, th = 0; i <= faces; i++)
154
array[out].n.y = (z == 0 ? -1 : 1);
160
for (i = (z == 0 ? 0 : faces);
161
(z == 0 ? i <= faces : i >= 0);
162
i += (z == 0 ? 1 : -1)) {
94
163
GLfloat x = cos (th);
95
164
GLfloat y = sin (th);
166
array[out] = array[0]; /* same normal and texture */
172
th += (z == 0 ? step : -step);
175
if (out >= arraysize) abort();
178
glVertexPointer (3, GL_FLOAT, sizeof(*array), &array[0].p);
179
glNormalPointer ( GL_FLOAT, sizeof(*array), &array[0].n);
180
glTexCoordPointer (2, GL_FLOAT, sizeof(*array), &array[0].s);
182
glDrawArrays ((wire_p ? GL_LINE_LOOP : GL_TRIANGLE_FAN), 0, out);
114
199
GLfloat x, y, x0, y0;
202
struct { XYZ p; XYZ n; GLfloat s, t; } *array;
204
arraysize = (faces+1) * 3;
205
array = (void *) calloc (arraysize, sizeof(*array));
206
if (! array) abort();
210
/* #### texture coords are currently not being computed */
119
glBegin(wire_p ? GL_LINES : GL_TRIANGLES);
127
222
for (i = 0; i < faces; i++)
129
glNormal3f(x0, 0, y0);
132
if (smooth) glNormal3f(x, 0, y);
224
array[out].p.x = x; /* bottom point A */
229
array[out].n = array[out].p; /* its own normal */
232
array[out].n.x = x0; /* mid-plane normal */
239
array[out].p.x = 0; /* tip point */
243
array[out].n.x = x0; /* mid-plane normal */
136
250
x0 = cos (th + s2);
141
if (smooth) glNormal3f(x, 0, y);
255
array[out].p.x = x; /* bottom point B */
260
array[out].n = array[out].p; /* its own normal */
262
array[out].n = array[out-1].n; /* same normal as other two */
266
if (out >= arraysize) abort();
270
glEnableClientState (GL_VERTEX_ARRAY);
271
glEnableClientState (GL_NORMAL_ARRAY);
272
glEnableClientState (GL_TEXTURE_COORD_ARRAY);
274
glVertexPointer (3, GL_FLOAT, sizeof(*array), &array[0].p);
275
glNormalPointer ( GL_FLOAT, sizeof(*array), &array[0].n);
276
glTexCoordPointer (2, GL_FLOAT, sizeof(*array), &array[0].s);
278
glDrawArrays ((wire_p ? GL_LINES : GL_TRIANGLES), 0, out);
152
glNormal3f(0, -1, 0);
153
glBegin(wire_p ? GL_LINE_LOOP : GL_TRIANGLE_FAN);
154
if (! wire_p) glVertex3f(0, 0, 0);
155
299
for (i = 0, th = 0; i <= faces; i++)
157
301
GLfloat x = cos (th);
158
302
GLfloat y = sin (th);
304
array[out] = array[0]; /* same normal and texture */
311
if (out >= arraysize) abort();
314
glVertexPointer (3, GL_FLOAT, sizeof(*array), &array[0].p);
315
glNormalPointer ( GL_FLOAT, sizeof(*array), &array[0].n);
316
glTexCoordPointer (2, GL_FLOAT, sizeof(*array), &array[0].s);
318
glDrawArrays ((wire_p ? GL_LINE_LOOP : GL_TRIANGLE_FAN), 0, out);