Why doesn't this work? Launching the
Why doesn't this work? Launching the program in RenderDoc and capturing a frame results in an error:
Incorrect API Use: Vertex buffer Buffer 50 bound to attribute 0: aPos (buffer slot 0) at draw is 0-sized!
Has this buffer been initialized?
1 Reply
BufferObject class:
VAO class:
Mesh class:
public class BufferObject<TType> where TType : unmanaged
{
private uint handleID;
private BufferTargetARB bufferType;
public unsafe BufferObject(TType[] data, BufferTargetARB bufferType, BufferUsageARB bufferUsage)
{
this.bufferType = bufferType;
handleID = gl.GenBuffer();
fixed (void* dataPointer = data)
{
gl.BufferData(this.bufferType, (nuint)(data.Length * sizeof(TType)), dataPointer, bufferUsage);
}
}
public unsafe BufferObject(List<TType> data, BufferTargetARB bufferType, BufferUsageARB bufferUsage)
{
this.bufferType = bufferType;
handleID = gl.GenBuffer();
fixed (void* dataPointer = data.ToArray())
{
gl.BufferData(bufferType, (nuint)(data.Count * sizeof(TType)), dataPointer, bufferUsage);
logger.Information("Buffer {bufferID} data: {data}", handleID, data);
}
}
public void Bind()
{
gl.BindBuffer(bufferType, handleID);
}
public void Unbind()
{
gl.BindBuffer(bufferType, 0);
}
public void Dispose()
{
gl.DeleteBuffer(handleID);
}
}
public class BufferObject<TType> where TType : unmanaged
{
private uint handleID;
private BufferTargetARB bufferType;
public unsafe BufferObject(TType[] data, BufferTargetARB bufferType, BufferUsageARB bufferUsage)
{
this.bufferType = bufferType;
handleID = gl.GenBuffer();
fixed (void* dataPointer = data)
{
gl.BufferData(this.bufferType, (nuint)(data.Length * sizeof(TType)), dataPointer, bufferUsage);
}
}
public unsafe BufferObject(List<TType> data, BufferTargetARB bufferType, BufferUsageARB bufferUsage)
{
this.bufferType = bufferType;
handleID = gl.GenBuffer();
fixed (void* dataPointer = data.ToArray())
{
gl.BufferData(bufferType, (nuint)(data.Count * sizeof(TType)), dataPointer, bufferUsage);
logger.Information("Buffer {bufferID} data: {data}", handleID, data);
}
}
public void Bind()
{
gl.BindBuffer(bufferType, handleID);
}
public void Unbind()
{
gl.BindBuffer(bufferType, 0);
}
public void Dispose()
{
gl.DeleteBuffer(handleID);
}
}
public class VAO<TVboType, TEboType> where TVboType : unmanaged where TEboType : unmanaged
{
private uint handleID;
public VAO(BufferObject<TVboType> VBO, BufferObject<TEboType> EBO)
{
handleID = gl.GenVertexArray();
}
public unsafe void Attributes(BufferObject<TVboType> vbo, uint layout, uint numComponents, VertexAttribPointerType type, uint vertexSize, uint offset)
{
vbo.Bind();
gl.VertexAttribPointer(layout, (int)numComponents, type, false, (uint)(vertexSize * sizeof(TVboType)), (void*)offset);
gl.EnableVertexAttribArray(layout);
vbo.Unbind();
}
public void Bind()
{
gl.BindVertexArray(handleID);
}
public void Unbind()
{
gl.BindVertexArray(0);
}
public void Dispose()
{
gl.DeleteVertexArray(handleID);
}
}
public class VAO<TVboType, TEboType> where TVboType : unmanaged where TEboType : unmanaged
{
private uint handleID;
public VAO(BufferObject<TVboType> VBO, BufferObject<TEboType> EBO)
{
handleID = gl.GenVertexArray();
}
public unsafe void Attributes(BufferObject<TVboType> vbo, uint layout, uint numComponents, VertexAttribPointerType type, uint vertexSize, uint offset)
{
vbo.Bind();
gl.VertexAttribPointer(layout, (int)numComponents, type, false, (uint)(vertexSize * sizeof(TVboType)), (void*)offset);
gl.EnableVertexAttribArray(layout);
vbo.Unbind();
}
public void Bind()
{
gl.BindVertexArray(handleID);
}
public void Unbind()
{
gl.BindVertexArray(0);
}
public void Dispose()
{
gl.DeleteVertexArray(handleID);
}
}
public unsafe class Mesh
{
private BufferObject<float> VBO;
private BufferObject<uint> EBO;
private VAO<float, uint> VAO;
private List<float> vertices;
private List<uint> indices;
public Mesh(List<float> vertices, List<uint> indices)
{
this.vertices = vertices;
this.indices = indices;
VAO = new VAO<float, uint>(VBO, EBO);
VAO.Bind();
VBO = new BufferObject<float>(this.vertices, BufferTargetARB.ArrayBuffer, BufferUsageARB.StaticDraw);
VBO.Bind();
EBO = new BufferObject<uint>(this.indices, BufferTargetARB.ElementArrayBuffer, BufferUsageARB.StaticDraw);
EBO.Bind();
VAO.Attributes(VBO, 0, 2, VertexAttribPointerType.Float, 2, 0 * sizeof(float));
/*VBO.Unbind();
EBO.Unbind();
VAO.Unbind();*/
}
public void Render()
{
VBO.Bind();
EBO.Bind();
gl.DrawElements(PrimitiveType.Triangles, (uint)indices.Count, DrawElementsType.UnsignedInt, null);
}
public void Dispose()
{
VBO.Dispose();
VAO.Dispose();
EBO.Dispose();
}
}
public unsafe class Mesh
{
private BufferObject<float> VBO;
private BufferObject<uint> EBO;
private VAO<float, uint> VAO;
private List<float> vertices;
private List<uint> indices;
public Mesh(List<float> vertices, List<uint> indices)
{
this.vertices = vertices;
this.indices = indices;
VAO = new VAO<float, uint>(VBO, EBO);
VAO.Bind();
VBO = new BufferObject<float>(this.vertices, BufferTargetARB.ArrayBuffer, BufferUsageARB.StaticDraw);
VBO.Bind();
EBO = new BufferObject<uint>(this.indices, BufferTargetARB.ElementArrayBuffer, BufferUsageARB.StaticDraw);
EBO.Bind();
VAO.Attributes(VBO, 0, 2, VertexAttribPointerType.Float, 2, 0 * sizeof(float));
/*VBO.Unbind();
EBO.Unbind();
VAO.Unbind();*/
}
public void Render()
{
VBO.Bind();
EBO.Bind();
gl.DrawElements(PrimitiveType.Triangles, (uint)indices.Count, DrawElementsType.UnsignedInt, null);
}
public void Dispose()
{
VBO.Dispose();
VAO.Dispose();
EBO.Dispose();
}
}