kkc0923's picture

How to write buffer data like streamwriter? (Solved)

Hi, Im working now for converting SharpDX project to OpenTK project. Im very newbie on OpenGL, and I have question for data transfering to Buffer.

In SharpDX, there is DataStream class for Buffer and It provides Write() method for write data to buffer stream.
I have quite big mesh data (over 200000 vertices), so I want to save my memory. But in OpenTK, it has only BufferData method and it needs original data array. I checked MapBuffer, but it returns only IntPtr and needs Unsafe oprtions.

Is there any method for creating buffer without data array, I mean like stream writing method without Unsafe?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

You can use BufferSubData or MapBufferRange to implement streaming updates. OpenTK itself does not offer an OO abstraction over OpenGL*, but implementing a DataStream-like interface is relatively straightforward.

For MapBufferRange, you can use the Marshal.Write* family of functions to avoid unsafe code if necessary.

For BufferSubData, you can use the ref T overload to specify an offset into the input buffer. For example:

var data = new Vertex[...];
GL.BufferSubData(..., ref data[100]); // start copying from the 100th vertex

The plain array overload is equivalent to an offset of 0:

GL.BufferSubData(..., data);
GL.BufferSubData(..., ref data[0]);

Keep in mind that you need to call BufferData once before calling BufferSubData. Pass IntPtr.Zero to BufferData to allocate an empty buffer of the required size and call BufferSubData as necessary to fill that buffer.

* The reason is simply that OpenGL < v4.5 is not an OO API. Generic OO abstractions are bound to fail in multiple ways.

kkc0923's picture