From 3064355e159669f4b7a79ee9b5b508dd24980920 Mon Sep 17 00:00:00 2001 From: MrScautHD <65916181+MrScautHD@users.noreply.github.com> Date: Sun, 21 Jul 2024 11:15:25 +0200 Subject: [PATCH] Add Color, Add Vertex --- src/Bliss/CSharp/Colors/Color.cs | 169 ++++++++++++++++++ src/Bliss/CSharp/Geometry/Vertex.cs | 71 ++++++++ .../CSharp/Rendering/Vulkan/BlissPipeline.cs | 6 +- 3 files changed, 243 insertions(+), 3 deletions(-) create mode 100644 src/Bliss/CSharp/Colors/Color.cs create mode 100644 src/Bliss/CSharp/Geometry/Vertex.cs diff --git a/src/Bliss/CSharp/Colors/Color.cs b/src/Bliss/CSharp/Colors/Color.cs new file mode 100644 index 0000000..2e08ba0 --- /dev/null +++ b/src/Bliss/CSharp/Colors/Color.cs @@ -0,0 +1,169 @@ +using System.Numerics; + +namespace Bliss.CSharp.Colors; + +public struct Color { + + public static readonly Color White = new Color(255, 255, 255, 255); + public static readonly Color Black = new Color(0, 0, 0, 255); + + public static readonly Color LightRed = new Color(255, 102, 102, 255); + public static readonly Color Red = new Color(255, 0, 0, 255); + public static readonly Color DarkRed = new Color(139, 0, 0, 255); + + public static readonly Color LightGreen = new Color(144, 238, 144, 255); + public static readonly Color Green = new Color(0, 255, 0, 255); + public static readonly Color DarkGreen = new Color(0, 100, 0, 255); + + public static readonly Color LightBlue = new Color(173, 216, 230, 255); + public static readonly Color Blue = new Color(0, 0, 255, 255); + public static readonly Color DarkBlue = new Color(0, 0, 139, 255); + + public static readonly Color LightYellow = new Color(255, 255, 153, 255); + public static readonly Color Yellow = new Color(255, 255, 0, 255); + public static readonly Color DarkYellow = new Color(204, 204, 0, 255); + + public static readonly Color LightCyan = new Color(224, 255, 255, 255); + public static readonly Color Cyan = new Color(0, 255, 255, 255); + public static readonly Color DarkCyan = new Color(0, 139, 139, 255); + + public static readonly Color LightMagenta = new Color(255, 153, 255, 255); + public static readonly Color Magenta = new Color(255, 0, 255, 255); + public static readonly Color DarkMagenta = new Color(139, 0, 139, 255); + + public static readonly Color LightOrange = new Color(255, 200, 0, 255); + public static readonly Color Orange = new Color(255, 165, 0, 255); + public static readonly Color DarkOrange = new Color(255, 140, 0, 255); + + public static readonly Color LightBrown = new Color(205, 133, 63, 255); + public static readonly Color Brown = new Color(165, 42, 42, 255); + public static readonly Color DarkBrown = new Color(101, 67, 33, 255); + + public static readonly Color LightPurple = new Color(147, 112, 219, 255); + public static readonly Color Purple = new Color(128, 0, 128, 255); + public static readonly Color DarkPurple = new Color(75, 0, 130, 255); + + public static readonly Color LightPink = new Color(255, 182, 193, 255); + public static readonly Color Pink = new Color(255, 192, 203, 255); + public static readonly Color DarkPink = new Color(231, 84, 128, 255); + + public static readonly Color LightGray = new Color(211, 211, 211, 255); + public static readonly Color Gray = new Color(128, 128, 128, 255); + public static readonly Color DarkGray = new Color(169, 169, 169, 255); + + public float R; + public float G; + public float B; + public float A; + + /// + /// Initializes a new instance of the class. + /// + /// The red component. + /// The green component. + /// The blue component. + /// The alpha component. + public Color(byte r, byte g, byte b, byte a) { + this.R = r; + this.G = g; + this.B = b; + this.A = a; + } + + /// + /// Initializes a new instance of the class using HSV values. + /// + /// The hue component, in degrees (0-360). + /// The saturation component, as a percentage (0-100). + /// The value (brightness) component, as a percentage (0-100). + public Color(float hue, float saturation, float value) { + hue /= 360; + saturation /= 100; + value /= 100; + + float chroma = value * saturation; + float hue2 = hue * 6f; + float x = chroma * (1 - Math.Abs( (hue2 % 2) - 1)); + float r = 0, g = 0, b = 0; + + if( 0 <= hue2 && hue2 < 1) { + r = chroma; + g = x; + b = 0; + } + else if( 1 <= hue2 && hue2 < 2) { + r = x; + g = chroma; + b = 0; + } + else if( 2 <= hue2 && hue2 < 3) { + r = 0; + g = chroma; + b = x; + } + else if( 3 <= hue2 && hue2 < 4) { + r = 0; + g = x; + b = chroma; + } + else if( 4 <= hue2 && hue2 < 5) { + r = x; + g = 0; + b = chroma; + } + else if( 5 <= hue2 && hue2 < 6) { + r = chroma; + g = 0; + b = x; + } + + float m = value - chroma; + this.R = (r + m) * 255; + this.G = (g + m) * 255; + this.B = (b + m) * 255; + this.A = 255; + } + + /// + /// Converts the color from RGB to HSV (Hue, Saturation, Value) format. + /// + /// A representing the HSV values, where: + /// - X is Hue (0-360 degrees), + /// - Y is Saturation (0-100 percent), + /// - Z is Value (0-100 percent). + public Vector3 GetHsv() { + float r = this.R / 255.0f; + float g = this.G / 255.0f; + float b = this.B / 255.0f; + + float max = Math.Max(Math.Max(r, g), b); + float min = Math.Min(Math.Min(r, g), b); + + float h = max; + float s = max; + float v = max; + + float diff = max - min; + + s = max == 0.0 ? 0 : diff / max; + + if (max == min) { + h = 0; + } + else { + if (max == r) { + h = (g - b) / diff + (g < b ? 6 : 0); + } + else if (max == g) { + h = (b - r) / diff + 2; + } + else if (max == b) { + h = (r - g) / diff + 4; + } + + h /= 6.0f; + } + + return new Vector3(h * 360, s * 100, v * 100); + } +} \ No newline at end of file diff --git a/src/Bliss/CSharp/Geometry/Vertex.cs b/src/Bliss/CSharp/Geometry/Vertex.cs new file mode 100644 index 0000000..9fbdaa8 --- /dev/null +++ b/src/Bliss/CSharp/Geometry/Vertex.cs @@ -0,0 +1,71 @@ +using System.Numerics; +using System.Runtime.InteropServices; +using Bliss.CSharp.Colors; +using Silk.NET.Vulkan; + +namespace Bliss.CSharp.Geometry; + +public class Vertex { + + public Vector3 Position; + public Color Color; + public Vector3 Normal; + public Vector2 Uv; + + /// + /// Initializes a new instance of the class. + /// + /// The position of the vertex. + /// The color of the vertex. + public Vertex(Vector3 pos, Color color) { + this.Position = pos; + this.Color = color; + } + + /// + /// Get the binding descriptions for the Vertex class. + /// + /// An array of VertexInputBindingDescription objects. + public static VertexInputBindingDescription[] GetBindingDescriptions() { + return new[] { + new VertexInputBindingDescription() { + Binding = 0, + Stride = (uint) Marshal.SizeOf(), + InputRate = VertexInputRate.Vertex + } + }; + } + + /// + /// Get the attribute descriptions for the Vertex class. + /// + /// An array of VertexInputAttributeDescription objects. + public static VertexInputAttributeDescription[] GetAttributeDescriptions() { + return new[] { + new VertexInputAttributeDescription() { + Binding = 0, + Location = 0, + Format = Format.R32G32B32Sfloat, + Offset = (uint) Marshal.OffsetOf(nameof(Position)) + }, + new VertexInputAttributeDescription() { + Binding = 0, + Location = 1, + Format = Format.R32G32B32Sfloat, + Offset = (uint) Marshal.OffsetOf(nameof(Color.GetHsv)) + }, + new VertexInputAttributeDescription() { + Binding = 0, + Location = 2, + Format = Format.R32G32B32Sfloat, + Offset = (uint) Marshal.OffsetOf(nameof(Normal)) + }, + new VertexInputAttributeDescription() { + Binding = 0, + Location = 3, + Format = Format.R32G32Sfloat, + Offset = (uint) Marshal.OffsetOf(nameof(Uv)) + } + }; + } +} \ No newline at end of file diff --git a/src/Bliss/CSharp/Rendering/Vulkan/BlissPipeline.cs b/src/Bliss/CSharp/Rendering/Vulkan/BlissPipeline.cs index f89c3fa..4c424ce 100644 --- a/src/Bliss/CSharp/Rendering/Vulkan/BlissPipeline.cs +++ b/src/Bliss/CSharp/Rendering/Vulkan/BlissPipeline.cs @@ -1,7 +1,7 @@ using System.Reflection; using System.Runtime.CompilerServices; +using Bliss.CSharp.Geometry; using Silk.NET.Core.Native; -using Silk.NET.SDL; using Silk.NET.Vulkan; using BlendFactor = Silk.NET.Vulkan.BlendFactor; @@ -149,7 +149,7 @@ private unsafe void CreateGraphicsPipeline(string vertPath, string fragPath, Pip PName = (byte*) SilkMarshal.StringToPtr("main"), Flags = PipelineShaderStageCreateFlags.None, PNext = null, - PSpecializationInfo = null, + PSpecializationInfo = null }; PipelineShaderStageCreateInfo fragShaderStageInfo = new() { @@ -159,7 +159,7 @@ private unsafe void CreateGraphicsPipeline(string vertPath, string fragPath, Pip PName = (byte*) SilkMarshal.StringToPtr("main"), Flags = PipelineShaderStageCreateFlags.None, PNext = null, - PSpecializationInfo = null, + PSpecializationInfo = null }; PipelineShaderStageCreateInfo* shaderStages = stackalloc[] {