Added documentation to Util types
This commit is contained in:
@@ -3,18 +3,39 @@ using OpenTK;
|
||||
|
||||
namespace Diamond.Util
|
||||
{
|
||||
/// <summary>
|
||||
/// Vertex buffer data for Wavefront meshes
|
||||
/// </summary>
|
||||
[VertexData]
|
||||
public struct ObjVertex
|
||||
{
|
||||
/// <summary>
|
||||
/// Vertex position (v)
|
||||
/// </summary>
|
||||
[VertexPointer("position", 3)]
|
||||
[VertexPointer("v", 3)]
|
||||
public Vector3 Position;
|
||||
|
||||
/// <summary>
|
||||
/// UV coordinate (vt)
|
||||
/// </summary>
|
||||
[VertexPointer("uv", 2)]
|
||||
[VertexPointer("vt", 2)]
|
||||
public Vector2 UV;
|
||||
|
||||
/// <summary>
|
||||
/// Vertex normal (vn)
|
||||
/// </summary>
|
||||
[VertexPointer("normal", 3)]
|
||||
[VertexPointer("vn", 3)]
|
||||
public Vector3 Normal;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new ObjVertex
|
||||
/// </summary>
|
||||
/// <param name="position">The vertex position (v)</param>
|
||||
/// <param name="uv">The uv coordinate (vt)</param>
|
||||
/// <param name="normal">The vertex normal (n)</param>
|
||||
public ObjVertex(Vector3 position, Vector2 uv, Vector3 normal)
|
||||
{
|
||||
Position = position;
|
||||
|
||||
@@ -5,12 +5,32 @@ using System.Linq;
|
||||
|
||||
namespace Diamond.Util
|
||||
{
|
||||
/// <summary>
|
||||
/// Provices access to a subset of an array
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class SubArray<T> : IEnumerable<T>
|
||||
{
|
||||
public T[] Array;
|
||||
public int Offset;
|
||||
public int Length;
|
||||
/// <summary>
|
||||
/// The array that this references
|
||||
/// </summary>
|
||||
public T[] Array { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The offset of this subarray
|
||||
/// </summary>
|
||||
public int Offset { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The length of this subarray
|
||||
/// </summary>
|
||||
public int Length { get; }
|
||||
|
||||
/// <summary>
|
||||
/// By-ref access to the underlying array
|
||||
/// </summary>
|
||||
/// <param name="i">The index of the subarray to access</param>
|
||||
/// <returns>A reference to the offset position </returns>
|
||||
public ref T this[int i]
|
||||
{
|
||||
get
|
||||
@@ -21,28 +41,36 @@ namespace Diamond.Util
|
||||
}
|
||||
}
|
||||
|
||||
public SubArray(T[] array) : this(array, 0, array.Length)
|
||||
/// <summary>
|
||||
/// Create a subarray covering an entire array
|
||||
/// </summary>
|
||||
/// <param name="array">The array to cover</param>
|
||||
public SubArray(T[] array)
|
||||
: this(array, 0, array.Length)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a subarray from an array
|
||||
/// </summary>
|
||||
/// <param name="array">The array to cover</param>
|
||||
/// <param name="offset">The offset of the subarray</param>
|
||||
/// <param name="length">The length of the subarray</param>
|
||||
public SubArray(T[] array, int offset, int length)
|
||||
{
|
||||
if (offset + length > array.Length)
|
||||
throw new IndexOutOfRangeException($"Cannot create subarray with length {length}) of " +
|
||||
$"array with length {array.Length} at index {offset}");
|
||||
|
||||
Array = array;
|
||||
Offset = offset;
|
||||
Length = length;
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
for (var i = 0; i < Length; i++)
|
||||
yield return Array[Offset + i];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a copy of the array within the bounds of the subarray
|
||||
/// </summary>
|
||||
/// <returns>A copied array from this subarray</returns>
|
||||
public T[] ToArray()
|
||||
{
|
||||
var arr = new T[Length];
|
||||
@@ -50,6 +78,18 @@ namespace Diamond.Util
|
||||
return arr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a copy of the array within the bounds of the subarray, and make this subarray cover that copy
|
||||
/// </summary>
|
||||
/// <returns>The new array that this subarray covers</returns>
|
||||
public T[] Extract()
|
||||
{
|
||||
var arr = ToArray();
|
||||
Offset = 0;
|
||||
Array = arr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (Length == 0)
|
||||
@@ -57,16 +97,56 @@ namespace Diamond.Util
|
||||
|
||||
return $"[{string.Join(", ", this)}]";
|
||||
}
|
||||
|
||||
#region IEnumerable
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enumerate over the array within the bounds of the subarray
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
for (var i = 0; i < Length; i++)
|
||||
yield return Array[Offset + i];
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Class for static SubArray operations
|
||||
/// </summary>
|
||||
public static class SubArray
|
||||
{
|
||||
public static T[] Join<T>(params SubArray<T>[] subArrays) => Join((IEnumerable<SubArray<T>>)subArrays);
|
||||
/// <summary>
|
||||
/// Make multiple subArrays cover the same array
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The element type</typeparam>
|
||||
/// <param name="subArrays">The SubArrays to join</param>
|
||||
/// <returns>The new underlying array</returns>
|
||||
public static T[] Join<T>(params SubArray<T>[] subArrays) => Join((IEnumerable<SubArray<T>>) subArrays);
|
||||
|
||||
/// <summary>
|
||||
/// Make multiple subArrays cover the same array
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The element type</typeparam>
|
||||
/// <param name="subArrays">The SubArrays to join</param>
|
||||
/// <returns>The new underlying array</returns>
|
||||
public static T[] Join<T>(IEnumerable<SubArray<T>> subArrays)
|
||||
{
|
||||
// todo: this breaks in the case: Join(a, b); Join(b, c)
|
||||
// possibly create "multiarray" class or similar which would manage these operations
|
||||
// and prevent this case from arising
|
||||
|
||||
var subArrList = subArrays.ToList(); // prevent multiple enumeration
|
||||
|
||||
HashSet<T[]> uniqueArrays = new HashSet<T[]>();
|
||||
foreach (var subArray in subArrays)
|
||||
foreach (var subArray in subArrList)
|
||||
{
|
||||
uniqueArrays.Add(subArray.Array);
|
||||
}
|
||||
@@ -88,7 +168,7 @@ namespace Diamond.Util
|
||||
System.Array.ConstrainedCopy(uniqueArray, 0, array, offsets[uniqueArray], uniqueArray.Length);
|
||||
}
|
||||
|
||||
foreach (var subArray in subArrays)
|
||||
foreach (var subArray in subArrList)
|
||||
{
|
||||
subArray.Offset = offsets[subArray.Array];
|
||||
subArray.Array = array;
|
||||
@@ -96,6 +176,5 @@ namespace Diamond.Util
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -3,20 +3,26 @@ using OpenTK;
|
||||
|
||||
namespace Diamond.Util
|
||||
{
|
||||
/// <summary>
|
||||
/// Vertex buffer data for instanced rendering
|
||||
/// </summary>
|
||||
[VertexData(Divisor = 1)]
|
||||
public struct TileData
|
||||
{
|
||||
/// <summary>
|
||||
/// The global position of the instance
|
||||
/// </summary>
|
||||
[VertexPointer("glbpos", 3)]
|
||||
[VertexPointer("global_pos", 3)]
|
||||
public Vector3 Position;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new TileData
|
||||
/// </summary>
|
||||
/// <param name="position">The global position of the instance</param>
|
||||
public TileData(Vector3 position)
|
||||
{
|
||||
Position = position;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{nameof(Position)}: {Position}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,27 @@
|
||||
#version 440
|
||||
|
||||
in vec3 position;
|
||||
in vec3 glbpos;
|
||||
in vec2 uv;
|
||||
in vec3 normal;
|
||||
// Wavefront vertices
|
||||
in vec3 v;
|
||||
in vec2 vt;
|
||||
in vec3 vn;
|
||||
|
||||
out vec2 vcoord;
|
||||
out float light;
|
||||
// Instance data
|
||||
in vec3 global_pos;
|
||||
|
||||
uniform mat4 view;
|
||||
uniform mat4 proj;
|
||||
|
||||
out vec2 vcoord;
|
||||
out float light;
|
||||
|
||||
void main ()
|
||||
{
|
||||
mat4 pv = proj * view;
|
||||
vec3 pos = glbpos + position;
|
||||
gl_Position = pv * vec4(pos, 1);
|
||||
vcoord = uv;
|
||||
mat4 proj_view = proj * view;
|
||||
vec3 pos = global_pos + v;
|
||||
|
||||
gl_Position = proj_view * vec4(pos, 1);
|
||||
|
||||
vcoord = vt;
|
||||
|
||||
light = dot(vec3(.2, .3, 1), normal) / 2 + .5;
|
||||
light = dot(vec3(.2, .3, 1), vn) / 2 + .5;
|
||||
}
|
||||
Reference in New Issue
Block a user