Sphere algorithm = hemisphere? Help
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
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:
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]
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); // originaldouble lat0 = M_PI * (-0.5 + (double) (i - 1) / (lats * 2)); // hemispheredouble z0 = sin(lat0);double zr0 = cos(lat0);//double lat1 = M_PI * (-0.5 + (double) i / lats); // originaldouble lat1 = M_PI * (-0.5 + (double) i / (lats * 2)); // hemisphere NOTE: I forgot this in my original post.. Added in edit some hours laterdouble 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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement