Public Group

# Sphere algorithm = hemisphere HELP

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

## Recommended Posts

How can i modify this Sphere algorithm to make a hemisphere? void drawSphere(double r, int lats, int longs) { int i, j; for(i = 0; i <= lats; i++) { double lat0 = M_PI * (-0.5 + (double) (i - 1) / lats); double z0 = sin(lat0); double zr0 = cos(lat0); double lat1 = M_PI * (-0.5 + (double) i / lats); double z1 = sin(lat1); double zr1 = cos(lat1); glBegin(GL_QUAD_STRIP); for(j = 0; j <= longs; j++) { double lng = 2 * M_PI * (double) (j - 1) / longs; double x = cos(lng); double y = sin(lng); glNormal3f(x * zr0, y * zr0, z0); glVertex3f(x * zr0, y * zr0, z0); glNormal3f(x * zr1, y * zr1, z1); glVertex3f(x * zr1, y * zr1, z1); } glEnd(); } }

##### Share on other sites
Try changing this:

double lat0 = M_PI * (-0.5 + (double) (i - 1) / lats);
double lat1 = M_PI * (-0.5 + (double) i / lats);

To this:

double lat0 = M_PI * 0.5 * ((double) (i - 1) / lats);
double lat1 = M_PI * 0.5 * ((double) i / lats);

I'd have to look at it more carefully to be sure that was correct though.

##### Share on other sites
Oops, that was me.

##### Share on other sites
Thanks very much!!

##### Share on other sites
The algorithm you've been given (may I suggest you attempt to understand it yourself; failing that, ask the person who gave it to you for an explanation) is already designed to handle drawing "parts" of a sphere. If I understand it as well as I think I do, you are currently calling it with 'lats' and 'longs' both set to 1, yes? Try sending .5 for each, in turn, and observe the result. :)

##### Share on other sites
Quote:
 Original post by ZahlmanThe algorithm you've been given (may I suggest you attempt to understand it yourself; failing that, ask the person who gave it to you for an explanation) is already designed to handle drawing "parts" of a sphere. If I understand it as well as I think I do, you are currently calling it with 'lats' and 'longs' both set to 1, yes? Try sending .5 for each, in turn, and observe the result. :)

Err.. no. lats and longs are how detailed the sphere should be.

Quote:
 for(i = 0; i <= lats; i++) {

Setting lats to 0.5 won't work.

@cwl100: You should try to avoid double posting. You posted this in both the OpenGL forum and the For beginners forum. At the very least say in the post that you posted it somewhere else.

##### Share on other sites
That's cross-posting, not double-posting, but yeah.

And yes, I clearly wasn't reading that properly.

The AP's code will work; you may want to pass in an extra parameter instead of hard-coding the .5, though. You should also be able to multiply 'lng' similarly to create a hemisphere cut "the other way".

##### Share on other sites
I've never much liked that method of subdividing spheres. It create too many small tri's at the top and bottom, wasting detail that could be used around the rest of it.
I prefer to subdivide a 12-point / 20 face object (cant remember the name), made of equilateral triangles. Or the 6 point / 8 face object. Then I'd do some boolean algebra on it, then save it as a mesh.

I'm too late to say this, but I think the cwl100 would gain a lot from playing around with the code for a bit more to try and solve it himself. I'm sure he would have managed, but that's okay. The whole give a man a fish vs teach him to fish thing ... ah never mind.

##### Share on other sites
Quote:
 Original post by iMalcI prefer to subdivide a 12-point / 20 face object (cant remember the name), made of equilateral triangles. Or the 6 point / 8 face object.

That's an icosahedron. But once you start subdividing an icosahedron, you either no longer have a perfect sphere, or your triangles are no longer equilateral. I guess for most purposes, though, it doesn't matter...

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 19
• 15
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634097
• Total Posts
3015510
×