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