成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

移動開發
本文將介紹“如何在Windows Phone 7 3D開發中使用紋理貼圖”,直接上一段代碼,該代碼使用VertexPositionColor渲染了一個三角形,程序運行一切正常。

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.     public class 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.  

本文轉自http://blog.csdn.net/caowenbin

【編輯推薦】

  1. Windows Phone 7中用好Silverlight開發利器
  2. Windows Phone 7的地圖控件
  3. Windows Phone 7的樞軸控件
  4. Windows Phone 7的全景視圖控件
  5. 使用獨立存儲開發Windows Phone 7應用程序
責任編輯:佚名 來源: 文斌的專欄
相關推薦

2010-09-08 11:26:26

Windows PhoXNA 4.0 3D游戲開發

2013-07-30 11:18:37

Windows PhoWindows Pho

2023-08-28 00:53:03

AI3D

2012-08-16 10:35:50

Windows Pho

2011-06-07 11:35:38

Windows Pho

2010-12-14 18:48:49

微軟

2011-03-21 09:05:40

IronRubyWindows Pho

2013-11-07 15:36:42

Windows Pho海外市場

2023-08-18 08:00:00

游戲開發3D模型

2010-04-08 17:40:23

Windows Pho

2010-12-01 09:01:31

獨立存儲Windows Pho

2010-08-13 08:21:11

Windows Pho

2010-03-09 10:51:15

Windows Pho

2022-12-08 08:00:00

.NET?7BitArray數據執行

2019-08-26 09:20:29

Windows 10虛擬桌面Windows

2011-02-18 09:47:42

SymbianWindows PhoAndroid

2013-04-17 10:24:29

Windows Pho

2011-03-30 11:21:41

Windows Pho開發大賽

2010-07-21 14:42:15

Windows Pho

2020-01-07 09:50:41

Windows 10上帝模式Windows
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产欧美一区二区三区在线看 | 日韩欧美久久 | 欧美自拍日韩 | 久久成人一区 | 久久亚洲国产 | 国产精品久久久久久吹潮 | 日本不卡一区 | 91国内精精品久久久久久婷婷 | 99综合| 色欧美片视频在线观看 | 国产精品久久 | 国产一区三区视频 | 国产精品久久在线观看 | 男人的天堂久久 | 一区二区三区四区国产精品 | 中文字幕日韩欧美一区二区三区 | 久久91精品国产一区二区 | 成人午夜视频在线观看 | 韩日一区二区三区 | 在线观看久草 | 国产丝袜一区二区三区免费视频 | 久久久一区二区三区四区 | 中文字幕91 | 国产精品毛片一区二区三区 | 国产精品激情在线 | 国产精品久久久久久久久久久免费看 | 亚洲国产精品99久久久久久久久 | 亚洲成av人片在线观看无码 | 91精品一区二区三区久久久久 | 国产一区久久久 | 国产成人免费在线观看 | 亚洲97 | 日本在线你懂的 | 一级黄色片一级黄色片 | 国产在线视频一区二区 | 免费在线观看av | 午夜伦理影院 | 欧美日韩亚洲一区 | 久久综合av | 色眯眯视频在线观看 | 久久久久久黄 |