OpenGL如何在C++中从半球创建一个球体

qjp7pelc  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(161)

所以,从我拥有的一种材料,我设法以某种方式完成了它到半球,最初的目的地。但现在我必须从说半球做一个球,我迷路了。我还没有在网上遇到一个答案,有第四个参数(夷平)。谁能告诉我我错过了什么?代码:

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();};
    }
}
ax6ht2ek

ax6ht2ek1#

对于一个完整的球面,raze必须等于r。但是,条件if (lat0>alpha && lat1>alpha)是错误的。它必须是:

if (lat0 >= -alpha && lat1 <= alpha)

注意,对于一个完整的球体,你需要画从-M_PI/2M_PI/2的切片,这意味着if (lat0 >= -M_PI/2 && lat1 < -M_PI/2)

相关问题