25dec5b21f5f3defd8b3ff8c09485988f0185adb
[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     glVertex3fv(vertices[2]);
36     glVertex3fv(vertices[1]);
37
38     glNormal3f(0, -1, 0);
39     glVertex3fv(vertices[3]);
40     glVertex3fv(vertices[4]);
41     glVertex3fv(vertices[5]);
42     glEnd();
43
44     glBegin((wire) ? GL_LINE_LOOP : GL_QUADS);
45     glNormal3f(1, 0, 1);
46     glVertex3fv(vertices[2]);
47     glVertex3fv(vertices[5]);
48     glVertex3fv(vertices[4]);
49     glVertex3fv(vertices[1]);
50
51     glNormal3f(-1, 0, 0);
52     glVertex3fv(vertices[0]);
53     glVertex3fv(vertices[1]);
54     glVertex3fv(vertices[4]);
55     glVertex3fv(vertices[3]);
56
57     glNormal3f(0, 0, -1);
58     glVertex3fv(vertices[2]);
59     glVertex3fv(vertices[5]);
60     glVertex3fv(vertices[3]);
61     glVertex3fv(vertices[0]);
62     glEnd();
63 }
64
65 void unit_cube(int wire)
66 {
67     glBegin((wire) ? GL_LINE_LOOP : GL_QUADS);
68
69     glNormal3f(0.0f, -1.0f, 0.0f);
70     glVertex3f(0.0f, -alpha, 0.0f);
71     glVertex3f(1.0f, -alpha, 0.0f);
72     glVertex3f(1.0f, -alpha, 1.0f);
73     glVertex3f(0.0f, -alpha, 1.0f);
74
75     glNormal3f(0.0f, 0.0f, 1.0f);
76     glVertex3f(0.0f, -alpha, 1.0f);
77     glVertex3f(1.0f, -alpha, 1.0f);
78     glVertex3f(1.0f, alpha, 1.0f);
79     glVertex3f(0.0f, alpha, 1.0f);
80
81     glNormal3f(0.0f, 0.0f, -1.0f);
82     glVertex3f(0.0f, -alpha, 0.0f);
83     glVertex3f(0.0f, alpha, 0.0f);
84     glVertex3f(1.0f, alpha, 0.0f);
85     glVertex3f(1.0f, -alpha, 0.0f);
86
87     glNormal3f(1.0f, 0.0f, 0.0f);
88     glVertex3f(1.0f, -alpha, 0.0f);
89     glVertex3f(1.0f, alpha, 0.0f);
90     glVertex3f(1.0f, alpha, 1.0f);
91     glVertex3f(1.0f, -alpha, 1.0f);
92
93     glNormal3f(-1.0f, 0.0f, 0.0f);
94     glVertex3f(0.0f, -alpha, 0.0f);
95     glVertex3f(0.0f, -alpha, 1.0f);
96     glVertex3f(0.0f, alpha, 1.0f);
97     glVertex3f(0.0f, alpha, 0.0f);
98
99     glNormal3f(0.0f, 1.0f, 0.0f);
100     glVertex3f(0.0f, alpha, 0.0f);
101     glVertex3f(0.0f, alpha, 1.0f);
102     glVertex3f(1.0f, alpha, 1.0f);
103     glVertex3f(1.0f, alpha, 0.0f);
104     glEnd();
105 }
106
107 void unit_rhomboid(int wire)
108 {
109     glBegin((wire) ? GL_LINE_LOOP : GL_QUADS);
110
111     glNormal3f(0.0f, 1.0f, 0.0f);
112     glVertex3f(0, alpha, 0);
113     glVertex3f(1, alpha, 1);
114     glVertex3f(1, alpha, 2);
115     glVertex3f(0, alpha, 1);
116
117     glNormal3f(0.0f, -1.0f, 0.0f);
118     glVertex3f(0, -alpha, 0);
119     glVertex3f(0, -alpha, 1);
120     glVertex3f(1, -alpha, 2);
121     glVertex3f(1, -alpha, 1);
122
123     glNormal3f(-1.0f, 0.0f, 0.0f);
124     glVertex3f(0, alpha, 0);
125     glVertex3f(0, alpha, 1);
126     glVertex3f(0, -alpha, 1);
127     glVertex3f(0, -alpha, 0);
128
129
130     glNormal3f(0.0f, 0.0f, 1.0f);
131     glVertex3f(0, alpha, 1);
132     glVertex3f(1, alpha, 2);
133     glVertex3f(1, -alpha, 2);
134     glVertex3f(0, -alpha, 1);
135
136     glNormal3f(1.0f, 0.0f, 0.0f);
137     glVertex3f(1, alpha, 1);
138     glVertex3f(1, -alpha, 1);
139     glVertex3f(1, -alpha, 2);
140     glVertex3f(1, alpha, 2);
141
142     glNormal3f(0.0f, 0.0f, 1.0f);
143     glVertex3f(0, alpha, 0);
144     glVertex3f(0, -alpha, 0);
145     glVertex3f(1, -alpha, 1);
146     glVertex3f(1, alpha, 1);
147
148     glEnd();
149 }
150
151 /* All of the pieces have the same thickness so all of the Y values are the same */
152
153 GLuint get_sm_tri_dl(int wire)
154 {
155     GLuint triangle = glGenLists(1);
156     glNewList(triangle, GL_COMPILE);
157     glScalef(small_scale, small_scale, small_scale);
158     tri_45_90(wire);
159     glEndList();
160     return triangle;
161 }
162
163 GLuint get_lg_tri_dl(int wire)
164 {
165     GLuint triangle = glGenLists(1);
166     glNewList(triangle, GL_COMPILE);
167     glScalef(large_scale, small_scale, large_scale);
168     tri_45_90(wire);
169     glEndList();
170     return triangle;
171 }
172
173 GLuint get_md_tri_dl(int wire)
174 {
175     GLuint triangle = glGenLists(1);
176     glNewList(triangle, GL_COMPILE);
177     glScalef(medium_scale, small_scale, medium_scale);
178     tri_45_90(wire);
179     glEndList();
180     return triangle;
181 }
182
183 GLuint get_square_dl(int wire)
184 {
185     GLuint square = glGenLists(1);
186     glNewList(square, GL_COMPILE);
187     glScalef(small_scale, small_scale, small_scale);
188     unit_cube(wire);
189     glEndList();
190     return square;
191 }
192
193 GLuint get_rhomboid_dl(int wire)
194 {
195     GLuint rhomboid = glGenLists(1);
196     glNewList(rhomboid, GL_COMPILE);
197     glScalef(small_scale, small_scale, small_scale);
198     unit_rhomboid(wire);
199     glEndList();
200     return rhomboid;
201 }