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