Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework;
- namespace CubeRender
- {
- public enum RenderObjectType
- {
- VertexIndex,
- Vertex
- }
- public abstract class RenderObject : IDisposable
- {
- private List<IVertexType> vertices = new List<IVertexType>();
- public List<IVertexType> Vertices { get { return vertices; } }
- private List<ushort> indices = new List<ushort>();
- public List<ushort> Indices { get { return indices; } }
- private VertexBuffer vertexBuffer;
- public VertexBuffer VertexBuffer { get { return vertexBuffer; } protected set { vertexBuffer = value; } }
- private IndexBuffer indexBuffer;
- public IndexBuffer IndexBuffer { get { return indexBuffer; } protected set { indexBuffer = value; } }
- private PrimitiveType primitiveType;
- public PrimitiveType PrimitiveType { get { return primitiveType; } set { primitiveType = value; } }
- private RenderObjectType type;
- public RenderObjectType Type { get { return type; } protected set { type = value; } }
- public int VerticeCount { get { return vertices.Count; } }
- private int? primitiveCount = null;
- public virtual int PrimitiveCount { get { if (primitiveCount == null) { return vertices.Count / 3; } else { return primitiveCount.Value; } } protected set { primitiveCount = value; } }
- public VertexDeclaration VertexDeclaration { get { return vertexBuffer.VertexDeclaration; } }
- private Vector3 rotation = Vector3.Zero;
- public Vector3 Rotation { get { return rotation; } set { if (rotation != value) { rotation = value; UpdateWorld(); } } }
- private Vector3 position = Vector3.Zero;
- public Vector3 Position { get { return position; } set { if (position != value) { position = value; UpdateWorld(); } } }
- private float scale = 1f;
- public float Scale { get { return scale; } set { if (scale != value) { scale = value; UpdateWorld(); } } }
- private Matrix? world;
- public Matrix World
- {
- get
- {
- if (world == null)
- {
- UpdateWorld();
- }
- return world.Value;
- }
- }
- protected BoundingBox? boundingBox = null;
- public abstract BoundingBox BoundingBox { get; }
- private void UpdateWorld()
- {
- world =
- Matrix.CreateScale(Scale) *
- Matrix.CreateRotationX(Rotation.X) *
- Matrix.CreateRotationY(Rotation.Y) *
- Matrix.CreateRotationZ(Rotation.Z) *
- Matrix.CreateTranslation(Position);
- boundingBox = null;
- }
- protected virtual void Initialize<T>(GraphicsDevice graphicsDevice, RenderObjectType type, PrimitiveType primitiveType) where T : struct, IVertexType
- {
- Type = type;
- PrimitiveType = primitiveType;
- vertexBuffer = new VertexBuffer(graphicsDevice, typeof(T), vertices.Count, BufferUsage.None);
- vertexBuffer.SetData<T>(vertices.Select(m => (T)m).ToArray());
- if (Type == RenderObjectType.VertexIndex)
- {
- indexBuffer = new IndexBuffer(graphicsDevice, typeof(ushort), indices.Count, BufferUsage.None);
- indexBuffer.SetData(indices.ToArray());
- }
- }
- protected void AddVertex<T>(T vertex) where T : struct, IVertexType
- {
- vertices.Add(vertex);
- }
- protected void AddIndex(int index)
- {
- if (index > ushort.MaxValue)
- throw new ArgumentOutOfRangeException("index");
- indices.Add((ushort)index);
- }
- ~RenderObject()
- {
- Dispose(false);
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (vertexBuffer != null)
- vertexBuffer.Dispose();
- if (indexBuffer != null)
- indexBuffer.Dispose();
- }
- }
- public virtual void Draw(GraphicsDevice graphicsDevice, BasicEffect effect)
- {
- if (effect == null || effect.IsDisposed)
- return;
- effect.World = this.World;
- foreach (EffectPass pass in effect.CurrentTechnique.Passes)
- pass.Apply();
- if (this.Type == RenderObjectType.VertexIndex)
- {
- graphicsDevice.Indices = this.IndexBuffer;
- graphicsDevice.SetVertexBuffer(this.VertexBuffer);
- graphicsDevice.DrawIndexedPrimitives(this.PrimitiveType, 0, 0, this.VerticeCount, 0, this.PrimitiveCount);
- }
- else if (this.Type == RenderObjectType.Vertex)
- {
- graphicsDevice.Indices = null;
- graphicsDevice.SetVertexBuffer(this.VertexBuffer);
- graphicsDevice.DrawIndexedPrimitives(this.PrimitiveType, 0, 0, this.VerticeCount, 0, this.PrimitiveCount);
- }
- }
- }
- public abstract class PrimitiveColor : RenderObject
- {
- public override BoundingBox BoundingBox
- {
- get
- {
- if (this.boundingBox == null)
- {
- boundingBox = BoundingBox.CreateFromPoints(this.Vertices.Select(m => Vector3.Transform(((VertexPositionColor)m).Position, this.World)));
- }
- return this.boundingBox.Value;
- }
- }
- }
- public class Cube2 : PrimitiveColor
- {
- public Cube2(GraphicsDevice graphicsDevice) : this(graphicsDevice, Color.White, 1f, 1f, 1f)
- {
- }
- public Cube2(GraphicsDevice graphicsDevice, Color color, float width = 1f, float height = 1f, float depth = 1f)
- {
- Vector3 size = new Vector3(width, height, depth);
- // A cube has six faces, each one pointing in a different direction.
- Vector3[] normals =
- {
- new Vector3(0, 0, 1),
- new Vector3(0, 0, -1),
- new Vector3(1, 0, 0),
- new Vector3(-1, 0, 0),
- new Vector3(0, 1, 0),
- new Vector3(0, -1, 0),
- };
- // Create each face in turn.
- foreach (Vector3 normal in normals)
- {
- // Get two vectors perpendicular to the face normal and to each other.
- Vector3 side1 = new Vector3(normal.Y, normal.Z, normal.X);
- Vector3 side2 = Vector3.Cross(normal, side1);
- // Six indices (two triangles) per face.
- AddIndex(VerticeCount + 0);
- AddIndex(VerticeCount + 1);
- AddIndex(VerticeCount + 2);
- AddIndex(VerticeCount + 0);
- AddIndex(VerticeCount + 2);
- AddIndex(VerticeCount + 3);
- // Four vertices per face.
- AddVertex(new VertexPositionColor((normal - side1 - side2) * size / 2, color));
- AddVertex(new VertexPositionColor((normal - side1 + side2) * size / 2, color));
- AddVertex(new VertexPositionColor((normal + side1 + side2) * size / 2, color));
- AddVertex(new VertexPositionColor((normal + side1 - side2) * size / 2, color));
- }
- PrimitiveCount = VerticeCount;
- Initialize<VertexPositionColor>(graphicsDevice, RenderObjectType.VertexIndex, PrimitiveType.TriangleList);
- }
- }
- public class Cube : PrimitiveColor
- {
- public Cube(GraphicsDevice graphicsDevice)
- {
- AddVertex<VertexPositionColor>(new VertexPositionColor(new Vector3(-1, -1, -1), Color.Black));
- AddVertex<VertexPositionColor>(new VertexPositionColor(new Vector3(-1, -1, 1), Color.Red));
- AddVertex<VertexPositionColor>(new VertexPositionColor(new Vector3(1, -1, 1), Color.Yellow));
- AddVertex<VertexPositionColor>(new VertexPositionColor(new Vector3(1, -1, -1), Color.Green));
- AddVertex<VertexPositionColor>(new VertexPositionColor(new Vector3(-1, 1, -1), Color.Blue));
- AddVertex<VertexPositionColor>(new VertexPositionColor(new Vector3(-1, 1, 1), Color.Magenta));
- AddVertex<VertexPositionColor>(new VertexPositionColor(new Vector3(1, 1, 1), Color.White));
- AddVertex<VertexPositionColor>(new VertexPositionColor(new Vector3(1, 1, -1), Color.Cyan));
- //bottom face
- AddIndex(0);
- AddIndex(2);
- AddIndex(3);
- AddIndex(0);
- AddIndex(1);
- AddIndex(2);
- // top face
- AddIndex(4);
- AddIndex(6);
- AddIndex(5);
- AddIndex(4);
- AddIndex(7);
- AddIndex(6);
- // front face
- AddIndex(5);
- AddIndex(2);
- AddIndex(1);
- AddIndex(5);
- AddIndex(6);
- AddIndex(2);
- // back face
- AddIndex(0);
- AddIndex(7);
- AddIndex(4);
- AddIndex(0);
- AddIndex(3);
- AddIndex(7);
- // left face
- AddIndex(0);
- AddIndex(4);
- AddIndex(1);
- AddIndex(1);
- AddIndex(4);
- AddIndex(5);
- //right face
- AddIndex(2);
- AddIndex(6);
- AddIndex(3);
- AddIndex(3);
- AddIndex(6);
- AddIndex(7);
- PrimitiveCount = VerticeCount*2;
- Initialize<VertexPositionColor>(graphicsDevice, RenderObjectType.VertexIndex, PrimitiveType.TriangleList);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement