forked from bit/DotRecastNetSim
refactoring rendering for frame
This commit is contained in:
parent
fb3a30d069
commit
62903cba9e
|
@ -2,10 +2,48 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Silk.NET.OpenGL;
|
||||
using DotRecast.Core;
|
||||
|
||||
namespace DotRecast.Recast.Demo.Draw;
|
||||
|
||||
public class ArrayBuffer<T>
|
||||
{
|
||||
private int _size;
|
||||
private T[] _items;
|
||||
public int Count => _size;
|
||||
|
||||
public ArrayBuffer()
|
||||
{
|
||||
_size = 0;
|
||||
_items = Array.Empty<T>();
|
||||
}
|
||||
|
||||
public void Add(T item)
|
||||
{
|
||||
if (0 >= _items.Length)
|
||||
{
|
||||
_items = new T[256];
|
||||
}
|
||||
|
||||
if (_items.Length <= _size)
|
||||
{
|
||||
var temp = new T[(int)(_size * 1.5)];
|
||||
Array.Copy(_items, 0, temp, 0, _items.Length);
|
||||
_items = temp;
|
||||
}
|
||||
_items[_size++] = item;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_size = 0;
|
||||
}
|
||||
|
||||
public T[] AsArray()
|
||||
{
|
||||
return _items;
|
||||
}
|
||||
}
|
||||
|
||||
public class ModernOpenGLDraw : OpenGLDraw
|
||||
{
|
||||
private GL _gl;
|
||||
|
@ -20,7 +58,8 @@ public class ModernOpenGLDraw : OpenGLDraw
|
|||
private float fogEnd;
|
||||
private bool fogEnabled;
|
||||
private int uniformViewMatrix;
|
||||
private readonly List<OpenGLVertex> vertices = new();
|
||||
private readonly ArrayBuffer<OpenGLVertex> vertices = new();
|
||||
private readonly ArrayBuffer<int> elements = new();
|
||||
private GLCheckerTexture _texture;
|
||||
private float[] _viewMatrix;
|
||||
private float[] _projectionMatrix;
|
||||
|
@ -202,15 +241,16 @@ public class ModernOpenGLDraw : OpenGLDraw
|
|||
byte* pVerts = (byte*)_gl.MapBuffer(GLEnum.ArrayBuffer, GLEnum.WriteOnly);
|
||||
byte* pElems = (byte*)_gl.MapBuffer(GLEnum.ElementArrayBuffer, GLEnum.WriteOnly);
|
||||
|
||||
using var unmanagedVerts = new UnmanagedMemoryStream(pVerts, vboSize, vboSize, FileAccess.Write);
|
||||
using var unmanagedElems = new UnmanagedMemoryStream(pElems, eboSize, eboSize, FileAccess.Write);
|
||||
//vertices.forEach(v => v.store(verts));
|
||||
fixed (void* v = vertices.AsArray())
|
||||
{
|
||||
System.Buffer.MemoryCopy(v, pVerts, vboSize, vboSize);
|
||||
}
|
||||
|
||||
using var verts = new BinaryWriter(unmanagedVerts);
|
||||
using var elems = new BinaryWriter(unmanagedElems);
|
||||
|
||||
vertices.forEach(v => v.store(verts));
|
||||
if (currentPrim == DebugDrawPrimitives.QUADS)
|
||||
{
|
||||
using var unmanagedElems = new UnmanagedMemoryStream(pElems, eboSize, eboSize, FileAccess.Write);
|
||||
using var elems = new BinaryWriter(unmanagedElems);
|
||||
for (int i = 0; i < vertices.Count; i += 4)
|
||||
{
|
||||
elems.Write(i);
|
||||
|
@ -223,14 +263,16 @@ public class ModernOpenGLDraw : OpenGLDraw
|
|||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < vertices.Count; i++)
|
||||
for (int i = elements.Count; i < vertices.Count; i++)
|
||||
{
|
||||
elems.Write(i);
|
||||
}
|
||||
elements.Add(i);
|
||||
}
|
||||
|
||||
verts.Flush();
|
||||
elems.Flush();
|
||||
fixed (void* e = elements.AsArray())
|
||||
{
|
||||
System.Buffer.MemoryCopy(e, pElems, eboSize, eboSize);
|
||||
}
|
||||
}
|
||||
|
||||
_gl.UnmapBuffer(GLEnum.ElementArrayBuffer);
|
||||
_gl.UnmapBuffer(GLEnum.ArrayBuffer);
|
||||
|
|
|
@ -1,17 +1,19 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using DotRecast.Core;
|
||||
|
||||
namespace DotRecast.Recast.Demo.Draw;
|
||||
|
||||
public class OpenGLVertex
|
||||
[StructLayout(LayoutKind.Explicit, Pack = 1)]
|
||||
public struct OpenGLVertex
|
||||
{
|
||||
private readonly float x;
|
||||
private readonly float y;
|
||||
private readonly float z;
|
||||
private readonly int color;
|
||||
private readonly float u;
|
||||
private readonly float v;
|
||||
[FieldOffset(0)] private readonly float x;
|
||||
[FieldOffset(4)] private readonly float y;
|
||||
[FieldOffset(8)] private readonly float z;
|
||||
[FieldOffset(12)] private readonly float u;
|
||||
[FieldOffset(16)] private readonly float v;
|
||||
[FieldOffset(20)] private readonly int color;
|
||||
|
||||
public OpenGLVertex(float[] pos, float[] uv, int color) :
|
||||
this(pos[0], pos[1], pos[2], uv[0], uv[1], color)
|
||||
|
|
Loading…
Reference in New Issue