Sphere generation

Started by
18 comments, last by wasd 20 years, 4 months ago
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
Advertisement
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]
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
~phil
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)
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
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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.
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...
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.
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement