在球形坐标之间步进(OpenGL,C ++,GLUT)

时间:2012-03-22 19:11:20

标签: c++ opengl glut

我使用球面坐标在球体表面上定义了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. 之间的差异
  3. 将差异除以'n'(产生's')
  4. 重绘'n'次,同时每次用's'加强theta和phi
  5. 在下文中,我已经定义了我的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'和'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个实心球体,但它们不会显示在我定义的终点之间。很明显,无论是我的数学还是语法都是棘手的(或者更有可能,两者都是)。

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

如果您想要与帧速率无关的一致时序,则需要跟踪时间的流逝并使用它来控制两点之间的插值距离。记住开始时间并计算所需的结束时间,然后计算每一帧,计算(float)(now-start)/(end-start)。这将为您提供介于0.0和1.0之间的值。将该值乘以每个球面坐标的增量并添加它们的起始角度,您将获得现在需要的角度。