OpenTK - VBO和Mesh,内存异常

时间:2013-07-26 00:53:58

标签: c# opengl opentk

我有一个班级,我正试图用它渲染一个三角形,但它一直在失败,我不知道为什么,有人能发现问题吗?我已经尝试过使用静态和动态vbo,但我一直遇到内存问题。

我收到的错误是:

” OpenTK.dll

中出现未处理的“System.AccessViolationException”类型异常

附加信息:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。 “

我的用法是:

        var m = new Mesh();

        m.Vertices = new float[] { 
            0, 0, 0, 
            5, 0, 0, 
            5, 5, 0
        };


        m._Indices = new ushort[] { 0, 1, 2 };
        m.Init();

我的班级是

namespace lolGL
{
    using System;
    using OpenTK.Graphics.OpenGL;
    using OpenTK;

    public class Mesh : Entity
    {
        public ushort[] _Indices = null;

        int m_vertexBuffer = 0;
        int m_indexBuffer = 0;

        bool use16BitIndices
        {
            get { return Vertices.Length <= 65536; }
        }

        public Mesh() { }

        public void Init()
        {
            GL.GenBuffers(1, out m_vertexBuffer);
            GL.BindBuffer(BufferTarget.ArrayBuffer, m_vertexBuffer);
            GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(BlittableValueType.StrideOf<float>(Vertices) * Vertices.Length), Vertices, BufferUsageHint.StaticDraw);

            GL.GenBuffers(1, out m_indexBuffer);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, m_indexBuffer);

            int indexSize = use16BitIndices ? sizeof(short) : sizeof(int);

            GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(indexSize * _Indices.Length), _Indices, BufferUsageHint.StaticDraw);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
        }

        public override void Render(OpenTK.FrameEventArgs e)
        {
            GL.BindBuffer(BufferTarget.ArrayBuffer, m_vertexBuffer);
            GL.BindBuffer(BufferTarget.ArrayBuffer, m_indexBuffer);

            GL.EnableClientState(ArrayCap.VertexArray);
            GL.VertexPointer(3, VertexPointerType.Float, Vector3.SizeInBytes, 0);

            if (use16BitIndices)
                GL.DrawElements(BeginMode.TriangleStrip, _Indices.Length, DrawElementsType.UnsignedShort, 0);
            else
                GL.DrawElements(BeginMode.TriangleStrip, _Indices.Length, DrawElementsType.UnsignedInt, 0);

            GL.Disable(EnableCap.VertexArray);

            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

        }

        public override void EmptyBuffer()
        {
            Vertices = null;
            _Indices = null;
        }

        public override void Dispose()
        {
            EmptyBuffer();
        }

        public override void Delete()
        {
            if (m_indexBuffer != 0)
                GL.DeleteBuffers(1, ref m_indexBuffer);

            if (m_vertexBuffer != 0)
                GL.DeleteBuffers(1, ref m_vertexBuffer);

            Dispose();
        }

        public override void ApplyColorMap(int[] colors)
        {
            throw new NotImplementedException();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在Render函数中,您将索引缓冲区绑定到错误的目标。

GL.BindBuffer(BufferTarget.ArrayBuffer, m_indexBuffer);

应该是:

GL.BindBuffer(BufferTarget.ElementArrayBuffer, m_indexBuffer);

根据你最后解除束缚的方式,这可能只是一个错字。

另外,不相关但你用use16BitIndices做的整件事是错误的。重要的不是数组的长度,而是数组中每个元素的大小。如果您的65536 顶点超过,则需要索引值大于65536的索引(ushort无法处理)。在这种情况下,您需要上传int[]