russell's picture

VBO DrawArrays

I want to draw a lot of lines by points using VBOs. I use following code:

 public void DrawLineVBO(List<Point> points, Color color)
{
	GL.Color3(color);
	int[] vertices = new int[points.Count * 2];
 
	int i = 0;
	foreach (Point point in points)
	{
		vertices[i] = point.X;
		vertices[++i] = point.Y;
		i++;
	}
 
	GL.VertexPointer(2, VertexPointerType.Int, 0, vertices);
	GL.EnableClientState(EnableCap.VertexArray);
	GL.DrawArrays(BeginMode.Lines, 0, vertices.Length);
	GL.DisableClientState(EnableCap.VertexArray);
}

If I call this code one time it works ok. But if I call this code many times (>10) I get AccessViolationException: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." How can I solve this problem?


Comments

Comment viewing options

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

VBOs do not work like that. You need to perform the following steps:

  1. generate a new VBO id (GenBuffers)
  2. bind it and upload your vertex data (BindBuffer and BufferData)
  3. bind it, specify the vertex format and render (BindBuffer, VertexPointer/NormalPointer/*Pointer, DrawArrays)

You only need to perform steps 1 and 2 once, when you create the VBO. Step 3 should be done whenever you render.