2 static const char sccsid[] = "@(#)b_sphere.c 4.11 98/06/16 xlockmore";
6 * BUBBLE3D (C) 1998 Richard W.M. Jones.
7 * b_sphere.c: Create a list of vertices and triangles in a
8 * normalized sphere, which is then later used as the basic shape
9 * for all bubbles. This code is run once when the program starts
15 /* The list of vertices created. */
16 typedef glb_vertex vertex;
17 static vertex *vertices = 0;
18 static int nr_vertices = 0, nr_vertices_allocated = 0;
20 /* The list of triangles created. */
21 typedef glb_triangle triangle;
22 static triangle *triangles = 0;
23 static int nr_triangles = 0, nr_triangles_allocated = 0;
25 /* Have we initialized the lists yet? */
26 static int initialized = 0;
28 #define EPSILON GLB_VERTICES_EPSILON
30 /* Should be taken care of already... but just in case */
31 #if !defined( __GNUC__ ) && !defined(__cplusplus) && !defined(c_plusplus)
36 close_enough(GLfloat * v1, GLfloat * v2)
38 return fabs((double) (v1[0] - v2[0])) <= EPSILON &&
39 fabs((double) (v1[1] - v2[1])) <= EPSILON &&
40 fabs((double) (v1[2] - v2[2])) <= EPSILON;
43 #define INCR(n) ((n == 0) ? (n = 1) : (n *= 2))
44 #define INCR_ALLOCATION(a, n, t) (a = (t *) realloc (a, INCR (n) * sizeof (t)))
47 save_vertex(GLfloat * v)
51 /* Inefficient, but we only do this a few times. Check to see if there's
52 * an existing vertex which is `close enough' to this one.
54 for (i = 0; i < nr_vertices; ++i)
55 if (close_enough(v, vertices[i]))
58 if (nr_vertices_allocated <= nr_vertices) {
60 vertices = (vertex *) malloc(INCR(nr_vertices_allocated) * sizeof (vertex));
62 INCR_ALLOCATION(vertices, nr_vertices_allocated, vertex);
65 vertices[nr_vertices][0] = v[0];
66 vertices[nr_vertices][1] = v[1];
67 vertices[nr_vertices][2] = v[2];
72 save_triangle(GLuint v1, GLuint v2, GLuint v3)
74 if (nr_triangles_allocated <= nr_triangles) {
76 triangles = (triangle *) malloc(INCR(nr_triangles_allocated) * sizeof (triangle));
78 INCR_ALLOCATION(triangles, nr_triangles_allocated, triangle);
81 triangles[nr_triangles][0] = v1;
82 triangles[nr_triangles][1] = v2;
83 triangles[nr_triangles][2] = v3;
84 return nr_triangles++;
88 normalize(GLfloat v[3])
90 GLfloat d = (GLfloat) sqrt((double) (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]));
97 v[0] = v[1] = v[2] = 0;
102 subdivide(GLfloat * v1, GLuint vi1,
103 GLfloat * v2, GLuint vi2,
104 GLfloat * v3, GLuint vi3,
110 save_triangle(vi1, vi2, vi3);
112 GLuint vi12, vi23, vi31;
113 GLfloat v12[3], v23[3], v31[3];
115 for (i = 0; i < 3; ++i) {
116 v12[i] = v1[i] + v2[i];
117 v23[i] = v2[i] + v3[i];
118 v31[i] = v3[i] + v1[i];
121 vi12 = save_vertex(v12);
123 vi23 = save_vertex(v23);
125 vi31 = save_vertex(v31);
126 subdivide(v1, vi1, v12, vi12, v31, vi31, depth - 1);
127 subdivide(v2, vi2, v23, vi23, v12, vi12, depth - 1);
128 subdivide(v3, vi3, v31, vi31, v23, vi23, depth - 1);
129 subdivide(v12, vi12, v23, vi23, v31, vi31, depth - 1);
133 #define ICO_X 0.525731112119133606
134 #define ICO_Z 0.850650808352039932
136 static GLfloat vdata[12][3] =
152 static GLuint tindices[20][3] =
176 /* Public interface: Create the sphere. */
178 glb_sphere_init(void)
185 for (i = 0; i < 20; ++i) {
186 subdivide(vdata[tindices[i][0]], save_vertex(vdata[tindices[i][0]]),
187 vdata[tindices[i][1]], save_vertex(vdata[tindices[i][1]]),
188 vdata[tindices[i][2]], save_vertex(vdata[tindices[i][2]]),
189 glb_config.subdivision_depth);
195 /* Return the vertices list. */
197 glb_sphere_get_vertices(int *nr_vertices_ptr)
200 *nr_vertices_ptr = nr_vertices;
204 /* Return the triangles list. */
206 glb_sphere_get_triangles(int *nr_triangles_ptr)
209 *nr_triangles_ptr = nr_triangles;
213 /* Free up memory. */
220 (void) free((void *) vertices);
221 (void) free((void *) triangles);
224 nr_vertices = nr_vertices_allocated = 0;
227 nr_triangles = nr_triangles_allocated = 0;