I am drawing an object in OpenTK that keeps changing position in each frame. For now, I keep changing the position array and buffering the new data by calling GL.BufferData
. Is there a way where I can update or map to one buffer array without making a new one in every frame? I noticed that I eventually get Out Of Memory
exception.
private void Drawobject(Vector3 cursorPos)
{
float[] cursors = new float[] { cursorPos.X-0.01f , cursorPos.Y-0.01f - 0.2f, cursorPos.Z, cursorPos.X-0.01f, cursorPos.Y-0.01f + 0.2f, cursorPos.Z, cursorPos.X-0.01f - 0.2f, cursorPos.Y-0.01f, cursorPos.Z, cursorPos.X-0.01f + 0.2f, cursorPos.Y-0.01f, cursorPos.Z };
int VertexBufferCursor, _vao;
VertexBufferCursor = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferCursor);
GL.BufferData(BufferTarget.ArrayBuffer, cursors.Count() * sizeof(float), cursors, BufferUsageHint.StaticDraw);
_vao = GL.GenVertexArray();
GL.BindVertexArray(_vao);
GL.EnableVertexAttribArray(0);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindVertexArray(0);
shader4.Use();
GL.BindVertexArray(_vao);
GL.DrawArrays(PrimitiveType.Lines, 0, 4);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindVertexArray(0);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
Drawobject(currentMousePos);
}
Create the Vertex Array Object and the Vertex Buffer Object once at initialization:
private int VertexBufferCursor;
private int _vao;
private void Init(Vector3 cursorPos)
{
float[] cursors = new float[] { cursorPos.X-0.01f , cursorPos.Y-0.01f - 0.2f, cursorPos.Z, cursorPos.X-0.01f, cursorPos.Y-0.01f + 0.2f, cursorPos.Z, cursorPos.X-0.01f - 0.2f, cursorPos.Y-0.01f, cursorPos.Z, cursorPos.X-0.01f + 0.2f, cursorPos.Y-0.01f, cursorPos.Z };
VertexBufferCursor = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferCursor);
GL.BufferData(BufferTarget.ArrayBuffer, cursors.Count() * sizeof(float), cursors, BufferUsageHint.StaticDraw);
_vao = GL.GenVertexArray();
GL.BindVertexArray(_vao);
GL.EnableVertexAttribArray(0);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindVertexArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
}
Update the buffer in every frame by glBufferSubData
:
private void Drawobject(Vector3 cursorPos)
{
float[] cursors = new float[] { cursorPos.X-0.01f , cursorPos.Y-0.01f - 0.2f, cursorPos.Z, cursorPos.X-0.01f, cursorPos.Y-0.01f + 0.2f, cursorPos.Z, cursorPos.X-0.01f - 0.2f, cursorPos.Y-0.01f, cursorPos.Z, cursorPos.X-0.01f + 0.2f, cursorPos.Y-0.01f, cursorPos.Z };
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferCursor);
GL.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, cursors.Count() * sizeof(float), cursors);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
shader4.Use();
GL.BindVertexArray(_vao);
GL.DrawArrays(PrimitiveType.Lines, 0, 4);
GL.BindVertexArray(0);
}