flopoloco's picture

I need clarification about BufferSubData

Hello,

I have this code.

float[] vertices = {
	 0.0f,  0.5f, 1.0f, 0.0f, 0.0f,
	 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
	-0.5f, -0.5f, 0.0f, 0.0f, 1.0f
};
 
// Create the buffer
int vbo = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData(
	BufferTarget.ArrayBuffer,
	(IntPtr)(vertices.Length * sizeof(float)),
	vertices, BufferUsageHint.StaticDraw
);

How can I use BufferSubData instead to declare the data like this?

float[] vertPosition = {
	 0.0f, 0.5f,
	 0.5f, -0.5f,
	-0.5f, -0.5f
};
 
float[] vertColor = {
	1.0f, 0.0f, 0.0f,
	0.0f, 1.0f, 0.0f,
	0.0f, 0.0f, 1.0f
};

Comments

Comment viewing options

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

You need to call BufferData to allocate the storage you need and BufferSubData to upload your data:

var offsetPosition = 0;
var sizePosition = vertPosition.Length * sizeof(float);
var offsetColor = sizePosition;
var sizeColor = vertColor.Length * sizeof(float);
var totalSize = sizePosition + sizeColor;
 
GL.BufferData(
        BufferTarget.ArrayBuffer,
	(IntPtr)totalSize,
        IntPtr.Zero, // allocate storage but don't upload data
        BufferUsageHint.StaticDraw);
 
GL.BufferSubData(
        BufferTarget.ArrayBuffer,
	(IntPtr)offsetPosition,
        (IntPtr)sizePosition,
        vertPosition);
 
GL.BufferSubData(
        BufferTarget.ArrayBuffer,
	(IntPtr)offsetColor,
        (IntPtr)sizeColor,
        vertColor);

Note that I haven't tested this code with a compiler. It is based on these:
http://www.opengl.org/sdk/docs/man/xhtml/glBufferData.xml
http://www.opengl.org/sdk/docs/man/xhtml/glBufferSubData.xml

Edit: there are supposed to be overloads for passing int directly, rather than casting to IntPtr. Will have to double-check this.