Jump to content
  • Advertisement
Sign in to follow this  
cwl100

Sphere algorithm = hemisphere? Help

This topic is 4849 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(); } } Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
Zedzeek beat me to it, but here's another way to do it + some more info.

Hi.. Haven't tried this, but I think it's correct:


// I haven't modified the lats, so 20 lats would mean 20 lats in the hemisphere (double the density of a full sphere with same number)
void drawHemiSphere(double r, int lats, int longs) {

int i, j;
for(i = 0; i <= lats; i++) { // I would initialize i to 1, and enforce odd number of lats for perfect equator
//double lat0 = M_PI * (-0.5 + (double) (i - 1) / lats); // original
double lat0 = M_PI * (-0.5 + (double) (i - 1) / (lats * 2)); // hemisphere
double z0 = sin(lat0);
double zr0 = cos(lat0);

//double lat1 = M_PI * (-0.5 + (double) i / lats); // original
double lat1 = M_PI * (-0.5 + (double) i / (lats * 2)); // hemisphere NOTE: I forgot this in my original post.. Added in edit some hours later
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();
}
}






One thing didn't make sense to me; by initializing i and j to 0 and doing i-1 and j-1 you'll get a negative value ("top" of sphere starts at -0.951 instead of -1), and you'll end up with lats+1 lats. Even worse, the "north pole" won't be closed as the radius at the first z0 would be -0.309 units instead of 0.

I chose not to "fix" this, since you didn't mention any problems with it.


summary :

(pi * -0.5) sin = -1, cos = 0 (z=-1, radius=0) (north pole)
(pi * 0) sin = 0, cos = 1 (z=0, radius = 1) (equator)
(pi * 0.5) sin = 1, cos = 0 (z=1, radius=0) (south pole)

To make a hemisphere you simply stop at sin(0) instead of sin(pi * 0.5).

[Edited by - frostburn on May 11, 2005 10:05:33 PM]

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!