#region License
/* FNA - XNA4 Reimplementation for Desktop Platforms
* Copyright 2009-2016 Ethan Lee and the MonoGame Team
*
* Released under the Microsoft Public License.
* See LICENSE for details.
*/
#endregion
#region Using Statements
using System;
#endregion
namespace Microsoft.Xna.Framework.Graphics.PackedVector
{
///
/// Packed vector type containing unsigned normalized values ranging from 0 to 1.
/// The x and z components use 5 bits, and the y component uses 6 bits.
///
public struct Rg32 : IPackedVector, IEquatable, IPackedVector
{
#region Public Properties
///
/// Gets and sets the packed value.
///
[CLSCompliant(false)]
public uint PackedValue
{
get
{
return packedValue;
}
set
{
packedValue = value;
}
}
#endregion
#region Private Variables
private uint packedValue;
#endregion
#region Public Constructors
///
/// Creates a new instance of Rg32.
///
/// The x component
/// The y component
public Rg32(float x, float y)
{
packedValue = Pack(x, y);
}
///
/// Creates a new instance of Rg32.
///
///
/// Vector containing the components for the packed vector.
///
public Rg32(Vector2 vector)
{
packedValue = Pack(vector.X, vector.Y);
}
#endregion
#region Public Methods
///
/// Gets the packed vector in Vector2 format.
///
/// The packed vector in Vector2 format
public Vector2 ToVector2()
{
return new Vector2(
(float) (((packedValue >> 16) & 0xFFFF) / 65535.0f),
(float) ((packedValue & 0xFFFF) / 65535.0f)
);
}
#endregion
#region IPackedVector Methods
///
/// Sets the packed vector from a Vector4.
///
/// Vector containing the components.
void IPackedVector.PackFromVector4(Vector4 vector)
{
packedValue = Pack(vector.X, vector.Y);
}
///
/// Gets the packed vector in Vector4 format.
///
/// The packed vector in Vector4 format
Vector4 IPackedVector.ToVector4()
{
return new Vector4(ToVector2(), 0.0f, 1.0f);
}
#endregion
#region Public Static Operators and Override Methods
///
/// Compares an object with the packed vector.
///
/// The object to compare.
/// True if the object is equal to the packed vector.
public override bool Equals(object obj)
{
return (obj is Rg32) && Equals((Rg32) obj);
}
///
/// Compares another Rg32 packed vector with the packed vector.
///
/// The Rg32 packed vector to compare.
/// True if the packed vectors are equal.
public bool Equals(Rg32 other)
{
return packedValue == other.packedValue;
}
///
/// Gets a string representation of the packed vector.
///
/// A string representation of the packed vector.
public override string ToString()
{
return ToVector2().ToString();
}
///
/// Gets a hash code of the packed vector.
///
/// The hash code for the packed vector.
public override int GetHashCode()
{
return packedValue.GetHashCode();
}
public static bool operator ==(Rg32 lhs, Rg32 rhs)
{
return lhs.packedValue == rhs.packedValue;
}
public static bool operator !=(Rg32 lhs, Rg32 rhs)
{
return lhs.packedValue != rhs.packedValue;
}
#endregion
#region Private Static Pack Method
private static uint Pack(float x, float y)
{
return (uint) (
(((int) (MathHelper.Clamp(x, 0, 1) * 65535.0f) & 0xFFFF) << 16) |
((int) (MathHelper.Clamp(y, 0, 1) * 65535.0f) & 0xFFFF)
);
}
#endregion
}
}