OpenGL:Gldrawelements给出了无与伦比的例外:System.AccessVioLationExcept



使用opentk与vb.net。

我的渲染方法:

 ' clear the screen
    GL.ClearColor(Color4.Purple)
    GL.Clear(ClearBufferMask.ColorBufferBit Or ClearBufferMask.DepthBufferBit)
    ' activate shader program and set uniforms
    shaderProgram.Use()
    projectionMatrix.Set(shaderProgram)
    ' bind vertex buffer and array objects
    vertexBuffer.Bind()
    vertexArray.Bind()
    ' upload vertices to GPU and draw them
    vertexBuffer.BufferData()
    vertexArray.enableAll()
    vertexBuffer.Draw()
    ' reset state for potential further draw calls (optional, but good practice)
    vertexArray.DisableAll()
    GL.BindVertexArray(0)
    GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0)
    GL.BindBuffer(BufferTarget.ArrayBuffer, 0)
    GL.UseProgram(0)
    ' swap backbuffer
    SwapBuffers()

所有这些函数都是抽象的,并在其中包含原始的gl {whting}命令。(是的,我无耻地复制了代码。)

我的Vertexbuffer的绘制代码:

Public Sub Bind()
    ' make this the active array buffer
    GL.BindBuffer(BufferTarget.ArrayBuffer, Me.handle)
End Sub
Public Sub BufferData()
    ' copy contained vertices to GPU memory
    GL.BufferData(BufferTarget.ArrayBuffer, New IntPtr(Me.vertexSize * Me.count), Me.vertices, BufferUsageHint.StreamDraw)
End Sub
Public Sub Draw()
    ' draw buffered vertices as triangles
    'GL.DrawArrays(PrimitiveType.Triangles, 0, Me.count)     <--commented
    GL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, 0) '<--line 44
End Sub

这给了我错误:

未经治疗的异常:system.AccessviolationException:尝试读取或写下受保护的内存。这通常表明其他记忆是损坏的。 在opentk.graphics.opengl.gl.drawelements(PrimitiveType模式,INT32计数,DawlelementStype类型, INT32索引) 在C: Users Tushar desktop genericgamedev-opentk-intro-Master vb vb src src ver中 TexBuffer.vb:第44行

我阅读的某个地方您需要在每次抽奖后都需要禁用Vertexarrays,所以我也这样做了,但无济于事。我看到了这个问题,但也没有帮助我(newbie)。

相反,在顶点缓冲区的绘制方法中使用gldrawarrays完美地呈现。但是我不想使用gldrawarrays。

Public Sub Draw()
    ' draw buffered vertices as triangles
    GL.DrawArrays(PrimitiveType.Triangles, 0, count)
    ' commented line -->  GL.DrawElements(PrimitiveType.Triangles, 5, DrawElementsType.UnsignedInt, 0)
End Sub

得到它!

感谢这位先生:Deathbyalgorithm

gldrawelements要求您指定索引缓冲区,这是我的代码所缺少的。因此,我创建了一个简单的IBO包装器:

Class IndexBuffer
    Private handle%
    Private data As UInteger()
    Sub New(ParamArray Indices As UInteger())
        handle = GL.GenBuffer()
        data = Indices
    End Sub
    Public Sub Bind()
        GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle)
    End Sub
    Public Sub BufferData()
        GL.BufferData(Of UInteger)(BufferTarget.ElementArrayBuffer, CType(Marshal.SizeOf(New UInteger) * data.Length, System.IntPtr), data, BufferUsageHint.StaticDraw)
    End Sub
End Class

,制作了一个字段" indexbuffer"在GameWindow课程中,并将其开始:

indexbuffer = New IndexBuffer({0, 1, 2, 3, 4, 5})

然后沿索引扣除数据并缓冲数据。我的最终渲染代码:

        ' clear the screen
    GL.ClearColor(Color4.Purple)
    GL.Clear(ClearBufferMask.ColorBufferBit Or ClearBufferMask.DepthBufferBit)
    ' activate shader program and set uniforms
    shaderProgram.Use()
    projectionMatrix.Set(shaderProgram)
    ' bind vertex buffer and array objects
    vbo.Bind()
    vertexArray.Bind()
    indexbuffer.Bind()
    ' upload vertices to GPU and draw them
    vbo.BufferData()
    indexbuffer.BufferData()
    GL.EnableClientState(ArrayCap.VertexArray) 'works without this line
    vbo.Draw()
    ' reset state for potential further draw calls (optional, but good practice)
    GL.BindVertexArray(0)
    GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0)
    GL.BindBuffer(BufferTarget.ArrayBuffer, 0)
    GL.DisableClientState(ArrayCap.VertexArray) 'works without this line
    GL.UseProgram(0)
    ' swap backbuffer
    SwapBuffers()

现在一切都起作用!

相关内容

  • 没有找到相关文章

最新更新