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