16f12b1286cacdd7b9ec2296d9b255209d420109
[xscreensaver] / hacks / glx / extrusion-screw.c
1 /* 
2  * screw.c
3  * 
4  * FUNCTION:
5  * Draws a 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 /* 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 #ifndef NULL
22 #define NULL ((void *) 0x0)
23 #endif /* NULL */
24
25 /* =========================================================== */
26
27 #define SCALE 1.3
28 #define CONTOUR(x,y) {                                  \
29    double ax, ay, alen;                                 \
30    contour[i][0] = SCALE * (x);                         \
31    contour[i][1] = SCALE * (y);                         \
32    if (i!=0) {                                          \
33       ax = contour[i][0] - contour[i-1][0];             \
34       ay = contour[i][1] - contour[i-1][1];             \
35       alen = 1.0 / sqrt (ax*ax + ay*ay);                \
36       ax *= alen;   ay *= alen;                         \
37       norms [i-1][0] = ay;                              \
38       norms [i-1][1] = -ax;                             \
39    }                                                    \
40    i++;                                                 \
41 }
42
43 #define NUM_PTS (25)
44
45 double contour [NUM_PTS][2];
46 double norms [NUM_PTS][2];
47
48 static void init_contour (void)
49 {
50    int i;
51
52    /* outline of extrusion */
53    i=0;
54    CONTOUR (1.0, 1.0);
55    CONTOUR (1.0, 2.9);
56    CONTOUR (0.9, 3.0);
57    CONTOUR (-0.9, 3.0);
58    CONTOUR (-1.0, 2.9);
59
60    CONTOUR (-1.0, 1.0);
61    CONTOUR (-2.9, 1.0);
62    CONTOUR (-3.0, 0.9);
63    CONTOUR (-3.0, -0.9);
64    CONTOUR (-2.9, -1.0);
65
66    CONTOUR (-1.0, -1.0);
67    CONTOUR (-1.0, -2.9);
68    CONTOUR (-0.9, -3.0);
69    CONTOUR (0.9, -3.0);
70    CONTOUR (1.0, -2.9);
71
72    CONTOUR (1.0, -1.0);
73    CONTOUR (2.9, -1.0);
74    CONTOUR (3.0, -0.9);
75    CONTOUR (3.0, 0.9);
76    CONTOUR (2.9, 1.0);
77
78    CONTOUR (1.0, 1.0);   /* repeat so that last normal is computed */
79 }
80    
81 /* =========================================================== */
82
83 extern float lastx;
84 extern float lasty;
85 extern float rot_x;
86 extern float rot_y;
87 extern float rot_z;
88
89 void InitStuff_screw (void)
90 {
91    int style;
92
93    /* configure the pipeline */
94    style = TUBE_JN_CAP;
95    style |= TUBE_CONTOUR_CLOSED;
96    style |= TUBE_NORM_FACET;
97    style |= TUBE_JN_ANGLE;
98    gleSetJoinStyle (style);
99
100    init_contour();
101 }
102
103 /* =========================================================== */
104
105 void DrawStuff_screw (void) {
106
107    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
108    glColor3f (0.5, 0.6, 0.6);
109
110    /* set up some matrices so that the object spins with the mouse */
111    glPushMatrix ();
112    glTranslatef (0.0, 0.0, -80.0);
113    glRotatef(rot_x, 1, 0, 0);
114    glRotatef(rot_y, 0, 1, 0);
115    glRotatef(rot_z, 0, 0, 1);
116 /*     glRotatef (130.0, 0.0, 1.0, 0.0); */
117 /*     glRotatef (65.0, 1.0, 0.0, 0.0); */
118
119    /* draw the brand and the handle */
120    gleScrew (20, contour, norms, 
121                  NULL, -6.0, 9.0, lasty);
122
123    glPopMatrix ();
124 }
125
126 /* ===================== END OF FILE ================== */