【问题标题】:Updating bufferarray every frame OpenTK每帧更新bufferarray OpenTK
【发布时间】:2020-09-02 13:27:38
【问题描述】:

我正在 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);
} 

【问题讨论】:

    标签: c# opengl vbo opentk vao


    【解决方案1】:

    在初始化时创建一次Vertex Array ObjectVertex 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更新每一帧的缓冲区:

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 2021-08-17
      • 1970-01-01
      • 2013-11-06
      • 1970-01-01
      相关资源
      最近更新 更多