经过前两节的学习,我们已经具备了创建三维空间的条件了,相信很多人已经跃跃欲试了,接下来,我们就动手开始在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”确定。界面如下图所示。
![](https://box.kancloud.cn/2016-04-08_570727fa0b206.gif)
在Solution Explorer窗口中右击项目名,执行Add->Class菜单项,如下图所示。
![](https://box.kancloud.cn/2016-04-08_570727fa240dc.gif)
在弹出的对话框中按下图选择,将类命名为Camera,点击“Add”按钮。
![](https://box.kancloud.cn/2016-04-08_570727fa3b783.gif)
为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);
}
}
}
~~~
运行程序,打开模拟器,运行结果如下图所示。
![](https://box.kancloud.cn/2016-04-08_570727fa574d8.gif)
不用怀疑,虽然屏幕上什么都没有,但事实上屏幕上所表现的已经是一个三维空间,我们有一个摄像机位于(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开发之后记(附源码)