forked from mirror/DotRecast
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.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Silk.NET.OpenGL;
|
using Silk.NET.OpenGL;
|
||||||
using DotRecast.Core;
|
|
||||||
|
|
||||||
namespace DotRecast.Recast.Demo.Draw;
|
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
|
public class ModernOpenGLDraw : OpenGLDraw
|
||||||
{
|
{
|
||||||
private GL _gl;
|
private GL _gl;
|
||||||
|
@ -20,7 +58,8 @@ public class ModernOpenGLDraw : OpenGLDraw
|
||||||
private float fogEnd;
|
private float fogEnd;
|
||||||
private bool fogEnabled;
|
private bool fogEnabled;
|
||||||
private int uniformViewMatrix;
|
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 GLCheckerTexture _texture;
|
||||||
private float[] _viewMatrix;
|
private float[] _viewMatrix;
|
||||||
private float[] _projectionMatrix;
|
private float[] _projectionMatrix;
|
||||||
|
@ -202,15 +241,16 @@ public class ModernOpenGLDraw : OpenGLDraw
|
||||||
byte* pVerts = (byte*)_gl.MapBuffer(GLEnum.ArrayBuffer, GLEnum.WriteOnly);
|
byte* pVerts = (byte*)_gl.MapBuffer(GLEnum.ArrayBuffer, GLEnum.WriteOnly);
|
||||||
byte* pElems = (byte*)_gl.MapBuffer(GLEnum.ElementArrayBuffer, GLEnum.WriteOnly);
|
byte* pElems = (byte*)_gl.MapBuffer(GLEnum.ElementArrayBuffer, GLEnum.WriteOnly);
|
||||||
|
|
||||||
using var unmanagedVerts = new UnmanagedMemoryStream(pVerts, vboSize, vboSize, FileAccess.Write);
|
//vertices.forEach(v => v.store(verts));
|
||||||
using var unmanagedElems = new UnmanagedMemoryStream(pElems, eboSize, eboSize, FileAccess.Write);
|
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)
|
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)
|
for (int i = 0; i < vertices.Count; i += 4)
|
||||||
{
|
{
|
||||||
elems.Write(i);
|
elems.Write(i);
|
||||||
|
@ -223,14 +263,16 @@ public class ModernOpenGLDraw : OpenGLDraw
|
||||||
}
|
}
|
||||||
else
|
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();
|
fixed (void* e = elements.AsArray())
|
||||||
elems.Flush();
|
{
|
||||||
|
System.Buffer.MemoryCopy(e, pElems, eboSize, eboSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_gl.UnmapBuffer(GLEnum.ElementArrayBuffer);
|
_gl.UnmapBuffer(GLEnum.ElementArrayBuffer);
|
||||||
_gl.UnmapBuffer(GLEnum.ArrayBuffer);
|
_gl.UnmapBuffer(GLEnum.ArrayBuffer);
|
||||||
|
|
|
@ -1,17 +1,19 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using DotRecast.Core;
|
using DotRecast.Core;
|
||||||
|
|
||||||
namespace DotRecast.Recast.Demo.Draw;
|
namespace DotRecast.Recast.Demo.Draw;
|
||||||
|
|
||||||
public class OpenGLVertex
|
[StructLayout(LayoutKind.Explicit, Pack = 1)]
|
||||||
|
public struct OpenGLVertex
|
||||||
{
|
{
|
||||||
private readonly float x;
|
[FieldOffset(0)] private readonly float x;
|
||||||
private readonly float y;
|
[FieldOffset(4)] private readonly float y;
|
||||||
private readonly float z;
|
[FieldOffset(8)] private readonly float z;
|
||||||
private readonly int color;
|
[FieldOffset(12)] private readonly float u;
|
||||||
private readonly float u;
|
[FieldOffset(16)] private readonly float v;
|
||||||
private readonly float v;
|
[FieldOffset(20)] private readonly int color;
|
||||||
|
|
||||||
public OpenGLVertex(float[] pos, float[] uv, int color) :
|
public OpenGLVertex(float[] pos, float[] uv, int color) :
|
||||||
this(pos[0], pos[1], pos[2], uv[0], uv[1], color)
|
this(pos[0], pos[1], pos[2], uv[0], uv[1], color)
|
||||||
|
|
Loading…
Reference in New Issue