using System; using Chernobyl.Graphics.Texture; using Chernobyl.Mathematics; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace Chernobyl.Graphics.Xna { /// /// Methods and other various API to make XNA easier to use with Chernobyl. /// public class Utility { /// /// Converts a Chernobyl.Math matrix (OpenTK matrix) to /// a XNA matrix. /// /// The matrix that is to take /// the conversion. /// The matrix to convert. public static void ToXNAMatrix(out Matrix xnaMatrix, Matrix4 matrix) { xnaMatrix.M11 = matrix.M11; xnaMatrix.M12 = matrix.M12; xnaMatrix.M13 = matrix.M13; xnaMatrix.M14 = matrix.M14; xnaMatrix.M21 = matrix.M21; xnaMatrix.M22 = matrix.M22; xnaMatrix.M23 = matrix.M23; xnaMatrix.M24 = matrix.M24; xnaMatrix.M31 = matrix.M31; xnaMatrix.M32 = matrix.M32; xnaMatrix.M33 = matrix.M33; xnaMatrix.M34 = matrix.M34; xnaMatrix.M41 = matrix.M41; xnaMatrix.M42 = matrix.M42; xnaMatrix.M43 = matrix.M43; xnaMatrix.M44 = matrix.M44; } /// /// Converts a Chernobyl TextureFormat to an XNA SurfaceFormat /// /// The TextureFormat to convert. public static SurfaceFormat ToSurfaceFormat(TextureFormat value) { switch (value) { case TextureFormat.Rgb32: return SurfaceFormat.Rgb32; case TextureFormat.Argb32: return SurfaceFormat.Color; case TextureFormat.Bgr32: return SurfaceFormat.Bgr32; } throw new ArgumentException("Unable to find a corresponding XNA SurfaceFormat for the Chernobyl TextureFormat provided: " + value.ToString(), "value"); } /// /// Converts a XNA surface format to an Chernobyl TextureFormat /// /// The TextureFormat to convert. public static TextureFormat ToTextureFormat(SurfaceFormat value) { switch (value) { case SurfaceFormat.Rgb32: return TextureFormat.Rgb32; case SurfaceFormat.Color: return TextureFormat.Argb32; case SurfaceFormat.Bgr32: return TextureFormat.Bgr32; } throw new ArgumentException("Unable to find a corresponding Chernobyl TextureFormat for the XNA SurfaceFormat provided: " + value.ToString(), "value"); } /// /// Converts a Chernobyl primitive type to an XNA primitive type. /// /// The Chernobyl primitive type to convert. /// The XNA primitive type. public static PrimitiveType ToXnaPrimitiveType(Chernobyl.Graphics.Drawing.PrimitiveType primitiveType) { PrimitiveType XnaPrimitiveType; switch (primitiveType) { case Chernobyl.Graphics.Drawing.PrimitiveType.LineList: XnaPrimitiveType = PrimitiveType.LineList; break; case Chernobyl.Graphics.Drawing.PrimitiveType.LineStrip: XnaPrimitiveType = PrimitiveType.LineStrip; break; case Chernobyl.Graphics.Drawing.PrimitiveType.PointList: XnaPrimitiveType = PrimitiveType.PointList; break; case Chernobyl.Graphics.Drawing.PrimitiveType.TriangleFan: XnaPrimitiveType = PrimitiveType.TriangleFan; break; case Chernobyl.Graphics.Drawing.PrimitiveType.TriangleList: XnaPrimitiveType = PrimitiveType.TriangleList; break; case Chernobyl.Graphics.Drawing.PrimitiveType.TriangleStrip: XnaPrimitiveType = PrimitiveType.TriangleStrip; break; default: throw new Exception("Unable to convert from an Chernobyl primitive type to an XNA Primitive Type."); } return XnaPrimitiveType; } /// /// Converts an XNA primitive type to a Chernobyl primitive type. /// /// The XNA primitive type to convert. /// The Chernobyl primitive type. public static Chernobyl.Graphics.Drawing.PrimitiveType ToChernobylPrimitiveType(PrimitiveType primitiveType) { switch (primitiveType) { case PrimitiveType.LineList: return Chernobyl.Graphics.Drawing.PrimitiveType.LineList; case PrimitiveType.LineStrip: return Chernobyl.Graphics.Drawing.PrimitiveType.LineStrip; case PrimitiveType.PointList: return Chernobyl.Graphics.Drawing.PrimitiveType.PointList; case PrimitiveType.TriangleFan: return Chernobyl.Graphics.Drawing.PrimitiveType.TriangleFan; case PrimitiveType.TriangleList: return Chernobyl.Graphics.Drawing.PrimitiveType.TriangleList; case PrimitiveType.TriangleStrip: return Chernobyl.Graphics.Drawing.PrimitiveType.TriangleStrip; } throw new Exception("Unable to convert from an XNA primitive type to a Chernobyl Primitive Type."); } /// /// Gets the number of primitives that a drawn for /// each primitive type based on the number of /// mesh elements. /// /// The primitive type to get the number of elements of. /// The number of mesh elements in the buffer /// The number of primitives to draw. public static int GetPrimitiveCount(PrimitiveType primitiveType, uint numMeshElements) { switch (primitiveType) { case PrimitiveType.LineList: return (int)numMeshElements / 2; case PrimitiveType.LineStrip: return (int)numMeshElements - 1; case PrimitiveType.PointList: return (int)numMeshElements; case PrimitiveType.TriangleFan: return (int)numMeshElements - 2; case PrimitiveType.TriangleList: return (int)numMeshElements / 3; case PrimitiveType.TriangleStrip: return (int)numMeshElements - 2; default: throw new Exception("Unknown XNA primitive type assigned to " + primitiveType.GetType().FullName); } } /// /// Converts a Chernobyl Color to an XNA color. /// /// The Chernobyl color to convert. /// The converted XNA color. public static Microsoft.Xna.Framework.Graphics.Color ToXnaColor(Chernobyl.Graphics.Color color) { return new Microsoft.Xna.Framework.Graphics.Color(color.R, color.G, color.B, color.A); } /// /// Converts an XNA Color to an Chernobyl color. /// /// The XNA color to convert. /// The converted Chernobyl color. public static Color ToChernobylColor(Microsoft.Xna.Framework.Graphics.Color color) { return new Color(color.R, color.G, color.B, color.A); } /// /// Converts a XNA Vector2 to a Chernobyl Vector2 /// /// The Chernobyl vector that is to take /// the result of the conversion. /// The XNA Vector2 to convert. /// The converter Chernobyl Vector2. public static void ToChernobylVector(out Mathematics.Vector2 chernobylVectorOut, ref Microsoft.Xna.Framework.Vector2 xnaVector) { chernobylVectorOut = new Mathematics.Vector2(xnaVector.X, xnaVector.Y); } } }