5b0b332b9d1ea15030abe4c12bfa096d407b7c6d
[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 extern float lastx;
105 extern float lasty;
106
107 /* ------------------------------------------------------- */
108 /* draw the extrusion */
109
110 void DrawStuff_joinoffset (void) 
111 {
112    double moved_contour [NCONTOUR][2];
113    int style, save_style;
114    int i;
115
116    for (i=0; i<cidx; i++) {
117       moved_contour[i][0] = contour_points [i][0];
118       moved_contour[i][1] = contour_points [i][1] + 0.05 * (lasty-200.0);
119    }
120
121    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
122
123    /* set up some matrices so that the object spins with the mouse */
124    glPushMatrix ();
125    glTranslatef (0.0, 4.0, -80.0);
126    glRotatef (0.5*lastx, 0.0, 1.0, 0.0);
127
128    gleExtrusion (cidx, moved_contour, contour_points, up_vector, 
129                  idx, points, colors);
130
131    glPopMatrix ();
132
133
134    /* draw a seond copy, this time with the raw style, to compare
135     * things against */
136    glPushMatrix ();
137    glTranslatef (0.0, -4.0, -80.0);
138    glRotatef (0.5*lastx, 0.0, 1.0, 0.0);
139
140    save_style = gleGetJoinStyle ();
141    style = save_style;
142    style &= ~TUBE_JN_MASK;
143    style |= TUBE_JN_RAW;
144    gleSetJoinStyle (style);
145
146    gleExtrusion (cidx, moved_contour, contour_points, up_vector, 
147                  idx, points, colors);
148
149    gleSetJoinStyle (save_style);
150    glPopMatrix ();
151
152 }
153
154 /* ------------------ end of file ----------------------------- */