66 lines
2.0 KiB
C#
66 lines
2.0 KiB
C#
using OpenTK.Graphics.OpenGL4;
|
|
|
|
namespace Voxel
|
|
{
|
|
static class Renderer
|
|
{
|
|
private static int _ssbo;
|
|
private static int _vao;
|
|
private static List<ChunkMesh> _chunkMeshes = new List<ChunkMesh>();
|
|
private static Shader _shader;
|
|
private static Texture _texture;
|
|
private static World _world;
|
|
|
|
static Renderer()
|
|
{
|
|
string vertexPath = "Shaders/shader.vert";
|
|
string fragmentPath = "Shaders/shader.frag";
|
|
string texturePath = "atlas.png";
|
|
|
|
_shader = new Shader(vertexPath, fragmentPath);
|
|
_texture = new Texture(texturePath);
|
|
|
|
_shader.SetInt("uTexture", 0);
|
|
|
|
_ssbo = GL.GenBuffer();
|
|
_vao = GL.GenVertexArray();
|
|
|
|
GL.BindVertexArray(_vao);
|
|
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _ssbo);
|
|
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 0, _ssbo);
|
|
}
|
|
|
|
public static void Render()
|
|
{
|
|
GL.BindVertexArray(_vao);
|
|
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _ssbo);
|
|
|
|
_shader.Use();
|
|
_shader.SetMatrix4("view", Camera.view);
|
|
_shader.SetMatrix4("projection", Camera.projection);
|
|
|
|
RenderWorld();
|
|
}
|
|
|
|
private static void RenderWorld()
|
|
{
|
|
if (_world == null) return;
|
|
|
|
foreach (Chunk chunk in _world.GetAllChunks())
|
|
{
|
|
ChunkMesh chunkMesh = chunk.GetChunkMesh();
|
|
byte[] data = chunkMesh.GetPackedData();
|
|
_shader.SetInt("chunkX", chunk.X);
|
|
_shader.SetInt("chunkY", chunk.Y);
|
|
GL.BufferData(BufferTarget.ShaderStorageBuffer, chunkMesh.Length * 8, data, BufferUsageHint.StaticRead);
|
|
GL.DrawArrays(PrimitiveType.Triangles, 0, chunkMesh.Length * 6);
|
|
}
|
|
}
|
|
|
|
public static void SetWorld(World world)
|
|
{
|
|
_world = world;
|
|
}
|
|
}
|
|
}
|