http://ftp.ksu.edu.tw/FTP/FreeBSD/distfiles/xscreensaver-4.23.tar.gz
[xscreensaver] / hacks / glx / tangram_shapes.c
1 /* tangram, Copyright (c) 2005 Jeremy English <jhe@jeremyenglish.org>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or
9  * implied warranty.
10  */
11
12 #include <GL/gl.h>
13
14 #define small_scale  ( 1 )
15 #define large_scale  ( 2 )
16 #define medium_scale ( 1.414213562 )
17
18 #define alpha (0.05)
19
20 static void tri_45_90(int wire)
21 {
22     GLfloat vertices[][3] = {
23         {0, alpha, 0},
24         {0, alpha, 1},
25         {1, alpha, 0},
26         {0, -alpha, 0},
27         {0, -alpha, 1},
28         {1, -alpha, 0}
29     };
30
31     glBegin((wire) ? GL_LINE_LOOP : GL_TRIANGLES);
32
33     glNormal3f(0, 1, 0);
34     glVertex3fv(vertices[0]);
35     glNormal3f(0, 1, 0);
36     glVertex3fv(vertices[2]);
37     glNormal3f(0, 1, 0);
38     glVertex3fv(vertices[1]);
39
40     glNormal3f(0, -1, 0);
41     glVertex3fv(vertices[3]);
42     glNormal3f(0, -1, 0);
43     glVertex3fv(vertices[4]);
44     glNormal3f(0, -1, 0);
45     glVertex3fv(vertices[5]);
46     glEnd();
47
48     glBegin((wire) ? GL_LINE_LOOP : GL_QUADS);
49     glNormal3f(1, 0, 1);
50     glVertex3fv(vertices[2]);
51     glNormal3f(1, 0, 1);
52     glVertex3fv(vertices[5]);
53     glNormal3f(1, 0, 1);
54     glVertex3fv(vertices[4]);
55     glNormal3f(1, 0, 1);
56     glVertex3fv(vertices[1]);
57
58     glNormal3f(-1, 0, 0);
59     glVertex3fv(vertices[0]);
60     glNormal3f(-1, 0, 0);
61     glVertex3fv(vertices[1]);
62     glNormal3f(-1, 0, 0);
63     glVertex3fv(vertices[4]);
64     glNormal3f(-1, 0, 0);
65     glVertex3fv(vertices[3]);
66
67     glNormal3f(0, 0, -1);
68     glVertex3fv(vertices[0]);
69     glNormal3f(0, 0, -1);
70     glVertex3fv(vertices[3]);
71     glNormal3f(0, 0, -1);
72     glVertex3fv(vertices[5]);
73     glNormal3f(0, 0, -1);
74     glVertex3fv(vertices[2]);
75     glEnd();
76 }
77
78 void unit_cube(int wire)
79 {
80     glBegin((wire) ? GL_LINE_LOOP : GL_QUADS);
81
82
83     glNormal3f(0.0f, 1.0f, 0.0f);
84     glVertex3f(0.0f, alpha, 0.0f);
85     glVertex3f(0.0f, alpha, 1.0f);
86     glVertex3f(1.0f, alpha, 1.0f);
87     glVertex3f(1.0f, alpha, 0.0f);
88
89     glNormal3f(0.0f, 0.0f, 1.0f);
90     glVertex3f(0.0f, -alpha, 1.0f);
91     glVertex3f(1.0f, -alpha, 1.0f);
92     glVertex3f(1.0f, alpha, 1.0f);
93     glVertex3f(0.0f, alpha, 1.0f);
94
95     glNormal3f(0.0f, 0.0f, -1.0f);
96     glVertex3f(0.0f, -alpha, 0.0f);
97     glVertex3f(0.0f, alpha, 0.0f);
98     glVertex3f(1.0f, alpha, 0.0f);
99     glVertex3f(1.0f, -alpha, 0.0f);
100
101     glNormal3f(1.0f, 0.0f, 0.0f);
102     glVertex3f(1.0f, -alpha, 0.0f);
103     glVertex3f(1.0f, alpha, 0.0f);
104     glVertex3f(1.0f, alpha, 1.0f);
105     glVertex3f(1.0f, -alpha, 1.0f);
106
107     glNormal3f(-1.0f, 0.0f, 0.0f);
108     glVertex3f(0.0f, -alpha, 0.0f);
109     glVertex3f(0.0f, -alpha, 1.0f);
110     glVertex3f(0.0f, alpha, 1.0f);
111     glVertex3f(0.0f, alpha, 0.0f);
112
113     glNormal3f(0.0f, -1.0f, 0.0f);
114     glVertex3f(0.0f, -alpha, 0.0f);
115     glVertex3f(1.0f, -alpha, 0.0f);
116     glVertex3f(1.0f, -alpha, 1.0f);
117     glVertex3f(0.0f, -alpha, 1.0f);
118
119     glEnd();
120 }
121
122 void unit_rhomboid(int wire)
123 {
124     glBegin((wire) ? GL_LINE_LOOP : GL_QUADS);
125
126     glNormal3f(0.0f, 1.0f, 0.0f);
127     glVertex3f(0, alpha, 0);
128     glVertex3f(1, alpha, 1);
129     glVertex3f(1, alpha, 2);
130     glVertex3f(0, alpha, 1);
131
132     glNormal3f(0.0f, -1.0f, 0.0f);
133     glVertex3f(0, -alpha, 0);
134     glVertex3f(0, -alpha, 1);
135     glVertex3f(1, -alpha, 2);
136     glVertex3f(1, -alpha, 1);
137
138     glNormal3f(-1.0f, 0.0f, 0.0f);
139     glVertex3f(0, alpha, 0);
140     glVertex3f(0, alpha, 1);
141     glVertex3f(0, -alpha, 1);
142     glVertex3f(0, -alpha, 0);
143
144
145     glNormal3f(0.0f, 0.0f, 1.0f);
146     glVertex3f(0, alpha, 1);
147     glVertex3f(1, alpha, 2);
148     glVertex3f(1, -alpha, 2);
149     glVertex3f(0, -alpha, 1);
150
151     glNormal3f(1.0f, 0.0f, 0.0f);
152     glVertex3f(1, alpha, 1);
153     glVertex3f(1, -alpha, 1);
154     glVertex3f(1, -alpha, 2);
155     glVertex3f(1, alpha, 2);
156
157     glNormal3f(0.0f, 0.0f, 1.0f);
158     glVertex3f(0, alpha, 0);
159     glVertex3f(0, -alpha, 0);
160     glVertex3f(1, -alpha, 1);
161     glVertex3f(1, alpha, 1);
162
163     glEnd();
164 }
165
166 /* All of the pieces have the same thickness so all of the Y values are the same */
167
168 GLuint get_sm_tri_dl(int wire)
169 {
170     GLuint triangle = glGenLists(1);
171     glNewList(triangle, GL_COMPILE);
172     glScalef(small_scale, small_scale, small_scale);
173     tri_45_90(wire);
174     glEndList();
175     return triangle;
176 }
177
178 GLuint get_lg_tri_dl(int wire)
179 {
180     GLuint triangle = glGenLists(1);
181     glNewList(triangle, GL_COMPILE);
182     glScalef(large_scale, small_scale, large_scale);
183     tri_45_90(wire);
184     glEndList();
185     return triangle;
186 }
187
188 GLuint get_md_tri_dl(int wire)
189 {
190     GLuint triangle = glGenLists(1);
191     glNewList(triangle, GL_COMPILE);
192     glScalef(medium_scale, small_scale, medium_scale);
193     tri_45_90(wire);
194     glEndList();
195     return triangle;
196 }
197
198 GLuint get_square_dl(int wire)
199 {
200     GLuint square = glGenLists(1);
201     glNewList(square, GL_COMPILE);
202     glScalef(small_scale, small_scale, small_scale);
203     unit_cube(wire);
204     glEndList();
205     return square;
206 }
207
208 GLuint get_rhomboid_dl(int wire)
209 {
210     GLuint rhomboid = glGenLists(1);
211     glNewList(rhomboid, GL_COMPILE);
212     glScalef(small_scale, small_scale, small_scale);
213     unit_rhomboid(wire);
214     glEndList();
215     return rhomboid;
216 }