2015-04-01 122 views
3

我想知道,在C#中是否存在与下面的代码相同的内容?C#相当于Java的FloatBuffer/ShortBuffer?

ShortBuffer sb = BufferUtils.createShortBuffer(Cubie.indexData.length); 
sb.put(Cubie.indexData).flip(); 

Gl.BufferData(BufferTarget.ElementArrayBuffer, sb, BufferUsageHint.StaticDraw); 
Gl.BindBuffer(BufferTarget.ElementArrayBuffer, 0); 
+3

如果你解释说,类做什么,这可能是比较容易回答的C#专家... – BradleyDotNET 2015-04-01 22:52:52

+1

@BradleyDotNET在这个项目中我使用opengl绘制一个立方体。在我的班'Cubie'中,我有一个short值的数组(indexData)。我希望能够在多维数据集移动时跟踪这些索引。在我的主类中,我希望能够检索这些值并将它们写入缓冲区。我还添加了以下几行代码。 – 2015-04-01 23:03:47

+0

那么...一个数组? “缓冲”部分有什么特别之处?原谅我对Java构造的无知。 – BradleyDotNET 2015-04-01 23:14:12

回答

1

首先这可能是一个小小的颠簸,但回答以防其他人来到这里或OP可能仍然需要这样的事情。

我将一个数组包装到缓冲区类中,并试图匹配ShortBuffer。当然,这不是一个完整的实现,所以可能还会有一些东西丢失或者必须实施的东西。

如果您想要拥有不同类型的缓冲区而不仅仅是简短的话,我将其作为通用的。

这里是Buffer<T>类。

/// <summary> 
    /// A generic buffer. 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    public class Buffer<T> 
    { 
     /// <summary> 
     /// The internal buffer. 
     /// </summary> 
     protected T[] _array; 

     /// <summary> 
     /// Creates a new generic buffer. 
     /// </summary> 
     /// <param name="array">The array.</param> 
     public Buffer(T[] array) 
     { 
      _array = new T[array.Length]; 
      Array.Copy(array, 0, _array, 0, _array.Length); 
     } 

     /// <summary> 
     /// Creates a new generic buffer. 
     /// </summary> 
     /// <param name="capacity">The capacity.</param> 
     public Buffer(int capacity) 
     { 
      _array = new T[capacity]; 
      // 0 filling for value-types 
      for (int i = 0; i < _array.Length; i++) 
       _array[i] = default(T); 
     } 

     /// <summary> 
     /// Gets the first value of the buffer. 
     /// </summary> 
     public T First 
     { 
      get { return _array[0]; } 
     } 

     /// <summary> 
     /// Gets the last value of the buffer. 
     /// </summary> 
     public T Last 
     { 
      get { return _array[_array.Length - 1]; } 
     } 

     /// <summary> 
     /// Gets the length of the buffer. 
     /// </summary> 
     public int Length 
     { 
      get { return _array.Length; } 
     } 

     /// <summary> 
     /// Gets values in the form of an array from a specific index. 
     /// </summary> 
     /// <param name="index">The index.</param> 
     /// <param name="length">The length.</param> 
     /// <returns>Returns the values in the form of an array.</returns> 
     public T[] Get(int index, int length) 
     { 
      var array = new T[length]; 
      Array.Copy(_array, index, array, 0, length); 
      return array; 
     } 

     /// <summary> 
     /// Gets a copy of the internal array. 
     /// </summary> 
     /// <returns>Returns the copied array.</returns> 
     public T[] Get() 
     { 
      var array = new T[_array.Length]; 
      Array.Copy(_array, 0, array, 0, array.Length); 
      return array; 
     } 

     /// <summary> 
     /// Gets a value by an index. 
     /// </summary> 
     /// <param name="index">The index.</param> 
     /// <returns>Returns the value.</returns> 
     public T GetValue(int index) 
     { 
      return _array[index]; 
     } 

     /// <summary> 
     /// Puts a value into the beginning of the buffer. 
     /// Note: Put does not replace the first value. 
     /// </summary> 
     /// <param name="value">The value.</param> 
     public void Put(T value) 
     { 
      var array = new T[_array.Length + 1]; 
      Array.Copy(_array, 0, array, 1, _array.Length); 
      array[0] = value; 

      _array = array; 
     } 

     /// <summary> 
     /// Puts a value into a specific index of the buffer. 
     /// Note: Put does not replace the value at the specific index. 
     /// </summary> 
     /// <param name="value">The value.</param> 
     /// <param name="index">The index.</param> 
     public void Put(T value, int index) 
     { 
      if (index == 0) 
      { 
       Put(value); 
      } 
      else if (index == _array.Length - 1) 
      { 
       Append(value); 
      } 
      else 
      { 
       var array = new T[_array.Length + 1]; 
       array[index] = value; 

       Array.Copy(_array, 0, array, 0, index); 
       Array.Copy(_array, index, array, index + 1, _array.Length - index); 

       _array = array; 
      } 
     } 

     /// <summary> 
     /// Inserts a value at a specific index of the buffer. 
     /// Note: Insert replaces the current value at the index. 
     /// </summary> 
     /// <param name="value">The value.</param> 
     /// <param name="index">The index.</param> 
     public void Insert(T value, int index) 
     { 
      _array[index] = value; 
     } 

     /// <summary> 
     /// Appends a value to the buffer. 
     /// </summary> 
     /// <param name="value">The value to append.</param> 
     public void Append(T value) 
     { 
      var array = new T[_array.Length + 1]; 
      Array.Copy(_array, 0, array, 0, _array.Length); 
      array[array.Length - 1] = value; 

      _array = array; 
     } 

     /// <summary> 
     /// Puts an array into the beginning of the buffer. 
     /// Note: Put does not replace the first values of the buffer. 
     /// </summary> 
     /// <param name="value">The value.</param> 
     public void Put(T[] value) 
     { 
      var array = new T[_array.Length + value.Length]; 
      Array.Copy(value, 0, array, 0, value.Length); 
      Array.Copy(_array, 0, array, value.Length, _array.Length); 

      _array = array; 
     } 

     /// <summary> 
     /// Puts an array into a specific index of the buffer. 
     /// Note: Put does not replace the array at the specific index. 
     /// </summary> 
     /// <param name="value">The value.</param> 
     /// <param name="index">The index.</param> 
     public void Put(T[] value, int index) 
     { 
      if (index == 0) 
      { 
       Put(value); 
      } 
      else if (index == _array.Length - 1) 
      { 
       Append(value); 
      } 
      else 
      { 
       var array = new T[_array.Length + value.Length]; 

       Array.Copy(value, 0, array, index, value.Length); 
       Array.Copy(_array, 0, array, 0, index); 
       Array.Copy(_array, index, array, index + value.Length, _array.Length - index); 

       _array = array; 
      } 
     } 

     /// <summary> 
     /// Appends an array to the buffer. 
     /// </summary> 
     /// <param name="value">The value.</param> 
     public void Append(T[] value) 
     { 
      var array = new T[_array.Length + value.Length]; 
      Array.Copy(_array, 0, array, 0, _array.Length); 
      Array.Copy(value, 0, array, _array.Length, value.Length); 

      _array = array; 
     } 

     /// <summary> 
     /// Puts a buffer into the beginning of the buffer. 
     /// Note: Put does not replace the first values of the buffer. 
     /// </summary> 
     /// <param name="value">The value.</param> 
     public void Put(Buffer<T> value) 
     { 
      Put(value._array); 
     } 

     /// <summary> 
     /// Puts a buffer into a specific index of the buffer. 
     /// Note: Put does not replace the buffer at the specific index. 
     /// </summary> 
     /// <param name="value">The value.</param> 
     /// <param name="index">The index.</param> 
     public void Put(Buffer<T> value, int index) 
     { 
      Put(value._array, index); 
     } 

     /// <summary> 
     /// Appends a buffer to the buffer. 
     /// </summary> 
     /// <param name="value">The value.</param> 
     public void Append(Buffer<T> value) 
     { 
      Append(value._array); 
     } 

     /// <summary> 
     /// Gets the bytes of the buffer. 
     /// </summary> 
     /// <returns>Returns a newly created byte array of the buffer.</returns> 
     public byte[] GetBytes() 
     { 
      var buffer = new byte[_array.Length * Marshal.SizeOf(typeof(T))]; 
      Buffer.BlockCopy(_array, 0, buffer, 0, buffer.Length); 
      return buffer; 
     } 
    } 

