/// <summary> /// Convert this array buffer object in a strongly-typed array. /// </summary> /// <typeparam name="T"> /// An arbitrary structure defining the returned array item. It doesn't need to be correlated with the ArrayBufferObject /// layout. /// </typeparam> /// <param name="arrayLength"> /// A <see cref="UInt32"/> that specify the number of elements of the returned array. /// </param> /// <returns> /// It returns an array having all items stored by this ArrayBufferObject. /// </returns> public T[] ToArray <T>(uint arrayLength) where T : struct { if (arrayLength > ItemCount) { throw new ArgumentOutOfRangeException("arrayLength", arrayLength, "cannot exceed items count"); } if (ClientBufferAddress == IntPtr.Zero) { throw new InvalidOperationException("no client buffer"); } Type arrayElementType = typeof(T); if (arrayElementType == null || !arrayElementType.IsValueType) { throw new InvalidOperationException("invalid array element type"); } // The base type should be corresponding ArrayBufferItemType arrayElementVertexType = ArrayBufferItem.GetArrayType(arrayElementType); if (ArrayBufferItem.GetArrayBaseType(_ArrayType) != ArrayBufferItem.GetArrayBaseType(arrayElementVertexType)) { throw new InvalidOperationException(String.Format("source base type of {0} incompatible with destination base type of {1}", arrayElementType.Name, ArrayBufferItem.GetArrayBaseType(_ArrayType))); } // Array element item size cannot exceed ItemSize uint arrayItemSize = ArrayBufferItem.GetArrayItemSize(arrayElementVertexType); if (arrayItemSize > ItemSize) { throw new ArgumentException("array element type too big", "array"); } T[] array = new T[arrayLength]; // Copy from buffer data to array data CopyArray(array, arrayItemSize, ClientBufferAddress, ItemSize, 0, arrayLength); return(array); }
/// <summary> /// Construct an ArrayBufferObject specifying its item layout on CPU side. /// </summary> /// <param name="vertexBaseType"> /// A <see cref="VertexBaseType"/> describing the item components base type on CPU side. /// </param> /// <param name="vertexLength"> /// A <see cref="UInt32"/> that specify how many components have the array item. /// </param> /// <param name="vertexRank"> /// A <see cref="UInt32"/> that specify how many columns have the array item of matrix type. /// </param> /// <param name="hint"> /// An <see cref="BufferObjectHint"/> that specify the data buffer usage hints. /// </param> public ArrayBufferObject(VertexBaseType vertexBaseType, uint vertexLength, uint vertexRank, BufferObjectHint hint) : this(ArrayBufferItem.GetArrayType(vertexBaseType, vertexLength, vertexRank), hint) { }
/// <summary> /// Copy this array buffer object to another one (strongly typed), but having a different array item type. /// </summary> /// <typeparam name="T"> /// A structure type used to determine the array items layout of the converted ArrayBufferObject. /// </typeparam> /// <returns> /// It returns a copy of this ArrayBufferObject, but having a different array item. The returned instance is actually /// a <see cref="ArrayBufferObject"/>; if it is desiderable a strongly typed <see cref="ArrayBufferObject"/>, use /// <see cref="ConvertItemType"/>. /// </returns> /// <exception cref="ArgumentException"> /// Exception thrown if the array base type of this ArrayBufferObject (<see cref="ArrayBaseType"/>) is different to the one /// derived from <typeparamref name="T"/>. /// </exception> /// <exception cref="InvalidOperationException"> /// Exception thrown if the number of base components of this ArrayBufferObject cannot be mapped into the base components /// count derived from <typeparamref name="T"/>. /// </exception> public ArrayBufferObject <T> ConvertItemType <T>() where T : struct { ArrayBufferItemType vertexArrayType = ArrayBufferItem.GetArrayType(typeof(T)); return((ArrayBufferObject <T>)ConvertItemType(vertexArrayType)); }