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