sphere generation

What I need is to generate a sphere but not using glu or aux, I need access to all vertexes for further manipulations. I need help to create some universal function which would generate an array of faces (which would be of course groups of vertexes) which all together would be a complete sphere. Sounds easy :wink: . Function parameters would be the radius and number of faces or vertexes. It may generate spherical coordinates, I can convert them. Any help or hints are welcome. One more thing, can somebody tell me where I could get for example source code of gluSphere? This would be very nice.

Simple stacks and slices approach:

for (stack = 0; stack < STACKS; ++stack) {
  for (slice = 0; slice < SLICES; ++slice) {
    y = 2.0 * stack / STACKS - 1.0;
    /* for better distribution, use y = -cos(PI * stack / STACKS) */
    r = sqrt(1 - y^2);
    x = r * sin(2.0 * PI * slice / SLICES);
    z = r * cos(2.0 * PI * slice / SLICES);

    vertex = radius * (x, y, z);
  }
}

This (pseudo)code will produce a set of vertices lying on the surface of a sphere. The vertices are grouped in STACKS rings lying on an x-z-plane in different heights. Each ring has SLICES vertices.

The code produces vertices suitable for GL_POINTS, but not for any other primitive type.

You can find one implementation of gluSphere in MESA .

Now this is what I call The Reply. Thanks.