½ñÌìÓеã¿ÕÓàʱ¼ä£¬ÔÚ¿´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¿ª·¢¡£
Èç¹û×ö¹ýZuneÉÏXNA 3.1¿ª·¢µÄÅóÓÑ¿ÉÄÜ»á¼ÇµÃ£¬ÔÚXNA 3.1ÖÐÊDz»Ö§³Ö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ÀàÖмÓÈëËùÐèÒªµÄһЩ˽ÓбäÁ¿£º
Matrix worldMatrix;
Matrix viewMatrix;
Matrix projectionMatrix;
VertexPositionNormalTexture[] cubeVertices;
VertexDeclaration vertexDeclaration;
VertexBuffer vertexBuffer;
BasicEffect basicEffect;
|
Martrix µÄÖÐÎÄÃû½Ð¡°¾ØÕó¡±£¬»¹Óиö·Òë½Ð¡°ºÚ¿ÍµÛ¹ú¡±¡¡³¶Ô¶ÁË£¬Ê²Ã´ÊǾØÕó£¿ÎÒÃǾͲ»½âÊÍÁË£¬Ö»ÒªÖªµÀ¾ØÕóÊÇÒ»ÇÐ3DÏßÐԱ仯µÄ»ù´¡¾Í¿ÉÒÔÁË¡£ÎÒÃDz»ÖªµÀ¾ØÕóÊÇʲô£¬È´Éí´¦ÆäÖС£ÔÚGame1ÀàÖÐÓÃÁËÈý¸ö¾ØÕó£ºworldMatrixÓÃÀ´ÃèÊöÊÀ½ç×ø±êϵ£»viewMatrixÓÃÀ´ÃèÊöÉãÓ°»ú×ø±êϵ£»projectionMatrixÓÃÀ´ÃèÊöÍ¶Ó°×ø±êϵ¡£ÕâЩ¶¼ÊÇ3DͼÐÎѧµÄ¸ÅÄ²»½âÊÍÁË¡£
ÁíÍâÁ½¸öÖØÒªµÄ±äÁ¿ÊÇvertexBufferºÍbasicEffect¡£vertexBuffer°üº¬ÁËһϵÁеÄÏòÁ¿£¬ÕâЩÏòÁ¿¹¹³ÉÁËÎÒÃÇÒªÏÔʾµÄÕý·½ÌåµÄ¸÷¸ö¶¥µã£»basicEffectÓÃÀ´ÃèÊöÒ»¸ö»ù´¡µÄäÖȾЧ¹û£¬ÆäÖÐÃèÊöÁË×ø±êϵ¡¢ÑÕÉ«ºÍµÆ¹âµÈ»ù±¾µÄÒªËØ£¬ÕâÐ©ÒªËØÊÇ3DͼÐÎÏÔʾµÄ»ù´¡¡£
½ÓÏÂÀ´´´½¨Ò»¸ö½ÐInitMatricesµÄ·½·¨£¬¶Ô¸÷¸ö×ø±êϵ½øÐгõʼ»¯£¬¼ÇµÃ£¬Õâ¸öInitMatricesµÄº¯ÊýÊÇÎÒÃÇ×Ô¼º´´½¨µÄ£¬´úÂëÈçÏ£º
private void InitMatrices()
{
// Initialize the world, view, and projection matrices.
float tilt = MathHelper.ToRadians(0); // 0 degree angle
// Use the world matrix to tilt the cube along x and y axes.
worldMatrix = Matrix.CreateRotationX(tilt) * Matrix.CreateRotationY(tilt);
viewMatrix = Matrix.CreateLookAt(new Vector3(5, 5, 5), Vector3.Zero, Vector3.Up);
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45), // 45 degree angle
(float)GraphicsDevice.Viewport.Width /
(float)GraphicsDevice.Viewport.Height,
1.0f, 100.0f);
}
|
ËãÁË£¬²»½âÊÍÁË£¬´ó¼ÒÖªµÀÕâ¶Î´úÂëÔÚ¸Éʲô¾ÍºÃÁË¡£½ÓÏÂÀ´£¬´´½¨Ò»¸ö½Ð×öInitEffectµÄº¯ÊýÖУ¬¶ÔbasicEffect½øÐгõʼ»¯£¬´úÂëÈçÏ£º
private void InitEffect()
{
// Initialize BasicEffect with transformation and light values
basicEffect = new BasicEffect(graphics.GraphicsDevice);
basicEffect.World = worldMatrix;
basicEffect.View = viewMatrix;
basicEffect.Projection = projectionMatrix;
// primitive color
basicEffect.AmbientLightColor = new Vector3(0.1f, 0.1f, 0.1f);
basicEffect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);
basicEffect.SpecularColor = new Vector3(0.25f, 0.25f, 0.25f);
basicEffect.SpecularPower = 5.0f;
basicEffect.Alpha = 1.0f;
basicEffect.LightingEnabled = true;
if (basicEffect.LightingEnabled)
{
basicEffect.DirectionalLight0.Enabled = true; // enable each light individually
if (basicEffect.DirectionalLight0.Enabled)
{
// x direction
basicEffect.DirectionalLight0.DiffuseColor = new Vector3(1, 0, 0); // range is 0 to 1
basicEffect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(-1, 0, 0));
// points from the light to the origin of the scene
basicEffect.DirectionalLight0.SpecularColor = Vector3.One;
}
basicEffect.DirectionalLight1.Enabled = true;
if (basicEffect.DirectionalLight1.Enabled)
{
// y direction
basicEffect.DirectionalLight1.DiffuseColor = new Vector3(0, 0.75f, 0);
basicEffect.DirectionalLight1.Direction = Vector3.Normalize(new Vector3(0, -1, 0));
basicEffect.DirectionalLight1.SpecularColor = Vector3.One;
}
basicEffect.DirectionalLight2.Enabled = true;
if (basicEffect.DirectionalLight2.Enabled)
{
// z direction
basicEffect.DirectionalLight2.DiffuseColor = new Vector3(0, 0, 0.5f);
basicEffect.DirectionalLight2.Direction = Vector3.Normalize(new Vector3(0, 0, -1));
basicEffect.DirectionalLight2.SpecularColor = Vector3.One;
}
}
}
|
È»ºóÒª¶ÔvertexDeclaration¡¢cubeVerticesºÍvertexBuffer±äÁ¿½øÐгõʼ»¯£¬ÎÒÃǽ«Õⲿ·Ö´úÂë·ÅÔÚInitVertexBufferº¯ÊýÖУº
private void InitVertexBuffer()
{
// Create a vertex declaration for the type VertexPositionNormalTexture
vertexDeclaration = new VertexDeclaration(new VertexElement[]
{
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
new VertexElement(12, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
new VertexElement(24, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
}
);
// Create the per vertex data
cubeVertices = new VertexPositionNormalTexture[36];
Vector3 topLeftFront = new Vector3(-1.0f, 1.0f, 1.0f);
Vector3 bottomLeftFront = new Vector3(-1.0f, -1.0f, 1.0f);
Vector3 topRightFront = new Vector3(1.0f, 1.0f, 1.0f);
Vector3 bottomRightFront = new Vector3(1.0f, -1.0f, 1.0f);
Vector3 topLeftBack = new Vector3(-1.0f, 1.0f, -1.0f);
Vector3 topRightBack = new Vector3(1.0f, 1.0f, -1.0f);
Vector3 bottomLeftBack = new Vector3(-1.0f, -1.0f, -1.0f);
Vector3 bottomRightBack = new Vector3(1.0f, -1.0f, -1.0f);
Vector2 textureTopLeft = new Vector2(0.0f, 0.0f);
Vector2 textureTopRight = new Vector2(1.0f, 0.0f);
Vector2 textureBottomLeft = new Vector2(0.0f, 1.0f);
Vector2 textureBottomRight = new Vector2(1.0f, 1.0f);
Vector3 frontNormal = new Vector3(0.0f, 0.0f, 1.0f);
Vector3 backNormal = new Vector3(0.0f, 0.0f, -1.0f);
Vector3 topNormal = new Vector3(0.0f, 1.0f, 0.0f);
Vector3 bottomNormal = new Vector3(0.0f, -1.0f, 0.0f);
Vector3 leftNormal = new Vector3(-1.0f, 0.0f, 0.0f);
Vector3 rightNormal = new Vector3(1.0f, 0.0f, 0.0f);
// Front face.
cubeVertices[0] =
new VertexPositionNormalTexture(
topLeftFront, frontNormal, textureTopLeft);
cubeVertices[1] =
new VertexPositionNormalTexture(
bottomLeftFront, frontNormal, textureBottomLeft);
cubeVertices[2] =
new VertexPositionNormalTexture(
topRightFront, frontNormal, textureTopRight);
cubeVertices[3] =
new VertexPositionNormalTexture(
bottomLeftFront, frontNormal, textureBottomLeft);
cubeVertices[4] =
new VertexPositionNormalTexture(
bottomRightFront, frontNormal, textureBottomRight);
cubeVertices[5] =
new VertexPositionNormalTexture(
topRightFront, frontNormal, textureTopRight);
// Back face.
cubeVertices[6] =
new VertexPositionNormalTexture(
topLeftBack, backNormal, textureTopRight);
cubeVertices[7] =
new VertexPositionNormalTexture(
topRightBack, backNormal, textureTopLeft);
cubeVertices[8] =
new VertexPositionNormalTexture(
bottomLeftBack, backNormal, textureBottomRight);
cubeVertices[9] =
new VertexPositionNormalTexture(
bottomLeftBack, backNormal, textureBottomRight);
cubeVertices[10] =
new VertexPositionNormalTexture(
topRightBack, backNormal, textureTopLeft);
cubeVertices[11] =
new VertexPositionNormalTexture(
bottomRightBack, backNormal, textureBottomLeft);
// Top face.
cubeVertices[12] =
new VertexPositionNormalTexture(
topLeftFront, topNormal, textureBottomLeft);
cubeVertices[13] =
new VertexPositionNormalTexture(
topRightBack, topNormal, textureTopRight);
cubeVertices[14] =
new VertexPositionNormalTexture(
topLeftBack, topNormal, textureTopLeft);
cubeVertices[15] =
new VertexPositionNormalTexture(
topLeftFront, topNormal, textureBottomLeft);
cubeVertices[16] =
new VertexPositionNormalTexture(
topRightFront, topNormal, textureBottomRight);
cubeVertices[17] =
new VertexPositionNormalTexture(
topRightBack, topNormal, textureTopRight);
// Bottom face.
cubeVertices[18] =
new VertexPositionNormalTexture(
bottomLeftFront, bottomNormal, textureTopLeft);
cubeVertices[19] =
new VertexPositionNormalTexture(
bottomLeftBack, bottomNormal, textureBottomLeft);
cubeVertices[20] =
new VertexPositionNormalTexture(
bottomRightBack, bottomNormal, textureBottomRight);
cubeVertices[21] =
new VertexPositionNormalTexture(
bottomLeftFront, bottomNormal, textureTopLeft);
cubeVertices[22] =
new VertexPositionNormalTexture(
bottomRightBack, bottomNormal, textureBottomRight);
cubeVertices[23] =
new VertexPositionNormalTexture(
bottomRightFront, bottomNormal, textureTopRight);
// Left face.
cubeVertices[24] =
new VertexPositionNormalTexture(
topLeftFront, leftNormal, textureTopRight);
cubeVertices[25] =
new VertexPositionNormalTexture(
bottomLeftBack, leftNormal, textureBottomLeft);
cubeVertices[26] =
new VertexPositionNormalTexture(
bottomLeftFront, leftNormal, textureBottomRight);
cubeVertices[27] =
new VertexPositionNormalTexture(
topLeftBack, leftNormal, textureTopLeft);
cubeVertices[28] =
new VertexPositionNormalTexture(
bottomLeftBack, leftNormal, textureBottomLeft);
cubeVertices[29] =
new VertexPositionNormalTexture(
topLeftFront, leftNormal, textureTopRight);
// Right face.
cubeVertices[30] =
new VertexPositionNormalTexture(
topRightFront, rightNormal, textureTopLeft);
cubeVertices[31] =
new VertexPositionNormalTexture(
bottomRightFront, rightNormal, textureBottomLeft);
cubeVertices[32] =
new VertexPositionNormalTexture(
bottomRightBack, rightNormal, textureBottomRight);
cubeVertices[33] =
new VertexPositionNormalTexture(
topRightBack, rightNormal, textureTopRight);
cubeVertices[34] =
new VertexPositionNormalTexture(
topRightFront, rightNormal, textureTopLeft);
cubeVertices[35] =
new VertexPositionNormalTexture(
bottomRightBack, rightNormal, textureBottomRight);
vertexBuffer = new VertexBuffer(
graphics.GraphicsDevice,
typeof(VertexPositionNormalTexture),
cubeVertices.Length,
BufferUsage.None
);
vertexBuffer.SetData(cubeVertices);
}
|
ÇëÔÁÂÎÒ°Ñ36¸ö¶¥µãµÄ´úÂë¶¼Ìù³öÀ´ÁË£¬Èç¹û²»Ìù³öÀ´£¬¿Ï¶¨»áÓÐÈ˲»²¹È«£¬È»ºó¾Í¿´²»µ½ÍêÕûµÄÕý·½ÌåÁË¡£
ÕâÀï¾ÍҪ˵µ½µÚÒ»¸ö´íÎóµãÁË£ºÎÄÕÂÖÐûÓÐÁгöËùÓÐ36¸ö¶¥µãµÄ¶¨Ò壬²»¹ýʾÀý´úÂëUseBasicEffectÖÐÁгöÁË£»ÁíÒ»¸öÎÊÌâÊÇVertexBufferµÄ¹¹Ô캯Êý·¢ÉúÁ˱仯£¬ÔÎĺÍʾÀý´úÂëÖеÄVertexBuffer¹¹Ô캯ÊýÊÇÕâÑùµÄ£º
vertexBuffer = new VertexBuffer(
graphics.GraphicsDevice,
VertexPositionNormalTexture.SizeInBytes * cubeVertices.Length,
BufferUsage.None
);
|
¶øÕýÈ·µÄд·¨Ó¦¸ÃÊÇ£º
vertexBuffer = new VertexBuffer(
graphics.GraphicsDevice,
typeof(VertexPositionNormalTexture),
cubeVertices.Length,
BufferUsage.None
);
|
VertexBufferÔö¼ÓÁËÒ»¸öTypeÀàÐ͵IJÎÊý£¨µÚ¶þ¸ö£©£¬ÎÒÃDZØÐë´«ÈëÒ»¸öIVertexType½Ó¿ÚµÄÅÉÉúÀàÐÍ£¬¹¹Ô캯Êý»áÓÃÀàÐͺͶ¥µãÁбíµÄ³¤¶È¼ÆËãVertexBufferµÄsize£¬ÕâÏÔÈ»±ÈÉϱߵÄʵÏÖºÃÁËÐí¶à¡£
·Ö±ðʵÏÖÁËÕâÈý¸ö³õʼ»¯º¯Êýºó£¬ÎÒÃÇÒªÔÚÕæÕýµÄ³õʼ»¯º¯ÊýInitializeÀïµ÷ÓÃÕâÈý¸öº¯Êý£¬×¢ÒâInitializeº¯Êý²»ÊÇ×Ô¼ºÌí¼ÓµÄ£¬ÔÚGame1ÀàÖб¾À´¾ÍÓУº
protected override void Initialize()
{
InitMatrices();
InitEffect();
InitVertexBuffer();
base.Initialize();
}
|
ºÃÁË£¬ÎÒÃÇÔÚDrawº¯ÊýÀïÔö¼Ó»æÖÆ·½·¨£º
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
RasterizerState rasterizerState1 = new RasterizerState();
rasterizerState1.CullMode = CullMode.None;
graphics.GraphicsDevice.RasterizerState = rasterizerState1;
GraphicsDevice.SetVertexBuffer(vertexBuffer);
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
graphics.GraphicsDevice.DrawPrimitives(
PrimitiveType.TriangleList,
0,
36
);
}
base.Draw(gameTime);
}
|
ÕâÀï°üº¬Á˵ڶþ¸ö´íÎóµã£¬ÔÎÄûÓÐÏÂÃæÕâ¾ä£¨ÉÏÎĸßÁÁ±ê³ö£©£º
GraphicsDevice.SetVertexBuffer(vertexBuffer); |
Èç¹ûûÓÐSetVertexBufferµÄµ÷Ó㬳ÌÐòÔÚÔËÐÐʱ»áÓöµ½ÏÂÃæµÄÒì³££º
An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.Graphics.dll
Additional information: A valid vertex buffer (and a valid index buffer if you are using indexed primitives)
must be set on the device before any draw operations may be performed.
|

