OpenTK (OpenGL) 如何正确使用多个对象的 BufferSubData 和 DrawElements

OpenTK (OpenGL) How to correctly use BufferSubData and DrawElements with multiple Objects

我正在尝试实现一种渲染器,它仅使用一个 VBO 和一个 EBO 来存储所有对象顶点和索引。 为此,我找了一些教程,终于得出了一个不错的结果。

问题是它只能与 ONE 单个 对象一起正常工作。一旦我想添加另一个,渲染就会显示出奇怪的行为。

你能帮我解决这个问题吗?

您可以在此处找到完整代码:https://github.com/BanditBloodwyn/TerritorySimulator。 重要的 类 是:

这是渲染器中的初始化方法:

    public void Initialize(GLShape[] shapeArray)
    {
        Shapes = shapeArray;

        GL.Enable(EnableCap.DepthTest);
        GL.ClearColor(0.0f, 0.0f, 0.10f, 1.0f);

        InitializeBuffers(Shapes);
        InitializeVertexArrayObject(Shapes);
        SetupShader();
        BindBuffers();
    }

子方法如下所示。

    private void InitializeBuffers(GLShape[] shapeArray)
    {
        int vertexBufferSize = shapeArray.Sum(shape => shape.VertexBufferSize);
        int indexBufferSize = shapeArray.Sum(shape => shape.IndexBufferSize);

        // Vertex buffer
        vertexBufferObject = GL.GenBuffer();
        GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferObject);
        GL.BufferData(BufferTarget.ArrayBuffer, vertexBufferSize, (IntPtr)0, BufferUsageHint.StaticDraw);

        IntPtr offset = (IntPtr)0;
        foreach (GLShape shape in shapeArray)
        {
            GL.BufferSubData(BufferTarget.ArrayBuffer, offset, shape.VertexBufferSize, shape.Vertices);
            offset += shape.VertexBufferSize;
        }

        // Element buffer
        elementBufferObject = GL.GenBuffer();
        GL.BindBuffer(BufferTarget.ElementArrayBuffer, elementBufferObject);
        GL.BufferData(BufferTarget.ElementArrayBuffer, indexBufferSize, (IntPtr)0, BufferUsageHint.StaticDraw);

        offset = (IntPtr)0;
        foreach (GLShape shape in shapeArray)
        {
            GL.BufferSubData(BufferTarget.ElementArrayBuffer, offset, shape.IndexBufferSize, shape.Indices);
            offset += shape.IndexBufferSize;
        }
    }

    private void InitializeVertexArrayObject(GLShape[] shapeArray)
    {
        foreach (GLShape shape in shapeArray)
        {
            shape.VertexArrayObject = GL.GenVertexArray();
            GL.BindVertexArray(shape.VertexArrayObject);
        }
    }

    private void SetupShader()
    {
        // shader
        string vertexPath = Path.Combine(Environment.CurrentDirectory, @"GLSL\", "Vertex.vert");
        string fragmentPath = Path.Combine(Environment.CurrentDirectory, @"GLSL\", "Fragment.frag");
        shader = new Shader(vertexPath, fragmentPath);
        shader.Use();

        int vertexLocation = shader.GetAttribLocation("aPosition");
        GL.EnableVertexAttribArray(vertexLocation);
        GL.VertexAttribPointer(
            vertexLocation, 
            3, 
            VertexAttribPointerType.Float, 
            false, 
            5 * sizeof(float), 
            0);

        int texCoordLocation = shader.GetAttribLocation("aTexCoord");
        GL.EnableVertexAttribArray(texCoordLocation);
        GL.VertexAttribPointer(
            texCoordLocation, 
            2, 
            VertexAttribPointerType.Float, 
            false, 
            5 * sizeof(float), 
            3 * sizeof(float));

        shader.SetInt("texture0", 0);
        shader.SetInt("texture1", 1);
    }

    private void BindBuffers()
    {
        GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferObject);
        GL.BindBuffer(BufferTarget.ElementArrayBuffer, elementBufferObject);
    }

渲染函数本身看起来像这样。

    public void Render()
    {
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

        if (Shapes == null || Shapes.Length == 0)
            return;

        IntPtr offset = (IntPtr)0;
        foreach (GLShape shape in Shapes)
        {
            foreach (var texture in shape.Textures)
            {
                if (LayerConfiguration.ShowEarthTexture)
                    texture.Key.Use(texture.Value);
                else
                    texture.Key.MakeTransparent(texture.Value);
            }

            ApplyModelTransforms(shape, out Matrix4 model);
            shader.SetMatrix4("model", model);
            shader.SetMatrix4("view", Camera.GetViewMatrix());

            GL.DrawElements(PrimitiveType.Triangles, shape.Indices.Length, DrawElementsType.UnsignedInt, offset);
            offset += shape.IndexBufferSize;
        }

        shader.SetMatrix4("projection", Camera.GetProjectionMatrix());
        shader.Use();
    }

As soon as I want to add another one, the rendering shows weird behavior

当然,因为第二个和后面的对象的索引是错误的。您需要将先前网格的顶点之和添加到索引中。对于第一个网格的索引,您必须添加 0,对于第二个网格的索引,您必须添加第一个网格的顶点数,对于第三个网格的索引,您必须添加顶点的总和第一个和第二个网格,...

offset = (IntPtr)0;
uint firstVertexIndex = 0;
foreach (GLShape shape in shapeArray)
{
    var indexArray = shape.Indices.Select(index => index + firstVertexIndex).ToArray();
                
    GL.BufferSubData(
        BufferTarget.ElementArrayBuffer, offset, shape.IndexBufferSize, indexArray);
    offset += shape.IndexBufferSize;
    firstVertexIndex += (uint)(shape.VertexBufferSize / (5 * sizeof(float)));
}

完成方法InitializeBuffers:

private void InitializeBuffers(GLShape[] shapeArray)
{
    int vertexBufferSize = shapeArray.Sum(shape => shape.VertexBufferSize);
    int indexBufferSize = shapeArray.Sum(shape => shape.IndexBufferSize);

    // Vertex buffer
    vertexBufferObject = GL.GenBuffer();
    GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferObject);
    GL.BufferData(BufferTarget.ArrayBuffer, vertexBufferSize, (IntPtr)0, BufferUsageHint.StaticDraw);

    IntPtr offset = (IntPtr)0;
    foreach (GLShape shape in shapeArray)
    {
        GL.BufferSubData(BufferTarget.ArrayBuffer, offset, shape.VertexBufferSize, shape.Vertices);
        offset += shape.VertexBufferSize;
    }

    // Element buffer
    elementBufferObject = GL.GenBuffer();
    GL.BindBuffer(BufferTarget.ElementArrayBuffer, elementBufferObject);
    GL.BufferData(BufferTarget.ElementArrayBuffer, indexBufferSize, (IntPtr)0, BufferUsageHint.StaticDraw);

    offset = (IntPtr)0;
    uint firstVertexIndex = 0;
    foreach (GLShape shape in shapeArray)
    {
        var indexArray = shape.Indices.Select(index => index + firstVertexIndex).ToArray();
                
        GL.BufferSubData(BufferTarget.ElementArrayBuffer, offset, shape.IndexBufferSize, indexArray);
        offset += shape.IndexBufferSize;
        firstVertexIndex += (uint)(shape.VertexBufferSize / (5 * sizeof(float)));
    }
}