سؤال

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.

EDIT The lengths of both arrays, vertices and indices, varry on each update.

هل كانت مفيدة؟

المحلول

Ok, i found out you cannot change the size of an openGL buffer once allocated. However, you can use openCL to manage your buffers, if the right extensions are available. This helped me a lot to reduce the memory footprint of my Terrain solution.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top