summaryrefslogtreecommitdiffstats
path: root/hacks/glx/extrusion-twistoid.c
blob: 82034afcfabe2d4facd6d20368163b6a9286ab9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* 
 * twistoid.c
 *
 * FUNCTION:
 * Show extrusion of open contours. Also, show how torsion is applied.
 *
 * HISTORY:
 * -- linas Vepstas October 1991
 * -- heavily modified to draw corrugated surface, Feb 1993, Linas
 * -- modified to demo twistoid March 1993
 * -- port to glut Linas Vepstas March 1995
 */

#include "extrusion.h"

#include <math.h>
#include <stdlib.h>

/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

/* controls shape of object */
extern float lastx;
extern float lasty;

#define OPENGL_10
/* =========================================================== */

#define NUM_TOID1_PTS 5
static double toid1_points[NUM_TOID1_PTS][3];
static float toid1_colors [NUM_TOID1_PTS][3];
static double toid1_twists [NUM_TOID1_PTS];

#define TSCALE (6.0)

#define TPTS(x,y) {				\
   toid1_points[i][0] = TSCALE * (x);		\
   toid1_points[i][1] = TSCALE * (y);		\
   toid1_points[i][2] = TSCALE * (0.0);		\
   i++;						\
}

#define TCOLS(r,g,b) {				\
   toid1_colors[i][0] = (r);			\
   toid1_colors[i][1] = (g);			\
   toid1_colors[i][2] = (b);			\
   i++;						\
}

#define TXZERO() {				\
   toid1_twists[i] = 0.0;			\
   i++;						\
}

static void init_toid1_line (void)
{
   int i;

   i=0;
   TPTS (-1.1, 0.0);
   TPTS (-1.0, 0.0);
   TPTS (0.0, 0.0);
   TPTS (1.0, 0.0);
   TPTS (1.1, 0.0);

   i=0;
   TCOLS (0.8, 0.8, 0.5);
   TCOLS (0.8, 0.4, 0.5);
   TCOLS (0.8, 0.8, 0.3);
   TCOLS (0.4, 0.4, 0.5);
   TCOLS (0.8, 0.8, 0.5);

   i=0;
   TXZERO ();
   TXZERO ();
   TXZERO ();
   TXZERO ();
   TXZERO ();
}

/* =========================================================== */

#define SCALE 0.6
#define TWIST(x,y) {						\
   double ax, ay, alen;						\
   twistation[i][0] = SCALE * (x);				\
   twistation[i][1] = SCALE * (y);				\
   if (i!=0) {							\
      ax = twistation[i][0] - twistation[i-1][0];		\
      ay = twistation[i][1] - twistation[i-1][1];		\
      alen = 1.0 / sqrt (ax*ax + ay*ay);			\
      ax *= alen;   ay *= alen;					\
      twist_normal [i-1][0] = - ay;				\
      twist_normal [i-1][1] = ax;				\
   }								\
   i++;								\
}

#define NUM_TWIS_PTS (20)

static double twistation [NUM_TWIS_PTS][2];
static double twist_normal [NUM_TWIS_PTS][2];

static void init_tripples (void)
{
   int i;
   double angle;
   double co, si;

   /* outline of extrusion */
   i=0;
   /* first, draw a semi-curcular "hump" */
   while (i< 11) {
      angle = M_PI * ((double) i) / 10.0;
      co = cos (angle);
      si = sin (angle);
      TWIST ((-7.0 -3.0*co), 1.8*si);
   }

   /* now, a zig-zag corrugation */
   while (1) {
      if (i >= NUM_TWIS_PTS) break;
      TWIST ((-10.0 +(double) i), 0.0);
      if (i >= NUM_TWIS_PTS) break;
      TWIST ((-9.5 +(double) i), 1.0);
   }
}

   
/* =========================================================== */

#define V3F(x,y,z) {					\
	float vvv[3]; 					\
	vvv[0] = x; vvv[1] = y; vvv[2] = z; v3f (vvv); 	\
}

#define N3F(x,y,z) {					\
	float nnn[3]; 					\
	nnn[0] = x; nnn[1] = y; nnn[2] = z; n3f (nnn); 	\
}

/* =========================================================== */

void DrawStuff_twistoid (void) {
   int i;

   toid1_twists[2] = (lastx-121.0) / 8.0;

   i=3;
/*
   TPTS (1.0, lasty /400.0);
   TPTS (1.1, 1.1 * lasty / 400.0);
*/
   TPTS (1.0, -(lasty-121.0) /200.0);
   TPTS (1.1, -1.1 * (lasty-121.0) / 200.0);

#ifdef IBM_GL_32
   rotate (230, 'x');
   rotate (230, 'y');
   scale (1.8, 1.8, 1.8);

   if (mono_color) {
      RGBcolor (178, 178, 204);
      twist_extrusion (NUM_TWIS_PTS, twistation, twist_normal, 
                NULL, NUM_TOID1_PTS, toid1_points, NULL, toid1_twists);
   } else {
      twist_extrusion (NUM_TWIS_PTS, twistation, twist_normal, 
              NULL, NUM_TOID1_PTS, toid1_points, toid1_colors, toid1_twists);
   }
#endif

#ifdef OPENGL_10
   glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glFrontFace(GL_CW);  /* jwz */

   /* set up some matrices so that the object spins with the mouse */
   glPushMatrix ();
/* glTranslatef (0.0, 0.0, -80.0); */
/* glRotated (43.0, 1.0, 0.0, 0.0); */
/* glRotated (43.0, 0.0, 1.0, 0.0); */
   glScaled (1.8, 1.8, 1.8);
   gleTwistExtrusion (NUM_TWIS_PTS, twistation, twist_normal, 
              NULL, NUM_TOID1_PTS, toid1_points, NULL, toid1_twists);
   glPopMatrix ();
#endif

}

/* =========================================================== */

void InitStuff_twistoid (void) 
{
   int js;

   init_toid1_line ();
   init_tripples ();

#ifdef IBM_GL_32
   js = getjoinstyle ();
   js &= ~TUBE_CONTOUR_CLOSED;
   setjoinstyle (js);
#endif

#ifdef OPENGL_10
   js = gleGetJoinStyle ();
   js &= ~TUBE_CONTOUR_CLOSED;
   gleSetJoinStyle (js);
#endif

}

/* ------------------ end of file -------------------- */