786d0c755c8e878a62fdada6b42f8f1eeef2af54
[xscreensaver] / hacks / glx / extrusion-joinoffset.c
1
2 /* cylinder drawing demo */
3 /* this demo demonstrates the various join styles */
4
5 /* required include files */
6 #include <GL/gl.h>
7 #include <GL/glut.h>
8 #include <GL/tube.h>
9
10 /* ------------------------------------------------------- */
11
12 /* the arrays in which we will store the polyline */
13 #define NPTS 100
14 double points [NPTS][3];
15 float colors [NPTS][3];
16 static int idx = 0;
17
18 /* some utilities for filling that array */
19 #define PSCALE 0.5
20 #define PNT(x,y,z) {                    \
21    points[idx][0] = PSCALE * x;         \
22    points[idx][1] = PSCALE * y;         \
23    points[idx][2] = PSCALE * z;         \
24    idx ++;                              \
25 }
26
27 #define COL(r,g,b) {                    \
28    colors[idx][0] = r;                  \
29    colors[idx][1] = g;                  \
30    colors[idx][2] = b;                  \
31 }
32
33 /* the arrays in which we will store the contour */
34 #define NCONTOUR 100
35 double contour_points [NCONTOUR][2];
36 int cidx = 0;
37
38 /* some utilities for filling that array */
39 #define C_PNT(x,y) {                    \
40    contour_points[cidx][0] = x;         \
41    contour_points[cidx][1] = y;         \
42    cidx ++;                             \
43 }
44
45
46 /* ------------------------------------------------------- */
47 /* 
48  * Initialize a bent shape with three segments. 
49  * The data format is a polyline.
50  *
51  * NOTE that neither the first, nor the last segment are drawn.
52  * The first & last segment serve only to determine that angle 
53  * at which the endcaps are drawn.
54  */
55
56 void InitStuff_joinoffset (void) 
57 {
58    COL (0.0, 0.0, 0.0);
59    PNT (16.0, 0.0, 0.0);
60
61    COL (0.2, 0.8, 0.5);
62    PNT (0.0, -16.0, 0.0);
63
64    COL (0.0, 0.8, 0.3);
65    PNT (-16.0, 0.0, 0.0);
66
67    COL (0.8, 0.3, 0.0);
68    PNT (0.0, 16.0, 0.0);
69
70    COL (0.2, 0.3, 0.9);
71    PNT (16.0, 0.0, 0.0);
72
73    COL (0.2, 0.8, 0.5);
74    PNT (0.0, -16.0, 0.0);
75
76    COL (0.0, 0.0, 0.0);
77    PNT (-16.0, 0.0, 0.0);
78
79    C_PNT (-0.8, -0.5);
80    C_PNT (-1.8, 0.0);
81    C_PNT (-1.2, 0.3);
82    C_PNT (-0.7, 0.8);
83    C_PNT (-0.2, 1.3);
84    C_PNT (0.0, 1.6);
85    C_PNT (0.2, 1.3);
86    C_PNT (0.7, 0.8);
87    C_PNT (1.2, 0.3);
88    C_PNT (1.8, 0.0);
89    C_PNT (0.8, -0.5);
90
91    gleSetJoinStyle (TUBE_JN_ANGLE | TUBE_CONTOUR_CLOSED | TUBE_JN_CAP);
92 }
93
94 double up_vector[3] = {1.0, 0.0, 0.0};
95
96 extern float lastx;
97 extern float lasty;
98
99 /* ------------------------------------------------------- */
100 /* draw the extrusion */
101
102 void DrawStuff_joinoffset (void) 
103 {
104    double moved_contour [NCONTOUR][2];
105    int style, save_style;
106    int i;
107
108    for (i=0; i<cidx; i++) {
109       moved_contour[i][0] = contour_points [i][0];
110       moved_contour[i][1] = contour_points [i][1] + 0.05 * (lasty-200.0);
111    }
112
113    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
114
115    /* set up some matrices so that the object spins with the mouse */
116    glPushMatrix ();
117    glTranslatef (0.0, 4.0, -80.0);
118    glRotatef (0.5*lastx, 0.0, 1.0, 0.0);
119
120    gleExtrusion (cidx, moved_contour, contour_points, up_vector, 
121                  idx, points, colors);
122
123    glPopMatrix ();
124
125
126    /* draw a seond copy, this time with the raw style, to compare
127     * things against */
128    glPushMatrix ();
129    glTranslatef (0.0, -4.0, -80.0);
130    glRotatef (0.5*lastx, 0.0, 1.0, 0.0);
131
132    save_style = gleGetJoinStyle ();
133    style = save_style;
134    style &= ~TUBE_JN_MASK;
135    style |= TUBE_JN_RAW;
136    gleSetJoinStyle (style);
137
138    gleExtrusion (cidx, moved_contour, contour_points, up_vector, 
139                  idx, points, colors);
140
141    gleSetJoinStyle (save_style);
142    glPopMatrix ();
143
144 }
145
146 /* ------------------ end of file ----------------------------- */