在球形坐标(OpenGL、C++、GLUT)之间跨步

Stepping between spherical coords (OpenGL, C++, GLUT)

本文关键字:GLUT 之间 C++ 坐标 OpenGL      更新时间:2023-10-16

我使用球面坐标在球面上定义了2个点。

// define end point positions
float theta_point_1 = (5/10.0)*M_PI;
float phi_point_1 = (5/10.0)*2*M_PI;
float x_point_1 = Radius * sin(theta_point_1) * cos(phi_point_1);
float y_point_1 = Radius * sin(theta_point_1) * sin(phi_point_1);
float z_point_1 = Radius * cos(theta_point_1);
float theta_point_2 = (7/10.0)*M_PI;
float phi_point_2 = (1/10.0)*2*M_PI;
float x_point_2 = Radius * sin(theta_point_2) * cos(phi_point_2);
float y_point_2 = Radius * sin(theta_point_2) * sin(phi_point_2);
float z_point_2 = Radius * cos(theta_point_2);

// draw end points
void end_points ()
{
    glColor3f (1.0, 1.0, 1.0);
    glPointSize(25.0);
    glBegin(GL_POINTS);
    glVertex3f(x_point_1,y_point_1,z_point_1);
    glVertex3f(x_point_2,y_point_2,z_point_2);
    glEnd();
}

为了在这两点之间切换,我执行以下操作:

  1. 求theta_points_1,2和phi_points_1,2
  2. 将差额除以n(让步为s)
  3. 重新绘制n次,同时每次将θ和phi增加s

在下文中,我定义了theta和phi值之间的差异,对它们进行了划分,然后重新绘制。

// begining spherical coords
float theta_point_1_value=5;
float phi_point_1_value=5;
// ending sperical coords
float theta_point_2_value=7;
float phi_point_2_value=1;
// dividing the difference evenly
float step_points=30;
float step_theta = 2/step_points;
float step_phi = 4/step_points;
// step between spherical coordinates
void stepping_points ()
{
    glColor3f (1.0, 0.0, 0.0); 
    for (int i = 1; i < step_points; i++)
    {
        float theta = (theta_point_1_value/10.0)*M_PI;
        float phi = (phi_point_1_value/10.0)*2*M_PI;
        float x = Radius * sin(theta) * cos(phi);
        float y = Radius * sin(theta) * sin(phi);
        float z = Radius * cos(theta);  
        glPushMatrix();         
        glTranslatef(x,y,z);
        glutSolidSphere (0.05,10,10);
        glPopMatrix(); 
    }  
    glEnd();
}

现在我明白了,这在同一位置显示了30个实心球体。因为我没有在任何重绘中包含"step_theta"或"step_phi"。

这就是我问题的根源。如何在重绘中使用"step_theta"answers"step_phi"?

我想做的是在我的"for"循环的顶部说这样的话:

    for (int i = 1; i < step_points; i++)
    {
        float theta_point_1_value = (theta_point_1_value+step_theta);
        float phi_point_1_value = (phi_point_1_value+step_phi);
        float theta = (theta_point_1_value/10.0)*M_PI;
        float phi = (phi_point_1_value/10.0)*2*M_PI;
        float x = Radius * sin(theta) * cos(phi);
        float y = Radius * sin(theta) * sin(phi);
        float z = Radius * cos(theta);  
        glPushMatrix();         
        glTranslatef(x,y,z);
        glutSolidSphere (0.05,10,10);
        glPopMatrix(); 
    } 

上面将重新绘制30个实心球体,但它们不会显示在我定义的端点之间。很明显,我的数学或语法都很糟糕(或者很可能两者都很糟糕)。

提示:循环变量i的范围是多少?您希望step_thetastep_phi的范围是多少?

当您在循环中声明一个变量时,它会超出范围,并在每次迭代后被销毁。因此,只有i的值在循环迭代之间发生变化。

另外:考虑使用向量/点类。(x_point_1, y_point_1)不是C++:)。

如果您希望无论帧速率如何都保持一致的定时,则需要跟踪时间的流逝,并使用它来控制在两点之间插值的距离。记住开始时间并计算所需的结束时间,然后每帧计算(float)(now-start)/(end-start)。这将为您提供一个介于0.0和1.0之间的值。将该值乘以每个球面坐标的德尔塔,再加上它们的起始角度,你就会得到现在需要的角度。