Reputation: 23
i want to use different arrays to store different data like the color postion and texture coords from my mesh but when i try and bind multiple buffers nothing is showing on screen.
if you have any idea on thow to fix please let me know
this is my render class:
class Renderer
{
public int VertextArrayObject;
public int VertexBufferObject;
public int IndexBuffer;
public int TextureCoordBuffer;
public Renderer()
{
VertextArrayObject = GL.GenVertexArray();
VertexBufferObject = GL.GenBuffer();
TextureCoordBuffer = GL.GenBuffer();
IndexBuffer = GL.GenBuffer();
GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
}
public void Draw(Shader shader, Matrix4 transform, Mesh mesh, Game game)
{
shader.Use();
GL.BindVertexArray(VertextArrayObject);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, IndexBuffer);
GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(sizeof(uint) * mesh.vertexIndices.Length), mesh.vertexIndices, BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject);
GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(sizeof(float) * (mesh.vertices.Length * 3)), mesh.vertices, BufferUsageHint.StaticDraw);
GL.BindVertexArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
shader.SetMatrix4("model", transform);
shader.SetMatrix4("view", game.view);
shader.SetMatrix4("projection", game.projection);
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject);
int vertexLocation = shader.GetAttribute("aPosition");
GL.VertexAttribPointer(vertexLocation, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, TextureCoordBuffer);
int texCoordLocation = shader.GetAttribute("aTexCoord");
GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.UnsignedInt, false, 0, 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, IndexBuffer);
GL.DrawElements(PrimitiveType.Triangles, mesh.vertexIndices.Length, DrawElementsType.UnsignedInt, 0);
}
}
if there is a better way of doing this other then the multi buffer thing i would love to hear it.
Upvotes: 2
Views: 367
Reputation: 211230
You need to enable the vertex attributes:
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject);
int vertexLocation = shader.GetAttribute("aPosition");
GL.EnableVertexAttribArray(vertexLocation);
GL.VertexAttribPointer(vertexLocation 3, VertexAttribPointerType.Float, false, 0, 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, TextureCoordBuffer);
texCoordLocation = shader.GetAttribute("aTexCoord");
GL.EnableVertexAttribArray(texCoordLocation);
GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.UnsignedInt, false, 0, 0);
See also Vertex Array Object
Upvotes: 1