Skip to content

Commit

Permalink
Allocation of some modules into separate projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Aug 9, 2024
1 parent da6637d commit 5bc21f2
Show file tree
Hide file tree
Showing 16 changed files with 205 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Hypercube.Client.Graphics.Drawing;
using Hypercube.Client.Graphics.Events;
using Hypercube.Client.Graphics.Shaders;
using Hypercube.Client.Graphics.Windows;
using Hypercube.Client.Resources.Caching;
using Hypercube.Graphics.Shaders;
using Hypercube.Math;
using Hypercube.Math.Matrices;
using Hypercube.OpenGL.Objects;
using Hypercube.Shared.Runtimes.Loop.Event;
using OpenToolkit.Graphics.OpenGL4;

Expand Down
1 change: 1 addition & 0 deletions Hypercube.Client/Hypercube.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<ItemGroup>
<ProjectReference Include="..\Hypercube.Math\Hypercube.Math.csproj" />
<ProjectReference Include="..\Hypercube.OpenGL\Hypercube.OpenGL.csproj" />
<ProjectReference Include="..\Hypercube.Shared\Hypercube.Shared.csproj" />
</ItemGroup>

Expand Down
10 changes: 7 additions & 3 deletions Hypercube.Client/Resources/Caching/ShaderSourceResource.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Hypercube.Client.Graphics.Realisation.OpenGL.Shaders;
using Hypercube.Client.Graphics.Shaders;
using Hypercube.Graphics.Shaders;
using Hypercube.OpenGL.Shaders;
using Hypercube.Shared.Dependency;
using Hypercube.Shared.Resources;
using Hypercube.Shared.Resources.Manager;
Expand Down Expand Up @@ -30,7 +30,11 @@ public ShaderSourceResource(ResourcePath path)

protected override void OnLoad(ResourcePath path, DependenciesContainer container)
{
ShaderProgram = new ShaderProgram(path, container.Resolve<IResourceLoader>());
var resourceLoader = container.Resolve<IResourceLoader>();
var vertSource = resourceLoader.ReadFileContentAllText($"{path}.vert");
var fragSource = resourceLoader.ReadFileContentAllText($"{path}.frag");

ShaderProgram = new ShaderProgram(vertSource, fragSource);
}

public void Dispose()
Expand Down
13 changes: 13 additions & 0 deletions Hypercube.GlfwImGui/Hypercube.GlfwImGui.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ImGui.NET" Version="1.91.0.1" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions Hypercube.Graphics/Hypercube.Graphics.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Hypercube.Math\Hypercube.Math.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
namespace Hypercube.Client.Graphics.Shaders;
using JetBrains.Annotations;

namespace Hypercube.Graphics.Shaders;

