2013-07-26 61 views
0

我有一個類,我試圖渲染一個三角形,但它保持失敗,我不知道爲什麼,任何人都可以發現問題?我試過使用靜態和動態vbo,但我不斷收到一個內存問題。OpenTK - VBO和網格,內存異常

收到錯誤是:

「 類型‘System.AccessViolationException’的未處理的異常發生在OpenTK.dll

額外的資料:嘗試讀取或寫入受保護的存儲器這通常一個。指示其他內存已損壞 「

我的用法是:

 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

在渲染函數中,您將索引緩衝區綁定到錯誤的目標。

GL.BindBuffer(BufferTarget.ArrayBuffer, m_indexBuffer); 

應該是:

GL.BindBuffer(BufferTarget.ElementArrayBuffer, m_indexBuffer); 

基於你在年底解除綁定的方式,這可能只是一個錯字。

此外,無關,但你正在做的整個事情use16BitIndices是錯誤的。這不是數組的長度,它是數組中每個元素的大小。如果你有超過65536頂點,你將需要大於65536(其中ushort無法處理)的值。在這種情況下,您需要上傳int[]

+0

關於另一個問題:請不要[批准反引號而不是代碼編輯](http://stackoverflow.com/review/suggested-edits/2600211),請參閱[here](http://meta.gaming.stackexchange.com/q/7437/88) –

+0

@TobiasKienzler我不好,我會在將來更加小心這些編輯。 –

+0

感謝您的理解:) –