简单XNA_Windows Phone 7 3D动画
2011-11-23 19:31
966 查看
using System; using System.Collections.Gener ic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input.Touch; using Microsoft.Xna.Framework.Media; namespace WindowsPhoneGame1 { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Matrix worldMatrix; Matrix viewMatrix; Matrix projectionMatrix; VertexPositionNormalTexture[] cubeVertices; VertexDeclaration vertexDeclaration; VertexBuffer vertexBuffer; BasicEffect basicEffect; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; // Frame rate is 30 fps by default for Windows Phone. TargetElapsedTime = TimeSpan.FromTicks(333333); } /// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { InitMatrices(); InitEffect(); InitVertexBuffer(); base.Initialize(); } 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<VertexPositionNormalTexture>(cubeVertices); } 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; } } } 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); } /// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // TODO: use this.Content to load your game content here } /// <summary> /// UnloadContent will be called once per game and is the place to unload /// all content. /// </summary> protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> 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); } /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> 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); } } }
相关文章推荐
- Windows Phone XNA 4.0 3D游戏开发
- WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体
- XNA 通过图片播放动画,Windows Phone 游戏开发
- 不用ViewPager一样做翻页,简单讲讲自定义3D动画
- Windows Phone XNA 4.0 3D游戏开发
- Windows Phone XNA 4.0 3D游戏开发
- (四期)简单添加TableViewCell的3D动画效果
- WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体
- 一种简单有效的3D模型的动画多线程方案
- Silverlight.XNA(C#)跨平台3D游戏研发手记:(九)3D 骨骼动画
- MATLAB 3D 动画简单实例
- 简单的阐述CoreAnimation中的3D动画
- XNA入门教程----(三) 简单的动画
- 一种简单有效的3D模型的动画多线程方案
- 使用XNA为Windows phone 7开发简单拼图游戏
- 一种简单有效的3D模型的动画多线程方案
- 一种简单有效的3D模型的动画多线程方案
- 一种简单有效的3D模型的动画多线程方案
- 一种简单有效的3D模型的动画多线程方案
- XNA入门教程----(三) 简单的动画