我正在OpenTK中绘制一个对象,该对象在每一帧中都保持变化的位置。现在,我继续通过调用GL.BufferData
来更改位置数组并缓冲新数据。有没有一种方法可以更新或映射到一个缓冲区数组,而不必在每个帧中都新建一个?我注意到我最终得到Out Of Memory
例外。
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);
}
初始化时一次创建Vertex Array Object和Vertex Buffer Object:
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);
}
通过glBufferSubData
在每帧中更新缓冲区:
glBufferSubData