#region usings
using OpenTK;
using OpenTK.Graphics.ES10;
using SM.Base.Textures;
using SM.Base.Types;
#endregion
namespace SM.Base.Drawing
{
///
/// Stores transformations for the textures.
///
public class TextureTransformation
{
///
/// The offset from the origin.
///
public CVector2 Offset = new CVector2(0);
///
/// The rotation of the texture.
///
public CVector1 Rotation = new CVector1(0);
///
/// The scale of the texture.
///
public CVector2 Scale = new CVector2(1);
///
/// Get the texture matrix.
///
///
public Matrix3 GetMatrix()
{
return CalculateMatrix(Offset, Scale, Rotation);
}
///
/// Sets the offset relative to the pixels of the texture.
///
/// The texture it should use.
/// The offset in pixel.
public void SetOffsetRelative(Texture texture, Vector2 pixelLocation)
{
Vector2 textureSize = new Vector2(texture.Width, texture.Height);
Offset.Set( Vector2.Divide(pixelLocation, textureSize) );
}
///
/// Sets the scale relative to the pixels of the texture.
///
/// The texture.
/// The scale in pixel.
public void SetScaleRelative(Texture texture, Vector2 rectangleSize)
{
Vector2 textureSize = new Vector2(texture.Width, texture.Height);
Scale.Set( Vector2.Divide(rectangleSize, textureSize) );
}
///
/// Sets the offset and scale relative to the pixels of the texture.
///
/// The texture.
/// Offset in pixel
/// Scale in pixel.
public void SetRectangleRelative(Texture texture, Vector2 location, Vector2 rectangleSize)
{
Vector2 textureSize = new Vector2(texture.Width, texture.Height);
Offset.Set(Vector2.Divide(location, textureSize));
Scale.Set(Vector2.Divide(rectangleSize, textureSize));
}
///
/// Calculates a texture matrix.
///
///
///
///
///
public static Matrix3 CalculateMatrix(Vector2 offset, Vector2 scale, float rotation)
{
float radians = MathHelper.DegreesToRadians(rotation);
Matrix3 result = Matrix3.CreateScale(scale.X, scale.Y, 1) * Matrix3.CreateRotationZ(radians);
result.Row2 = new Vector3(offset.X, offset.Y, 1);
return result;
}
}
}