Windows Phone 7對3D的支持還是不錯的,據說是用OpenGL/ES做的,使用起來倒是也有點那種感覺。另外,寫本文的另一個原因是我的第一個3D試驗竟然遇到了問題,花了1個小時才搞定,所以也一并記錄下來,供遇到和我同樣的問題的朋友參考。
本文就不講XNA 4.0的游戲框架了,直接上一段代碼,該代碼使用VertexPositionColor渲染了一個三角形,程序運行一切正常。
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 WindowsPhoneGame1{ public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D image; VertexPositionColor[] trangle; VertexBuffer vertexBuffer; BasicEffect basicEffect; Camera camera; Matrix world = Matrix.Identity; public Game1() { graphics = new GraphicsDeviceManager(this); graphics.IsFullScreen = true; Content.RootDirectory = "Content"; camera = new Camera(this, new Vector3(0, 0, 5), Vector3.Zero, new Vector3(0, 1, 0)); // Frame rate is 30 fps by default for Windows Phone. TargetElapsedTime = TimeSpan.FromTicks(333333); } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); image = Content.Load<Texture2D>(@"Images/Tulips"); trangle = new VertexPositionColor[]{ new VertexPositionColor(new Vector3(0, 1, 0), Color.Red), new VertexPositionColor(new Vector3(1, -1, 0), Color.Green), new VertexPositionColor(new Vector3(-1,-1, 0), Color.Blue) }; vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, typeof(VertexPositionColor), 3, BufferUsage.None); vertexBuffer.SetData<VertexPositionColor>(trangle); basicEffect = new BasicEffect(GraphicsDevice); GraphicsDevice.SetVertexBuffer(vertexBuffer); } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); basicEffect.World = world; basicEffect.View = camera.view; basicEffect.Projection = camera.projection; basicEffect.VertexColorEnabled = true; foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, trangle, 0, 1); } base.Draw(gameTime); } }}
運行結果如下:

在確認了3D開發的這種代碼結構以后,用VertexPositionTexture渲染同樣的三角形,只是這次采用紋理貼圖,代碼如下:
VertexPositionTexture[] trangleTexture; protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); image = Content.Load<Texture2D>(@"Images/Tulips"); trangleTexture = new VertexPositionTexture[]{ new VertexPositionTexture(new Vector3(0, 1, 0),new Vector2(0.5f,0) ), new VertexPositionTexture(new Vector3(1, -1, 0),new Vector2(1,1f) ), new VertexPositionTexture(new Vector3(-1,-1, 0),new Vector2(0,1f) ) }; vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), trangleTexture.Length, BufferUsage.None); vertexBuffer.SetData<VertexPositionTexture>(trangleTexture); basicEffect = new BasicEffect(GraphicsDevice); GraphicsDevice.SetVertexBuffer(vertexBuffer); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); basicEffect.World = world; basicEffect.View = camera.view; basicEffect.Projection = camera.projection; basicEffect.Texture = image; basicEffect.TextureEnabled = true; foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, trangleTexture, 0, 1); } base.Draw(gameTime); }
啰嗦一句,在此代碼中VertexPositionTexture的第二個Vetex2代表的是UV坐標,對應的含義是(0,0)點對應了紋理圖片的左上角,(1,1)點對應了紋理圖片的右下角。
上述代碼在運行的時候會在VS2010的輸出窗口中顯示:
A first chance exception of type 'System.NotSupportedException' occurred in Microsoft.Xna.Framework.Graphics.dll
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in Microsoft.Xna.Framework.dll
同時模擬器里的程序直接退出,看不到結果。原因是什么呢?疑惑并仔細檢視代碼中……
與前一個彩色三角形對比,頂點順序沒變,攝像機位置沒變,投影矩陣沒變,按說是不可能出現這種問題的,而且程序直接崩了,沒有信息拋出,真是很郁悶。
經過不斷的試錯,在宣布放棄之前,忽然想起來關于紋理方面的一個注意事項。有過3D開發經驗的朋友都知道,紋理是要求符合2的整數次方對齊的,而我所加載的來自于外部任意圖片的紋理不符合這一要求,所以程序掛了。
又查了一些資料,找到了準確的原因。原來是Windows Phone 7 的XNA中默認的紋理尋址模式使用了Wrap,造成了與GPU的不兼容,如果改成Clamp就好了。
看來在這個地方微軟得要有文檔說明才好,否則還真是難找問題所在。修改后的代碼如下:
protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); image = Content.Load<Texture2D>(@"Images/Tulips"); trangleTexture = new VertexPositionTexture[]{ new VertexPositionTexture(new Vector3(0, 1, 0),new Vector2(0.5f,0) ), new VertexPositionTexture(new Vector3(1, -1, 0),new Vector2(1,1f) ), new VertexPositionTexture(new Vector3(-1,-1, 0),new Vector2(0,1f) ) }; vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), trangleTexture.Length, BufferUsage.None); vertexBuffer.SetData<VertexPositionTexture>(trangleTexture); basicEffect = new BasicEffect(GraphicsDevice); GraphicsDevice.SetVertexBuffer(vertexBuffer); GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp; }
最終的模擬器結果是:

不管怎么說,Windows Phone 7的XNA游戲開發框架以及3D方面的開發接口還是很出色的,頂一下微軟,并希望這個平臺能盡快發展起來。
附Camera的代碼:
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.Media;namespace WindowsPhoneGame1{ public class Camera : Microsoft.Xna.Framework.GameComponent { public Matrix view{get;protected set;} public Matrix projection { get; protected set; } public Camera(Game game,Vector3 pos,Vector3 target,Vector3 up) : base(game) { view = Matrix.CreateLookAt(pos, target, up); projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)game.Window.ClientBounds.Width / (float)game.Window.ClientBounds.Height, 1, 100); } public override void Initialize() { base.Initialize(); } public override void Update(GameTime gameTime) { base.Update(gameTime); } }}
——歡迎轉載,請注明出處 [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開發之后記(附源碼)