下面是关于如何实现ShortBuffer

/// <summary> 
    /// A 16 bit integer buffer. 
    /// </summary> 
    public class Int16Buffer : Buffer<short> 
    { 
     /// <summary> 
     /// Creates a new instance of Int16Buffer. 
     /// </summary> 
     /// <param name="array">The array to build a buffer upon.</param> 
     public Int16Buffer(short[] array) 
      : base(array) { } 

     /// <summary> 
     /// Creates a new instance of Int16Buffer. 
     /// </summary> 
     /// <param name="capacity">The start capacity of the buffer.</param> 
     public Int16Buffer(int capacity) 
      : base(capacity) { } 
    } 

淡然的示例实现也可以做

// Creates a buffer of 10 short elements ... 
var shortBuffer = new Buffer<short>(10); 

最后,这里是一些示例用法。

var buffer1 = new Int16Buffer(new short[] { 1, 2, 4, 5 }); 
buffer1.Put(0); 
buffer1.Put(3, 3); 
buffer1.Append(6); 

var buffer2 = new Int16Buffer(new short[] { 1, 2, 4, 5 }); 
buffer2.Put(0); 
buffer2.Put(3, 3); 
buffer2.Append(6); 

buffer1.Put(buffer2); 

var buffer3 = new Int16Buffer(new short[] { 1, 2, 4, 5 }); 
buffer3.Put(0); 
buffer3.Put(3, 3); 
buffer3.Append(6); 

buffer1.Put(buffer3, 7); 

foreach (var b in buffer1.Get()) 
{ 
    Console.WriteLine(b); 
} 

你可以在这里输出:

ShortBuffer Example