如果做过Zune上XNA 3.1开发的朋友可能会记得,在XNA 3.1中是不支持3D开发的,XNA 4.0中加入的3D支持类,主要包含在Microsoft.Xna.Framework.Graphics命名空间中。

微软前段时间发布根据XNA结构Windows Phone 7游戏开发实例,看到Silverlight for Phone和XNA 4.0的开发文章已经有了不少,并且质量很高。而XNA 4.0的3D开发这个范畴的文章还不是许多,XNA 4.0的3D类库规划的非常好,比iPhone和Android的OpenGLES类库高出一个层次。今后学习3D开发,用XNA类库也是个不错的挑选,并且Windows Phone模拟器对3D的支撑也非常好。仅有的惋惜是,Windows Phone不支撑C++的3D开发。

详解Windows Phone XNA 4.0 3D游戏开发  Windows 第1张

51CTO引荐专题:Windows Phone使用开发详解

程序代码编译环境Visual Stuido 2010, Windows Phone 7 SDK, XNA 4.0 Game Studio, 下载链接:http://files.cnblogs.com/aawolf/XNA_aawolf_3D.rar

假如做过Zune上XNA 3.1开发的朋友可能会记住,在XNA 3.1中是不支撑3D开发的,XNA 4.0中参加的3D支撑类,首要包含在Microsoft.Xna.Framework.Graphics命名空间中。假如XNA 4.0中的3D概念与OpenGLES十分相似,咱们能够找到许多相对应的函数、办法等,某种意义上,XNA 4.0的3D支撑是对OpenGLES 2.0的封装。

一个简略的3D程序

咱们就从一个简略的3D程序开端吧,这个程序的本来介绍在下面这个链接里。

http://msdn.microsoft.com/en-us/library/bb203926.aspx

不过移植到Windows Phone 7上时,仍是遇到了一些小问题,有的是文档的问题,有的是接口改变。如安在Visual Studio 2010里创立XNA 4.0的工程就不多说了,咱们能够参阅我写的《Windows Phone开发工具初体验》,链接如下:

http://www.cnblogs.com/aawolf/archive/2010/08/28/1811438.html

XNA 4.0的程序是派生自Microsoft.Xna.Framework.Game的类,开发者需求重载Game的四个办法:Initialize(初始化)、LoadContent(加载内容)、UnloadContent(卸载内容)、Update(更新)和Draw(制作)等办法。

首要,咱们在Game1类中参加所需求的一些私有变量:

  1. MatrixworldMatrix;
  2. MatrixviewMatrix;
  3. MatrixprojectionMatrix;
  4. VertexPositionNormalTexture[]cubeVertices;
  5. VertexDeclarationvertexDeclaration;
  6. VertexBuffervertexBuffer;
  7. BasicEffectbasicEffect;

Martrix 的中文名叫“矩阵”,还有个翻译叫“黑客帝国”……扯远了,什么是矩阵?咱们就不解说了,只需知道矩阵是全部3D线性改变的根底就能够了。咱们不知道矩阵是什么,却身处其间。在Game1类中用了三个矩阵:worldMatrix用来描绘国际坐标系;viewMatrix用来描绘开麦拉坐标系;projectionMatrix用来描绘投影坐标系。这些都是3D图形学的概念,不解说了。

别的两个重要的变量是vertexBuffer和basicEffect。vertexBuffer包含了一系列的向量,这些向量构成了咱们要显现的正方体的各个极点;basicEffect用来描绘一个根底的烘托作用,其间描绘了坐标系、色彩和灯火等根本的要素,这些要素是3D图形显现的根底。

接下来创立一个叫InitMatrices的办法,对各个坐标系进行初始化,记住,这个InitMatrices的函数是咱们自己创立的,代码如下:

  1. privatevoidInitMatrices()
  2. {
  3. //Initializetheworld,view,andprojectionmatrices.
  4. floattilt=MathHelper.ToRadians(0);//0degreeangle
  5. //Usetheworldmatrixtotiltthecubealongxandyaxes.
  6. worldMatrix=Matrix.CreateRotationX(tilt)*Matrix.CreateRotationY(tilt);
  7. viewMatrix=Matrix.CreateLookAt(newVector3(5,5,5),Vector3.Zero,Vector3.Up);
  8. projectionMatrix=Matrix.CreatePerspectiveFieldOfView(
  9. MathHelper.ToRadians(45),//45degreeangle
  10. (float)GraphicsDevice.Viewport.Width/
  11. (float)GraphicsDevice.Viewport.Height,
  12. 1.0f,100.0f);
  13. }

