Windows Phone 7 3D開發中使用紋理貼圖

來源: CSDN  發布時間: 2011-03-23 10:34  閱讀: 911 次  推薦: 0   原文鏈接   [收藏]  
摘要:Windows Phone 7對3D的支持還是不錯的,據說是用OpenGL/ES做的,使用起來倒是也有點那種感覺。另外,寫本文的另一個原因是我的第一個3D試驗竟然遇到了問題,花了1個小時才搞定,所以也一并記錄下來,供遇到和我同樣的問題的朋友參考。

  Windows Phone 7對3D的支持還是不錯的,據說是用OpenGL/ES做的,使用起來倒是也有點那種感覺。本文就不講XNA 4.0的游戲框架了,直接上一段代碼,該代碼使用VertexPositionColor渲染了一個三角形,程序運行一切正常。

1. + expand sourceview plaincopy to clipboardprint?
2.

  運行結果如下:

運行結果

  在確認了3D開發的這種代碼結構以后,用VertexPositionTexture渲染同樣的三角形,只是這次采用紋理貼圖,代碼如下:

1.
2. view plaincopy to clipboardprint?
3. VertexPositionTexture[] trangleTexture;
4.
5. protected override void LoadContent()
6. {
7. spriteBatch =new SpriteBatch(GraphicsDevice);
8.
9. image = Content.Load<Texture2D>(@"Images/Tulips");
10. trangleTexture =new VertexPositionTexture[]{
11. new VertexPositionTexture(new Vector3(0, 1, 0),new Vector2(0.5f,0) ),
12. new VertexPositionTexture(new Vector3(1, -1, 0),new Vector2(1,1f) ),
13. new VertexPositionTexture(new Vector3(-1,-1, 0),new Vector2(0,1f) )
14. };
15.
16. vertexBuffer =new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), trangleTexture.Length, BufferUsage.None);
17. vertexBuffer.SetData<VertexPositionTexture>(trangleTexture);
18.
19. basicEffect =new BasicEffect(GraphicsDevice);
20.
21. GraphicsDevice.SetVertexBuffer(vertexBuffer);
22. }
23.
24. protected override void Draw(GameTime gameTime)
25. {
26. GraphicsDevice.Clear(Color.CornflowerBlue);
27.
28. basicEffect.World = world;
29. basicEffect.View = camera.view;
30. basicEffect.Projection = camera.projection;
31. basicEffect.Texture = image;
32. basicEffect.TextureEnabled =true;
33.
34. foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
35. {
36. pass.Apply();
37. GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, trangleTexture, 0, 1);
38. }
39. base.Draw(gameTime);
40. }

  啰嗦一句,在此代碼中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就好了。

  看來在這個地方微軟得要有文檔說明才好,否則還真是難找問題所在。修改后的代碼如下:

1. view plaincopy to clipboardprint?
2. protected override void LoadContent()
3. {
4. // Create a new SpriteBatch, which can be used to draw textures.
5. spriteBatch =new SpriteBatch(GraphicsDevice);
6.
7. image = Content.Load<Texture2D>(@"Images/Tulips");
8.
9. trangleTexture =new VertexPositionTexture[]{
10. new VertexPositionTexture(new Vector3(0, 1, 0),new Vector2(0.5f,0) ),
11. new VertexPositionTexture(new Vector3(1, -1, 0),new Vector2(1,1f) ),
12. new VertexPositionTexture(new Vector3(-1,-1, 0),new Vector2(0,1f) )
13. };
14.
15. vertexBuffer =new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), trangleTexture.Length, BufferUsage.None);
16. vertexBuffer.SetData<VertexPositionTexture>(trangleTexture);
17.
18. basicEffect =new BasicEffect(GraphicsDevice);
19.
20. GraphicsDevice.SetVertexBuffer(vertexBuffer);
21. GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
22. }
23.

  最終的模擬器結果是:

最終的模擬器結果

  不管怎么說,Windows Phone 7的XNA游戲開發框架以及3D方面的開發接口還是很出色的,頂一下微軟,并希望這個平臺能盡快發展起來。

  附Camera的代碼:

1. view plaincopy to clipboardprint?
2. using System;
3. using System.Collections.Generic;
4. using System.Linq;
5. using Microsoft.Xna.Framework;
6. using Microsoft.Xna.Framework.Audio;
7. using Microsoft.Xna.Framework.Content;
8. using Microsoft.Xna.Framework.GamerServices;
9. using Microsoft.Xna.Framework.Graphics;
10. using Microsoft.Xna.Framework.Input;
11. using Microsoft.Xna.Framework.Media;
12.
13.
14. namespace WindowsPhoneGame1
15. {
16. publicclass Camera : Microsoft.Xna.Framework.GameComponent
17. {
18. public Matrix view{get;protected set;}
19. public Matrix projection { get; protected set; }
20.
21. public Camera(Game game,Vector3 pos,Vector3 target,Vector3 up)
22. : base(game)
23. {
24. view = Matrix.CreateLookAt(pos, target, up);
25. projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)game.Window.ClientBounds.Width / (float)game.Window.ClientBounds.Height, 1, 100);
26. }
27.
28. public override void Initialize()
29. {
30. base.Initialize();
31. }
32.
33. public override void Update(GameTime gameTime)
34. {
35. base.Update(gameTime);
36. }
37. }
38. }
39.
0
0
 
 
 

文章列表

arrow
arrow
    全站熱搜

    大師兄 發表在 痞客邦 留言(0) 人氣()