#region File Description //----------------------------------------------------------------------------- // SpinningTriangleControl.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using System.Diagnostics; using System.Windows.Forms; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; #endregion namespace Darwinbots3.DrawingSurface { /// /// Example control inherits from GraphicsDeviceControl, which allows it to /// render using a GraphicsDevice. This control shows how to draw animating /// 3D graphics inside a WinForms application. It hooks the Application.Idle /// event, using this to invalidate the control, which will cause the animation /// to constantly redraw. /// class SpinningTriangleControl : GraphicsDeviceControl { VertexDeclaration vertexDeclaration; BasicEffect effect; Stopwatch timer; // Vertex positions and colors used to display a spinning triangle. public readonly VertexPositionColor[] Vertices = { new VertexPositionColor(new Vector3(-1, -1, 0), Color.Green), new VertexPositionColor(new Vector3( 1, -1, 0), Color.GreenYellow), new VertexPositionColor(new Vector3( 0, 1, 0), Color.Yellow), }; /// /// Initializes the control. /// protected override void Initialize() { // Create our vertex declaration. vertexDeclaration = new VertexDeclaration(GraphicsDevice, VertexPositionColor.VertexElements); // Create our effect. effect = new BasicEffect(GraphicsDevice, null); effect.VertexColorEnabled = true; // Start the animation timer. timer = Stopwatch.StartNew(); // Hook the idle event to constantly redraw our animation. Application.Idle += delegate { Invalidate(); }; } /// /// Draws the control. /// protected override void Draw() { GraphicsDevice.Clear(Color.CornflowerBlue); // Spin the triangle according to how much time has passed. float time = (float)timer.Elapsed.TotalSeconds; float yaw = time * 0.7f; float pitch = time * 0.8f; float roll = time * 0.9f; // Set transform matrices. float aspect = GraphicsDevice.Viewport.AspectRatio; effect.World = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll); effect.View = Matrix.CreateLookAt(new Vector3(0, 0, -5), Vector3.Zero, Vector3.Up); effect.Projection = Matrix.CreatePerspectiveFieldOfView(1, aspect, 1, 10); // Set renderstates. GraphicsDevice.RenderState.CullMode = CullMode.None; GraphicsDevice.VertexDeclaration = vertexDeclaration; // Draw the triangle. effect.Begin(); effect.CurrentTechnique.Passes[0].Begin(); GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, Vertices, 0, 1); effect.CurrentTechnique.Passes[0].End(); effect.End(); } } }