Game1.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Input;
  7. using tainicom.Aether.Animation;
  8. using tainicom.Aether.Shaders.Components;
  9. namespace Samples.Animation
  10. {
  11. enum DrawMode : int
  12. {
  13. CPU,
  14. GPU,
  15. }
  16. public class Game1 : Microsoft.Xna.Framework.Game
  17. {
  18. GraphicsDeviceManager graphics;
  19. SpriteBatch spriteBatch;
  20. SpriteFont font;
  21. InfiniteGridComponent grid;
  22. Model _model_CPU;
  23. Model _model_GPU;
  24. Animations _animations;
  25. DrawMode drawMode = DrawMode.CPU;
  26. KeyboardState prevKeyboardState;
  27. public Game1()
  28. {
  29. graphics = new GraphicsDeviceManager(this);
  30. Content.RootDirectory = "Content";
  31. }
  32. protected override void Initialize()
  33. {
  34. base.Initialize();
  35. }
  36. protected override void LoadContent()
  37. {
  38. spriteBatch = new SpriteBatch(GraphicsDevice);
  39. font = Content.Load<SpriteFont>("font");
  40. grid = new InfiniteGridComponent(GraphicsDevice, Content);
  41. grid.Initialize();
  42. _model_CPU = Content.Load<Model>("Dude/dude");
  43. _model_GPU = Content.Load<Model>("Dude/dude_GPU");
  44. _animations = _model_CPU.GetAnimations(); // Animation Data are the same between the two models
  45. var clip = _animations.Clips["Take 001"];
  46. _animations.SetClip(clip);
  47. }
  48. protected override void UnloadContent()
  49. {
  50. }
  51. protected override void Update(GameTime gameTime)
  52. {
  53. var keyboardState = Keyboard.GetState();
  54. var gamePadState = GamePad.GetState(PlayerIndex.One);
  55. // Allows the game to exit
  56. if (keyboardState.IsKeyDown(Keys.Escape) || gamePadState.Buttons.Back == ButtonState.Pressed)
  57. this.Exit();
  58. if ((keyboardState.IsKeyDown(Keys.Space) && prevKeyboardState.IsKeyUp(Keys.Space)) || gamePadState.Buttons.A == ButtonState.Pressed)
  59. {
  60. int drawModesCount = Enum.GetValues(drawMode.GetType()).Length;
  61. drawMode = (DrawMode)(((int)drawMode + 1) % drawModesCount);
  62. }
  63. prevKeyboardState = keyboardState;
  64. _animations.Update(gameTime.ElapsedGameTime, true, Matrix.Identity);
  65. base.Update(gameTime);
  66. }
  67. private Vector3 Position = Vector3.Zero;
  68. private float Zoom = 100f;
  69. private float RotationY = 0.0f;
  70. private float RotationX = 0.0f;
  71. private Matrix gameWorldRotation = Matrix.Identity;
  72. Stopwatch sw = new Stopwatch();
  73. double msecMin = double.MaxValue;
  74. double msecMax = 0;
  75. double avg = 0;
  76. double acc = 0;
  77. int c;
  78. protected override void Draw(GameTime gameTime)
  79. {
  80. GraphicsDevice.Clear(Color.Black);
  81. float aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
  82. Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 0.01f, 500.0f);
  83. Matrix view = Matrix.CreateLookAt(
  84. new Vector3(0.0f, 35.0f, -Zoom),
  85. new Vector3(0.0f, 35.0f, 0),
  86. Vector3.Up);
  87. // Draw Grid
  88. grid.Projection = projection;
  89. grid.View = view;
  90. //grid.EditMatrix = Matrix.Identity; // XY plane
  91. grid.EditMatrix = Matrix.CreateFromAxisAngle(Vector3.UnitX, MathHelper.ToRadians(-90)); // XZ plane
  92. grid.Draw(gameTime);
  93. GraphicsDevice.BlendState = BlendState.Opaque;
  94. GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
  95. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  96. GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
  97. Model m = _model_CPU;
  98. if (drawMode == DrawMode.CPU)
  99. m = _model_CPU;
  100. else if (drawMode == DrawMode.GPU)
  101. m = _model_GPU;
  102. Matrix[] transforms = new Matrix[m.Bones.Count];
  103. m.CopyAbsoluteBoneTransformsTo(transforms);
  104. sw.Reset();
  105. sw.Start();
  106. foreach (ModelMesh mesh in m.Meshes)
  107. {
  108. foreach (var part in mesh.MeshParts)
  109. {
  110. if (drawMode == DrawMode.CPU)
  111. ((BasicEffect)part.Effect).SpecularColor = Vector3.Zero;
  112. else if (drawMode == DrawMode.GPU)
  113. ((SkinnedEffect)part.Effect).SpecularColor = Vector3.Zero;
  114. ConfigureEffectMatrices((IEffectMatrices)part.Effect, Matrix.Identity, view, projection);
  115. ConfigureEffectLighting((IEffectLights)part.Effect);
  116. if (drawMode == DrawMode.CPU)
  117. part.UpdateVertices(_animations.AnimationTransforms); // animate vertices on CPU
  118. else if (drawMode == DrawMode.GPU)
  119. ((SkinnedEffect)part.Effect).SetBoneTransforms(_animations.AnimationTransforms);// animate vertices on GPU
  120. }
  121. mesh.Draw();
  122. }
  123. sw.Stop();
  124. double msec = sw.Elapsed.TotalMilliseconds;
  125. msecMin = Math.Min(msecMin, msec);
  126. if(avg != 0 )
  127. msecMax = Math.Max(msecMax, msec);
  128. acc += msec; c++;
  129. if(c>60*2)
  130. {
  131. avg = acc/c;
  132. acc = c = 0;
  133. }
  134. spriteBatch.Begin();
  135. spriteBatch.DrawString(font, "Draw Mode: " + drawMode, new Vector2(32, 32), Color.White);
  136. spriteBatch.DrawString(font, msec.ToString("#0.000",CultureInfo.InvariantCulture) + "ms", new Vector2(32, GraphicsDevice.Viewport.Height - 130), Color.White);
  137. spriteBatch.DrawString(font, avg.ToString("#0.000",CultureInfo.InvariantCulture) + "ms (avg)", new Vector2(32, GraphicsDevice.Viewport.Height - 100), Color.White);
  138. spriteBatch.DrawString(font, msecMin.ToString("#0.000",CultureInfo.InvariantCulture) + "ms (min)", new Vector2(32, GraphicsDevice.Viewport.Height - 70), Color.White);
  139. spriteBatch.DrawString(font, msecMax.ToString("#0.000",CultureInfo.InvariantCulture) + "ms (max)", new Vector2(32, GraphicsDevice.Viewport.Height - 40), Color.White);
  140. spriteBatch.End();
  141. base.Draw(gameTime);
  142. }
  143. private void ConfigureEffectMatrices(IEffectMatrices effect, Matrix world, Matrix view, Matrix projection)
  144. {
  145. effect.World = world;
  146. effect.View = view;
  147. effect.Projection = projection;
  148. }
  149. private void ConfigureEffectLighting(IEffectLights effect)
  150. {
  151. effect.EnableDefaultLighting();
  152. effect.DirectionalLight0.Direction = Vector3.Backward;
  153. effect.DirectionalLight0.Enabled = true;
  154. effect.DirectionalLight1.Enabled = false;
  155. effect.DirectionalLight2.Enabled = false;
  156. }
  157. }
  158. }