算了,不解说了,咱们知道这段代码在干什么就好了。接下来,创立一个叫做InitEffect的函数中,对basicEffect进行初始化,代码如下:

  1. privatevoidInitEffect()
  2. {
  3. //InitializeBasicEffectwithtransformationandlightvalues
  4. basicEffect=newBasicEffect(graphics.GraphicsDevice);
  5. basicEffect.World=worldMatrix;
  6. basicEffect.View=viewMatrix;
  7. basicEffect.Projection=projectionMatrix;
  8. //primitivecolor
  9. basicEffect.AmbientLightColor=newVector3(0.1f,0.1f,0.1f);
  10. basicEffect.DiffuseColor=newVector3(1.0f,1.0f,1.0f);
  11. basicEffect.SpecularColor=newVector3(0.25f,0.25f,0.25f);
  12. basicEffect.SpecularPower=5.0f;
  13. basicEffect.Alpha=1.0f;
  14. basicEffect.LightingEnabled=true;
  15. if(basicEffect.LightingEnabled)
  16. {
  17. basicEffect.DirectionalLight0.Enabled=true;//enableeachlightindividually
  18. if(basicEffect.DirectionalLight0.Enabled)
  19. {
  20. //xdirection
  21. basicEffect.DirectionalLight0.DiffuseColor=newVector3(1,0,0);//rangeis0to1
  22. basicEffect.DirectionalLight0.Direction=Vector3.Normalize(newVector3(-1,0,0));
  23. //pointsfromthelighttotheoriginofthescene
  24. basicEffect.DirectionalLight0.SpecularColor=Vector3.One;
  25. }
  26. basicEffect.DirectionalLight1.Enabled=true;
  27. if(basicEffect.DirectionalLight1.Enabled)
  28. {
  29. //ydirection
  30. basicEffect.DirectionalLight1.DiffuseColor=newVector3(0,0.75f,0);
  31. basicEffect.DirectionalLight1.Direction=Vector3.Normalize(newVector3(0,-1,0));
  32. basicEffect.DirectionalLight1.SpecularColor=Vector3.One;
  33. }
  34. basicEffect.DirectionalLight2.Enabled=true;
  35. if(basicEffect.DirectionalLight2.Enabled)
  36. {
  37. //zdirection
  38. basicEffect.DirectionalLight2.DiffuseColor=newVector3(0,0,0.5f);
  39. basicEffect.DirectionalLight2.Direction=Vector3.Normalize(newVector3(0,0,-1));
  40. basicEffect.DirectionalLight2.SpecularColor=Vector3.One;
  41. }
  42. }
  43. }

