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