/// <summary>
/// Part of the shader <see cref="IShaderProgram"/>, basically
/// a fragment or vertex shader.
/// </summary>
[PublicAPI]
public interface IShader : IDisposable
{
int Handle { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using Hypercube.Math.Matrices;
using Hypercube.Math.Vectors;
using JetBrains.Annotations;

namespace Hypercube.Client.Graphics.Shaders;
namespace Hypercube.Graphics.Shaders;

/// <summary>
/// What is usually called just a shader,
/// when created creates a fragment and vertex shader,
/// which are <see cref="Attach"/> and dispose afterwards.
/// </summary>
[PublicAPI]
public interface IShaderProgram : IDisposable
{
int Handle { get; }
Expand Down
47 changes: 47 additions & 0 deletions Hypercube.ImGui/GlfwImGuiController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using ImGuiNET;
using JetBrains.Annotations;

namespace Hypercube.ImGui;

[PublicAPI]
public class GlfwImGuiController : IDisposable
{
private readonly nint _window;

private readonly bool[] _mousePressed;
private readonly nint[] _mouseCursors;

private ImGuiIOPtr _io;

public GlfwImGuiController(nint window)
{
_window = window;
_mousePressed = new bool[(int) ImGuiMouseButton.COUNT];
_mouseCursors = new nint[(int) ImGuiMouseCursor.COUNT];

ImGuiNET.ImGui.CreateContext();
ImGuiNET.ImGui.StyleColorsDark();

InitializeIo();
InitializeGlfw();
}

public void InitializeIo()
{
_io = ImGuiNET.ImGui.GetIO();

_io.BackendFlags |= ImGuiBackendFlags.HasMouseCursors;
_io.BackendFlags |= ImGuiBackendFlags.HasSetMousePos;
_io.BackendFlags |= ImGuiBackendFlags.RendererHasVtxOffset;
}

public void InitializeGlfw()
{

}

public void Dispose()
{

}
}
18 changes: 18 additions & 0 deletions Hypercube.ImGui/Hypercube.ImGui.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ImGui.NET" Version="1.91.0.1" />
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Hypercube.OpenGL\Hypercube.OpenGL.csproj" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions Hypercube.Math/Matrices/Matrix3X3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ public Matrix3X3(Vector3 value) : this(value, value, value)
{
}

public Matrix3X3(Matrix3X3 matrix3X3) : this(matrix3X3.Row0, matrix3X3.Row1, matrix3X3.Row2)
{
}

/// <summary>
/// Creates 3x3 matrix
/// <code>
Expand Down
21 changes: 21 additions & 0 deletions Hypercube.OpenGL/Hypercube.OpenGL.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0" />
<PackageReference Include="OpenToolkit.Core" Version="4.0.0-pre9.3" />
<PackageReference Include="OpenToolkit.Graphics" Version="4.0.0-pre9.3" />
<PackageReference Include="OpenToolkit.Input" Version="4.0.0-pre9.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Hypercube.Graphics\Hypercube.Graphics.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using OpenToolkit.Graphics.OpenGL4;
using JetBrains.Annotations;
using OpenToolkit.Graphics.OpenGL4;

namespace Hypercube.Client.Graphics.Realisation.OpenGL;
namespace Hypercube.OpenGL.Objects;

public sealed class ArrayObject
[PublicAPI]
public class ArrayObject
{
public const int Null = 0;

public readonly int Handle;

private bool _binded;
private bool _bound;

public ArrayObject()
{
Expand All @@ -17,28 +19,28 @@ public ArrayObject()

public void Bind()
{
if (_binded)
if (_bound)
return;

_binded = true;
_bound = true;
GL.BindVertexArray(Handle);
}

public void Unbind()
{
if (!_binded)
if (!_bound)
return;

_binded = false;
_bound = false;
GL.BindVertexArray(Null);
}

public void DrawElements(int start, int count)
public static void DrawElements(int start, int count)
{
DrawElements(BeginMode.Triangles, start, count, DrawElementsType.UnsignedInt);
}

public void DrawElements(BeginMode mode, int start, int count, DrawElementsType type)
public static void DrawElements(BeginMode mode, int start, int count, DrawElementsType type)
{
GL.DrawElements(mode, count, type, start);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using Hypercube.Shared.Utilities.Helpers;
using System.Runtime.InteropServices;
using JetBrains.Annotations;
using OpenToolkit.Graphics.OpenGL4;

namespace Hypercube.Client.Graphics.Realisation.OpenGL;
namespace Hypercube.OpenGL.Objects;

public sealed class BufferObject : IDisposable
[PublicAPI]
public class BufferObject : IDisposable
{
public const int Null = 0;

public readonly int Handle;
public readonly BufferTarget BufferTarget;

private bool _binded;
private bool _bound;

public BufferObject(BufferTarget target)
{
Expand All @@ -20,19 +22,19 @@ public BufferObject(BufferTarget target)

public void Bind()
{
if (_binded)
if (_bound)
return;

_binded = true;
_bound = true;
GL.BindBuffer(BufferTarget, Handle);
}

public void Unbind()
{
if (!_binded)
if (!_bound)
return;

_binded = false;
_bound = false;
GL.BindBuffer(BufferTarget, Null);
}

Expand All @@ -44,7 +46,7 @@ public void Delete()
public void SetData<T>(T[] data, BufferUsageHint hint = BufferUsageHint.StaticDraw) where T : struct
{
Bind();
GL.BufferData(BufferTarget, data.Length * MarshalHelper.SizeOf<T>(), data, hint);
GL.BufferData(BufferTarget, data.Length * Marshal.SizeOf(default(T)), data, hint);
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Hypercube.Client.Graphics.Shaders;
using Hypercube.Graphics.Shaders;
using JetBrains.Annotations;
using OpenToolkit.Graphics.OpenGL4;

namespace Hypercube.Client.Graphics.Realisation.OpenGL.Shaders;
namespace Hypercube.OpenGL.Shaders;

public sealed class Shader : IShader
[PublicAPI]
public class Shader : IShader
{
public int Handle { get; }
public ShaderType Type { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
using System.Collections.Frozen;
using Hypercube.Client.Graphics.Shaders;
using Hypercube.Graphics.Shaders;
using Hypercube.Math.Matrices;
using Hypercube.Math.Vectors;
using Hypercube.Shared.Resources;
using Hypercube.Shared.Resources.Manager;
using JetBrains.Annotations;
using OpenToolkit.Graphics.OpenGL4;

namespace Hypercube.Client.Graphics.Realisation.OpenGL.Shaders;
namespace Hypercube.OpenGL.Shaders;

public sealed class ShaderProgram : IShaderProgram
[PublicAPI]
public class ShaderProgram : IShaderProgram
{
public int Handle { get; private set; }

private readonly FrozenDictionary<string, int> _uniformLocations;

public ShaderProgram(string path, IResourceLoader loader) : this(new ResourcePath($"{path}.vert"), new ResourcePath($"{path}.frag"),
loader)
{
}

private ShaderProgram(ResourcePath vertPath, ResourcePath fragPath, IResourceLoader resourceLoader)
public ShaderProgram(string vertSource, string fragSource)
{
var shaders = new HashSet<IShader>
{
CreateShader(vertPath, ShaderType.VertexShader, resourceLoader),
CreateShader(fragPath, ShaderType.FragmentShader, resourceLoader)
CreateShader(vertSource, ShaderType.VertexShader),
CreateShader(fragSource, ShaderType.FragmentShader)
};

Handle = GL.CreateProgram();
Expand Down Expand Up @@ -112,11 +108,10 @@ public void SetUniform(string name, Vector3 value)

public void SetUniform(string name, Matrix3X3 value, bool transpose = false)
{
throw new NotImplementedException();
unsafe
{
//var matrix = transpose ? Matrix3X3.Transpose(value) : new Matrix3X3(value);
//GL.UniformMatrix3(GL.GetUniformLocation(_handle, name), 1, false, (float*)&matrix);
var matrix = transpose ? Matrix3X3.Transpose(value) : new Matrix3X3(value);
GL.UniformMatrix3(GL.GetUniformLocation(Handle, name), 1, false, (float*) &matrix);
}
}

Expand Down Expand Up @@ -159,10 +154,8 @@ private void LinkProgram()
throw new Exception($"Error occurred whilst linking Program({Handle})");
}

private IShader CreateShader(ResourcePath path, ShaderType type, IResourceLoader resourceLoader)
private IShader CreateShader(string source, ShaderType type)
{
var source = resourceLoader.ReadFileContentAllText(path);
var shader = new Shader(source, type);
return shader;
return new Shader(source, type);
}
}
Loading

0 comments on commit 5bc21f2

Please sign in to comment.