From ae05d34df525440581b344a774116deb6e97fc4e Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Fri, 23 Jan 2009 15:24:57 +0000 Subject: [PATCH] Added Color4 structure and overloads for Materialv/Lightv. --- Source/OpenTK/Graphics/Color4.cs | 149 ++++++++++++++++++++++++++ Source/OpenTK/Graphics/GL/GLHelper.cs | 38 +++++-- 2 files changed, 178 insertions(+), 9 deletions(-) create mode 100644 Source/OpenTK/Graphics/Color4.cs diff --git a/Source/OpenTK/Graphics/Color4.cs b/Source/OpenTK/Graphics/Color4.cs new file mode 100644 index 00000000..246acf95 --- /dev/null +++ b/Source/OpenTK/Graphics/Color4.cs @@ -0,0 +1,149 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Drawing; + +namespace OpenTK.Graphics +{ + /// + /// Represents a color with 4 floating-point components (R, G, B, A). + /// + public struct Color4 : IEquatable + { + #region Fields + + /// + /// The red component of this Color4 structure. + /// + public readonly float R; + + /// + /// The green component of this Color4 structure. + /// + public readonly float G; + + /// + /// The blue component of this Color4 structure. + /// + public readonly float B; + + /// + /// The alpha component of this Color4 structure. + /// + public readonly float A; + + #endregion + + #region Constructors + + /// + /// Constructs a new Color4 structure from the specified components. + /// + /// The red component of the new Color4 structure. + /// The green component of the new Color4 structure. + /// The blue component of the new Color4 structure. + /// The alpha component of the new Color4 structure. + public Color4(float r, float g, float b, float a) + { + R = r; + G = g; + B = b; + A = a; + } + + /// + /// Constructs a new Color4 structure from the specified System.Drawing.Color. + /// + /// The System.Drawing.Color containing the component values. + public Color4(Color color) + { + R = color.R / (float)Byte.MaxValue; + G = color.G / (float)Byte.MaxValue; + B = color.B / (float)Byte.MaxValue; + A = color.A / (float)Byte.MaxValue; + } + + #endregion + + #region Conversions + + /// + /// Converts the specified System.Drawing.Color to a Color4 structure. + /// + /// The System.Drawing.Color to convert. + /// A new Color4 structure containing the converted components. + public static implicit operator Color4(Color color) + { + return new Color4(color); + } + + /// + /// Converts the specified Color4 to a System.Drawing.Color structure. + /// + /// The Color4 to convert. + /// A new System.Drawing.Color structure containing the converted components. + public static explicit operator Color(Color4 color) + { + return Color.FromArgb( + (int)(color.A * Byte.MaxValue), + (int)(color.R * Byte.MaxValue), + (int)(color.G * Byte.MaxValue), + (int)(color.B * Byte.MaxValue)); + } + + #endregion + + #region IEquatable Members + + /// + /// Compares whether this Color4 structure is equal to the specified Color4. + /// + /// The Color4 structure to compare to. + /// True if both Color4 structures contain the same components; false otherwise. + public bool Equals(Color4 other) + { + return + this.R == other.R && + this.G == other.G && + this.B == other.B && + this.A == other.A; + } + + #endregion + + #region Overrides + + /// + /// Compares whether this Color4 structure is equal to the specified object. + /// + /// An object to compare to. + /// True obj is a Color4 structure with the same components as this Color4; false otherwise. + public override bool Equals(object obj) + { + if (!(obj is Color4)) + return false; + + return Equals((Color4)obj); + } + + /// + /// Calculates the hash code for this Color4 structure. + /// + /// A System.Int32 containing the hashcode of this Color4 structure. + public override int GetHashCode() + { + return ((Color)this).GetHashCode(); + } + + /// + /// Creates a System.String that describes this Color4 structure. + /// + /// A System.String that describes this Color4 structure. + public override string ToString() + { + return String.Format("{{R, G, B, A = {0}, {1}, {2}, {3}}}", R.ToString(), G.ToString(), B.ToString(), A.ToString()); + } + + #endregion + } +} diff --git a/Source/OpenTK/Graphics/GL/GLHelper.cs b/Source/OpenTK/Graphics/GL/GLHelper.cs index 3ddb68bd..56a6acd6 100644 --- a/Source/OpenTK/Graphics/GL/GLHelper.cs +++ b/Source/OpenTK/Graphics/GL/GLHelper.cs @@ -54,7 +54,7 @@ namespace OpenTK.Graphics /// /// /// - /// + /// /// /// public static partial class GL @@ -659,6 +659,34 @@ namespace OpenTK.Graphics #endregion + #region public static void Material() overloads + + public static void Materialv(MaterialFace face, MaterialParameter pname, Vector4 @params) + { + Materialv(face, pname, ref @params.X); + } + + public static void Materialv(MaterialFace face, MaterialParameter pname, Color4 @params) + { + unsafe { GL.Materialv(face, pname, (float*)&@params); } + } + + #endregion + + #region public static void Light() overloads + + public static void Lightv(LightName name, LightParameter pname, Vector4 @params) + { + GL.Lightv(name, pname, ref @params.X); + } + + public static void Lightv(LightName name, LightParameter pname, Color4 @params) + { + unsafe { GL.Lightv(name, pname, (float*)&@params); } + } + + #endregion + #region --- Overloads for OpenTK.Math --- public static void Normal3(Vector3 normal) @@ -770,14 +798,6 @@ namespace OpenTK.Graphics } } - public static void Materialv(MaterialFace face, MaterialParameter pname, Vector4 @params) - { - unsafe - { - Materialv(face, pname, &@params.X); - } - } - #endregion #region public static void ShaderSource(Int32 shader, System.String @string)