【发布时间】:2020-07-16 17:34:51
【问题描述】:
我正在尝试使用 C# 和 OpenGL 显示一个四边形(或两个三角形)。下面是我的网格类的代码。在其中,您既有 VBO 和 VAO 的创建,也有我用来渲染网格的函数。
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);
}
}
}
这里是我调用函数来渲染网格的地方。
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”类型的未处理异常 尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
【问题讨论】: