fixed rendering
This commit is contained in:
53
Renderer.cs
53
Renderer.cs
@@ -1,4 +1,5 @@
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace Voxel
|
||||
{
|
||||
@@ -6,10 +7,13 @@ namespace Voxel
|
||||
{
|
||||
private static int _ssbo;
|
||||
private static int _vao;
|
||||
private static bool _buffersDirty;
|
||||
|
||||
private static Dictionary<(int, int), int> _chunkBufferSizes = new Dictionary<(int, int), int>();
|
||||
private static List<ChunkMesh> _chunkMeshes = new List<ChunkMesh>();
|
||||
private static Shader _shader;
|
||||
private static Texture _texture;
|
||||
private static World _world;
|
||||
private static readonly Texture _texture;
|
||||
private static World? _world;
|
||||
|
||||
static Renderer()
|
||||
{
|
||||
@@ -27,6 +31,8 @@ namespace Voxel
|
||||
|
||||
GL.BindVertexArray(_vao);
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _ssbo);
|
||||
|
||||
GL.BufferData(BufferTarget.ShaderStorageBuffer, 1024 * 1024 * 2, IntPtr.Zero, BufferUsageHint.DynamicDraw);
|
||||
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 0, _ssbo);
|
||||
}
|
||||
|
||||
@@ -39,9 +45,35 @@ namespace Voxel
|
||||
_shader.SetMatrix4("view", Camera.view);
|
||||
_shader.SetMatrix4("projection", Camera.projection);
|
||||
|
||||
if (_buffersDirty)
|
||||
{
|
||||
UpdateAllChunksBuffer();
|
||||
_buffersDirty = false;
|
||||
}
|
||||
|
||||
RenderWorld();
|
||||
}
|
||||
|
||||
private static void UpdateAllChunksBuffer()
|
||||
{
|
||||
if (_world == null) return;
|
||||
|
||||
int offset = 0;
|
||||
foreach (Chunk chunk in _world.GetAllChunks())
|
||||
{
|
||||
ChunkMesh chunkMesh = chunk.GetChunkMesh();
|
||||
|
||||
if (chunkMesh.NeedsUpdate)
|
||||
{
|
||||
byte[] data = chunkMesh.GetPackedData();
|
||||
GL.BufferSubData(BufferTarget.ShaderStorageBuffer, (IntPtr)offset * 8, chunkMesh.Size * 8, data);
|
||||
}
|
||||
|
||||
_chunkBufferSizes[(chunk.X, chunk.Y)] = offset;
|
||||
offset += chunkMesh.Size * 8;
|
||||
}
|
||||
}
|
||||
|
||||
private static void RenderWorld()
|
||||
{
|
||||
if (_world == null) return;
|
||||
@@ -49,17 +81,28 @@ namespace Voxel
|
||||
foreach (Chunk chunk in _world.GetAllChunks())
|
||||
{
|
||||
ChunkMesh chunkMesh = chunk.GetChunkMesh();
|
||||
byte[] data = chunkMesh.GetPackedData();
|
||||
|
||||
if (chunkMesh.Size == 0) continue;
|
||||
|
||||
if (!_chunkBufferSizes.TryGetValue((chunk.X, chunk.Y), out int offset)) continue;
|
||||
|
||||
_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);
|
||||
|
||||
//GL.MemoryBarrier(MemoryBarrierFlags.ShaderStorageBarrierBit);
|
||||
GL.DrawArrays(PrimitiveType.Triangles, offset * 6, chunkMesh.Size * 6);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetWorld(World world)
|
||||
{
|
||||
_world = world;
|
||||
_buffersDirty = true;
|
||||
}
|
||||
|
||||
public static void MarkBuffersDirty()
|
||||
{
|
||||
_buffersDirty = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user