feat: add chunk renderer with interleaved vertex buffer
This commit is contained in:
52
Rendering/ChunkRenderer.cs
Normal file
52
Rendering/ChunkRenderer.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
|
||||
namespace VoxelEngine.Rendering;
|
||||
|
||||
public sealed class ChunkRenderer : IDisposable
|
||||
{
|
||||
private const int FloatsPerVertex = 6; // pos.xyz, uv, shade
|
||||
|
||||
private readonly int _vao;
|
||||
private readonly int _vbo;
|
||||
private readonly int _vertexCount;
|
||||
|
||||
public ChunkRenderer(float[] vertices)
|
||||
{
|
||||
_vertexCount = vertices.Length / FloatsPerVertex;
|
||||
int stride = FloatsPerVertex * sizeof(float);
|
||||
|
||||
_vao = GL.GenVertexArray();
|
||||
GL.BindVertexArray(_vao);
|
||||
|
||||
_vbo = GL.GenBuffer();
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, _vbo);
|
||||
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw);
|
||||
|
||||
// location 0: position (vec3) at offset 0
|
||||
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, stride, 0);
|
||||
GL.EnableVertexAttribArray(0);
|
||||
|
||||
// location 1: uv (vec2) at offset 3 floats
|
||||
GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, stride, 3 * sizeof(float));
|
||||
GL.EnableVertexAttribArray(1);
|
||||
|
||||
// location 2: shade (float) at offset 5 floats
|
||||
GL.VertexAttribPointer(2, 1, VertexAttribPointerType.Float, false, stride, 5 * sizeof(float));
|
||||
GL.EnableVertexAttribArray(2);
|
||||
|
||||
GL.BindVertexArray(0);
|
||||
}
|
||||
|
||||
public void Draw()
|
||||
{
|
||||
GL.BindVertexArray(_vao);
|
||||
GL.DrawArrays(PrimitiveType.Triangles, 0, _vertexCount);
|
||||
GL.BindVertexArray(0);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
GL.DeleteBuffer(_vbo);
|
||||
GL.DeleteVertexArray(_vao);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user