计算向上
这是我的代码:
Vector3f CameraUpVector() const
{
Vector3f cameraDirection = m_cameraTarget - m_cameraPosition;
cameraDirection = cameraDirection.Normalize();
////////////
Vector3f cameraUp = cameraDirection.Cross(Vector3f(1.0f, 0.0f, 0.0f));
return cameraUp;
}
//////////
Vector3f cameraUp = CameraUpVector();
///////////////
p.SetCamera(m_cameraPosition , m_cameraTarget , cameraUp);
这个计算好吗?
如果不是 - 我怎么能改变它?
答案 0 :(得分:1)
不,你会发现相机在前向矢量周围滚动。我会改变你的代码如下(假设右手坐标系和世界向上矢量是(0,1,0)):
Vector3f CameraUpVector() const
{
Vector3f cameraDirection = m_cameraTarget - m_cameraPosition;
Vector3f cameraRight = cameraDirection.Cross(Vector3f(0.0f, 1.0f, 0.0f));
Vector3f cameraUp = cameraRight.Cross(cameraDirection );
return cameraUp.Normalize();
}
请注意,您应该处理摄像机目标位于摄像机位置正上方或下方的特殊情况(通过防止发生这种情况或保留最后一个有效摄像机向上矢量的记录并使用它来代替世界在这种情况下的矢量)