'OpenGL how to create a sphere from half-sphere in c++
So, from a material I have, I managed to somehow complete it to half-sphere, the original destination. But now I have to make a sphere from the said half-sphere and I'm lost. I haven't met an answer online that has a fourth parameter (raze). Can someone tell me what I'm missing? The code:
void drawSphere(double r, int lats, int longs, double raze) {
double alpha = acos((r - raze)/r);
bool ind = true;
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);
if (lat0>alpha && lat1>alpha){
if (ind){
ind = false;
double z0 = sin(alpha);
double zr0 = cos(alpha);
double lat1 = M_PI * (-0.5 + (double) (i-1) / 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);
glColor3f(1, 0, 0);
//glNormal3f(x * zr0, y * zr0, z0);
glVertex3f(r * x * zr0, r * y * zr0, r * z0);
//glNormal3f(x * zr1, y * zr1, z1);
glVertex3f(r * x * zr1, r * y * zr1, r * z1);
}
glEnd();
}
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);
glColor3f(1, 0, 0);
//glNormal3f(x * zr0, y * zr0, z0);
glVertex3f(r * x * zr0, r * y * zr0, r * z0);
//glNormal3f(x * zr1, y * zr1, z1);
glVertex3f(r * x * zr1, r * y * zr1, r * z1);
}
glEnd();};
}
}
Solution 1:[1]
For a full sphere raze must be equal r. However, the condition if (lat0>alpha && lat1>alpha) is wrong. It has to be:
if (lat0 >= -alpha && lat1 <= alpha)
Note that for a full sphere you need to draw slices from -M_PI/2 to M_PI/2. That means if (lat0 >= -M_PI/2 && lat1 < -M_PI/2).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
