在3D场景中实现“抓取”相机平移工具

时间:2011-06-02 20:21:26

标签: opengl 3d

在我的场景中,我想要“抓住”地形,然后在移动光标时让相机平移(其高度,视图矢量,视野等等都保持不变)。

所以最初的“抓取”点将是世界空间中的工作点,我希望这一点在我拖动时保持在光标下。

我目前的解决方案是采用先前和当前的屏幕点,取消投影,从另一个中减去一个,并使用该矢量翻译我的相机。这接近我想要的,但是光标并没有完全保持在初始场景位置,如果你从地形边缘附近开始,这可能会有问题。

// Calculate scene points
MthPoint3D current_scene_point =
   camera->screenToScene(current_point.x, current_point.y);
MthPoint3D previous_scene_point =
   camera->screenToScene(previous_point.x, previous_point.y);

// Make sure the cursor didn't go off the terrain
if (current_scene_point.x != MAX_FLOAT &&
    previous_scene_point.x != MAX_FLOAT)
{
   // Move the camera to match the distance
   // covered by the cursor in the scene
   camera->translate(
      MthVector3D(
         previous_scene_point.x - current_scene_point.x,
         previous_scene_point.y - current_scene_point.y,
         0.0));
}

任何想法都表示赞赏。

1 个答案:

答案 0 :(得分:1)

多睡一会儿:

  • 获取交叉点的初始位置,在模型空间中的世界空间(相对于模型的原点)

即使用screenToScene()

  • 创建从相机通过鼠标位置的光线:{ray.start,ray.dir}

ray.start是camera.pos,ray.dir是(screenToScene() - camera.pos)

  • 解决NewPos = ray.start + x * ray.dir,知道NewPos.y = initialpos_worldspace.y;

- > ray.start.y + x * ray.dir.y = initialpos_worldspace.y

- > x =(initialpos_worldspace.y - ray.start.y)/rad.dir.y(小心dividebyzeroexception)

- >在NewPos_worldspace = ray.start + x * ray.dir

中重新注入x
  • 从中减去initialpos_modelspace以“重新居中”模型

但最后一点看起来很可疑。

相关问题