Skip to content

Commit 0733482

Browse files
committed
Removed math library as dependency
1 parent 67a09f8 commit 0733482

File tree

9 files changed

+198
-51
lines changed

9 files changed

+198
-51
lines changed

Example/DebugDrawRenderer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ namespace Example
1515
using Silk.NET.OpenGL.Legacy;
1616
#endif
1717

18-
using Hexa.NET.Mathematics;
1918
using Silk.NET.Windowing;
19+
using System.Numerics;
2020

2121
public class DebugDrawRenderer
2222
{
@@ -45,7 +45,7 @@ public DebugDrawRenderer(GL gl, IWindow window)
4545
{
4646
_gl = gl;
4747
context = DebugDraw.CreateContext();
48-
context.SetViewport(new Viewport(0, 0, window.Size.X, window.Size.Y));
48+
context.SetViewport(Vector2.Zero, new(window.Size.X, window.Size.Y));
4949
CreateDeviceObjects();
5050
}
5151

@@ -120,7 +120,7 @@ private unsafe void SetupRenderState(DebugDrawData drawData)
120120

121121
private unsafe void Render(DebugDrawData drawData)
122122
{
123-
Viewport viewport = drawData.Viewport;
123+
DebugDrawViewport viewport = drawData.Viewport;
124124
int framebufferWidth = (int)(viewport.Width);
125125
int framebufferHeight = (int)(viewport.Height);
126126
if (framebufferWidth <= 0 || framebufferHeight <= 0)

Example/Example.csproj

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
<Nullable>enable</Nullable>
8-
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9-
</PropertyGroup>
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9+
</PropertyGroup>
1010

11-
<ItemGroup>
12-
<PackageReference Include="Silk.NET" Version="2.21.0" />
13-
<PackageReference Include="Silk.NET.Input.Extensions" Version="2.21.0" />
14-
<PackageReference Include="Silk.NET.OpenGL" Version="2.21.0" />
15-
<PackageReference Include="System.Text.Json" Version="8.0.4" />
16-
</ItemGroup>
11+
<ItemGroup>
12+
<PackageReference Include="Silk.NET" Version="2.21.0" />
13+
<PackageReference Include="Silk.NET.Input.Extensions" Version="2.21.0" />
14+
<PackageReference Include="Silk.NET.OpenGL" Version="2.21.0" />
15+
<PackageReference Include="System.Text.Json" Version="8.0.4" />
16+
<PackageReference Include="Hexa.NET.Math" Version="2.0.1" />
17+
</ItemGroup>
1718

18-
<ItemGroup>
19-
<ProjectReference Include="..\Hexa.NET.DebugDraw\Hexa.NET.DebugDraw.csproj" />
20-
</ItemGroup>
19+
<ItemGroup>
20+
<ProjectReference Include="..\Hexa.NET.DebugDraw\Hexa.NET.DebugDraw.csproj" />
21+
</ItemGroup>
2122

2223
</Project>

Example/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private static void Main(string[] args)
3434
{
3535
// Adjust the viewport to the new window size
3636
gl.Viewport(s);
37-
DebugDraw.SetViewport(new Viewport(0, 0, s.X, s.Y));
37+
DebugDraw.SetViewport(Vector2.Zero, new(s.X, s.Y));
3838
};
3939

4040
Vector3 position = new(-5, 5, -5);

Hexa.NET.DebugDraw/DebugDraw.cs

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace Hexa.NET.DebugDraw
22
{
3-
using Hexa.NET.Mathematics;
43
using System;
54
using System.Numerics;
65
using System.Runtime.CompilerServices;
@@ -17,6 +16,9 @@ public static unsafe class DebugDraw
1716
private const int COL32_B_SHIFT = 16;
1817
private const int COL32_A_SHIFT = 24;
1918
private const uint COL32_A_MASK = 0xFF000000;
19+
private const float PI = MathF.PI;
20+
private const float PI2 = MathF.PI * 2.0f;
21+
private const float PIDIV2 = MathF.PI / 2.0f;
2022

2123
public static DebugDrawContext CreateContext()
2224
{
@@ -216,14 +218,14 @@ private static void DrawPreComputed(DebugDrawPrimitiveTopology topology, Vector3
216218
/// Sets the viewport for rendering debug shapes.
217219
/// </summary>
218220
/// <param name="viewport">The viewport to set.</param>
219-
public static void SetViewport(Viewport viewport)
221+
public static void SetViewport(Vector2 offset, Vector2 size)
220222
{
221223
if (currentContext == null)
222224
{
223225
throw new InvalidOperationException("DebugDraw context is not set. Call DebugDraw.SetContext() before drawing.");
224226
}
225227

226-
currentContext.SetViewport(viewport);
228+
currentContext.SetViewport(new DebugDrawViewport(offset, size));
227229
}
228230

229231
/// <summary>
@@ -272,16 +274,63 @@ public static void ExecuteCommandList(DebugDrawCommandList commandList)
272274
/// <summary>
273275
/// Draws a bounding frustum in the specified color.
274276
/// </summary>
275-
/// <param name="frustum">The bounding frustum to be drawn.</param>
277+
/// <param name="frustum">The bounding frustum to be drawn. Expects 9 corners</param>
276278
/// <param name="col">The color of the frustum.</param>
277279
///
278-
public static void DrawFrustum(BoundingFrustum frustum, Vector4 col)
280+
public static void DrawFrustum(Vector3* frustumCorners, int cornerCount, Vector4 col)
279281
{
282+
if (cornerCount != 9)
283+
{
284+
throw new ArgumentException("Frustum must have 9 corners", nameof(frustumCorners));
285+
}
286+
CurrentList.BeginDraw();
287+
288+
uint color = ColorConvertFloat4ToU32(col);
289+
290+
CurrentList.ReserveGeometry(9, 24);
291+
var indices = CurrentList.Indices + CurrentList.IndexCount;
292+
var vertices = CurrentList.Vertices + CurrentList.VertexCount;
293+
294+
indices[0] = 0; indices[1] = 1;
295+
indices[2] = 1; indices[3] = 2;
296+
indices[4] = 2; indices[5] = 3;
297+
indices[6] = 3; indices[7] = 0;
298+
indices[8] = 0; indices[9] = 4;
299+
indices[10] = 1; indices[11] = 5;
300+
indices[12] = 2; indices[13] = 6;
301+
indices[14] = 3; indices[15] = 7;
302+
indices[16] = 4; indices[17] = 5;
303+
indices[18] = 5; indices[19] = 6;
304+
indices[20] = 6; indices[21] = 7;
305+
indices[22] = 7; indices[23] = 4;
306+
307+
for (int i = 0; i < 9; i++)
308+
{
309+
vertices[i].Color = color;
310+
vertices[i].Position = frustumCorners[i];
311+
vertices[i].UV = WhiteUV;
312+
}
313+
314+
CurrentList.RecordCmd(DebugDrawPrimitiveTopology.LineList);
315+
}
316+
317+
/// <summary>
318+
/// Draws a bounding frustum in the specified color.
319+
/// </summary>
320+
/// <param name="frustum">The bounding frustum to be drawn. Expects 9 corners</param>
321+
/// <param name="col">The color of the frustum.</param>
322+
///
323+
public static void DrawFrustum(Span<Vector3> frustumCorners, Vector4 col)
324+
{
325+
if (frustumCorners.Length < 9)
326+
{
327+
throw new ArgumentException("Frustum must have 9 corners", nameof(frustumCorners));
328+
}
280329
CurrentList.BeginDraw();
281330

282331
uint color = ColorConvertFloat4ToU32(col);
283332

284-
CurrentList.ReserveGeometry(BoundingFrustum.CornerCount, 24);
333+
CurrentList.ReserveGeometry(9, 24);
285334
var indices = CurrentList.Indices + CurrentList.IndexCount;
286335
var vertices = CurrentList.Vertices + CurrentList.VertexCount;
287336

@@ -298,11 +347,10 @@ public static void DrawFrustum(BoundingFrustum frustum, Vector4 col)
298347
indices[20] = 6; indices[21] = 7;
299348
indices[22] = 7; indices[23] = 4;
300349

301-
var corners = frustum.Corners;
302-
for (int i = 0; i < BoundingFrustum.CornerCount; i++)
350+
for (int i = 0; i < 9; i++)
303351
{
304352
vertices[i].Color = color;
305-
vertices[i].Position = corners[i];
353+
vertices[i].Position = frustumCorners[i];
306354
vertices[i].UV = WhiteUV;
307355
}
308356

@@ -315,7 +363,7 @@ public static void DrawFrustum(BoundingFrustum frustum, Vector4 col)
315363
/// <param name="box">The bounding box to be drawn.</param>
316364
/// <param name="col">The color of the box.</param>
317365
///
318-
public static void DrawBoundingBox(BoundingBox box, Vector4 col)
366+
public static void DrawBoundingBox(Vector3 min, Vector3 max, Vector4 col)
319367
{
320368
CurrentList.BeginDraw();
321369

@@ -339,14 +387,14 @@ public static void DrawBoundingBox(BoundingBox box, Vector4 col)
339387
indices[20] = 6; indices[21] = 7;
340388
indices[22] = 7; indices[23] = 4;
341389

342-
vertices[0].Position = new Vector3(box.Min.X, box.Max.Y, box.Min.Z);
343-
vertices[1].Position = new Vector3(box.Min.X, box.Min.Y, box.Min.Z);
344-
vertices[2].Position = new Vector3(box.Max.X, box.Min.Y, box.Min.Z);
345-
vertices[3].Position = new Vector3(box.Max.X, box.Max.Y, box.Min.Z);
346-
vertices[4].Position = new Vector3(box.Min.X, box.Max.Y, box.Max.Z);
347-
vertices[5].Position = new Vector3(box.Min.X, box.Min.Y, box.Max.Z);
348-
vertices[6].Position = new Vector3(box.Max.X, box.Min.Y, box.Max.Z);
349-
vertices[7].Position = new Vector3(box.Max.X, box.Max.Y, box.Max.Z);
390+
vertices[0].Position = new Vector3(max.X, max.Y, min.Z);
391+
vertices[1].Position = new Vector3(max.X, max.Y, min.Z);
392+
vertices[2].Position = new Vector3(max.X, max.Y, max.Z);
393+
vertices[3].Position = new Vector3(max.X, max.Y, max.Z);
394+
vertices[4].Position = new Vector3(max.X, max.Y, max.Z);
395+
vertices[5].Position = new Vector3(max.X, max.Y, max.Z);
396+
vertices[6].Position = new Vector3(max.X, max.Y, max.Z);
397+
vertices[7].Position = new Vector3(max.X, max.Y, max.Z);
350398

351399
for (int i = 0; i < vertexCount; i++)
352400
{
@@ -561,9 +609,9 @@ public static void DrawBoundingBox(BoundingBox box, Vector4 col)
561609
/// <param name="sphere">The bounding sphere to be drawn.</param>
562610
/// <param name="col">The color of the sphere.</param>
563611
///
564-
public static void DrawBoundingSphere(BoundingSphere sphere, Vector4 col)
612+
public static void DrawBoundingSphere(Vector3 center, float radius, Vector4 col)
565613
{
566-
DrawPreComputed(DebugDrawPrimitiveTopology.LineList, spherePositions, sphereIndices, Matrix4x4.CreateScale(sphere.Radius) * Matrix4x4.CreateTranslation(sphere.Center), col);
614+
DrawPreComputed(DebugDrawPrimitiveTopology.LineList, spherePositions, sphereIndices, Matrix4x4.CreateScale(radius) * Matrix4x4.CreateTranslation(center), col);
567615
}
568616

569617
/// <summary>
@@ -710,7 +758,7 @@ public static void DrawRing(Vector3 origin, Quaternion orientation, Vector3 majo
710758

711759
indices[(c_ringSegments - 1) * 2 + 1] = 0;
712760

713-
float fAngleDelta = MathUtil.PI2 / c_ringSegments;
761+
float fAngleDelta = PI2 / c_ringSegments;
714762

715763
// Instead of calling cos/sin for each segment we calculate
716764
// the sign of the angle delta and then incrementally calculate sin
@@ -763,7 +811,7 @@ public static void DrawRing(Vector3 origin, Vector3 majorAxis, Vector3 minorAxis
763811

764812
indices[(c_ringSegments - 1) * 2 + 1] = 0;
765813

766-
float fAngleDelta = MathUtil.PI2 / c_ringSegments;
814+
float fAngleDelta = PI2 / c_ringSegments;
767815

768816
// Instead of calling cos/sin for each segment we calculate
769817
// the sign of the angle delta and then incrementally calculate sin
@@ -818,7 +866,7 @@ public static void DrawRing(Vector3 origin, (Vector3 majorAxis, Vector3 minorAxi
818866

819867
Vector3 majorAxis = ellipse.majorAxis;
820868
Vector3 minorAxis = ellipse.minorAxis;
821-
float fAngleDelta = MathUtil.PI2 / c_ringSegments;
869+
float fAngleDelta = PI2 / c_ringSegments;
822870

823871
// Instead of calling cos/sin for each segment we calculate
824872
// the sign of the angle delta and then incrementally calculate sin
@@ -876,7 +924,7 @@ public static void DrawRingBillboard(Vector3 origin, Vector3 camPos, Vector3 cam
876924

877925
Vector3 majorAxis = ellipse.majorAxis;
878926
Vector3 minorAxis = ellipse.minorAxis;
879-
float fAngleDelta = MathUtil.PI2 / c_ringSegments;
927+
float fAngleDelta = PI2 / c_ringSegments;
880928

881929
var mat = MathUtil.BillboardLH(origin, camPos, camUp, camForward);
882930

Hexa.NET.DebugDraw/DebugDrawContext.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace Hexa.NET.DebugDraw
22
{
3-
using Hexa.NET.Mathematics;
43
using System.Collections.Generic;
54
using System.Numerics;
65

@@ -22,7 +21,7 @@ internal DebugDrawContext()
2221

2322
public Matrix4x4 Camera => drawData.Camera;
2423

25-
public Viewport Viewport => drawData.Viewport;
24+
public DebugDrawViewport Viewport => drawData.Viewport;
2625

2726
public nint FontTextureId { get; set; }
2827

@@ -33,11 +32,16 @@ public void SetCamera(Matrix4x4 camera)
3332
drawData.Camera = camera;
3433
}
3534

36-
public void SetViewport(Viewport viewport)
35+
public void SetViewport(DebugDrawViewport viewport)
3736
{
3837
drawData.Viewport = viewport;
3938
}
4039

40+
public void SetViewport(Vector2 offset, Vector2 size)
41+
{
42+
drawData.Viewport = new(offset, size);
43+
}
44+
4145
public void NewFrame()
4246
{
4347
drawData.CmdLists.Clear();

Hexa.NET.DebugDraw/DebugDrawData.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Hexa.NET.DebugDraw
44
{
5-
using Hexa.NET.Mathematics;
65
using System.Numerics;
76

87
/// <summary>
@@ -28,7 +27,7 @@ public unsafe class DebugDrawData
2827
/// <summary>
2928
/// Gets or sets the viewport information.
3029
/// </summary>
31-
public Viewport Viewport;
30+
public DebugDrawViewport Viewport;
3231

3332
/// <summary>
3433
/// Gets or sets the camera matrix.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace Hexa.NET.DebugDraw
2+
{
3+
using System.Numerics;
4+
5+
public struct DebugDrawViewport
6+
{
7+
/// <summary>
8+
/// Position of the pixel coordinate of the upper-left corner of the viewport.
9+
/// </summary>
10+
public float X;
11+
12+
/// <summary>
13+
/// Position of the pixel coordinate of the upper-left corner of the viewport.
14+
/// </summary>
15+
public float Y;
16+
17+
/// <summary>
18+
/// Width dimension of the viewport.
19+
/// </summary>
20+
public float Width;
21+
22+
/// <summary>
23+
/// Height dimension of the viewport.
24+
/// </summary>
25+
public float Height;
26+
27+
/// <summary>
28+
/// Gets a <see cref="Vector2"/> representing the offset of the viewport.
29+
/// </summary>
30+
public readonly Vector2 Offset => new(X, Y);
31+
32+
/// <summary>
33+
/// Gets a <see cref="Vector2"/> representing the size of the viewport.
34+
/// </summary>
35+
public readonly Vector2 Size => new(Width, Height);
36+
37+
public DebugDrawViewport(Vector2 offset, Vector2 size)
38+
{
39+
X = offset.X;
40+
Y = offset.Y;
41+
Width = size.X;
42+
Height = size.Y;
43+
}
44+
}
45+
}

Hexa.NET.DebugDraw/Hexa.NET.DebugDraw.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<PropertyGroup>
1919
<PackageId>Hexa.NET.DebugDraw</PackageId>
2020
<AssemblyVersion>1.0.0</AssemblyVersion>
21-
<PackageVersion>2.2.0</PackageVersion>
21+
<PackageVersion>2.3.0</PackageVersion>
2222
<Authors>Juna</Authors>
2323
<AssemblyName>Hexa.NET.DebugDraw</AssemblyName>
2424
<PackageProjectUrl>https://github.com/HexaEngine/Hexa.NET.DebugDraw</PackageProjectUrl>
@@ -36,7 +36,6 @@
3636
</ItemGroup>
3737

3838
<ItemGroup>
39-
<PackageReference Include="Hexa.NET.Math" Version="2.0.0-*" />
4039
<PackageReference Include="Hexa.NET.Utilities" Version="2.0.0" />
4140
</ItemGroup>
4241

0 commit comments

Comments
 (0)