我想知道,C#是否与以下代码等效?
ShortBuffer sb = BufferUtils.createShortBuffer(Cubie.indexData.length);
sb.put(Cubie.indexData).flip();
Gl.BufferData(BufferTarget.ElementArrayBuffer, sb, BufferUsageHint.StaticDraw);
Gl.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
最佳答案
首先,这可能会有点麻烦,但是要回答,以防有人来这里或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