OpenGL 如何在 C++ 中从半球体创建球体
OpenGL how to create a sphere from half-sphere in c++
所以,从我的 material 开始,我设法以某种方式将它完成到半球体,即最初的目的地。但是现在我必须从所说的半球体中制作一个球体,我迷路了。我还没有在网上遇到有第四个参数(raze)的答案。有人能告诉我我错过了什么吗?
代码:
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();};
}
}
对于完整的球体 raze
必须等于 r
。但是,条件 if (lat0>alpha && lat1>alpha)
是错误的。它必须是:
if (lat0 >= -alpha && lat1 <= alpha)
请注意,对于完整的球体,您需要绘制从 -M_PI/2
到 M_PI/2
的切片。这意味着 if (lat0 >= -M_PI/2 && lat1 < -M_PI/2)
.
所以,从我的 material 开始,我设法以某种方式将它完成到半球体,即最初的目的地。但是现在我必须从所说的半球体中制作一个球体,我迷路了。我还没有在网上遇到有第四个参数(raze)的答案。有人能告诉我我错过了什么吗? 代码:
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();};
}
}
对于完整的球体 raze
必须等于 r
。但是,条件 if (lat0>alpha && lat1>alpha)
是错误的。它必须是:
if (lat0 >= -alpha && lat1 <= alpha)
请注意,对于完整的球体,您需要绘制从 -M_PI/2
到 M_PI/2
的切片。这意味着 if (lat0 >= -M_PI/2 && lat1 < -M_PI/2)
.