Advertisement
default_ex

VertexElementAttribute

Jul 25th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
  2. public class VertexElementAttribute : Attribute
  3. {
  4.     static Dictionary<Type, VertexElement[]> ms_elementCache = new Dictionary<Type, VertexElement[]>();
  5.  
  6.     public static VertexElement[] GetVertexElements(Type vertexType)
  7.     {
  8.         if (!vertexType.IsValueType || vertexType.IsPrimitive || vertexType.IsEnum)
  9.             throw new ArgumentException("The type specified must be a structure.", "vertexType");
  10.  
  11.         VertexElement[] vertexElements;
  12.         if (!ms_elementCache.TryGetValue(vertexType, out vertexElements))
  13.         {
  14.             FieldInfo[] fields = vertexType.GetFields(BindingFlags.Instance |
  15.                                                         BindingFlags.Public |
  16.                                                         BindingFlags.NonPublic);
  17.             if (fields != null)
  18.             {
  19.                 List<VertexElement> elementList = new List<VertexElement>();
  20.                 for (int f = 0; f < fields.Length; f++)
  21.                 {
  22.                     object[] veAttribs = fields[f].GetCustomAttributes(typeof(VertexElementAttribute), false);
  23.                     if (veAttribs == null || veAttribs.Length < 1)
  24.                         continue;
  25.  
  26.                     VertexElementAttribute vertexAttrib = veAttribs[0] as VertexElementAttribute;
  27.                     if (vertexAttrib == null)
  28.                         continue;
  29.  
  30.                     VertexElement newElement = new VertexElement();
  31.                     newElement.Name = vertexAttrib.Name;
  32.                     newElement.Normalized = vertexAttrib.Normalized;
  33.                     newElement.Type = vertexAttrib.DataType;
  34.                     newElement.Size = vertexAttrib.Size;
  35.                     newElement.Offset = Marshal.OffsetOf(vertexType, fields[f].Name);
  36.                     elementList.Add(newElement);
  37.                 }
  38.  
  39.                 if (elementList.Count > 0)
  40.                 {
  41.                     vertexElements = elementList.ToArray();
  42.                     ms_elementCache.Add(vertexType, vertexElements);
  43.                 }
  44.             }
  45.         }
  46.  
  47.         VertexElement[] result = null;
  48.         if (vertexElements != null)
  49.         {
  50.             result = new VertexElement[vertexElements.Length];
  51.             Array.Copy(vertexElements, result, vertexElements.Length);
  52.         }
  53.         return result;
  54.     }
  55.  
  56.     public static bool Apply(Type vertexType, ShaderProgram program, int stride = 0)
  57.     {
  58.         VertexElement[] vertexElements = GetVertexElements(vertexType);
  59.         if (vertexElements != null)
  60.         {
  61.             for (int i = 0; i < vertexElements.Length; i++)
  62.             {
  63.                 vertexElements[i].Apply(program, stride);
  64.             }
  65.             return true;
  66.         }
  67.         return false;
  68.     }
  69.  
  70.     public string Name { get; set; }
  71.     public VertexAttribPointerType DataType { get; set; }
  72.     public int Size { get; set; }
  73.     public bool Normalized { get; set; }
  74.  
  75.     public VertexElementAttribute(string name, VertexAttribPointerType dataType, int size, bool normalized = false)
  76.         : base()
  77.     {
  78.         Name = name;
  79.         DataType = dataType;
  80.         Size = size;
  81.         Normalized = normalized;
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement