如果做过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开发。
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类中参加所需求的一些私有变量:
- MatrixworldMatrix;
- MatrixviewMatrix;
- MatrixprojectionMatrix;
- VertexPositionNormalTexture[]cubeVertices;
- VertexDeclarationvertexDeclaration;
- VertexBuffervertexBuffer;
- BasicEffectbasicEffect;
Martrix 的中文名叫“矩阵”,还有个翻译叫“黑客帝国”……扯远了,什么是矩阵?咱们就不解说了,只需知道矩阵是全部3D线性改变的根底就能够了。咱们不知道矩阵是什么,却身处其间。在Game1类中用了三个矩阵:worldMatrix用来描绘国际坐标系;viewMatrix用来描绘开麦拉坐标系;projectionMatrix用来描绘投影坐标系。这些都是3D图形学的概念,不解说了。
别的两个重要的变量是vertexBuffer和basicEffect。vertexBuffer包含了一系列的向量,这些向量构成了咱们要显现的正方体的各个极点;basicEffect用来描绘一个根底的烘托作用,其间描绘了坐标系、色彩和灯火等根本的要素,这些要素是3D图形显现的根底。
接下来创立一个叫InitMatrices的办法,对各个坐标系进行初始化,记住,这个InitMatrices的函数是咱们自己创立的,代码如下:
- privatevoidInitMatrices()
- {
- //Initializetheworld,view,andprojectionmatrices.
- floattilt=MathHelper.ToRadians(0);//0degreeangle
- //Usetheworldmatrixtotiltthecubealongxandyaxes.
- worldMatrix=Matrix.CreateRotationX(tilt)*Matrix.CreateRotationY(tilt);
- viewMatrix=Matrix.CreateLookAt(newVector3(5,5,5),Vector3.Zero,Vector3.Up);
- projectionMatrix=Matrix.CreatePerspectiveFieldOfView(
- MathHelper.ToRadians(45),//45degreeangle
- (float)GraphicsDevice.Viewport.Width/
- (float)GraphicsDevice.Viewport.Height,
- 1.0f,100.0f);
- }
算了,不解说了,咱们知道这段代码在干什么就好了。接下来,创立一个叫做InitEffect的函数中,对basicEffect进行初始化,代码如下:
- privatevoidInitEffect()
- {
- //InitializeBasicEffectwithtransformationandlightvalues
- basicEffect=newBasicEffect(graphics.GraphicsDevice);
- basicEffect.World=worldMatrix;
- basicEffect.View=viewMatrix;
- basicEffect.Projection=projectionMatrix;
- //primitivecolor
- basicEffect.AmbientLightColor=newVector3(0.1f,0.1f,0.1f);
- basicEffect.DiffuseColor=newVector3(1.0f,1.0f,1.0f);
- basicEffect.SpecularColor=newVector3(0.25f,0.25f,0.25f);
- basicEffect.SpecularPower=5.0f;
- basicEffect.Alpha=1.0f;
- basicEffect.LightingEnabled=true;
- if(basicEffect.LightingEnabled)
- {
- basicEffect.DirectionalLight0.Enabled=true;//enableeachlightindividually
- if(basicEffect.DirectionalLight0.Enabled)
- {
- //xdirection
- basicEffect.DirectionalLight0.DiffuseColor=newVector3(1,0,0);//rangeis0to1
- basicEffect.DirectionalLight0.Direction=Vector3.Normalize(newVector3(-1,0,0));
- //pointsfromthelighttotheoriginofthescene
- basicEffect.DirectionalLight0.SpecularColor=Vector3.One;
- }
- basicEffect.DirectionalLight1.Enabled=true;
- if(basicEffect.DirectionalLight1.Enabled)
- {
- //ydirection
- basicEffect.DirectionalLight1.DiffuseColor=newVector3(0,0.75f,0);
- basicEffect.DirectionalLight1.Direction=Vector3.Normalize(newVector3(0,-1,0));
- basicEffect.DirectionalLight1.SpecularColor=Vector3.One;
- }
- basicEffect.DirectionalLight2.Enabled=true;
- if(basicEffect.DirectionalLight2.Enabled)
- {
- //zdirection
- basicEffect.DirectionalLight2.DiffuseColor=newVector3(0,0,0.5f);
- basicEffect.DirectionalLight2.Direction=Vector3.Normalize(newVector3(0,0,-1));
- basicEffect.DirectionalLight2.SpecularColor=Vector3.One;
- }
- }
- }
然后要对vertexDeclaration、cubeVertices和vertexBuffer变量进行初始化,咱们将这部分代码放在InitVertexBuffer函数中:
- privatevoidInitVertexBuffer()
- {
- //CreateavertexdeclarationforthetypeVertexPositionNormalTexture
- vertexDeclaration=newVertexDeclaration(newVertexElement[]
- {
- newVertexElement(0,VertexElementFormat.Vector3,VertexElementUsage.Position,0),
- newVertexElement(12,VertexElementFormat.Vector3,VertexElementUsage.Normal,0),
- newVertexElement(24,VertexElementFormat.Vector2,VertexElementUsage.TextureCoordinate,0)
- }
- );
- //Createthepervertexdata
- cubeVertices=newVertexPositionNormalTexture[36];
- Vector3topLeftFront=newVector3(-1.0f,1.0f,1.0f);
- Vector3bottomLeftFront=newVector3(-1.0f,-1.0f,1.0f);
- Vector3topRightFront=newVector3(1.0f,1.0f,1.0f);
- Vector3bottomRightFront=newVector3(1.0f,-1.0f,1.0f);
- Vector3topLeftBack=newVector3(-1.0f,1.0f,-1.0f);
- Vector3topRightBack=newVector3(1.0f,1.0f,-1.0f);
- Vector3bottomLeftBack=newVector3(-1.0f,-1.0f,-1.0f);
- Vector3bottomRightBack=newVector3(1.0f,-1.0f,-1.0f);
- Vector2textureTopLeft=newVector2(0.0f,0.0f);
- Vector2textureTopRight=newVector2(1.0f,0.0f);
- Vector2textureBottomLeft=newVector2(0.0f,1.0f);
- Vector2textureBottomRight=newVector2(1.0f,1.0f);
- Vector3frontNormal=newVector3(0.0f,0.0f,1.0f);
- Vector3backNormal=newVector3(0.0f,0.0f,-1.0f);
- Vector3topNormal=newVector3(0.0f,1.0f,0.0f);
- Vector3bottomNormal=newVector3(0.0f,-1.0f,0.0f);
- Vector3leftNormal=newVector3(-1.0f,0.0f,0.0f);
- Vector3rightNormal=newVector3(1.0f,0.0f,0.0f);
- //Frontface.
- cubeVertices[0]=
- newVertexPositionNormalTexture(
- topLeftFront,frontNormal,textureTopLeft);
- cubeVertices[1]=
- newVertexPositionNormalTexture(
- bottomLeftFront,frontNormal,textureBottomLeft);
- cubeVertices[2]=
- newVertexPositionNormalTexture(
- topRightFront,frontNormal,textureTopRight);
- cubeVertices[3]=
- newVertexPositionNormalTexture(
- bottomLeftFront,frontNormal,textureBottomLeft);
- cubeVertices[4]=
- newVertexPositionNormalTexture(
- bottomRightFront,frontNormal,textureBottomRight);
- cubeVertices[5]=
- newVertexPositionNormalTexture(
- topRightFront,frontNormal,textureTopRight);
- //Backface.
- cubeVertices[6]=
- newVertexPositionNormalTexture(
- topLeftBack,backNormal,textureTopRight);
- cubeVertices[7]=
- newVertexPositionNormalTexture(
- topRightBack,backNormal,textureTopLeft);
- cubeVertices[8]=
- newVertexPositionNormalTexture(
- bottomLeftBack,backNormal,textureBottomRight);
- cubeVertices[9]=
- newVertexPositionNormalTexture(
- bottomLeftBack,backNormal,textureBottomRight);
- cubeVertices[10]=
- newVertexPositionNormalTexture(
- topRightBack,backNormal,textureTopLeft);
- cubeVertices[11]=
- newVertexPositionNormalTexture(
- bottomRightBack,backNormal,textureBottomLeft);
- //Topface.
- cubeVertices[12]=
- newVertexPositionNormalTexture(
- topLeftFront,topNormal,textureBottomLeft);
- cubeVertices[13]=
- newVertexPositionNormalTexture(
- topRightBack,topNormal,textureTopRight);
- cubeVertices[14]=
- newVertexPositionNormalTexture(
- topLeftBack,topNormal,textureTopLeft);
- cubeVertices[15]=
- newVertexPositionNormalTexture(
- topLeftFront,topNormal,textureBottomLeft);
- cubeVertices[16]=
- newVertexPositionNormalTexture(
- topRightFront,topNormal,textureBottomRight);
- cubeVertices[17]=
- newVertexPositionNormalTexture(
- topRightBack,topNormal,textureTopRight);
- //Bottomface.
- cubeVertices[18]=
- newVertexPositionNormalTexture(
- bottomLeftFront,bottomNormal,
知优网 » 详解Windows Phone XNA 4.0 3D游戏开发