Advertisement
ZeronSix

Toolkit Example

Aug 22nd, 2013
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.67 KB | None | 0 0
  1. // Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20.  
  21. using System;
  22. using System.Collections.Generic;
  23.  
  24. using SharpDX;
  25. using SharpDX.Direct3D;
  26. using SharpDX.Direct3D11;
  27. using SharpDX.Toolkit;
  28. using SharpDX.Toolkit.Input;
  29.  
  30. namespace ModelRendering
  31. {
  32.     // Use this namespace here in case we need to use Direct3D11 namespace as well, as this
  33.     // namespace will override the Direct3D11.
  34.     using SharpDX.Toolkit.Graphics;
  35.  
  36.     /// <summary>
  37.     /// Simple SpriteBatchAndFont application using SharpDX.Toolkit.
  38.     /// The purpose of this application is to use SpriteBatch and SpriteFont.
  39.     /// </summary>
  40.     public class ModelRenderingGame : Game
  41.     {
  42.         private GraphicsDeviceManager graphicsDeviceManager;
  43.         private SpriteBatch spriteBatch;
  44.         private SpriteFont arial16BMFont;
  45.  
  46.         private PointerManager pointer;
  47.  
  48.         private Model model;
  49.  
  50.         private List<Model> models;
  51.  
  52.         private BoundingSphere modelBounds;
  53.         private Matrix world;
  54.         private Matrix view;
  55.         private Matrix projection;
  56.  
  57.         /// <summary>
  58.         /// Initializes a new instance of the <see cref="ModelRenderingGame" /> class.
  59.         /// </summary>
  60.         public ModelRenderingGame()
  61.         {
  62.             // Creates a graphics manager. This is mandatory.
  63.             graphicsDeviceManager = new GraphicsDeviceManager(this);
  64.             graphicsDeviceManager.PreferredGraphicsProfile = new FeatureLevel[] { FeatureLevel.Level_9_1, };
  65.  
  66.             pointer = new PointerManager(this);
  67.  
  68.             // Setup the relative directory to the executable directory
  69.             // for loading contents with the ContentManager
  70.             Content.RootDirectory = "Content";
  71.         }
  72.  
  73.         protected override void LoadContent()
  74.         {
  75.             // Load the fonts
  76.             arial16BMFont = Content.Load<SpriteFont>("Arial16");
  77.  
  78.             // Load the model (by default the model is loaded with a BasicEffect. Use ModelContentReaderOptions to change the behavior at loading time.
  79.             models = new List<Model>();
  80.             foreach (var modelName in new[] { "Dude", "Duck", "Car", "Happy", "Knot", "Skull", "Sphere", "Teapot" })
  81.             {
  82.                 model = Content.Load<Model>(modelName);
  83.                
  84.                 // Enable default lighting  on model.
  85.                 BasicEffect.EnableDefaultLighting(model, true);
  86.  
  87.                 models.Add(model);
  88.             }
  89.             model = models[0];
  90.  
  91.             // Instantiate a SpriteBatch
  92.             spriteBatch = ToDisposeContent(new SpriteBatch(GraphicsDevice));
  93.  
  94.             base.LoadContent();
  95.         }
  96.  
  97.         protected override void Initialize()
  98.         {
  99.             Window.Title = "Model Rendering Demo";
  100.             base.Initialize();
  101.         }
  102.  
  103.         protected override void Update(GameTime gameTime)
  104.         {
  105.             base.Update(gameTime);
  106.  
  107.             var pointerState = pointer.GetState();
  108.             if (pointerState.Points.Count > 0 && pointerState.Points[0].EventType == PointerEventType.Released)
  109.             {
  110.                 // Go to next model when pressing key space
  111.                 model = models[(models.IndexOf(model) + 1) % models.Count];
  112.             }
  113.  
  114.             // Calculate the bounds of this model
  115.             modelBounds = model.CalculateBounds();
  116.  
  117.             // Calculates the world and the view based on the model size
  118.             const float MaxModelSize = 10.0f;
  119.             var scaling = MaxModelSize / modelBounds.Radius;
  120.             view = Matrix.LookAtLH(new Vector3(0, 0, - MaxModelSize * 2.5f), new Vector3(0, 0, 0), Vector3.UnitY);
  121.             projection = Matrix.PerspectiveFovLH(0.9f, (float)GraphicsDevice.BackBuffer.Width / GraphicsDevice.BackBuffer.Height, 0.1f, MaxModelSize * 10.0f);
  122.             world = Matrix.Translation(-modelBounds.Center.X, -modelBounds.Center.Y, -modelBounds.Center.Z) * Matrix.Scaling(scaling) * Matrix.RotationY((float)gameTime.TotalGameTime.TotalSeconds);
  123.         }
  124.  
  125.         protected override void Draw(GameTime gameTime)
  126.         {
  127.             // Clears the screen with the Color.CornflowerBlue
  128.             GraphicsDevice.Clear(Color.CornflowerBlue);
  129.  
  130.             // Draw the model
  131.             model.Draw(GraphicsDevice, world, view, projection);
  132.  
  133.             // Render the text
  134.             spriteBatch.Begin();
  135.             spriteBatch.DrawString(arial16BMFont, "Press the pointer to switch models...\r\nCurrent Model: " + model.Name, new Vector2(16, 16), Color.White);
  136.             spriteBatch.End();
  137.  
  138.             // Handle base.Draw
  139.             base.Draw(gameTime);
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement