Sunday, 8 September 2013

OpenTK (OpenGL) correct usage of BufferData and BufferSubData

OpenTK (OpenGL) correct usage of BufferData and BufferSubData

i'm working on a Marching Cubes implementation in C# with OpenTK, i have
no problems with the algorythm but i cant figure out how to get BufferData
and BufferSubData to run properly.
I have confirmed that the vertices and indices run as usual by deleting
and creating new buffers on each update, however doing this every frame
will have a huge impact on performance so it is no option.
After googling the hell out of it, my current setup looks as follows:
class ChunkVertexBuffer : IIndexSource, IVertexSource, IDisposable
{
private int indexBufferHandle;
private int vertexBufferHandle;
[StructLayout(LayoutKind.Sequential)]
public struct N3fV3f
{
public Vector3 normal;
public Vector3 position;
}
public ChunkVertexBuffer()
{
int[] handles = new int[2];
GL.GenBuffers(2, handles);
indexBufferHandle = handles[0];
vertexBufferHandle = handles[1];
}
public void BufferData(N3fV3f[] vertices, uint[] indices)
{
GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBufferHandle);
GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferHandle);
int vertexStride = BlittableValueType.StrideOf(vertices);
int indexStride = sizeof(uint);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length *
vertexStride), IntPtr.Zero, BufferUsageHint.StreamDraw);
GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)0,
(IntPtr)(vertices.Length * vertexStride), vertices);
GL.BufferData(BufferTarget.ElementArrayBuffer,
(IntPtr)(indices.Length * indexStride), IntPtr.Zero,
BufferUsageHint.StreamDraw);
GL.BufferSubData(BufferTarget.ElementArrayBuffer, (IntPtr)0,
(IntPtr)(indices.Length * indexStride), indices);
}
}
I have also tried many variations, but the result was always the same,
nothing got drawn. GL.GetError() returned NoError at this setup.

No comments:

Post a Comment