【发布时间】:2012-03-11 22:48:19
【问题描述】:
我正在使用用于 OpenGL 的 OpenTK 和用于 C# ide 的 monodevelop 进行简单的太阳系模拟。进展顺利,我有行星围绕太阳运行,照明正常。当我想倾斜行星的轴时,问题就开始了;无论我在我的方法中放置新的旋转,它都不会明显旋转。一旦它使我的球体的发光面以某种方式围绕它旋转。如果我在使行星绕其轴旋转的旋转之前或之后放置旋转,那么行星的顶部会变得奇怪,好像法线被破坏了一样。
public void Display (float time)
{
GL.Rotate (axialTilt, tiltAxis); // This rotation causes the issues, no matter
// where I put it
GL.PushMatrix (); // This push/pop was added later, but it
// doesn't help
rotationalPosition += rotationSpeed * time;
GL.Rotate (rotationalPosition, Vector3.UnitZ);
planet.Draw ();
GL.PopMatrix ();
if (ring != null) {
ring.Draw ();
}
if (moons != null) {
foreach (Orbit o in moons) {
o.Draw (time);
}
}
}
这是由以下人员调用的:
public void Draw (float time)
{
GL.PushMatrix ();
GL.Rotate (angle, orientation); // This will allow an angled axis for
// moons and other planetary orbits.
orbitPosition += orbitSpeed * time;
GL.Rotate (orbitPosition, Vector3.UnitZ);
GL.Translate (orbitDistance, 0, 0);
GL.Rotate (-orbitPosition, Vector3.UnitZ); // Rotate backward, so rotating around
// the orbit axis doesn't rotate the
// planet.
orbitingBody.Display (time);
GL.PopMatrix ();
}
因此很奇怪。这里应该没有什么会导致法线出现问题,而且倾斜似乎在正确的位置。
【问题讨论】:
-
您调用
GL.Rotate (rotationalPosition, Vector3.UnitZ);的位置是您想要围绕其轴旋转您的行星的位置,对吗?如果它有一个倾斜的轴,你不应该围绕 z 轴旋转,你应该围绕 z 轴旋转它,该 z 轴由行星轴向倾斜旋转。 -
然而,你的行星绘图代码看起来确实是不必要的复杂。您可以将行星的极坐标转换为笛卡尔坐标并通过它进行转换。
x = orbitDistance*cos(orbitPosition); y=orbitDistance*sin(orbitPosition);简化代码可能会帮助您发现问题。