From http://www.jwz.org/xscreensaver/xscreensaver-5.18.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 #ifdef HAVE_CONFIG_H
13 # include "config.h"
14 #endif /* HAVE_CONFIG_H */
15
16 #ifndef HAVE_COCOA
17 # include <GL/gl.h>
18 #endif
19
20 #ifdef HAVE_JWZGLES
21 # include "jwzgles.h"
22 #endif /* HAVE_JWZGLES */
23
24 #include "tangram_shapes.h"
25
26 #define small_scale  ( 1 )
27 #define large_scale  ( 2 )
28 #define medium_scale ( 1.414213562 )
29
30 #define alpha (0.05)
31
32 static void
33 tri_45_90(int wire)
34 {
35     GLfloat vertices[][3] = {
36         {0, alpha, 0},
37         {0, alpha, 1},
38         {1, alpha, 0},
39         {0, -alpha, 0},
40         {0, -alpha, 1},
41         {1, -alpha, 0}
42     };
43
44     glBegin((wire) ? GL_LINE_LOOP : GL_TRIANGLES);
45
46     glNormal3f(0, 1, 0);
47     glVertex3fv(vertices[0]);
48     glNormal3f(0, 1, 0);
49     glVertex3fv(vertices[2]);
50     glNormal3f(0, 1, 0);
51     glVertex3fv(vertices[1]);
52
53     glNormal3f(0, -1, 0);
54     glVertex3fv(vertices[3]);
55     glNormal3f(0, -1, 0);
56     glVertex3fv(vertices[4]);
57     glNormal3f(0, -1, 0);
58     glVertex3fv(vertices[5]);
59     glEnd();
60
61     glBegin((wire) ? GL_LINE_LOOP : GL_QUADS);
62     glNormal3f(1, 0, 1);
63     glVertex3fv(vertices[2]);
64     glNormal3f(1, 0, 1);
65     glVertex3fv(vertices[5]);
66     glNormal3f(1, 0, 1);
67     glVertex3fv(vertices[4]);
68     glNormal3f(1, 0, 1);
69     glVertex3fv(vertices[1]);
70
71     glNormal3f(-1, 0, 0);
72     glVertex3fv(vertices[0]);
73     glNormal3f(-1, 0, 0);
74     glVertex3fv(vertices[1]);
75     glNormal3f(-1, 0, 0);
76     glVertex3fv(vertices[4]);
77     glNormal3f(-1, 0, 0);
78     glVertex3fv(vertices[3]);
79
80     glNormal3f(0, 0, -1);
81     glVertex3fv(vertices[0]);
82     glNormal3f(0, 0, -1);
83     glVertex3fv(vertices[3]);
84     glNormal3f(0, 0, -1);
85     glVertex3fv(vertices[5]);
86     glNormal3f(0, 0, -1);
87     glVertex3fv(vertices[2]);
88     glEnd();
89 }
90
91 static void
92 unit_cube(int wire)
93 {
94     glBegin((wire) ? GL_LINE_LOOP : GL_QUADS);
95
96
97     glNormal3f(0.0f, 1.0f, 0.0f);
98     glVertex3f(0.0f, alpha, 0.0f);
99     glVertex3f(0.0f, alpha, 1.0f);
100     glVertex3f(1.0f, alpha, 1.0f);
101     glVertex3f(1.0f, alpha, 0.0f);
102
103     glNormal3f(0.0f, 0.0f, 1.0f);
104     glVertex3f(0.0f, -alpha, 1.0f);
105     glVertex3f(1.0f, -alpha, 1.0f);
106     glVertex3f(1.0f, alpha, 1.0f);
107     glVertex3f(0.0f, alpha, 1.0f);
108
109     glNormal3f(0.0f, 0.0f, -1.0f);
110     glVertex3f(0.0f, -alpha, 0.0f);
111     glVertex3f(0.0f, alpha, 0.0f);
112     glVertex3f(1.0f, alpha, 0.0f);
113     glVertex3f(1.0f, -alpha, 0.0f);
114
115     glNormal3f(1.0f, 0.0f, 0.0f);
116     glVertex3f(1.0f, -alpha, 0.0f);
117     glVertex3f(1.0f, alpha, 0.0f);
118     glVertex3f(1.0f, alpha, 1.0f);
119     glVertex3f(1.0f, -alpha, 1.0f);
120
121     glNormal3f(-1.0f, 0.0f, 0.0f);
122     glVertex3f(0.0f, -alpha, 0.0f);
123     glVertex3f(0.0f, -alpha, 1.0f);
124     glVertex3f(0.0f, alpha, 1.0f);
125     glVertex3f(0.0f, alpha, 0.0f);
126
127     glNormal3f(0.0f, -1.0f, 0.0f);
128     glVertex3f(0.0f, -alpha, 0.0f);
129     glVertex3f(1.0f, -alpha, 0.0f);
130     glVertex3f(1.0f, -alpha, 1.0f);
131     glVertex3f(0.0f, -alpha, 1.0f);
132
133     glEnd();
134 }
135
136 static void
137 unit_rhomboid(int wire)
138 {
139     glBegin((wire) ? GL_LINE_LOOP : GL_QUADS);
140
141     glNormal3f(0.0f, 1.0f, 0.0f);
142     glVertex3f(0, alpha, 0);
143     glVertex3f(1, alpha, 1);
144     glVertex3f(1, alpha, 2);
145     glVertex3f(0, alpha, 1);
146
147     glNormal3f(0.0f, -1.0f, 0.0f);
148     glVertex3f(0, -alpha, 0);
149     glVertex3f(0, -alpha, 1);
150     glVertex3f(1, -alpha, 2);
151     glVertex3f(1, -alpha, 1);
152
153     glNormal3f(-1.0f, 0.0f, 0.0f);
154     glVertex3f(0, alpha, 0);
155     glVertex3f(0, alpha, 1);
156     glVertex3f(0, -alpha, 1);
157     glVertex3f(0, -alpha, 0);
158
159
160     glNormal3f(0.0f, 0.0f, 1.0f);
161     glVertex3f(0, alpha, 1);
162     glVertex3f(1, alpha, 2);
163     glVertex3f(1, -alpha, 2);
164     glVertex3f(0, -alpha, 1);
165
166     glNormal3f(1.0f, 0.0f, 0.0f);
167     glVertex3f(1, alpha, 1);
168     glVertex3f(1, -alpha, 1);
169     glVertex3f(1, -alpha, 2);
170     glVertex3f(1, alpha, 2);
171
172     glNormal3f(0.0f, 0.0f, 1.0f);
173     glVertex3f(0, alpha, 0);
174     glVertex3f(0, -alpha, 0);
175     glVertex3f(1, -alpha, 1);
176     glVertex3f(1, alpha, 1);
177
178     glEnd();
179 }
180
181 /* All of the pieces have the same thickness so all of the Y values are the same */
182
183 GLuint
184 tangram_get_sm_tri_dl(int wire)
185 {
186     GLuint triangle = glGenLists(1);
187     glNewList(triangle, GL_COMPILE);
188     glScalef(small_scale, small_scale, small_scale);
189     tri_45_90(wire);
190     glEndList();
191     return triangle;
192 }
193
194 GLuint
195 tangram_get_lg_tri_dl(int wire)
196 {
197     GLuint triangle = glGenLists(1);
198     glNewList(triangle, GL_COMPILE);
199     glScalef(large_scale, small_scale, large_scale);
200     tri_45_90(wire);
201     glEndList();
202     return triangle;
203 }
204
205 GLuint
206 tangram_get_md_tri_dl(int wire)
207 {
208     GLuint triangle = glGenLists(1);
209     glNewList(triangle, GL_COMPILE);
210     glScalef(medium_scale, small_scale, medium_scale);
211     tri_45_90(wire);
212     glEndList();
213     return triangle;
214 }
215
216 GLuint
217 tangram_get_square_dl(int wire)
218 {
219     GLuint square = glGenLists(1);
220     glNewList(square, GL_COMPILE);
221     glScalef(small_scale, small_scale, small_scale);
222     unit_cube(wire);
223     glEndList();
224     return square;
225 }
226
227 GLuint
228 tangram_get_rhomboid_dl(int wire)
229 {
230     GLuint rhomboid = glGenLists(1);
231     glNewList(rhomboid, GL_COMPILE);
232     glScalef(small_scale, small_scale, small_scale);
233     unit_rhomboid(wire);
234     glEndList();
235     return rhomboid;
236 }