2de65370f060d1433fddc3d21199bcfc43de9520
[xscreensaver] / hacks / glx / extrusion-twistoid.c
1 /* 
2  * twistoid.c
3  *
4  * FUNCTION:
5  * Show extrusion of open contours. Also, show how torsion is applied.
6  *
7  * HISTORY:
8  * -- linas Vepstas October 1991
9  * -- heavily modified to draw corrugated surface, Feb 1993, Linas
10  * -- modified to demo twistoid March 1993
11  * -- port to glut Linas Vepstas March 1995
12  */
13
14 /* required include files */
15 #include <math.h>
16 #include <stdlib.h>
17 #include <GL/gl.h>
18 #include <GL/glut.h>
19 #include <GL/tube.h>
20
21 /* Some <math.h> files do not define M_PI... */
22 #ifndef M_PI
23 #define M_PI 3.14159265358979323846
24 #endif
25
26 extern float lastx;
27 extern float lasty;
28 extern float rot_x;
29 extern float rot_y;
30 extern float rot_z;
31
32 #define OPENGL_10
33 /* =========================================================== */
34
35 #define NUM_TOID1_PTS 5
36 double toid1_points[NUM_TOID1_PTS][3];
37 float toid1_colors [NUM_TOID1_PTS][3];
38 double toid1_twists [NUM_TOID1_PTS];
39
40 #define TSCALE (6.0)
41
42 #define TPTS(x,y) {                             \
43    toid1_points[i][0] = TSCALE * (x);           \
44    toid1_points[i][1] = TSCALE * (y);           \
45    toid1_points[i][2] = TSCALE * (0.0);         \
46    i++;                                         \
47 }
48
49 #define TCOLS(r,g,b) {                          \
50    toid1_colors[i][0] = (r);                    \
51    toid1_colors[i][1] = (g);                    \
52    toid1_colors[i][2] = (b);                    \
53    i++;                                         \
54 }
55
56 #define TXZERO() {                              \
57    toid1_twists[i] = 0.0;                       \
58    i++;                                         \
59 }
60
61 void init_toid1_line (void)
62 {
63    int i;
64
65    i=0;
66    TPTS (-1.1, 0.0);
67    TPTS (-1.0, 0.0);
68    TPTS (0.0, 0.0);
69    TPTS (1.0, 0.0);
70    TPTS (1.1, 0.0);
71
72    i=0;
73    TCOLS (0.8, 0.8, 0.5);
74    TCOLS (0.8, 0.4, 0.5);
75    TCOLS (0.8, 0.8, 0.3);
76    TCOLS (0.4, 0.4, 0.5);
77    TCOLS (0.8, 0.8, 0.5);
78
79    i=0;
80    TXZERO ();
81    TXZERO ();
82    TXZERO ();
83    TXZERO ();
84    TXZERO ();
85 }
86
87 /* =========================================================== */
88
89 #define SCALE 0.6
90 #define TWIST(x,y) {                                            \
91    double ax, ay, alen;                                         \
92    twistation[i][0] = SCALE * (x);                              \
93    twistation[i][1] = SCALE * (y);                              \
94    if (i!=0) {                                                  \
95       ax = twistation[i][0] - twistation[i-1][0];               \
96       ay = twistation[i][1] - twistation[i-1][1];               \
97       alen = 1.0 / sqrt (ax*ax + ay*ay);                        \
98       ax *= alen;   ay *= alen;                                 \
99       twist_normal [i-1][0] = - ay;                             \
100       twist_normal [i-1][1] = ax;                               \
101    }                                                            \
102    i++;                                                         \
103 }
104
105 #define NUM_TWIS_PTS (20)
106
107 double twistation [NUM_TWIS_PTS][2];
108 double twist_normal [NUM_TWIS_PTS][2];
109
110 void init_tripples (void)
111 {
112    int i;
113    double angle;
114    double co, si;
115
116    /* outline of extrusion */
117    i=0;
118    /* first, draw a semi-curcular "hump" */
119    while (i< 11) {
120       angle = M_PI * ((double) i) / 10.0;
121       co = cos (angle);
122       si = sin (angle);
123       TWIST ((-7.0 -3.0*co), 1.8*si);
124    }
125
126    /* now, a zig-zag corrugation */
127    while (i< NUM_TWIS_PTS) {
128       TWIST ((-10.0 +(double) i), 0.0);
129       TWIST ((-9.5 +(double) i), 1.0);
130    }
131 }
132
133    
134 /* =========================================================== */
135
136 #define V3F(x,y,z) {                                    \
137         float vvv[3];                                   \
138         vvv[0] = x; vvv[1] = y; vvv[2] = z; v3f (vvv);  \
139 }
140
141 #define N3F(x,y,z) {                                    \
142         float nnn[3];                                   \
143         nnn[0] = x; nnn[1] = y; nnn[2] = z; n3f (nnn);  \
144 }
145
146 /* =========================================================== */
147
148 void DrawStuff_twistoid (void) {
149    int i;
150
151    toid1_twists[2] = (lastx-121.0) / 8.0;
152
153    i=3;
154 /*
155    TPTS (1.0, lasty /400.0);
156    TPTS (1.1, 1.1 * lasty / 400.0);
157 */
158    TPTS (1.0, -(lasty-121.0) /200.0);
159    TPTS (1.1, -1.1 * (lasty-121.0) / 200.0);
160
161 #ifdef IBM_GL_32
162    rotate (230, 'x');
163    rotate (230, 'y');
164    scale (1.8, 1.8, 1.8);
165
166    if (mono_color) {
167       RGBcolor (178, 178, 204);
168       twist_extrusion (NUM_TWIS_PTS, twistation, twist_normal, 
169                 NULL, NUM_TOID1_PTS, toid1_points, NULL, toid1_twists);
170    } else {
171       twist_extrusion (NUM_TWIS_PTS, twistation, twist_normal, 
172               NULL, NUM_TOID1_PTS, toid1_points, toid1_colors, toid1_twists);
173    }
174 #endif
175
176 #ifdef OPENGL_10
177    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
178
179    /* set up some matrices so that the object spins with the mouse */
180    glPushMatrix ();
181    glTranslatef (0.0, 0.0, -80.0);
182    glRotatef(rot_x, 1, 0, 0);
183    glRotatef(rot_y, 0, 1, 0);
184    glRotatef(rot_z, 0, 0, 1);
185 /*     glRotated (43.0, 1.0, 0.0, 0.0); */
186 /*     glRotated (43.0, 0.0, 1.0, 0.0); */
187    glScaled (1.8, 1.8, 1.8);
188    gleTwistExtrusion (NUM_TWIS_PTS, twistation, twist_normal, 
189               NULL, NUM_TOID1_PTS, toid1_points, NULL, toid1_twists);
190    glPopMatrix ();
191 #endif
192
193 }
194
195 /* =========================================================== */
196
197 void InitStuff_twistoid (void) 
198 {
199    int js;
200
201    init_toid1_line ();
202    init_tripples ();
203
204 #ifdef IBM_GL_32
205    js = getjoinstyle ();
206    js &= ~TUBE_CONTOUR_CLOSED;
207    setjoinstyle (js);
208 #endif
209
210 #ifdef OPENGL_10
211    js = gleGetJoinStyle ();
212    js &= ~TUBE_CONTOUR_CLOSED;
213    gleSetJoinStyle (js);
214 #endif
215
216 }
217
218 /* ------------------ end of file -------------------- */