如何:移动照相机并使其对准目标
更新:2007 年 11 月
可以使用视图变换在 Direct3D 场景中移动照相机并使其对准目标。
说明: |
---|
托管 Direct3D 移动应用程序需要使用适用于 Pocket PC 和 Smartphone 的 Windows Mobile 5.0 版软件。有关 Windows Mobile 软件和 SDK 的信息,请参见 .NET Compact Framework 的外部资源。 |
视图变换会将 Direct3D 对象从世界空间转换为屏幕空间。当执行视图变换时,如果您了解照相机(取景器)位置、照相机目标和坐标系统,则可以操作屏幕上的结果,确保将世界空间坐标正确转换为屏幕空间坐标。
世界空间基于左手坐标系统。在左手坐标系统中,正的 y 轴值将向上移动,正的 x 轴值将向右移动,而正的 z 轴值将朝远离假想取景器的方向移动。相反,在右手坐标系统中,正的 z 轴值将朝靠近取景器的方向移动。
设备的 Transform 属性将返回一个 Matrix 结构,用于说明变换状态。若要移动照相机并使其对准目标,请使用 LookAtLH 方法将一个视图转换矩阵传递给设备对象以处理变换。
在下面的示例中,照相机最初从场景后面某个位于 z 轴上的正值点开始,并对准原点附近的某个点。由于照相机位于场景后方,正的 z 轴值将使对象靠近照相机(而非远离照相机)。此外,正的 x 轴值将使对象远离左侧(而非远离右侧)。
示例
下面的代码示例演示一个应用程序的视图变换,该应用程序将为表示船的基元框状网格创建动画。有关完整的示例代码,请参见如何:变换 Direct3D 对象。
此代码示例包含以下几个对象:
' Set up the view matrix. You can define a view matrix with a camera position,
' a point to look at (camera target), and an "up" direction.
' First vector passed to LookAtLH is the camera position.
' Second vector passed to LookAtLH is the camera target.
' Third vector passed to LookAtLH defines the "up" direction.
' In this example, you set the camera seven units up along the z-axis ("behind"
' the scene), down one unit, and left two units. You then point the camera
' just above the origin and define "up" to be in the y-direction.
If Not isShipDeparted Then
device.Transform.View = Matrix.LookAtLH(New Vector3(- 2, - 1, 7), New Vector3(0, 1, 0), New Vector3(0, 1, 0))
Else
' Handles movement of camera after
' the ship "fires" the main engines.
device.Transform.View = Matrix.LookAtLH(New Vector3(xCameraPosition, yCameraPosition, 7), New Vector3(0, 1, 0), New Vector3(0, 1, 0))
xCameraPosition += 0.01F
yCameraPosition += 0.01F
End If
// Set up the view matrix. You can define a view matrix with a camera position,
// a point to look at (camera target), and an "up" direction.
// First vector passed to LookAtLH is the camera position.
// Second vector passed to LookAtLH is the camera target.
// Third vector passed to LookAtLH defines the "up" direction.
// Here, you set the camera seven units up along the z-axis ("behind"
// the scene), down one unit, and left two units. You then point the camera
// just above the origin and define "up" to be in the y-direction.
if (!isShipDeparted)
{
device.Transform.View = Matrix.LookAtLH(new Vector3(-2, -1, 7),
new Vector3(0, 1, 0), new Vector3(0, 1, 0));
}
else
{
// Handles movement of camera after
// the ship "fires" the main engines.
device.Transform.View = Matrix.LookAtLH(new Vector3(xCameraPosition,
yCameraPosition, 7), new Vector3(0, 1, 0), new Vector3(0, 1, 0));
xCameraPosition += 0.01f;
yCameraPosition += 0.01f;
}
编译代码
此示例需要使用如何:变换 Direct3D 对象中的完整示例代码。