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