【发布时间】:2015-03-26 18:19:53
【问题描述】:
我正在编写一个简单的场景图,并且我有一个网格类来跟踪顶点数组对象、顶点缓冲区对象和索引缓冲区对象。
当我初始化网格对象并用数据填充它们时,似乎最后创建的顶点缓冲区是绘制时使用的缓冲区,即使我绑定了不同的顶点数组对象也是如此。
渲染时,如果我只绑定顶点数组,除了最后一个初始化的网格之外的任何网格都具有最后一个初始化网格的顶点(但正确的索引,使它们看起来不对)。
我让它工作的唯一方法是在我绑定顶点数组对象之后再次绑定顶点缓冲区对象。这与我在任何地方阅读的每个文档或评论相矛盾。
// Geometry class
class Geometry
{
public override void Render()
{
this.Mesh.bind(); // bind mesh
this.Material.bind(); // bind shader. This also uses attribute locations stored in the shader object to enable vertex attributes
this.Data.Render();
this.Mesh.unbind(); // bind 0 to vertex array
this.Material.unbind(); // use program 0
}
}
// base mesh class
class Mesh
{
public override void init()
{
// ... truncated code for creating vertices ..
vertexArray = GL.GenVertexArray();
GL.BindVertexArray(vertexArray);
GL.GenBuffers(2, buffers);
GL.BindBuffer(BufferTarget.ArrayBuffer, buffers[0]);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * Vertex.SizeInBytes), vertices, BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, buffers[1]);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Count * sizeof(uint)), indices.ToArray(), BufferUsageHint.StaticDraw);
numPoints = vertices.Length;
numIndices = indices.Count;
GL.BindVertexArray(0);
}
public void bind()
{
GL.BindVertexArray(vertexArray);
// I had to add this line to make meshes render correctly
GL.BindBuffer(BufferTarget.ArrayBuffer, buffers[0]);
}
public void unbind()
{
GL.BindVertexArray(0);
}
public override void Render()
{
GL.DrawElements(PrimitiveType.Triangles, numIndices, DrawElementsType.UnsignedInt, IntPtr.Zero);
}
public override void Update(float t, float deltaT, int xDelta, int yDelta, int zDelta, MouseState mouse, KeyboardState keyboard)
{
}
}
【问题讨论】: