Skip to content

4. Initializing a vertex buffer

theproadam edited this page Apr 23, 2020 · 4 revisions

To maximize performance, renderXF uses its own unmanaged buffers. To make things easier, renderXF accepts a pre existing float array which stores the vertex data.

Creating a Buffer

//Load vertex data into an array
float[] vertexpoints = LoadVertexData();

//Create a buffer compatible with renderXF
GLBuffer myBuffer = new GLBuffer(vertexpoints, Stride, MemoryLocation.Heap);
WARNING: It is extremely important to make sure the stride value is correct. If it is not, renderXF may crash.

Deleting a Buffer

GLBuffers will self dispose, however this may take up to a few seconds after runtime. To dispose instantly, just call the Dispose() function.

if (myBuffer != null)
    myBuffer.Dispose();

Memory Location

renderXF can store the buffer in the heap, or the stack. Storing the buffer on the stack is experimental and is easily subject to random stack overflow errors. It is recommended to store all buffers in heap memory.

//Buffer allocated in the heap
GLBuffer heap_Buffer = new GLBuffer(vertexpoints, Stride, MemoryLocation.Heap);

//Buffer allocated in the stack
GLBuffer stackBuffer = new GLBuffer(vertexpoints, Stride, MemoryLocation.Stack);
The only reason this option exists was to test the speed differences, to which there are none.

Getting the raw memory address

It is also possible to get the true memory address via the GetAddress() function.

IntPtr addr = myBuffer.GetAddress();
float* fldr = (float*)myBuffer.GetAddress();

This can be useful for things such as creating a separate normal buffer, then getting its its normal values per face via float pointer.

Clone this wiki locally