HOW TO:移動及對準鏡頭
更新:2007 年 11 月
您可以使用檢視轉換來移動和對準 Direct3D 場景中的鏡頭。
注意事項: |
---|
Managed Direct3D Mobile 應用程式需要適用於 Pocket PC 和 Smartphone 的 Windows Mobile 5.0 版軟體。如需 Windows Mobile 軟體和 SDK 的詳細資訊,請參閱 .NET Compact Framework 的外部資源。 |
檢視轉換會將 Direct3D 物件從全局空間轉譯到螢幕空間。當您執行檢視轉換時,您對鏡頭 (或檢視器) 位置、鏡頭對準和座標系統的了解可讓您管理螢幕上的結果,並可確保您的全局空間座標能夠正確轉換成螢幕空間座標。
全局空間是以左手座標系統為基礎,其中正數 Y 軸值會向上移,正數 X 軸值會向右移,正數 Z 軸值則是遠離假想的檢視器。相反地,在右手的座標系統中,正數 Z 軸值則會朝檢視器方向移動。
裝置的 Transform 屬性會傳回 Matrix 結構以描述轉換的狀態。若要移動和對準鏡頭,請使用 LookAtLH 方法將檢視轉換矩陣傳遞至裝置物件,以便處理轉換。
在下面的範例中,鏡頭一開始是從場景後面開始,起點是在 Z 軸的正數點位置,並且對準靠近原點的某個點。由於鏡頭位於場景後方,因此正數 Z 軸值是讓物件放置得靠近鏡頭,而非遠離鏡頭。此外,正數 X 軸值是讓物件放置在左邊,而非右邊。
範例
下列程式碼範例示範應用程式的檢視轉換,此應用程式會呈現表示船隻之基本方塊網狀結構的動畫。如需完整的範例程式碼,請參閱 HOW TO:轉換 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;
}
編譯程式碼
這個範例需要 HOW TO:轉換 Direct3D 物件內容中的完整範例程式碼。
請參閱
概念
.NET Compact Framework HOW TO 主題