ÔÎĵĵ÷Ó÷½Ê½ºÍUseBasicEffectµÄʵÏÖ·½Ê½ÍêÈ«²»Í¬£¬ËùÒÔ´ó¼ÒҪעÒâһϡ£±Ï¾¹ÊÇBeta°æ£¬ºÜ¶àÎĵµ»¹Ã»ÓÐ×îºóÍê³É¡£
ºÃÁË£¬µ½ÕâÀÆäʵÎÒÃDZàÒëÔËÐиóÌÐòµÄ»°£¬¾Í¿ÉÒÔ¿´µ½»æÖƳöµÄÁ¢·½ÌåÀ´ÁË¡£µ«ÊÇ£¬ÎÒ»¹ÏëÔټӵ㡪¡ªÈÃÁ¢·½ÌåÐýתÆðÀ´¡£
ÔÚUpdateº¯ÊýÖÐÔö¼ÓÏÂÃæÁ½¾ä£¨¸ßÁÁÏÔʾ£©£º
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
Matrix matrix = Matrix.CreateRotationX(0.1f);
basicEffect.World = basicEffect.World * matrix;
base.Update(gameTime);
}
|
ÎÒÃÇ´´½¨ÁËÒ»¸öÑØXÖáÐýת0.1¶ÈµÄ¾ØÕó£¬ÓëbasicEffectÖеÄÊÀ½ç×ø±êϵÏà³Ë£¬¾Í¿ÉÒÔʹÎÒÃÇ»æÖƳöÀ´µÄÁ¢·½Ìåÿ´ÎUpdateʱ£¬¶¼ÑØ×ÅXÖáÐýת0.1f¶È¡£ÒòΪ½Ç¶ÈÊÇfloatÐÍ£¬Ç§Íò±ðÍüÁË0.1fÖ®ºóµÄÄǸöf¡£

ºÃÁË£¬³ÌÐò×îºóµÄÑù×Ó¾ÍÊÇÕâÑùµÄ¡£ÔÚµÚһƪÎÄÕÂÀÎÒÁôÁ˺ܶàÎÊÌ⣬±ÈÈç3DµÄ»ù±¾¸ÅÄî¡¢×ø±êϵ¡¢µÆ¹â¡¢²ÄÖÊ¡¢Ðýת£¬Ï£ÍûÔÚºó±ßÄܹ»±È½Ï´ÓÈݵؽâÊÍÕâЩ֪ʶ¡£ÎÒÏÖÔÚΨһµÄÏ£ÍûÊÇ£¬²»ÒªµÈµ½Áù¸öÔºó²ÅÓÐʱ¼äÔÙдµÚ¶þƪ¡¡
|