經過前兩節的學習,我們已經具備了創建三維空間的條件了,相信很多人已經躍躍欲試了,接下來,我們就動手開始在Windows Phone中的3D開發之旅。
打開Visual Studio 2010(什么?還沒有Windows Phone的開發環境?唉,自己處理一下吧,有問題問[百度](http://www.baidu.com)),新建一個項目(File->New->Project),在彈出的對話框中選擇XNA Game Studio 4.0中的Windows Phone Game,在Name中就命名為“Hello”吧,點擊“OK”確定。界面如下圖所示。

在Solution Explorer窗口中右擊項目名,執行Add->Class菜單項,如下圖所示。

在彈出的對話框中按下圖選擇,將類命名為Camera,點擊“Add”按鈕。

為Camera類添加兩個屬性:
public Matrix view{get;protected set;}
public Matrix projection { get; protectedset; }
修改Camera類的構造函數為:
~~~
public Camera(Game game, Vector3 pos,Vector3 target, Vector3 up, float fieldOfView, float aspectRatio, floatnearPlaneDistance,float farPlaneDistance)
?????????? : base(game)
?????? {
?????????? view = Matrix.CreateLookAt(pos,target, up);
?????????? projection =Matrix.CreatePerspectiveFieldOfView(fieldOfView,aspectRatio,nearPlaneDistance,farPlaneDistance);
?????? }
~~~
該類的完整代碼見《[在Windows Phone中進行3D開發之二攝像機](http://blog.csdn.net/caowenbin/article/details/6871332)》一文。
????? 打開Game1類,添加如下成員變量:
?????? Camera camera;
?????? Matrix world = Matrix.Identity;
?????? BasicEffect basicEffect;
在Game1()構造方法中添加代碼:
graphics.IsFullScreen =true;
在LoacContent()方法中添加代碼:
~~~
?????????? camera = new Camera(this, newVector3(0, 0, 5), Vector3.Zero, Vector3.Up, MathHelper.PiOver4,GraphicsDevice.Viewport.AspectRatio, 1.0f, 50.0f);
?????????? Components.Add(camera);
?????????? basicEffect = newBasicEffect(GraphicsDevice);
~~~
這段代碼中,MathHelper是一個數學上的輔助類,其PiOver4屬性是π/4,即45度角。Viewport.AspectRatio是長寬比。
在Draw()方法中添加代碼:
~~~
?????????? basicEffect.World = world;
?????????? basicEffect.View = camera.view;
?????????? basicEffect.Projection =camera.projection;
?????????? foreach (EffectPass pass inbasicEffect.CurrentTechnique.Passes)
?????????? {
?????????????? pass.Apply();
?????????? }
~~~
BasicEffect類的作用是控制渲染效果,在3D開發中必不可少。通常用在Draw()方法中進行設置以改變渲染器的行為,后文中我們還將用它控制顏色、光線等很多特效。在前幾節內容中用的較多的就是BasicEffect的三個屬性,即代碼中的World、View、Projection,分別代表世界矩陣、攝像機矩陣、投影矩陣。這里我們要在坐標原點繪制對象,所以World設置了單位矩陣,即Mathix.Identity。
BasicEffect里包含一些Technique,每個Technique又由若干EffectPass組成,我們要在每個EffectPass上進行Apply請求,才能在該EffectPass上進行繪制。這個Apply()方法的調用就像我們進行2D時調用SpriteBatch.Begin()一樣,是繪制開始前必須調用的方法。因此在使用BasicEffect時,需要一個foreach循環。
完整的Game1類代碼如下:
~~~
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace Hello
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
Camera camera;
Matrix world = Matrix.Identity;
BasicEffect basicEffect;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
// Extend battery life under lock.
InactiveSleepTime = TimeSpan.FromSeconds(1);
graphics.IsFullScreen = true;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
camera = new Camera(this, new Vector3(0, 0, 5), Vector3.Zero, Vector3.Up, MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 1.0f, 50.0f);
Components.Add(camera);
basicEffect = new BasicEffect(GraphicsDevice);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
basicEffect.World = world;
basicEffect.View = camera.view;
basicEffect.Projection = camera.projection;
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
}
base.Draw(gameTime);
}
}
}
~~~
運行程序,打開模擬器,運行結果如下圖所示。

不用懷疑,雖然屏幕上什么都沒有,但事實上屏幕上所表現的已經是一個三維空間,我們有一個攝像機位于(0,0,5)位置,也就是屏幕外,攝像機方向指向了原點,使用了45度視角和屏幕的寬高比進行成像,近平面是1,遠平面是50。在下節中,我們就將在其中加入物體。
——歡迎轉載,請注明出處 [http://blog.csdn.net/caowenbin](http://blog.csdn.net/caowenbin) ——
- 前言
- Windows Phone 7開發環境初體驗
- Windows Phone 7 3D開發中使用紋理貼圖
- 在Windows Phone中進行3D開發之一坐標系
- 在Windows Phone中進行3D開發之二攝像機
- 在Windows Phone中進行3D開發之三空間
- 在Windows Phone中進行3D開發之四三角形
- 在Windows Phone中進行3D開發之五平移縮放
- 在Windows Phone中進行3D開發之六旋轉
- 在Windows Phone中進行3D開發之七紋理
- 在Windows Phone中進行3D開發之八光照
- 在Windows Phone中進行3D開發之九模型
- 在Windows Phone中進行3D開發之十組件
- 在Windows Phone中進行3D開發之十一天空
- 在Windows Phone中進行3D開發之十二飛行
- 在Windows Phone中進行3D開發之十三陽光
- 在Windows Phone中進行3D開發之后記(附源碼)