Advertisement
Guest User

Billboard_GridComponent

a guest
Mar 3rd, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.13 KB | None | 0 0
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2011 dhpoware. All Rights Reserved.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a
  5. // copy of this software and associated documentation files (the "Software"),
  6. // to deal in the Software without restriction, including without limitation
  7. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. // and/or sell copies of the Software, and to permit persons to whom the
  9. // Software is furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22.  
  23. using System;
  24. using System.Collections.Generic;
  25. using System.Linq;
  26. using Microsoft.Xna.Framework;
  27. using Microsoft.Xna.Framework.Audio;
  28. using Microsoft.Xna.Framework.Content;
  29. using Microsoft.Xna.Framework.GamerServices;
  30. using Microsoft.Xna.Framework.Graphics;
  31. using Microsoft.Xna.Framework.Input;
  32. using Microsoft.Xna.Framework.Media;
  33.  
  34. namespace Dhpoware
  35. {
  36.     /// <summary>
  37.     /// An XNA component to draw a square grid of lines on the world X-Z plane.
  38.     /// This component is useful for debugging your games and applications.
  39.     /// Remember to set the projection matrix and the view matrix before you
  40.     /// enter the main rendering loop in your game and application to ensure
  41.     /// the GridComponent is rendered correctly.
  42.     /// </summary>
  43.     public class GridComponent : Microsoft.Xna.Framework.DrawableGameComponent
  44.     {
  45.         private VertexBuffer vertexBuffer;
  46.         private BasicEffect basicEffect;
  47.         private RasterizerState rasterizerState;
  48.         private bool created;
  49.  
  50.     #region Properties
  51.         public Color GridColor { get; set; }
  52.         public Vector3 GridPosition { get; set; }
  53.         public int GridSize { get; set; }
  54.         public int GridSpacing { get; set; }
  55.         public Matrix ProjectionMatrix { get; set; }
  56.         public Matrix ViewMatrix { get; set; }
  57.     #endregion
  58.  
  59.     #region Constuctors
  60.         public GridComponent(Game game) : base(game)
  61.         {
  62.             GridColor = Color.LightGray;
  63.             GridPosition = Vector3.Zero;
  64.             GridSize = 256;
  65.             GridSpacing = 16;
  66.         }
  67.     #endregion
  68.  
  69.     #region Public methods
  70.         public void CreateGrid()
  71.         {
  72.             float x = GridPosition.X - (GridSize * 0.5f);
  73.             float y = GridPosition.Y;
  74.             float z = GridPosition.Z - (GridSize * 0.5f);
  75.             int index = 0;
  76.             int numberOfLines = 2 * ((GridSize / GridSpacing) + 1);
  77.             VertexPositionColor[] vertices = new VertexPositionColor[numberOfLines * 2];
  78.  
  79.             // Horizontal grid lines.
  80.             for (int i = 0; i < numberOfLines / 2; ++i)
  81.             {
  82.                 vertices[index++] = new VertexPositionColor(new Vector3(x, y, z + i * GridSpacing), GridColor);
  83.                 vertices[index++] = new VertexPositionColor(new Vector3(x + GridSize, y, z + i * GridSpacing), GridColor);
  84.             }
  85.  
  86.             // Vertical grid lines.
  87.             for (int i = 0; i < numberOfLines / 2; ++i)
  88.             {
  89.                 vertices[index++] = new VertexPositionColor(new Vector3(x + i * GridSpacing, y, z), GridColor);
  90.                 vertices[index++] = new VertexPositionColor(new Vector3(x + i * GridSpacing, y, z + GridSize), GridColor);
  91.             }
  92.  
  93.             vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), vertices.Length, BufferUsage.WriteOnly);
  94.             vertexBuffer.SetData(vertices);
  95.  
  96.             created = true;
  97.         }
  98.  
  99.         public void CreateGrid(Color color, Vector3 position, int size, int spacing)
  100.         {
  101.             GridColor = color;
  102.             GridPosition = position;
  103.             GridSize = size;
  104.             GridSpacing = spacing;
  105.  
  106.             CreateGrid();
  107.         }
  108.  
  109.         public override void Draw(GameTime gameTime)
  110.         {
  111.             if (created)
  112.             {
  113.                 DepthStencilState prevDepthStencilState = GraphicsDevice.DepthStencilState;
  114.                 RasterizerState prevRasterizerState = GraphicsDevice.RasterizerState;
  115.  
  116.                 GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  117.                 GraphicsDevice.RasterizerState = rasterizerState;
  118.                 GraphicsDevice.SetVertexBuffer(vertexBuffer);
  119.  
  120.                 basicEffect.LightingEnabled = false;
  121.                 basicEffect.World = Matrix.Identity;
  122.                 basicEffect.View = ViewMatrix;
  123.                 basicEffect.Projection = ProjectionMatrix;
  124.  
  125.                 foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
  126.                 {
  127.                     pass.Apply();
  128.                     GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, vertexBuffer.VertexCount / 2);
  129.                 }
  130.  
  131.                 GraphicsDevice.SetVertexBuffer(null);
  132.                 GraphicsDevice.RasterizerState = prevRasterizerState;
  133.                 GraphicsDevice.DepthStencilState = prevDepthStencilState;
  134.             }
  135.                                    
  136.             base.Draw(gameTime);
  137.         }
  138.  
  139.         public override void Initialize()
  140.         {
  141.             base.Initialize();
  142.  
  143.             basicEffect = new BasicEffect(GraphicsDevice);
  144.            
  145.             rasterizerState = new RasterizerState();
  146.             rasterizerState.CullMode = CullMode.None;
  147.             rasterizerState.FillMode = FillMode.Solid;
  148.  
  149.             if (!created)
  150.                 CreateGrid();
  151.         }
  152.     #endregion
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement