66 lines
1.9 KiB
C#
66 lines
1.9 KiB
C#
using OpenTK.Graphics.OpenGL4;
|
|
|
|
namespace Voxel
|
|
{
|
|
static class Renderer
|
|
{
|
|
private static int _ssbo;
|
|
private static int _vao;
|
|
private static List<FaceData> _faces = new List<FaceData>();
|
|
private static Shader _shader;
|
|
private static Texture _texture;
|
|
private static bool _needsUpdate = false;
|
|
|
|
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);
|
|
|
|
if (_needsUpdate)
|
|
{
|
|
_needsUpdate = false;
|
|
byte[] data = _faces.SelectMany(f => f.Pack()).ToArray();
|
|
GL.BufferData(BufferTarget.ShaderStorageBuffer, data.Length, data, BufferUsageHint.StaticRead);
|
|
}
|
|
|
|
_shader.Use();
|
|
_shader.SetMatrix4("view", Camera.view);
|
|
_shader.SetMatrix4("projection", Camera.projection);
|
|
|
|
GL.DrawArrays(PrimitiveType.Triangles, 0, _faces.Count * 6);
|
|
|
|
//Console.WriteLine("Rendered " + _faces.Count.ToString() + " faces");
|
|
}
|
|
|
|
public static void AddFaces(List<FaceData> faces)
|
|
{
|
|
_faces.AddRange(faces);
|
|
_needsUpdate = true;
|
|
}
|
|
|
|
public static void ClearFaces()
|
|
{
|
|
_faces.Clear();
|
|
}
|
|
}
|
|
}
|