Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using SharpDX;
- using SharpDX.Direct3D11;
- using VoidwalkerEngine.Framework.Maths;
- namespace VoidwalkerEngine.Framework.DirectX.Rendering
- {
- public class BoundingBoxBuilder
- {
- private Vector3 _minimum;
- public Vector3 Minimum
- {
- get
- {
- return _minimum;
- }
- set
- {
- this._minimum = value;
- Rebuild();
- }
- }
- private Vector3 _maximum;
- public Vector3 Maximum
- {
- get
- {
- return _maximum;
- }
- set
- {
- this._maximum = value;
- Rebuild();
- }
- }
- private Vector3 _translation;
- public Vector3 Translation
- {
- get
- {
- return _translation;
- }
- set
- {
- this._translation = value;
- Rebuild();
- }
- }
- private Vector3 _rotation;
- public Vector3 Rotation
- {
- get
- {
- return _rotation;
- }
- set
- {
- this._rotation = value;
- Rebuild();
- }
- }
- private BoundingBox _internalBoundingBox;
- public Vector3[] Corners { get; private set; }
- public ModelMesh BoundingBoxMesh;
- public BoundingBoxBuilder()
- {
- Corners = new Vector3[8];
- Rebuild();
- }
- public void Update(Vector3 translation, Vector3 rotation)
- {
- this._translation = translation;
- this._rotation = rotation;
- Rebuild();
- }
- public BoundingBoxBuilder(Vector3 minimum, Vector3 maximum)
- {
- this._minimum = minimum;
- this._maximum = maximum;
- Corners = new Vector3[8];
- Rebuild();
- }
- /// <summary>
- /// Clears the current Translation and Rotation of this builder, then rebuilds it.
- /// </summary>
- public void Clear()
- {
- this._translation = Vector3.Zero;
- this._rotation = Vector3.Zero;
- Rebuild();
- }
- /// <summary>
- /// Creates BoundingBox data using vertices.
- /// </summary>
- /// <param name="vertices"></param>
- public void FromVertices(Vertex[] vertices)
- {
- // Calculate Bounding Box
- float minX = float.PositiveInfinity;
- float maxX = float.NegativeInfinity;
- float minY = float.PositiveInfinity;
- float maxY = float.NegativeInfinity;
- float minZ = float.PositiveInfinity;
- float maxZ = float.NegativeInfinity;
- for (int i = 0; i < vertices.Length; i++)
- {
- Vector3 vertex = vertices[i].Location;
- // Check for maximum
- if (vertex.X > maxX)
- {
- maxX = vertex.X;
- }
- if (vertex.Y > maxY)
- {
- maxY = vertex.Y;
- }
- if (vertex.Z > maxZ)
- {
- maxZ = vertex.Z;
- }
- // Check for Minimum
- if (vertex.X < minX)
- {
- minX = vertex.X;
- }
- if (vertex.Y < minY)
- {
- minY = vertex.Y;
- }
- if (vertex.Z < minZ)
- {
- minZ = vertex.Z;
- }
- }
- this.Minimum = new Vector3(minX, minY, minZ);
- this.Maximum = new Vector3(maxX, maxY, maxZ);
- }
- public Matrix GetTransform()
- {
- return VoidwalkerMath.CreateRotationMatrix(this.Rotation) * Matrix.Translation(this.Translation);
- }
- private void Rebuild()
- {
- Matrix transform = GetTransform();
- this.Corners[0] = Vector3.TransformCoordinate(new Vector3(this.Minimum.X, this.Maximum.Y, this.Maximum.Z), transform);
- this.Corners[1] = Vector3.TransformCoordinate(new Vector3(this.Maximum.X, this.Maximum.Y, this.Maximum.Z), transform);
- this.Corners[2] = Vector3.TransformCoordinate(new Vector3(this.Maximum.X, this.Minimum.Y, this.Maximum.Z), transform);
- this.Corners[3] = Vector3.TransformCoordinate(new Vector3(this.Minimum.X, this.Minimum.Y, this.Maximum.Z), transform);
- this.Corners[4] = Vector3.TransformCoordinate(new Vector3(this.Minimum.X, this.Maximum.Y, this.Minimum.Z), transform);
- this.Corners[5] = Vector3.TransformCoordinate(new Vector3(this.Maximum.X, this.Maximum.Y, this.Minimum.Z), transform);
- this.Corners[6] = Vector3.TransformCoordinate(new Vector3(this.Maximum.X, this.Minimum.Y, this.Minimum.Z), transform);
- this.Corners[7] = Vector3.TransformCoordinate(new Vector3(this.Minimum.X, this.Minimum.Y, this.Minimum.Z), transform);
- this._internalBoundingBox = BoundingBox.FromPoints(this.Corners);
- }
- /// <summary>
- /// Gets the currently built BoundingBox.
- /// </summary>
- /// <returns></returns>
- public BoundingBox ToBoundingBox()
- {
- return _internalBoundingBox;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement