如何在Windows Phone 7 3D開發中使用紋理貼圖
Windows Phone 7對3D的支持還是不錯的,據說是用OpenGL/ES做的,使用起來倒是也有點那種感覺。本文就不講XNA 4.0的游戲框架了,直接上一段代碼,該代碼使用VertexPositionColor渲染了一個三角形,程序運行一切正常。
- + expand sourceview plaincopy to clipboardprint?
運行結果如下:
在確認了3D開發的這種代碼結構以后,用VertexPositionTexture渲染同樣的三角形,只是這次采用紋理貼圖,代碼如下:
- view plaincopy to clipboardprint?
- 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就好了。
看來在這個地方微軟得要有文檔說明才好,否則還真是難找問題所在。修改后的代碼如下:
- view plaincopy to clipboardprint?
- 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的代碼:
- view plaincopy to clipboardprint?
- 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
【編輯推薦】