我试图显示四(或两个三角形)与C#和OpenGL。下面是我为我的网类代码。在它,你必须兼具创造驻维也纳各组织和维罗的,以及我用来呈现网格的功能。
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();
}
我得到的错误是,如果有帮助。但我认为这只是意味着我要么宣布维也纳各组织的错误或我使用了错误的功能来呈现它。
An unhandled exception of type 'System.AccessViolationException' occurred in OpenTK.dll
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
提前致谢。
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
。它不是一个属性,也没有启用。
进一步更索引缓冲器是由顶点数组对象说明。该指令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);
}
该客户端能力的陈述顶点数组对象。因此,它可以在规定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);
}
所有必要的规范的顶点数组对象陈述,所以它足以结合的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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句