From 98cdbd7d2011c7c984b52b4023138d2526449bce Mon Sep 17 00:00:00 2001 From: ikpil Date: Mon, 17 Apr 2023 23:46:00 +0900 Subject: [PATCH] remove new float[3] --- src/DotRecast.Recast.Demo/Draw/ArrayBuffer.cs | 43 +++++++++++++++++++ src/DotRecast.Recast.Demo/Draw/DebugDraw.cs | 2 +- .../Draw/ModernOpenGLDraw.cs | 41 +----------------- src/DotRecast.Recast.Demo/Draw/OpenGLDraw.cs | 2 +- .../Draw/OpenGLVertex.cs | 4 +- .../Draw/RecastDebugDraw.cs | 26 +++++------ 6 files changed, 61 insertions(+), 57 deletions(-) create mode 100644 src/DotRecast.Recast.Demo/Draw/ArrayBuffer.cs diff --git a/src/DotRecast.Recast.Demo/Draw/ArrayBuffer.cs b/src/DotRecast.Recast.Demo/Draw/ArrayBuffer.cs new file mode 100644 index 0000000..5f9cea9 --- /dev/null +++ b/src/DotRecast.Recast.Demo/Draw/ArrayBuffer.cs @@ -0,0 +1,43 @@ +using System; + +namespace DotRecast.Recast.Demo.Draw; + +public class ArrayBuffer +{ + private int _size; + private T[] _items; + public int Count => _size; + + public ArrayBuffer() + { + _size = 0; + _items = Array.Empty(); + } + + 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; + } +} \ No newline at end of file diff --git a/src/DotRecast.Recast.Demo/Draw/DebugDraw.cs b/src/DotRecast.Recast.Demo/Draw/DebugDraw.cs index 56d4698..b28765f 100644 --- a/src/DotRecast.Recast.Demo/Draw/DebugDraw.cs +++ b/src/DotRecast.Recast.Demo/Draw/DebugDraw.cs @@ -201,7 +201,7 @@ public class DebugDraw getOpenGlDraw().vertex(x, y, z, color); } - public void vertex(float[] pos, int color, float[] uv) + public void vertex(Vector3f pos, int color, Vector2f uv) { getOpenGlDraw().vertex(pos, color, uv); } diff --git a/src/DotRecast.Recast.Demo/Draw/ModernOpenGLDraw.cs b/src/DotRecast.Recast.Demo/Draw/ModernOpenGLDraw.cs index 11dd3a7..f6e0708 100644 --- a/src/DotRecast.Recast.Demo/Draw/ModernOpenGLDraw.cs +++ b/src/DotRecast.Recast.Demo/Draw/ModernOpenGLDraw.cs @@ -6,45 +6,6 @@ using Silk.NET.OpenGL; namespace DotRecast.Recast.Demo.Draw; -public class ArrayBuffer -{ - private int _size; - private T[] _items; - public int Count => _size; - - public ArrayBuffer() - { - _size = 0; - _items = Array.Empty(); - } - - 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; @@ -331,7 +292,7 @@ public class ModernOpenGLDraw : OpenGLDraw } - public void vertex(float[] pos, int color, float[] uv) + public void vertex(Vector3f pos, int color, Vector2f uv) { vertices.Add(new OpenGLVertex(pos, uv, color)); } diff --git a/src/DotRecast.Recast.Demo/Draw/OpenGLDraw.cs b/src/DotRecast.Recast.Demo/Draw/OpenGLDraw.cs index 2f324a8..d5143e8 100644 --- a/src/DotRecast.Recast.Demo/Draw/OpenGLDraw.cs +++ b/src/DotRecast.Recast.Demo/Draw/OpenGLDraw.cs @@ -18,7 +18,7 @@ public interface OpenGLDraw void vertex(float[] pos, int color); void vertex(Vector3f pos, int color); - void vertex(float[] pos, int color, float[] uv); + void vertex(Vector3f pos, int color, Vector2f uv); void vertex(float x, float y, float z, int color, float u, float v); diff --git a/src/DotRecast.Recast.Demo/Draw/OpenGLVertex.cs b/src/DotRecast.Recast.Demo/Draw/OpenGLVertex.cs index 2cc73a8..b00f9c5 100644 --- a/src/DotRecast.Recast.Demo/Draw/OpenGLVertex.cs +++ b/src/DotRecast.Recast.Demo/Draw/OpenGLVertex.cs @@ -15,8 +15,8 @@ public struct OpenGLVertex [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) + public OpenGLVertex(Vector3f pos, Vector2f uv, int color) : + this(pos[0], pos[1], pos[2], uv.x, uv.y, color) { } diff --git a/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs b/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs index 8bbcbfc..da769d6 100644 --- a/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs +++ b/src/DotRecast.Recast.Demo/Draw/RecastDebugDraw.cs @@ -42,9 +42,9 @@ public class RecastDebugDraw : DebugDraw { float walkableThr = (float)Math.Cos(walkableSlopeAngle / 180.0f * Math.PI); - float[] uva = new float[2]; - float[] uvb = new float[2]; - float[] uvc = new float[2]; + Vector2f uva = Vector2f.Zero; + Vector2f uvb = Vector2f.Zero; + Vector2f uvc = Vector2f.Zero; texture(true); @@ -52,7 +52,7 @@ public class RecastDebugDraw : DebugDraw begin(DebugDrawPrimitives.TRIS); for (int i = 0; i < tris.Length; i += 3) { - float[] norm = new float[] { normals[i], normals[i + 1], normals[i + 2] }; + Vector3f norm = Vector3f.Of(normals[i], normals[i + 1], normals[i + 2]); int color; char a = (char)(220 * (2 + norm[0] + norm[1]) / 4); @@ -65,9 +65,9 @@ public class RecastDebugDraw : DebugDraw color = duRGBA(a, a, a, 255); } - float[] va = new float[] { verts[tris[i] * 3], verts[tris[i] * 3 + 1], verts[tris[i] * 3 + 2] }; - float[] vb = new float[] { verts[tris[i + 1] * 3], verts[tris[i + 1] * 3 + 1], verts[tris[i + 1] * 3 + 2] }; - float[] vc = new float[] { verts[tris[i + 2] * 3], verts[tris[i + 2] * 3 + 1], verts[tris[i + 2] * 3 + 2] }; + Vector3f va = Vector3f.Of(verts[tris[i] * 3], verts[tris[i] * 3 + 1], verts[tris[i] * 3 + 2]); + Vector3f vb = Vector3f.Of(verts[tris[i + 1] * 3], verts[tris[i + 1] * 3 + 1], verts[tris[i + 1] * 3 + 2]); + Vector3f vc = Vector3f.Of(verts[tris[i + 2] * 3], verts[tris[i + 2] * 3 + 1], verts[tris[i + 2] * 3 + 2]); int ax = 0, ay = 0; if (Math.Abs(norm[1]) > Math.Abs(norm[ax])) @@ -83,12 +83,12 @@ public class RecastDebugDraw : DebugDraw ax = (1 << ax) & 3; // +1 mod 3 ay = (1 << ax) & 3; // +1 mod 3 - uva[0] = va[ax] * texScale; - uva[1] = va[ay] * texScale; - uvb[0] = vb[ax] * texScale; - uvb[1] = vb[ay] * texScale; - uvc[0] = vc[ax] * texScale; - uvc[1] = vc[ay] * texScale; + uva.x = va[ax] * texScale; + uva.y = va[ay] * texScale; + uvb.x = vb[ax] * texScale; + uvb.y = vb[ay] * texScale; + uvc.x = vc[ax] * texScale; + uvc.y = vc[ay] * texScale; vertex(va, color, uva); vertex(vb, color, uvb);