[英]C# Attempted to read or write protected memory. OpenGL/OpenTK libraries
[英]OpenGL / OpenTK Drawing with Indices : Attempted to Read or Write Protected Memory Issue
我正在嘗試使用 C# 和 OpenGL 顯示一個四邊形(或兩個三角形)。 下面是我的網格 class 的代碼。 在其中您可以創建 VBO 和 VAO,以及我用來渲染網格的 function。
using System;
using System.Collections.Generic;
using System.Text;
using OpenTK.Graphics.OpenGL;
using BuildMe.Core;
using OpenTK;
namespace BuildMe.Render
{
class Mesh
{
private Vector3[] verts;
private uint[] indices;
private int instances;
private int VAO;
public uint[] Indices { get => indices; set => indices = value; }
public int Instances { get => instances; set => instances = value; }
public Vector3[] Verts { get => verts; set => verts = value; }
public Mesh(Vector3[] verts, uint[] indices, int instances)
{
this.verts = verts;
this.indices = indices;
this.instances = instances;
this.VAO = CreateVAO();
StoreVAOData();
}
private int CreateVAO()
{
int VAO = GL.GenVertexArray();
RenderLoop.VAOs.Add(VAO);
return (VAO);
}
private void StoreVAOData()
{
GL.BindVertexArray(VAO);
LoadVerts();
LoadIndices();
GL.BindVertexArray(0);
}
private void LoadVerts()
{
int vbo = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData(BufferTarget.ArrayBuffer, Vector3.SizeInBytes * verts.Length, verts, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
RenderLoop.VBOs.Add(vbo);
}
private void LoadIndices()
{
int vbo = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo);
GL.BufferData(BufferTarget.ElementArrayBuffer, sizeof(int) * indices.Length, indices, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(0, 1, VertexAttribPointerType.UnsignedInt, false, sizeof(int), 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
RenderLoop.VBOs.Add(vbo);
}
public int GetVAO()
{
return (VAO);
}
public void Render()
{
GL.BindVertexArray(this.GetVAO());
GL.EnableVertexAttribArray(0);
GL.EnableVertexAttribArray(1);
GL.DrawElements(PrimitiveType.Triangles, this.Indices.Length, DrawElementsType.UnsignedInt, 0);
GL.DisableVertexAttribArray(0);
GL.DisableVertexAttribArray(1);
GL.BindVertexArray(0);
}
}
}
這是我調用 function 來渲染網格的地方。
private void Render(object sender, FrameEventArgs e)
{
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.IndexArray);
GL.Color3(Color.Green);
foreach (Mesh mesh in SceneMeshes)
mesh.Render();
}
如果有幫助,這是我遇到的錯誤。 但我認為這只是意味着我要么聲明了 VBO 錯誤,要么我使用了錯誤的函數來渲染它。
OpenTK.dll 中發生了“System.AccessViolationException”類型的未處理異常嘗試讀取或寫入受保護的 memory。 這通常表明其他 memory 已損壞。
EnableClientState
和VertexAttribPointer
不一起交互。 如果要使用客戶端功能,則必須使用VertexPointer
(請參閱glVertexPointer
):
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0);
GL.VertexPointer(3, VertexAttribPointerType.Float, Vector3.SizeInBytes, 0);
GL.EnableClientState(ArrayCap.IndexArray);
不符合您的預期。 ArrayCap.IndexArray
用於顏色索引屬性。
索引緩沖區綁定到目標BufferTarget.ElementArrayBuffer
。 它不是一個屬性,也不必啟用。
此外,索引緩沖區在頂點數組 object 中說明。 指令GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
會破壞綁定:
private void LoadIndices()
{
int ibo = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo);
GL.BufferData(BufferTarget.ElementArrayBuffer, sizeof(int) * indices.Length, indices, BufferUsageHint.StaticDraw);
// GL.VertexAttribPointer(...) <--- REMOVE
// GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); // <--- REMOVE
RenderLoop.VBOs.Add(ibo);
}
客戶端功能在頂點數組 Object中說明。 因此可以在LoadVerts()
中說明:
private void LoadVerts()
{
int vbo = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData(BufferTarget.ArrayBuffer, Vector3.SizeInBytes * verts.Length, verts, BufferUsageHint.StaticDraw);
// GL.VertexAttribPointer(...); <---- REMOVE
GL.VertexPointer(3, VertexAttribPointerType.Float, Vector3.SizeInBytes, 0); // <--- ADD
GL.EnableClientState(ArrayCap.VertexArray); // <--- ADD
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
RenderLoop.VBOs.Add(vbo);
}
頂點數組 Object 中說明了所有必要的規范,因此在繪圖調用之前綁定 VAO 就足夠了:
public void Render()
{
GL.BindVertexArray(this.GetVAO());
GL.DrawElements(PrimitiveType.Triangles, this.Indices.Length, DrawElementsType.UnsignedInt, 0);
GL.BindVertexArray(0);
}
private void Render(object sender, FrameEventArgs e)
{
GL.Color3(Color.Green);
foreach (Mesh mesh in SceneMeshes)
mesh.Render();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.