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