自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在Windows Phone 7 3D開(kāi)發(fā)中使用紋理貼圖

移動(dòng)開(kāi)發(fā)
本文將介紹“如何在Windows Phone 7 3D開(kāi)發(fā)中使用紋理貼圖”,直接上一段代碼,該代碼使用VertexPositionColor渲染了一個(gè)三角形,程序運(yùn)行一切正常。

Windows Phone 7對(duì)3D的支持還是不錯(cuò)的,據(jù)說(shuō)是用OpenGL/ES做的,使用起來(lái)倒是也有點(diǎn)那種感覺(jué)。本文就不講XNA 4.0的游戲框架了,直接上一段代碼,該代碼使用VertexPositionColor渲染了一個(gè)三角形,程序運(yùn)行一切正常。

  1. + expand sourceview plaincopy to clipboardprint?  
  2.  

運(yùn)行結(jié)果如下:

運(yùn)行結(jié)果

在確認(rèn)了3D開(kāi)發(fā)的這種代碼結(jié)構(gòu)以后,用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的第二個(gè)Vetex2代表的是UV坐標(biāo),對(duì)應(yīng)的含義是(0,0)點(diǎn)對(duì)應(yīng)了紋理圖片的左上角,(1,1)點(diǎn)對(duì)應(yīng)了紋理圖片的右下角。

上述代碼在運(yùn)行的時(shí)候會(huì)在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
同時(shí)模擬器里的程序直接退出,看不到結(jié)果。原因是什么呢?疑惑并仔細(xì)檢視代碼中……

與前一個(gè)彩色三角形對(duì)比,頂點(diǎn)順序沒(méi)變,攝像機(jī)位置沒(méi)變,投影矩陣沒(méi)變,按說(shuō)是不可能出現(xiàn)這種問(wèn)題的,而且程序直接崩了,沒(méi)有信息拋出,真是很郁悶。

經(jīng)過(guò)不斷的試錯(cuò),在宣布放棄之前,忽然想起來(lái)關(guān)于紋理方面的一個(gè)注意事項(xiàng)。有過(guò)3D開(kāi)發(fā)經(jīng)驗(yàn)的朋友都知道,紋理是要求符合2的整數(shù)次方對(duì)齊的,而我所加載的來(lái)自于外部任意圖片的紋理不符合這一要求,所以程序掛了。

又查了一些資料,找到了準(zhǔn)確的原因。原來(lái)是Windows Phone 7 的XNA中默認(rèn)的紋理尋址模式使用了Wrap,造成了與GPU的不兼容,如果改成Clamp就好了。

看來(lái)在這個(gè)地方微軟得要有文檔說(shuō)明才好,否則還真是難找問(wèn)題所在。修改后的代碼如下:

  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.  

最終的模擬器結(jié)果是:

最終的模擬器結(jié)果

不管怎么說(shuō),Windows Phone 7的XNA游戲開(kāi)發(fā)框架以及3D方面的開(kāi)發(fā)接口還是很出色的,頂一下微軟,并希望這個(gè)平臺(tái)能盡快發(fā)展起來(lái)。

附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.  

本文轉(zhuǎn)自http://blog.csdn.net/caowenbin

【編輯推薦】

  1. Windows Phone 7中用好Silverlight開(kāi)發(fā)利器
  2. Windows Phone 7的地圖控件
  3. Windows Phone 7的樞軸控件
  4. Windows Phone 7的全景視圖控件
  5. 使用獨(dú)立存儲(chǔ)開(kāi)發(fā)Windows Phone 7應(yīng)用程序
責(zé)任編輯:佚名 來(lái)源: 文斌的專欄
相關(guān)推薦

2010-09-08 11:26:26

Windows PhoXNA 4.0 3D游戲開(kāi)發(fā)

2023-08-28 00:53:03

AI3D

2013-07-30 11:18:37

Windows PhoWindows Pho

2011-03-21 09:05:40

IronRubyWindows Pho

2010-12-14 18:48:49

微軟

2011-06-07 11:35:38

Windows Pho

2012-08-16 10:35:50

Windows Pho

2013-11-07 15:36:42

Windows Pho海外市場(chǎng)

2023-08-18 08:00:00

游戲開(kāi)發(fā)3D模型

2010-12-01 09:01:31

獨(dú)立存儲(chǔ)Windows Pho

2010-04-08 17:40:23

Windows Pho

2022-12-08 08:00:00

.NET?7BitArray數(shù)據(jù)執(zhí)行

2010-03-09 10:51:15

Windows Pho

2010-08-13 08:21:11

Windows Pho

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

2024-07-03 14:29:38

2010-07-21 14:42:15

Windows Pho

2011-03-30 11:21:41

Windows Pho開(kāi)發(fā)大賽
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)