http://slackware.bholcomb.com/slackware/slackware-11.0/source/xap/xscreensaver/xscree...
[xscreensaver] / hacks / glx / extrusion-taper.c
1 /* 
2  * taper.c
3  * 
4  * FUNCTION:
5  * Draws a tapered screw shape.
6  *
7  * HISTORY:
8  * -- created by Linas Vepstas October 1991
9  * -- heavily modified to draw more texas shapes, Feb 1993, Linas
10  * -- converted to use GLUT -- December 1995, Linas
11  *
12  */
13
14 #include "extrusion.h"
15
16 #include <math.h>
17 #include <stdlib.h>
18
19 #ifndef NULL
20 #define NULL ((void *) 0x0)
21 #endif /* NULL */
22
23 /* Some <math.h> files do not define M_PI... */
24 #ifndef M_PI
25 #define M_PI 3.14159265358979323846
26 #endif
27
28 /* =========================================================== */
29
30 #define SCALE 3.33333
31 #define CONTOUR(x,y) {                                  \
32    double ax, ay, alen;                                 \
33    contour[i][0] = SCALE * (x);                         \
34    contour[i][1] = SCALE * (y);                         \
35    if (i!=0) {                                          \
36       ax = contour[i][0] - contour[i-1][0];             \
37       ay = contour[i][1] - contour[i-1][1];             \
38       alen = 1.0 / sqrt (ax*ax + ay*ay);                \
39       ax *= alen;   ay *= alen;                         \
40       norms [i-1][0] = ay;                              \
41       norms [i-1][1] = -ax;                             \
42    }                                                    \
43    i++;                                                 \
44 }
45
46 #define NUM_PTS (25)
47
48 static double contour [NUM_PTS][2];
49 static double norms [NUM_PTS][2];
50
51 static void init_contour (void)
52 {
53    int i;
54
55    /* outline of extrusion */
56    i=0;
57    CONTOUR (1.0, 1.0);
58    CONTOUR (1.0, 2.9);
59    CONTOUR (0.9, 3.0);
60    CONTOUR (-0.9, 3.0);
61    CONTOUR (-1.0, 2.9);
62
63    CONTOUR (-1.0, 1.0);
64    CONTOUR (-2.9, 1.0);
65    CONTOUR (-3.0, 0.9);
66    CONTOUR (-3.0, -0.9);
67    CONTOUR (-2.9, -1.0);
68
69    CONTOUR (-1.0, -1.0);
70    CONTOUR (-1.0, -2.9);
71    CONTOUR (-0.9, -3.0);
72    CONTOUR (0.9, -3.0);
73    CONTOUR (1.0, -2.9);
74
75    CONTOUR (1.0, -1.0);
76    CONTOUR (2.9, -1.0);
77    CONTOUR (3.0, -0.9);
78    CONTOUR (3.0, 0.9);
79    CONTOUR (2.9, 1.0);
80
81    CONTOUR (1.0, 1.0);   /* repeat so that last normal is computed */
82 }
83    
84 /* =========================================================== */
85
86 #define PSIZE 40
87 static double path[PSIZE][3];
88 static double twist[PSIZE];
89 static double taper[PSIZE];
90
91 static void init_taper (void) {
92    int j;
93    double z, deltaz;
94    double ang, dang;
95
96    z = -10.0;
97    deltaz = 0.5;
98
99    ang = 0.0;
100    dang = 20.0;
101    for (j=0; j<40; j++) {
102       path[j][0] = 0x0;
103       path[j][1] = 0x0;
104       path[j][2] = z;
105
106       twist[j] = ang;
107       ang += dang;
108
109       taper[j] = 0.1 * sqrt (9.51*9.51 - z*z);
110
111       z += deltaz;
112    }
113
114    taper[0] = taper[1];
115    taper[39] = taper[38];
116
117 }
118
119 /* =========================================================== */
120
121 /* controls shape of object */
122 extern float lastx;
123 extern float lasty;
124
125 void InitStuff_taper (void)
126 {
127    int style;
128
129    /* configure the pipeline */
130    style = TUBE_JN_CAP;
131    style |= TUBE_CONTOUR_CLOSED;
132    style |= TUBE_NORM_FACET;
133    style |= TUBE_JN_ANGLE;
134    gleSetJoinStyle (style);
135
136    init_contour();
137    init_taper();
138 }
139
140 /* =========================================================== */
141
142 static void gleTaper (int ncp, 
143                gleDouble contour[][2], 
144                gleDouble cont_normal[][2], 
145                gleDouble up[3],
146                int npoints,
147                gleDouble point_array[][3],
148                float color_array[][3],
149                gleDouble taper[],
150                gleDouble twist[])
151 {
152    int j;
153    gleAffine *xforms;
154    double co, si, angle;
155
156    /* malloc the extrusion array and the twist array */
157    xforms = (gleAffine *) malloc (npoints * sizeof(gleAffine));
158
159    for (j=0; j<npoints; j++) {
160       angle = (M_PI/180.0) * twist[j];
161       si = sin (angle);
162       co = cos (angle);
163       xforms[j][0][0] = taper[j] * co;
164       xforms[j][0][1] = - taper[j] * si;
165       xforms[j][0][2] = 0.0;
166       xforms[j][1][0] = taper[j] * si;
167       xforms[j][1][1] = taper[j] * co;
168       xforms[j][1][2] = 0.0;
169    }
170
171    gleSuperExtrusion (ncp,               /* number of contour points */
172                 contour,    /* 2D contour */
173                 cont_normal, /* 2D contour normals */
174                 up,           /* up vector for contour */
175                 npoints,           /* numpoints in poly-line */
176                 point_array,        /* polyline */
177                 color_array,        /* color of polyline */
178                 xforms);
179
180    free (xforms);
181 }
182
183 /* =========================================================== */
184
185 void DrawStuff_taper (void) {
186    int j;
187    double ang, dang;
188    double z, deltaz;
189    double ponent;
190    z=-1.0;
191    deltaz = 1.999/38;
192    ang = 0.0;
193    dang = lasty/40.0;
194    ponent = fabs (lastx/540.0);
195    for (j=1; j<39; j++) {
196       twist[j] = ang;
197       ang += dang;
198
199       taper[j] = pow ((1.0 - pow (fabs(z), 1.0/ponent)), ponent);
200       z += deltaz;
201    }
202
203    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
204    glColor3f (0.5, 0.6, 0.6);
205
206    /* set up some matrices so that the object spins with the mouse */
207    glPushMatrix ();
208    /* glTranslatef (0.0, 0.0, -80.0); */
209    /* glRotatef (130.0, 0.0, 1.0, 0.0); */
210    /* glRotatef (65.0, 1.0, 0.0, 0.0); */
211
212    /* draw the brand and the handle */
213    gleTaper (20, contour, norms,  NULL, 40, path, NULL, taper, twist);
214
215    glPopMatrix ();
216 }
217
218 /* ===================== END OF FILE ================== */