在球形坐标(OpenGL、C++、GLUT)之间跨步
Stepping between spherical coords (OpenGL, C++, 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();
}
为了在这两点之间切换,我执行以下操作:
- 求theta_points_1,2和phi_points_1,2
- 将差额除以n(让步为s)
- 重新绘制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_theta
和step_phi
的范围是多少?
当您在循环中声明一个变量时,它会超出范围,并在每次迭代后被销毁。因此,只有i
的值在循环迭代之间发生变化。
另外:考虑使用向量/点类。(x_point_1, y_point_1)
不是C++:)。
如果您希望无论帧速率如何都保持一致的定时,则需要跟踪时间的流逝,并使用它来控制在两点之间插值的距离。记住开始时间并计算所需的结束时间,然后每帧计算(float)(now-start)/(end-start)
。这将为您提供一个介于0.0和1.0之间的值。将该值乘以每个球面坐标的德尔塔,再加上它们的起始角度,你就会得到现在需要的角度。
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- 类与私有变量的其他类之间的线程安全性
- 如何在cpp文件之间切换窗口?在Qt中
- 线程之间的布尔停止信号
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 我想知道长双倍和双倍之间的区别
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 构造函数和转换运算符之间的重载解析
- 在球形坐标(OpenGL、C++、GLUT)之间跨步