62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
using OpenTK.Graphics.OpenGL4;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
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 bool _needsUpdate = false;
|
|
|
|
public static void OnLoad()
|
|
{
|
|
string vertexPath = "Shaders/shader.vert";
|
|
string fragmentPath = "Shaders/shader.frag";
|
|
_shader = new Shader(vertexPath, fragmentPath);
|
|
|
|
_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;
|
|
Console.WriteLine("Update buffer");
|
|
uint[] data = _faces.SelectMany(f => f.Pack()).ToArray();
|
|
GL.BufferData(BufferTarget.ShaderStorageBuffer, data.Length * sizeof(uint), 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;
|
|
}
|
|
}
|
|
}
|