#### Archived

This topic is now archived and is closed to further replies.

# Sphere generation

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

## Recommended Posts

Does anyone know how I can generate a sphere mesh? I''m using D3D with vertex buffers and index buffers. Basically, I want to create planets and comets and such. I figure I can just add some noise to a sphere to create the meshes for comets, but I need to know how to generate vertices and indices for a sphere. Thanx --------------------------------------- Let''s struggle for our dream of Game! http://andrewporritt.4t.com

##### Share on other sites
You can do this using spherical polar coordinates to generate the vertices.

There is an article here that gives you some of the theory and will explain how to do it.

Basically you can do this to generate the vertices:

for (float theta = -(PI/2.0f); theta < (PI/2.0f); theta += dtheta){	for (float phi=0; phi < (PI*2.0f); phi += dphi)	{		//Position of vertex		p_Vertices[counter].x = (float) (radius * sin(theta) * cos(phi));		p_Vertices[counter].y = (float) (radius * sin(theta) * sin(phi));		p_Vertices[counter].z = (float) (radius * cos(theta));		counter++;	}}

Where dtheta and dphi are the increment for theta and phi respectively, and the radius is obviously the radius of the sphere.

Calculate the number of vertices and then build a simple index buffer and you're away..! This is off the top of my head so I hope it works. Otherwise just check out the article, its pretty good.

Good luck
Kyle

[edited by - Kyle N on November 28, 2003 6:18:49 PM]

##### Share on other sites
Alternatively, if you want triangles of equal area, you can use the subdivision method: create an octahedron, and subdivide each triangle. Normalise the position vectors of each vertex to the radius of the sphere. Repeat until sufficiently tesselated. By the way, the normals of a sphere are always equal to its position vector, normalised to length 1.

- JQ

##### Share on other sites
I use a subdivided icosahedron for my planets... like this. I''ve found that octahedrons get more distorted around the points where four triangles meet.

(But if you don''t get too close to your spheres, or don''t care about a bit of texture stretching, octahedrons are mathematically easier)

##### Share on other sites
That looks nice, but what about a lower poly version? Do they still look as good. I''m hoping to have quite a few things on screen at once.

---------------------------------------

Let''s struggle for our dream of Game!

http://andrewporritt.4t.com

##### Share on other sites
I agree with Fingers - an icosahedron works best. With Gouraud shading you can probably get a decent sphere in about 35-40 faces. You can also do a Google search for "sphere tesselation" to get several alternative methods.

##### Share on other sites
That picture is with OpenGL lighting and no LOD of any kind to show the surface details... If you just need a smooth sphere or use per-pixel lighting you don''t need nearly as many polygons for this kind of view (though on modern hardware you''ll be fillrate limited up to a few thousand polys).

The polycount increases exponentially with each subdivision; starting with 20, it goes up to 80, 320, 1280, 5120... 320 starts to look nice and round. At that point the silhouette is ~24-sided (the atmosphere "skirt" in my pic is 32-sided). That''s about the detail level of the planets in Battlecruiser games.

##### Share on other sites
yup, use an isocahedron.

dont use recursive subdivision though. however easier to code, it gives a much bigger difference in triangle size compared to dividing one face of an icocahedron at once.

to further reduce the sizedifference in triangles close to the vertices of the original isocahedron and those in the centre of one i also applied a displacement to the subdivided vertices.

i have the code for this lying around somewhere. the difference between the biggest and smallest triangles was <9%, which is the best ive seen around on the net. look really smooth. the code is in basic and very messy, so i doubt id be helping you with it though...

##### Share on other sites
Hmmm, I think you''re both wrong. A regular sphere works best,
as it has the most polys at the poles.

An octa and iso sphere look like crap at the poles. There''s only at most 4 or 8 faces, so the texture is stretched horribly among them.

##### Share on other sites
quote:
Original post by Anonymous Poster
Hmmm, I think you''re both wrong. A regular sphere works best,
as it has the most polys at the poles.

An octa and iso sphere look like crap at the poles. There''s only at most 4 or 8 faces, so the texture is stretched horribly among them.

That depends on how your texture coordinates are applied. If your texture coordinates are good, the texture will be mapped nicely.

##### Share on other sites
With an icosahedron, the distortion at the points is pretty minimal, assuming you''re mapping an equilateral triangle texture to each of the 20 sides of the icosahedron... After tesselation the angle between the triangle sides at a point is ~72 degrees. That''s reasonably close to the 60 degree angles of an equilateral triangle (an octahedron will have ~90 degree angles at the points) and doesn''t have any of the pinching artifacts you get if you use a rectangular texture and spherical mapping.

Now that I''m thinking about it, you could probably get an almost completely undistorted mapping if you "inflate" the triangle of UV coordinates a little so that the corners are 72 degrees. I like having the vertices evenly spaced though. Another technique that''ll work on any mesh is to use a cube map, which might be the easiest on hardware that supports it (you don''t even have to make UV coordinates, just use the vertices themselves).

ApochPiQ: I use recursive subdivision because it also functions as a handy tree structure for collision detection and other uses. I tried a more equal-length method of subdivision but the improvement wasn''t dramatic enough to justify a less efficient tree.

##### Share on other sites
well, seeing is believing.

Take 3 spheres, your regular sphere, iso, and octa, and texture map them using the same
mapping equation, and compare them.

I say the regular sphere looks the best, since it can have many polys at the poles.

It looks like it doesn''t matter how many subdivisions you apply, an iso always has
6 polys at the poles, octa will always have 8 polys at the poles. I''m saying the texture
mapping looks horrible for these two, because you have to stretch just 6 or 8 faces
around 360 degrees.

Take you regular sphere, since it has so many polys at the pole, I think this will

##### Share on other sites
oops, I mean 4 polys at the poles for octahedron, not 8.

##### Share on other sites
I don't think you understand what we're talking about. You do *NOT* use spherical mapping with an octahedron or icosahedron, you texture each of the original facets. The texturing at the poles has roughly the same texel density as on the equator, with approx. 25% lateral stretching for an icosahedron. If the textures match properly the point isn't visible without looking at the wireframe. (edit: and even then it isn't blatantly obvious)

Here's an example of icosahedral mapping (click on the thumbnail to see a larger image). Imagine this globe "inflated" to a round shape; each of the 12 points will have the same slight distortion but not nearly as dramatic as the "infinite" pinching you get with spherical mapping at the poles.

[edited by - Fingers_ on December 1, 2003 4:01:14 PM]

##### Share on other sites
yeah, that''s what I''m doing, spherical mapping on all 3 types of spheres.

What Kyle N posted.

##### Share on other sites
quote:

Hmmm, I think you''re both wrong. A regular sphere works best,
as it has the most polys at the poles.

An octa and iso sphere look like crap at the poles. There''s only at most 4 or 8 faces, so the texture is stretched horribly among them.

hmm i think you dont know what youre talking about.

spheres created with polar coordinates need much more polys to look smooth at the equator. besides the lightning looks messed up.

in general it just sucks ass.

if you subdivide a 20-faced primitive, youll have none of that shit, and if as previously suggested texture each face seperatly there is no issue with the textures. and since youll be using procedural textures for this sort of thing most of the time anyway, this is no problem.

##### Share on other sites
well, I don''t know exactly what the original poster f8k8 wanted. I got the impression
he like everyone else is putting his spheres in a game, so no, he''s not doing procedural,
he''s using texture maps.

And everyone uses spherical mapping because well, it''s easy and that''s what''s
everyone uses.

Post some code for your iso mapping sh*t.

##### Share on other sites
Er.. I don''t see why anybody would use spherical mapping in a game, because of the problems we''ve talked about. Generally if you can see the poles of a planet you don''t want to use spherical mapping.

My screenshot in the 4th post uses icosahedral mapping and has at least three "poles" visible to the camera. You can only notice them because the detail texture alignment causes a bit of a seam between each facet. The easiest one to see is in the lower right corner near the terminator because it''s on the ocean.. on land the seams are all but invisible even with the detail texture.

As for code, it''s trivial. Each facet is mapped to an equilateral triangle in the UV''s (or approximated as <0.5,0> <1,1> <0,1> for a square texture). That''s all there is to it. The only non-trivial part is generating triangular textures that tile seamlessly, which takes either a good artist, a lot of time or specialized tools (or a scanner and one of those paper globes ).

##### Share on other sites
that''s doesn''t sound very good for gaming. If you''re texture mapping a sphere for a game, you
don''t want a texture for each face, you want one texture for your entire sphere, and that''s what
spherical mapping will give you. And there''s only one seam to deal with, so that''s another plus.
That''s why I use it for its simplicity and easiness to use.

And that''s what they use for planetary globes, cartography, etc..., although the texture is spherical
distorted to compensate for texture distortion.

http://astronomy.swin.edu.au/~pbourke/projection/spherical/
http://astronomy.swin.edu.au/~pbourke/texture/polargrid/
http://astronomy.swin.edu.au/~pbourke/texture/spheremap/
http://astronomy.swin.edu.au/~pbourke/texture/texturemapping/