Opentk-opengl,在一个VBO中显示多条线,未连接



有没有一种方法可以使用单个缓冲区来呈现未连接到其他行的缓冲区?

目前,我正在为每行创建一个VBO,并试图渲染数千行,但我认为我所做的不正确,有人能提供一个解决方案吗?目前这是我的行渲染代码:

问候!

using OpenTK.Graphics.OpenGL;
using System;
using OpenTK;
using lolGL;
internal class Polyline : Entity
{
    ~Polyline()
    {
        EmptyBuffer();
    }
    public Polyline(float[] points)
    {
        this.vbo_size = points.Length;
        GL.GenBuffers(2, this.vbo_id);
        GL.BindBuffer(BufferTarget.ArrayBuffer, this.vbo_id[0]);
        GL.BufferData<float>(BufferTarget.ArrayBuffer, new IntPtr(points.Length * BlittableValueType.StrideOf<float>(points)), points, BufferUsageHint.StaticDraw);
        Vertices = points;
    }
    public override void ApplyColorMap(int[] colors)
    {
        GL.BindBuffer(BufferTarget.ArrayBuffer, this.vbo_id[1]);
        GL.BufferData<int>(BufferTarget.ArrayBuffer, new IntPtr(colors.Length * BlittableValueType.StrideOf<int>(colors)), colors, BufferUsageHint.StaticDraw);
        Colors = colors;
        this.HasColor = true;
    }
    public override void Render(FrameEventArgs e)
    {
        if (!this.Visible)
            return;
        GL.PointSize(this.PointSize);
        GL.EnableClientState(ArrayCap.VertexArray);
        GL.BindBuffer(BufferTarget.ArrayBuffer, this.vbo_id[0]);
        GL.VertexPointer(3, VertexPointerType.Float, Vector3.SizeInBytes, new IntPtr(0));
        if (this.HasColor)
        {
            GL.EnableClientState(ArrayCap.ColorArray);
            GL.BindBuffer(BufferTarget.ArrayBuffer, this.vbo_id[1]);
            GL.ColorPointer(4, ColorPointerType.UnsignedByte, 4, IntPtr.Zero);
        }
        GL.DrawArrays(BeginMode.Lines, 0, this.vbo_size);
        GL.DisableClientState(ArrayCap.VertexArray);
        GL.DisableClientState(ArrayCap.ColorArray);
        GL.DisableClientState(ArrayCap.IndexArray);
        GL.DisableClientState(ArrayCap.NormalArray);
    }
    public override void Dispose()
    {
        EmptyBuffer();
    }
    public override void EmptyBuffer()
    {
        Vertices = (float[])null;
        Colors = (int[])null;
    }
    public override void Delete()
    {
        GL.DeleteBuffers(vbo_id.Length, vbo_id);
        this.vbo_id = new int[2];
        Dispose();
    }
}

创建数千个VBO或发出数千个draw调用很少是个好主意。

是的,可以将顶点打包到单个大型VBO中,以容纳所需数量的单条线(每条线两个顶点)。它的工作原理类似于GL_TRIANGLESGL_QUADS。只需确保使用GL_LINES(允许单独的线条)而不是GL_LINE_LOOP绘制即可。

阅读OpenGL wiki 上关于GL_LINES的部分

"顶点0和1被视为直线。顶点2和3被视为一条直线。依此类推。如果用户指定了非偶数个顶点,则会忽略多余的顶点。"

最新更新