My Blog

SharpDX: creare un cubo ed applicare zoom e rotazione

by lupok on domenica 16 febbraio 2014 18:34

In questo esempio vengono caricati i vertici di un cubo e mediante mouse vengono applicate trasformazione per ruotare e scalare l'oggetto 3D. 

Per visualizzare e gestire la visualizzazione del solido creato viene utilizzata la classe Game, le operazioni di rotazione e zoom avvengono intercettando i movimenti del mouse mediante i messaggi tradizionali di windows ed alternativamente mediante DirectInput. 
Per relalizzare questo esempio e' stata utilizzata la versione 2.5.0 di SharpDX, SharpDX e' una libreria fondata su DirectX o meglio e' un wrapper per .NET delle DirectX. 

 

//#define USE_DIRECT_INPUT using System; using SharpDX; using SharpDX.Toolkit; namespace MyGameMoveCubeTest {    using SharpDX.Toolkit.Graphics;    using SharpDX.Toolkit.Input;    public class MyGameMoveCube : Game    {       private GraphicsDeviceManager graphicsDeviceManager;       private BasicEffect basicEffect;       private Buffer<VertexPositionColor> vertices;       private VertexInputLayout inputLayout; #if USE_DIRECT_INPUT       private MouseManager mouseManager;       private MouseState mouseState; #endif       ///        /// Initializes a new instance of the  class.       ///        public MyGameMoveCube()       {          graphicsDeviceManager = new GraphicsDeviceManager(this); #if USE_DIRECT_INPUT                   mouseManager = new MouseManager(this); #endif          Content.RootDirectory = "Content";       }       protected override void LoadContent()       {          // Creates a basic effect          basicEffect = ToDisposeContent(new BasicEffect(GraphicsDevice)          {             VertexColorEnabled = true,             View = Matrix.LookAtLH(new Vector3(-5, -5, -5), new Vector3(0, 0, 0), Vector3.UnitY),             Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)GraphicsDevice.BackBuffer.Width / GraphicsDevice.BackBuffer.Height, 0.1f, 100.0f),             World = Matrix.Identity          });          // Creates vertices for the cube          vertices = ToDisposeContent(Buffer.Vertex.New(              GraphicsDevice,              new[]                     {                         new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.Orange), // Front                         new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.Orange),                         new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.Orange),                         new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.Orange),                         new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.Orange),                         new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.Orange),                         new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.Green), // BACK                         new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.Green),                         new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.Green),                         new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.Green),                         new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.Green),                         new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.Green),                         new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.OrangeRed), // Top                         new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.OrangeRed),                         new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.OrangeRed),                         new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.OrangeRed),                         new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.OrangeRed),                         new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.OrangeRed),                         new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.Yellow), // Bottom                         new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.Yellow),                         new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.Yellow),                         new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.Yellow),                         new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.Yellow),                         new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.Yellow),                         new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.Blue), // Left                         new VertexPositionColor(new Vector3(-1.0f, -1.0f, 1.0f), Color.Blue),                         new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.Blue),                         new VertexPositionColor(new Vector3(-1.0f, -1.0f, -1.0f), Color.Blue),                         new VertexPositionColor(new Vector3(-1.0f, 1.0f, 1.0f), Color.Blue),                         new VertexPositionColor(new Vector3(-1.0f, 1.0f, -1.0f), Color.Blue),                         new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.Violet), // Right                         new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.Violet),                         new VertexPositionColor(new Vector3(1.0f, -1.0f, 1.0f), Color.Violet),                         new VertexPositionColor(new Vector3(1.0f, -1.0f, -1.0f), Color.Violet),                         new VertexPositionColor(new Vector3(1.0f, 1.0f, -1.0f), Color.Violet),                         new VertexPositionColor(new Vector3(1.0f, 1.0f, 1.0f), Color.Violet),                     }));          ToDisposeContent(vertices);          // Create an input layout from the vertices          inputLayout = VertexInputLayout.FromBuffer(0, vertices);          base.LoadContent();       }       bool _isDown = false;       float _xDelta = 0;       float _yDelta = 0; #if !USE_DIRECT_INPUT       float _wDelta = 0; #endif       System.Drawing.PointF _origin = System.Drawing.PointF.Empty;       protected override void Initialize()       {          Window.Title = "MyGameMoveCube Test";          Window.IsMouseVisible = true; #if !USE_DIRECT_INPUT          ((SharpDX.Windows.RenderForm)Window.NativeWindow).MouseMove += (sender, e) =>          {             if (_isDown)             {                _xDelta = _origin.X - e.X;                _yDelta = _origin.Y - e.Y;             }          };          ((SharpDX.Windows.RenderForm)Window.NativeWindow).MouseDown += (sender, e) =>          {             _isDown = true;             _origin = new System.Drawing.PointF(_xDelta + e.Location.X, _yDelta + e.Location.Y);          };          ((SharpDX.Windows.RenderForm)Window.NativeWindow).MouseUp += (sender, e) =>          {             _isDown = false;          };          ((SharpDX.Windows.RenderForm)Window.NativeWindow).MouseWheel += (sender, e) =>          {             _wDelta += e.Delta;          }; #endif          base.Initialize();       }       protected override void Update(GameTime gameTime)       { #if USE_DIRECT_INPUT          mouseState = mouseManager.GetState();          if (mouseState.Left == ButtonState.Pressed)          {             if (_isDown == false)             {                _isDown = true;                _origin = new System.Drawing.PointF(_xDelta + mouseState.X, _yDelta + mouseState.Y);             }          }          else          {             _isDown = false;          }          if (_isDown)          {             _xDelta = _origin.X - mouseState.X;             _yDelta = _origin.Y - mouseState.Y;          }          float rotationY = _xDelta * 10F;          float rotationX = _yDelta * 10F;          float wDelta = convertMouseWheelDelta(mouseState.WheelDelta); #else          float rotationY = _xDelta / 100F;          float rotationX = _yDelta / 100F;          float wDelta = convertMouseWheelDelta(_wDelta); #endif          basicEffect.World = Matrix.RotationY(rotationY) * Matrix.RotationX(rotationX) * Matrix.Scaling(wDelta, wDelta, wDelta);          basicEffect.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)GraphicsDevice.BackBuffer.Width / GraphicsDevice.BackBuffer.Height, 0.1f, 100.0f);          // Handle base.Update          base.Update(gameTime);       }       protected override void Draw(GameTime gameTime)       {          // Clears the screen with the Color.CornflowerBlue          GraphicsDevice.Clear(Color.CornflowerBlue);          // Setup the vertices          GraphicsDevice.SetVertexBuffer(vertices);          GraphicsDevice.SetVertexInputLayout(inputLayout);          // Apply the basic effect technique and draw the rotating cube          basicEffect.CurrentTechnique.Passes[0].Apply();          GraphicsDevice.Draw(PrimitiveType.TriangleList, vertices.ElementCount);          // Handle base.Draw          base.Draw(gameTime);       }       private float convertMouseWheelDelta(float delta)       {          return (float)Math.Exp(delta / 1000f);       }    } } 

MyGameMoveCubeTest.zip

Blogs Parent Separator My Blog
Author
lupok

My Blog

Tags