Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------------------------------------------------------------------
- // Copyright (c) 2011 dhpoware. All Rights Reserved.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a
- // copy of this software and associated documentation files (the "Software"),
- // to deal in the Software without restriction, including without limitation
- // the rights to use, copy, modify, merge, publish, distribute, sublicense,
- // and/or sell copies of the Software, and to permit persons to whom the
- // Software is furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- namespace Dhpoware
- {
- /// <summary>
- /// An XNA component to draw a square grid of lines on the world X-Z plane.
- /// This component is useful for debugging your games and applications.
- /// Remember to set the projection matrix and the view matrix before you
- /// enter the main rendering loop in your game and application to ensure
- /// the GridComponent is rendered correctly.
- /// </summary>
- public class GridComponent : Microsoft.Xna.Framework.DrawableGameComponent
- {
- private VertexBuffer vertexBuffer;
- private BasicEffect basicEffect;
- private RasterizerState rasterizerState;
- private bool created;
- #region Properties
- public Color GridColor { get; set; }
- public Vector3 GridPosition { get; set; }
- public int GridSize { get; set; }
- public int GridSpacing { get; set; }
- public Matrix ProjectionMatrix { get; set; }
- public Matrix ViewMatrix { get; set; }
- #endregion
- #region Constuctors
- public GridComponent(Game game) : base(game)
- {
- GridColor = Color.LightGray;
- GridPosition = Vector3.Zero;
- GridSize = 256;
- GridSpacing = 16;
- }
- #endregion
- #region Public methods
- public void CreateGrid()
- {
- float x = GridPosition.X - (GridSize * 0.5f);
- float y = GridPosition.Y;
- float z = GridPosition.Z - (GridSize * 0.5f);
- int index = 0;
- int numberOfLines = 2 * ((GridSize / GridSpacing) + 1);
- VertexPositionColor[] vertices = new VertexPositionColor[numberOfLines * 2];
- // Horizontal grid lines.
- for (int i = 0; i < numberOfLines / 2; ++i)
- {
- vertices[index++] = new VertexPositionColor(new Vector3(x, y, z + i * GridSpacing), GridColor);
- vertices[index++] = new VertexPositionColor(new Vector3(x + GridSize, y, z + i * GridSpacing), GridColor);
- }
- // Vertical grid lines.
- for (int i = 0; i < numberOfLines / 2; ++i)
- {
- vertices[index++] = new VertexPositionColor(new Vector3(x + i * GridSpacing, y, z), GridColor);
- vertices[index++] = new VertexPositionColor(new Vector3(x + i * GridSpacing, y, z + GridSize), GridColor);
- }
- vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), vertices.Length, BufferUsage.WriteOnly);
- vertexBuffer.SetData(vertices);
- created = true;
- }
- public void CreateGrid(Color color, Vector3 position, int size, int spacing)
- {
- GridColor = color;
- GridPosition = position;
- GridSize = size;
- GridSpacing = spacing;
- CreateGrid();
- }
- public override void Draw(GameTime gameTime)
- {
- if (created)
- {
- DepthStencilState prevDepthStencilState = GraphicsDevice.DepthStencilState;
- RasterizerState prevRasterizerState = GraphicsDevice.RasterizerState;
- GraphicsDevice.DepthStencilState = DepthStencilState.Default;
- GraphicsDevice.RasterizerState = rasterizerState;
- GraphicsDevice.SetVertexBuffer(vertexBuffer);
- basicEffect.LightingEnabled = false;
- basicEffect.World = Matrix.Identity;
- basicEffect.View = ViewMatrix;
- basicEffect.Projection = ProjectionMatrix;
- foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
- {
- pass.Apply();
- GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, vertexBuffer.VertexCount / 2);
- }
- GraphicsDevice.SetVertexBuffer(null);
- GraphicsDevice.RasterizerState = prevRasterizerState;
- GraphicsDevice.DepthStencilState = prevDepthStencilState;
- }
- base.Draw(gameTime);
- }
- public override void Initialize()
- {
- base.Initialize();
- basicEffect = new BasicEffect(GraphicsDevice);
- rasterizerState = new RasterizerState();
- rasterizerState.CullMode = CullMode.None;
- rasterizerState.FillMode = FillMode.Solid;
- if (!created)
- CreateGrid();
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement