我正在用OpenGL编写一个太阳系模拟器。因此,太阳系中的一个组成部分是地球轨道(围绕太阳的简单圆圈,由gluDisk功能创建)。
我想知道如何检索此磁盘的法向矢量,因为我需要将其用作摄像机的旋转矢量(需要跟随地球围绕太阳旋转)。
这是创建轨道的代码(Java),它运行良好。 使用此代码,我如何检索此磁盘的正常情况? (因为磁盘包含在某个平面中,由某些法线定义)。
gl.glPushMatrix();
// if tilt is 0, align the orbit with the xz plane
gl.glRotated(90d - orbitTilt, 1, 0, 0);
gl.glTranslated(0, 0, 0); // orbit is around the origin
gl.glColor3d(0.5, 0.5, 0.5); // gray
// draw orbit
glu.gluQuadricDrawStyle(quad, GLU.GLU_SILHOUETTE);
glu.gluDisk(quad, 0, position.length(), slices, 1);
gl.glPopMatrix();
答案 0 :(得分:1)
在转换之前,磁盘位于xy平面中。这意味着法线指向z方向(0, 0, 1)
。实际上,gluDisk()
会在渲染过程中发出这些法线,并且您指定的变换将应用于它们,因此渲染时您不必做任何事情。
要自己计算变换后的法线,您只需要应用旋转。翻译不用于转换法线。
因此,我们需要将围绕x轴的90 - t
旋转应用于向量(0, 0, 1)
。应用相应的旋转矩阵给出:
[ 1 0 0 ] [ 0 ] [ 0 ]
[ 0 cos(90-t) -sin(90-t) ] * [ 0 ] = [ -sin(90-t) ]
[ 0 sin(90-t) cos(90-t) ] [ 1 ] [ cos(90-t) ]
应用几个三角标识:
cos(90 - t) = sin(t)
sin(90 - t) = cos(t)
给出:
[ 0 ] [ 0 ]
[ -sin(90-t) ] = [ -cos(t) ]
[ cos(90-t) ] [ sin(t) ]
在代码中应用sin()
和cos()
函数时,请记住角度以弧度为单位指定。因此,如果您的orbitTilt
角度目前为度数,则正常情况为:
xNormal = 0.0;
yNormal = -cos(orbitTilt * (M_PI / 180.0));
zNormal = sin(orbitTilt * (M_PI / 180.0));