Jump to content
  • Advertisement
Sign in to follow this  
cwl100

Sphere algorithm = hemisphere HELP

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
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. :)


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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by iMalc
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.


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!