## freeglut-developer

 [Freeglut-developer] WireSphere From: philip _ - 2002-12-10 21:16:46 ``` Hi i was looking at the code of glutWireSphere and i was wondering if somebody could tell me the idea(algorithm) behind this code. Thanks. _________________________________________________________________ MSN 8 with e-mail virus protection service: 2 months FREE* http://join.msn.com/?page=features/virus ```
 Re: [Freeglut-developer] WireSphere From: Stephen J Baker - 2002-12-10 23:03:53 ```On Tue, 10 Dec 2002, philip _ wrote: > i was looking at the code of glutWireSphere and i was wondering if somebody > could tell me the idea(algorithm) behind this code. This is a simple 'stacks and slices' implementation of a sphere. Imagine a sphere cut horizontally into a number of stacked sections - and cut again in slices like an Orange. So, this algorithm renders a line strip down each of the slices - and a line loop around each of the stacks. The result is something that looks like the lines of latitude and longitude on a globe of the Earth. The code generates all the intersection points in a double-'for' loop and stores them into a 'malloc'ed array. The points are generated by calculating a 'latitude' and 'longitude' angle (in radians) and doing a simple polar-to-cartesian conversion using some trig functions to generate 3D coordinates from the two angles. The index into the 'vertex' array is calculated to simulate a 3 dimensional array (indexed by the slice number, the segment number and 0,1 or 2 for the X,Y or Z coordinate). Then, it loops around each of the slices generating a line strip for each slice (with an inner loop indexing into the table of points generating each of the vertices). Finally there is a third double-'for' loop that generates a line loop for each stack - with the inner loop indexing into that same table of points. Since the code always generates a sphere of radius 1.0 units, there is a glScale command applied to scale it up to the requested radius. There is an alternative algorithm for generating a sphere (which is harder to understand - but to my mind makes nicer looking spheres). You can find sources for it (and an alternative implementation of the stacks and slices approach) in the PLIB 'ssgAux' library at http://plib.sf.net This second algorithm takes a regular octahedron (eight triangular faces) centered on the centerpoint of the sphere we are trying to generate. Then it splits each triangle into four smaller triangles like this: ________ ________ \ / \ /\ / \ / \/__\/ \ / ==> \ / \/ \/ ...then displaces the resulting vertices outwards so that they lie on the surface of the sphere. You can then repeat this procedure as many times as you like to recursively generate progressively more precise sphere's using more and more triangles. There are lots of cool things about this second algorithm. One is that the triangles are more equilateral than the stacks and slices approach - which improves the smoothness of the sphere for a similar number of triangles. The other is that the sphere looks pretty similar from all directions, it doesn't have any strange changes in shading at the poles. The PLIB graphics library provides both implementations because some applications (like texturing a 'planet' from a flat 'map') work better with stacks & slices, where others look better with the recursively subdivided octahedron approach. ---- Steve Baker (817)619-2657 (Vox/Vox-Mail) L3Com/Link Simulation & Training (817)619-2466 (Fax) Work: sjbaker@... http://www.link.com Home: sjbaker1@... http://www.sjbaker.org ```