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