然后要对vertexDeclaration、cubeVertices和vertexBuffer变量进行初始化,咱们将这部分代码放在InitVertexBuffer函数中:

  1. privatevoidInitVertexBuffer()
  2. {
  3. //CreateavertexdeclarationforthetypeVertexPositionNormalTexture
  4. vertexDeclaration=newVertexDeclaration(newVertexElement[]
  5. {
  6. newVertexElement(0,VertexElementFormat.Vector3,VertexElementUsage.Position,0),
  7. newVertexElement(12,VertexElementFormat.Vector3,VertexElementUsage.Normal,0),
  8. newVertexElement(24,VertexElementFormat.Vector2,VertexElementUsage.TextureCoordinate,0)
  9. }
  10. );
  11. //Createthepervertexdata
  12. cubeVertices=newVertexPositionNormalTexture[36];
  13. Vector3topLeftFront=newVector3(-1.0f,1.0f,1.0f);
  14. Vector3bottomLeftFront=newVector3(-1.0f,-1.0f,1.0f);
  15. Vector3topRightFront=newVector3(1.0f,1.0f,1.0f);
  16. Vector3bottomRightFront=newVector3(1.0f,-1.0f,1.0f);
  17. Vector3topLeftBack=newVector3(-1.0f,1.0f,-1.0f);
  18. Vector3topRightBack=newVector3(1.0f,1.0f,-1.0f);
  19. Vector3bottomLeftBack=newVector3(-1.0f,-1.0f,-1.0f);
  20. Vector3bottomRightBack=newVector3(1.0f,-1.0f,-1.0f);
  21. Vector2textureTopLeft=newVector2(0.0f,0.0f);
  22. Vector2textureTopRight=newVector2(1.0f,0.0f);
  23. Vector2textureBottomLeft=newVector2(0.0f,1.0f);
  24. Vector2textureBottomRight=newVector2(1.0f,1.0f);
  25. Vector3frontNormal=newVector3(0.0f,0.0f,1.0f);
  26. Vector3backNormal=newVector3(0.0f,0.0f,-1.0f);
  27. Vector3topNormal=newVector3(0.0f,1.0f,0.0f);
  28. Vector3bottomNormal=newVector3(0.0f,-1.0f,0.0f);
  29. Vector3leftNormal=newVector3(-1.0f,0.0f,0.0f);
  30. Vector3rightNormal=newVector3(1.0f,0.0f,0.0f);
  31. //Frontface.
  32. cubeVertices[0]=
  33. newVertexPositionNormalTexture(
  34. topLeftFront,frontNormal,textureTopLeft);
  35. cubeVertices[1]=
  36. newVertexPositionNormalTexture(
  37. bottomLeftFront,frontNormal,textureBottomLeft);
  38. cubeVertices[2]=
  39. newVertexPositionNormalTexture(
  40. topRightFront,frontNormal,textureTopRight);
  41. cubeVertices[3]=
  42. newVertexPositionNormalTexture(
  43. bottomLeftFront,frontNormal,textureBottomLeft);
  44. cubeVertices[4]=
  45. newVertexPositionNormalTexture(
  46. bottomRightFront,frontNormal,textureBottomRight);
  47. cubeVertices[5]=
  48. newVertexPositionNormalTexture(
  49. topRightFront,frontNormal,textureTopRight);
  50. //Backface.
  51. cubeVertices[6]=
  52. newVertexPositionNormalTexture(
  53. topLeftBack,backNormal,textureTopRight);
  54. cubeVertices[7]=
  55. newVertexPositionNormalTexture(
  56. topRightBack,backNormal,textureTopLeft);
  57. cubeVertices[8]=
  58. newVertexPositionNormalTexture(
  59. bottomLeftBack,backNormal,textureBottomRight);
  60. cubeVertices[9]=
  61. newVertexPositionNormalTexture(
  62. bottomLeftBack,backNormal,textureBottomRight);
  63. cubeVertices[10]=
  64. newVertexPositionNormalTexture(
  65. topRightBack,backNormal,textureTopLeft);
  66. cubeVertices[11]=
  67. newVertexPositionNormalTexture(
  68. bottomRightBack,backNormal,textureBottomLeft);
  69. //Topface.
  70. cubeVertices[12]=
  71. newVertexPositionNormalTexture(
  72. topLeftFront,topNormal,textureBottomLeft);
  73. cubeVertices[13]=
  74. newVertexPositionNormalTexture(
  75. topRightBack,topNormal,textureTopRight);
  76. cubeVertices[14]=
  77. newVertexPositionNormalTexture(
  78. topLeftBack,topNormal,textureTopLeft);
  79. cubeVertices[15]=
  80. newVertexPositionNormalTexture(
  81. topLeftFront,topNormal,textureBottomLeft);
  82. cubeVertices[16]=
  83. newVertexPositionNormalTexture(
  84. topRightFront,topNormal,textureBottomRight);
  85. cubeVertices[17]=
  86. newVertexPositionNormalTexture(
  87. topRightBack,topNormal,textureTopRight);
  88. //Bottomface.
  89. cubeVertices[18]=
  90. newVertexPositionNormalTexture(
  91. bottomLeftFront,bottomNormal,
转载请说明出处
知优网 » 详解Windows Phone XNA 4.0 3D游戏开发

发表评论

您需要后才能发表评论