Mircea Ispas
Mircea Ispas

Reputation: 20780

How to generate geometry for part of a sphere

There are few algorithms to generate the points needed to render a sphere, but I couldn't not find a good algorithm nor to adapt a full sphere algorithm to generate the points for a part of the sphere.

Let's assume I have the min/max latitude, min/max longitude and the sphere radius. How do I generate a mesh to be able to render this part of the sphere?

Upvotes: 0

Views: 264

Answers (1)

H.U.N.
H.U.N.

Reputation: 95

I made hemisphere & part of hemisphere mesh. I edit songho Ahn's source code slightly and you can refer from following link :

http://www.songho.ca/opengl/gl_sphere.html

the main method of class Sphere are buildVertices*

You can get desired result from editing this method.

I made hemisphere like this :

void HemiSphere::buildVerticesSmooth()
{
...

float sectorStep = 2 *PI / sectorCount;
float stackStep = (PI / 2) / stackCount;
float sectorAngle, stackAngle;

for(int i = 0; i <= stackCount; ++i)
{
    stackAngle = -PI / 2 + i * stackStep;                   // starting from -pi/2 to 0
    xy = radius * cosf(stackAngle);             // r * cos(u)
    z = radius *( 1 + sinf(stackAngle) );              // r * sin(u)
...

enter image description here

and I also made semi-hemisphere which cut its floor so that it have even floor on the bottom. To do this, leave x y coordinate unchanged, just edit z coordinate to 0.

void HemiSphere::buildVerticesSmooth()
{

....

float sectorStep = 2 *PI / sectorCount;
float stackStep = (PI / 2) / stackCount;
float sectorAngle, stackAngle;
float d = sqrt(radius*radius - flat_radius*flat_radius)/radius;

for(int i = 0; i <= stackCount; ++i)
{
    stackAngle = -PI / 2 + i * stackStep;                   // starting from -pi/2 to 0
    xy = radius * cosf(stackAngle);             // r * cos(u)
    z = radius *( d + sinf(stackAngle) );              // r * sin(u)
    if (z<0.0) z = 0.0;

....

Hope this helps.

Upvotes: 1

Related Questions