0

有没有一种方法可以使用单个缓冲区渲染未连接到教其他行的单独行的缓冲区?

目前我正在为每条线创建一个 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();
    }
}
4

1 回答 1

1

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

是的,您可以在一个大型 VBO 中为任意数量的单独行打包顶点(每行两个顶点)。它的工作原理类似于GL_TRIANGLESor GL_QUADS。只需确保使用GL_LINES(允许单独的线条)而不是GL_LINE_LOOP.

阅读OpenGL wiki 上的部分GL_LINES

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

于 2013-07-30T01:49:42.190 回答