我应该如何在 OpenGL 中使用多个缓冲区
how am i supposed to use multiple buffers in OpenGL
我想使用不同的数组来存储不同的数据,例如来自我的网格的颜色位置和纹理坐标,但是当我尝试绑定多个缓冲区时,屏幕上没有显示任何内容。
如果您对如何修复有任何想法,请告诉我
这是我的渲染图 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);
}
}
如果除了多缓冲区之外还有更好的方法,我很想听听。
您需要启用顶点属性:
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);
我想使用不同的数组来存储不同的数据,例如来自我的网格的颜色位置和纹理坐标,但是当我尝试绑定多个缓冲区时,屏幕上没有显示任何内容。
如果您对如何修复有任何想法,请告诉我
这是我的渲染图 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);
}
}
如果除了多缓冲区之外还有更好的方法,我很想听听。
您需要启用顶点属性:
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);