Import from seb-0701a.tar.gz
[neverball-archive.git] / src / ball.c
blob13431a2ce4daf68f58de49697ed2bc4b081c496f
1 /*
2 * Copyright (C) 2003 Robert Kooima
4 * SUPER EMPTY BALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
15 #include "gl.h"
16 #include "vec3.h"
18 /*---------------------------------------------------------------------------*/
20 static void section(int d,
21 const double p0[3], const double c0[4],
22 const double p1[3], const double c1[4],
23 const double p2[3], const double c2[4])
25 if (d == 0)
27 glColor4dv(c0);
28 glNormal3dv(p0);
29 glVertex3dv(p0);
31 glColor4dv(c1);
32 glNormal3dv(p1);
33 glVertex3dv(p1);
35 glColor4dv(c2);
36 glNormal3dv(p2);
37 glVertex3dv(p2);
39 else
41 double p01[3], p12[3], p20[3];
42 double c01[4], c12[4], c20[4];
44 v_mid(p01, p0, p1);
45 v_mid(p12, p1, p2);
46 v_mid(p20, p2, p0);
48 v_nrm(p01, p01);
49 v_nrm(p12, p12);
50 v_nrm(p20, p20);
52 c01[0] = (c0[0] + c1[0]) / 2.0;
53 c01[1] = (c0[1] + c1[1]) / 2.0;
54 c01[2] = (c0[2] + c1[2]) / 2.0;
55 c01[3] = (c0[3] + c1[3]) / 2.0;
57 c12[0] = (c1[0] + c2[0]) / 2.0;
58 c12[1] = (c1[1] + c2[1]) / 2.0;
59 c12[2] = (c1[2] + c2[2]) / 2.0;
60 c12[3] = (c1[3] + c2[3]) / 2.0;
62 c20[0] = (c2[0] + c0[0]) / 2.0;
63 c20[1] = (c2[1] + c0[1]) / 2.0;
64 c20[2] = (c2[2] + c0[2]) / 2.0;
65 c20[3] = (c2[3] + c0[3]) / 2.0;
67 section(d - 1, p0, c0, p01, c01, p20, c20);
68 section(d - 1, p01, c01, p1, c1, p12, c12);
69 section(d - 1, p20, c20, p12, c12, p2, c2);
70 section(d - 1, p01, c01, p12, c12, p20, c20);
74 GLuint ball_init(int d)
76 static const float s[3] = { 1.0f, 1.0f, 1.0f };
78 static const double p[6][3] = {
79 { +1.0, 0.0, 0.0 },
80 { -1.0, 0.0, 0.0 },
81 { 0.0, +1.0, 0.0 },
82 { 0.0, -1.0, 0.0 },
83 { 0.0, 0.0, +1.0 },
84 { 0.0, 0.0, -1.0 },
86 static const double c[2][4] = {
87 { 0.0, 0.0, 0.0, 0.5 },
88 { 1.0, 1.0, 1.0, 0.5 },
91 GLuint list = glGenLists(1);
93 glNewList(list, GL_COMPILE);
94 glPushAttrib(GL_LIGHTING_BIT);
96 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, s);
97 glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 64.0f);
99 glEnable(GL_COLOR_MATERIAL);
101 glBegin(GL_TRIANGLES);
103 section(d, p[4], c[0], p[0], c[0], p[2], c[0]);
104 section(d, p[0], c[1], p[5], c[1], p[2], c[1]);
105 section(d, p[5], c[0], p[1], c[0], p[2], c[0]);
106 section(d, p[1], c[1], p[4], c[1], p[2], c[1]);
108 section(d, p[0], c[1], p[4], c[1], p[3], c[1]);
109 section(d, p[5], c[0], p[0], c[0], p[3], c[0]);
110 section(d, p[1], c[1], p[5], c[1], p[3], c[1]);
111 section(d, p[4], c[0], p[1], c[0], p[3], c[0]);
113 glEnd();
115 glPopAttrib();
116 glEndList();
118 return list;
121 void ball_draw(GLuint list, double r,
122 const double p[3],
123 const double e[3][3])
125 double M[16];
127 m_basis(M, e[0], e[1], e[2]);
129 glPushAttrib(GL_DEPTH_BUFFER_BIT);
131 glDepthMask(GL_FALSE);
133 glDisable(GL_TEXTURE_2D);
134 glPushMatrix();
136 glTranslated(p[0], p[1], p[2]);
137 glMultMatrixd(M);
138 glScaled(r, r, r);
140 glCullFace(GL_FRONT);
141 glCallList(list);
142 glCullFace(GL_BACK);
143 glCallList(list);
145 glPopMatrix();
146 glEnable(GL_TEXTURE_2D);
148 glPopAttrib();
151 /*---------------------------------------------------------------------------*/