# OpenGL 3D Sphere

This topic is 3989 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I'd like to know, how can I draw a 3D sphere using triangles, supposing all I have are those simples triangle fan and triangle quad functions on OpenGL? I don't want to use glutWireSphere or any of the sort. Thanks!

##### Share on other sites
x = cos(a)*cos(b)
y = sin(b)
z = sin(a)*cos(b)

If you plug in values for a (longitude) in the interval [-pi,pi] and values for b (latitude) in the interval [-pi/2,pi/2], you'll get the corresponding points on the sphere. Now a tessellation of the rectangle [-pi,pi]x[-pi/2,pi/2] will be converted to a tessellation of the sphere.

Does that help?

##### Share on other sites
The method just suggested was the typical method of generating a sphere. It has the advantage of being very simple, but it has the disadvantage of not having a uniform distribution of verticies. This means that depending what direction you look at it (for a given triangle count) it looks more or less round. You can trivially render it be drawing the "poles" with triangle fans, and every lattitude band with a triangle strip. this corresponds to a triangulated rectangular grid, with the exception that the top and bottom strips have no "diagonal edge" meaning they'd look like rectangles, except the top and bottom edge correspond to the poles and are degenerate (ie those edges are points).

Depending on your requirements, you may want what is sometimes called an "ico sphere" or a geodesic sphere. One can calculate the vertex locations be starting with an octahedron (or a tetrahedron) whose verticies lie on the surface of the sphere you wish to render and subdividing each triangle into four by adding a vertex at the centre of each edge (and connecting them to form new edges). The location of these new verticies is constrained to lie on the surface of the sphere you are rendering. For a given sphere radius this defines a one parameter family of tesselations with uniform distribution of verticies throughout the surface. This compares to the previous method which describes a two parameter family (number of lines of longitude and lattitude) which is vertex heavy near the poles. By contrast the ico/geodesic sphere has no (mathematical) poles though clearly there is a top and bottom point determined by the original figure (in the case of an octahedron these are the top and bottom points, and for the tetrahedron, it's the top and bottom points of the first subdivision).
Hope this helps,

Dan

##### Share on other sites
Could you explain better about the whole rectangle tesselation thing? I didn't understand. I know what tesselation is (dividing geometric figures in triangles) but not what [-pi,pi]x[-pi/2,pi/2] is. Also, OpenGL coordinates start from (0,0,0) right So I should do a translation of those angle limits (a and b) if I want the sphere to go to the middle of the screen?

##### Share on other sites
Yet another way is to make a "normalized" cube; i.e. draw a cube, normalize its vertices to one, then split each face quad into four quads by adding a point in its center. Normalize each added point and repeat for each quad down to the level of subdivision you require. This works well if you want to fake cube mapping, and the vertex distribution is nice. Of course the normalization makes it slow for drawing immediately, so make sure you only create it once and store the verts somewhere.

##### Share on other sites
What I would do is download a free 3D program like blender or trueSpace light and use them to create a sphere. Export it out into a raw file format that have all your vertices. Import that into your program and render away! If you need to change the size of it, just use the scale operation.

1. 1
2. 2
3. 3
Rutin
24
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002228
×