0

Is there a way i can render a buffer of individual lines that are not connected to teach other, using a single buffer?

At the moment i am creating one VBO for each line and I'm trying to render thousands of lines but and i don't think What I'm doing is correct, can somebody provide a solution, at the present time this is my line rendering code:

Regards!

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();
    }
}
Dean
  • 499
  • 6
  • 13
  • 34

1 Answers1

1

Creating thousands of VBOs, or issuing thousands of draw calls is rarely a good idea.

Yes, you can pack vertices in a single large VBO for as many number of individual lines as you want (two vertices per line). It works similar to GL_TRIANGLES or GL_QUADS. Just make sure to draw using GL_LINES (which allows individual lines) rather than GL_LINE_LOOP.

Read the section on GL_LINES on the OpenGL wiki

"Vertices 0 and 1 are considered a line. Vertices 2 and 3 are considered a line. And so on. If the user specifies a non-even number of vertices, then the extra vertex is ignored."

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Justin Meiners
  • 10,754
  • 6
  • 50
  • 92