diff --git a/GlitchyEditor/content/Shaders/GammaCorrect.hlsl b/GlitchyEditor/content/Shaders/GammaCorrect.hlsl new file mode 100644 index 0000000..f7f6b40 --- /dev/null +++ b/GlitchyEditor/content/Shaders/GammaCorrect.hlsl @@ -0,0 +1,33 @@ +Texture2D Texture : register(t0); +SamplerState TextureSampler : register(s0); + +struct VS_IN +{ + float2 Position : POSITION; + float2 TexCoord : TEXCOORD0; +}; + +struct PS_IN +{ + float4 Position : SV_POSITION; + float2 TexCoord : TEXCOORD; +}; + +PS_IN VS(VS_IN input) +{ + PS_IN output; + + output.Position = float4(input.Position, 0, 1); + output.TexCoord = input.TexCoord; + + return output; +} + +float4 PS(PS_IN input) : SV_TARGET +{ + float4 color = Texture.Sample(TextureSampler, input.TexCoord); + + return float4(pow(color.rgb, 1.0f / 2.2f), color.a); +} + +#effect[VS = VS, PS = PS] diff --git a/GlitchyEditor/content/Shaders/SimpleTonemapping.hlsl b/GlitchyEditor/content/Shaders/SimpleTonemapping.hlsl index 1dcd64d..f1236ef 100644 --- a/GlitchyEditor/content/Shaders/SimpleTonemapping.hlsl +++ b/GlitchyEditor/content/Shaders/SimpleTonemapping.hlsl @@ -28,9 +28,7 @@ float4 PS(PS_IN input) : SV_TARGET float4 rawColor = CameraTarget.Sample(CameraTargetSampler, input.TexCoord); float3 color = rawColor.rgb / (rawColor.rgb + 1.0f); - - color = pow(color, 1.0f / 2.2f); - + return float4(color, 1); } diff --git a/GlitchyEditor/content/Shaders/spritebatch.hlsl b/GlitchyEditor/content/Shaders/spritebatch.hlsl index 3adacbf..df19e91 100644 --- a/GlitchyEditor/content/Shaders/spritebatch.hlsl +++ b/GlitchyEditor/content/Shaders/spritebatch.hlsl @@ -38,6 +38,9 @@ PS_Input VS(VS_Input input) output.Texcoord = input.UVTransform.xy + input.UVTransform.zw * input.Texcoord; output.Color = input.Color; + // Premultiply Alpha + output.Color.rgb *= output.Color.a; + #ifdef EDITOR output.EntityId = input.EntityId; #endif diff --git a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf index f656e61..d2539ba 100644 --- a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf +++ b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf @@ -247,7 +247,9 @@ namespace GlitchyEditor.EditWindows private static void ShowSpriteRendererComponentEditor(Entity entity, SpriterRendererComponent* spriteRendererComponent) { - ImGui.ColorEdit4("Color", ref spriteRendererComponent.Color); + ColorRGBA spriteColor = ColorRGBA.LinearToSRGB(spriteRendererComponent.Color); + if (ImGui.ColorEdit4("Color", ref spriteColor)) + spriteRendererComponent.Color = ColorRGBA.SRgbToLinear(spriteColor); } private static void ShowMeshRendererComponentEditor(Entity entity, MeshRendererComponent* meshRendererComponent) @@ -293,14 +295,26 @@ namespace GlitchyEditor.EditWindows if (variable.Columns == 3) { material.GetVariable(variable.Name, var value); + + value = (Vector3)ColorRGB.LinearToSRGB((ColorRGB)value); + if (ImGui.ColorEdit3(displayName.Ptr, *(float[3]*)&value)) + { + value = (Vector3)ColorRGB.SRgbToLinear((ColorRGB)value); material.SetVariable(variable.Name, value); + } } else if (variable.Columns == 4) { material.GetVariable(variable.Name, var value); + + value = (Vector4)ColorRGBA.LinearToSRGB((ColorRGBA)value); + if (ImGui.ColorEdit4(displayName.Ptr, *(float[4]*)&value)) + { + value = (Vector4)ColorRGBA.SRgbToLinear((ColorRGBA)value); material.SetVariable(variable.Name, value); + } } } else if (variable.Type == .Float && variable.Rows == 1) @@ -390,9 +404,9 @@ namespace GlitchyEditor.EditWindows ImGui.NextColumn(); LabelColumn("Color"); - ColorRGB color = light.Color; + ColorRGB color = ColorRGB.LinearToSRGB(light.Color); if (ImGui.ColorEdit3("##Color", ref color)) - light.Color = color; + light.Color = ColorRGB.SRgbToLinear(color); ImGui.NextColumn(); LabelColumn("Illuminance"); @@ -444,6 +458,7 @@ namespace GlitchyEditor.EditWindows ShowComponentButton("Camera"); ShowComponentButton("Sprite Renderer"); + ShowComponentButton("Light"); ImGui.EndCombo(); } diff --git a/GlitchyEditor/src/EditWindows/SceneViewportWindow.bf b/GlitchyEditor/src/EditWindows/SceneViewportWindow.bf index 356f17e..186a7a0 100644 --- a/GlitchyEditor/src/EditWindows/SceneViewportWindow.bf +++ b/GlitchyEditor/src/EditWindows/SceneViewportWindow.bf @@ -98,9 +98,9 @@ namespace GlitchyEditor.EditWindows if(_renderTarget != null) { - //ImGui.Image(_renderTarget.GetViewBinding(0), viewportSize); + ImGui.Image(_renderTarget.GetViewBinding(0), viewportSize); //ImGui.Image(_editor.CurrentCamera.RenderTarget.GetViewBinding(0), viewportSize); - ImGui.Image(_editor.CurrentScene.[Friend]_compositeTarget.GetViewBinding(0), viewportSize); + //ImGui.Image(_editor.CurrentScene.[Friend]_compositeTarget.GetViewBinding(0), viewportSize); } diff --git a/GlitchyEditor/src/EditorLayer.bf b/GlitchyEditor/src/EditorLayer.bf index 9cd8e1a..e3e19b0 100644 --- a/GlitchyEditor/src/EditorLayer.bf +++ b/GlitchyEditor/src/EditorLayer.bf @@ -126,11 +126,6 @@ namespace GlitchyEditor RenderCommand.Clear(_viewportTarget, .Color | .Depth, .(0.2f, 0.2f, 0.2f), 1.0f, 0); - RenderCommand.SetRenderTargetGroup(_viewportTarget, true); - RenderCommand.BindRenderTargets(); - - RenderCommand.SetViewport(Viewport(0, 0, _viewportTarget.Width, _viewportTarget.Height)); - RenderCommand.SetBlendState(_alphaBlendState); RenderCommand.SetDepthStencilState(_depthStencilState); @@ -177,7 +172,7 @@ namespace GlitchyEditor { if (_editor.EntityHierarchyWindow.SelectedEntities.Contains(.(entity, _scene))) { - DebugRenderer.DrawViewFrustum(transform.WorldTransform, camera.Camera.Projection, _camera.Projection * _camera.View); + DebugRenderer.DrawViewFrustum(transform.WorldTransform, camera.Camera.Projection, _camera.Projection * _camera.View, .White); } Matrix world = Billboard(transform.WorldTransform); @@ -204,7 +199,7 @@ namespace GlitchyEditor Matrix world = Billboard(transform.WorldTransform); float alpha = CalculateAlpha(transform.WorldTransform.Translation); - Renderer2D.DrawQuad(world, _iconDirectionalLight, ColorRGBA(light.SceneLight.Color.Red * alpha, light.SceneLight.Color.Green * alpha, light.SceneLight.Color.Blue * alpha, alpha), .(0, 0, 1, 1), entity.Index); + Renderer2D.DrawQuad(world, _iconDirectionalLight, ColorRGBA(light.SceneLight.Color.R * alpha, light.SceneLight.Color.G * alpha, light.SceneLight.Color.B * alpha, alpha), .(0, 0, 1, 1), entity.Index); //Renderer2D.DrawQuad(world, _iconDirectionalLight, ColorRGBA(light.SceneLight.Color, alpha), .(0, 0, 1, 1), entity.Index); } } diff --git a/GlitchyEngine/src/Math/Color.bf b/GlitchyEngine/src/Math/Color.bf new file mode 100644 index 0000000..69ceb13 --- /dev/null +++ b/GlitchyEngine/src/Math/Color.bf @@ -0,0 +1,234 @@ +using System; + +namespace GlitchyEngine.Math +{ + /// Represents a four component color with 8 bit per channel + [CRepr] + public struct Color + { + // from DirectXColors.h + // Todo: We probably loose some precision here... Could init them with the correct value + public const Color AliceBlue = .( 0.941176534f, 0.972549081f, 1.000000000f, 1.000000000f ); + public const Color AntiqueWhite = .( 0.980392218f, 0.921568692f, 0.843137324f, 1.000000000f ); + public const Color Aqua = .( 0.000000000f, 1.000000000f, 1.000000000f, 1.000000000f ); + public const Color Aquamarine = .( 0.498039246f, 1.000000000f, 0.831372619f, 1.000000000f ); + public const Color Azure = .( 0.941176534f, 1.000000000f, 1.000000000f, 1.000000000f ); + public const Color Beige = .( 0.960784376f, 0.960784376f, 0.862745166f, 1.000000000f ); + public const Color Bisque = .( 1.000000000f, 0.894117713f, 0.768627524f, 1.000000000f ); + public const Color Black = .( 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f ); + public const Color BlanchedAlmond = .( 1.000000000f, 0.921568692f, 0.803921640f, 1.000000000f ); + public const Color Blue = .( 0.000000000f, 0.000000000f, 1.000000000f, 1.000000000f ); + public const Color BlueViolet = .( 0.541176498f, 0.168627456f, 0.886274576f, 1.000000000f ); + public const Color Brown = .( 0.647058845f, 0.164705887f, 0.164705887f, 1.000000000f ); + public const Color BurlyWood = .( 0.870588303f, 0.721568644f, 0.529411793f, 1.000000000f ); + public const Color CadetBlue = .( 0.372549027f, 0.619607866f, 0.627451003f, 1.000000000f ); + public const Color Chartreuse = .( 0.498039246f, 1.000000000f, 0.000000000f, 1.000000000f ); + public const Color Chocolate = .( 0.823529482f, 0.411764741f, 0.117647067f, 1.000000000f ); + public const Color Coral = .( 1.000000000f, 0.498039246f, 0.313725501f, 1.000000000f ); + public const Color CornflowerBlue = .( 0.392156899f, 0.584313750f, 0.929411829f, 1.000000000f ); + public const Color Cornsilk = .( 1.000000000f, 0.972549081f, 0.862745166f, 1.000000000f ); + public const Color Crimson = .( 0.862745166f, 0.078431375f, 0.235294133f, 1.000000000f ); + public const Color Cyan = .( 0.000000000f, 1.000000000f, 1.000000000f, 1.000000000f ); + public const Color DarkBlue = .( 0.000000000f, 0.000000000f, 0.545098066f, 1.000000000f ); + public const Color DarkCyan = .( 0.000000000f, 0.545098066f, 0.545098066f, 1.000000000f ); + public const Color DarkGoldenrod = .( 0.721568644f, 0.525490224f, 0.043137256f, 1.000000000f ); + public const Color DarkGray = .( 0.662745118f, 0.662745118f, 0.662745118f, 1.000000000f ); + public const Color DarkGreen = .( 0.000000000f, 0.392156899f, 0.000000000f, 1.000000000f ); + public const Color DarkKhaki = .( 0.741176486f, 0.717647076f, 0.419607878f, 1.000000000f ); + public const Color DarkMagenta = .( 0.545098066f, 0.000000000f, 0.545098066f, 1.000000000f ); + public const Color DarkOliveGreen = .( 0.333333343f, 0.419607878f, 0.184313729f, 1.000000000f ); + public const Color DarkOrange = .( 1.000000000f, 0.549019635f, 0.000000000f, 1.000000000f ); + public const Color DarkOrchid = .( 0.600000024f, 0.196078449f, 0.800000072f, 1.000000000f ); + public const Color DarkRed = .( 0.545098066f, 0.000000000f, 0.000000000f, 1.000000000f ); + public const Color DarkSalmon = .( 0.913725555f, 0.588235319f, 0.478431404f, 1.000000000f ); + public const Color DarkSeaGreen = .( 0.560784340f, 0.737254918f, 0.545098066f, 1.000000000f ); + public const Color DarkSlateBlue = .( 0.282352954f, 0.239215702f, 0.545098066f, 1.000000000f ); + public const Color DarkSlateGray = .( 0.184313729f, 0.309803933f, 0.309803933f, 1.000000000f ); + public const Color DarkTurquoise = .( 0.000000000f, 0.807843208f, 0.819607913f, 1.000000000f ); + public const Color DarkViolet = .( 0.580392182f, 0.000000000f, 0.827451050f, 1.000000000f ); + public const Color DeepPink = .( 1.000000000f, 0.078431375f, 0.576470613f, 1.000000000f ); + public const Color DeepSkyBlue = .( 0.000000000f, 0.749019623f, 1.000000000f, 1.000000000f ); + public const Color DimGray = .( 0.411764741f, 0.411764741f, 0.411764741f, 1.000000000f ); + public const Color DodgerBlue = .( 0.117647067f, 0.564705908f, 1.000000000f, 1.000000000f ); + public const Color Firebrick = .( 0.698039234f, 0.133333340f, 0.133333340f, 1.000000000f ); + public const Color FloralWhite = .( 1.000000000f, 0.980392218f, 0.941176534f, 1.000000000f ); + public const Color ForestGreen = .( 0.133333340f, 0.545098066f, 0.133333340f, 1.000000000f ); + public const Color Fuchsia = .( 1.000000000f, 0.000000000f, 1.000000000f, 1.000000000f ); + public const Color Gainsboro = .( 0.862745166f, 0.862745166f, 0.862745166f, 1.000000000f ); + public const Color GhostWhite = .( 0.972549081f, 0.972549081f, 1.000000000f, 1.000000000f ); + public const Color Gold = .( 1.000000000f, 0.843137324f, 0.000000000f, 1.000000000f ); + public const Color Goldenrod = .( 0.854902029f, 0.647058845f, 0.125490203f, 1.000000000f ); + public const Color Gray = .( 0.501960814f, 0.501960814f, 0.501960814f, 1.000000000f ); + public const Color Green = .( 0.000000000f, 0.501960814f, 0.000000000f, 1.000000000f ); + public const Color GreenYellow = .( 0.678431392f, 1.000000000f, 0.184313729f, 1.000000000f ); + public const Color Honeydew = .( 0.941176534f, 1.000000000f, 0.941176534f, 1.000000000f ); + public const Color HotPink = .( 1.000000000f, 0.411764741f, 0.705882370f, 1.000000000f ); + public const Color IndianRed = .( 0.803921640f, 0.360784322f, 0.360784322f, 1.000000000f ); + public const Color Indigo = .( 0.294117659f, 0.000000000f, 0.509803951f, 1.000000000f ); + public const Color Ivory = .( 1.000000000f, 1.000000000f, 0.941176534f, 1.000000000f ); + public const Color Khaki = .( 0.941176534f, 0.901960850f, 0.549019635f, 1.000000000f ); + public const Color Lavender = .( 0.901960850f, 0.901960850f, 0.980392218f, 1.000000000f ); + public const Color LavenderBlush = .( 1.000000000f, 0.941176534f, 0.960784376f, 1.000000000f ); + public const Color LawnGreen = .( 0.486274540f, 0.988235354f, 0.000000000f, 1.000000000f ); + public const Color LemonChiffon = .( 1.000000000f, 0.980392218f, 0.803921640f, 1.000000000f ); + public const Color LightBlue = .( 0.678431392f, 0.847058892f, 0.901960850f, 1.000000000f ); + public const Color LightCoral = .( 0.941176534f, 0.501960814f, 0.501960814f, 1.000000000f ); + public const Color LightCyan = .( 0.878431439f, 1.000000000f, 1.000000000f, 1.000000000f ); + public const Color LightGoldenrodYellow = .( 0.980392218f, 0.980392218f, 0.823529482f, 1.000000000f ); + public const Color LightGreen = .( 0.564705908f, 0.933333397f, 0.564705908f, 1.000000000f ); + public const Color LightGray = .( 0.827451050f, 0.827451050f, 0.827451050f, 1.000000000f ); + public const Color LightPink = .( 1.000000000f, 0.713725507f, 0.756862819f, 1.000000000f ); + public const Color LightSalmon = .( 1.000000000f, 0.627451003f, 0.478431404f, 1.000000000f ); + public const Color LightSeaGreen = .( 0.125490203f, 0.698039234f, 0.666666687f, 1.000000000f ); + public const Color LightSkyBlue = .( 0.529411793f, 0.807843208f, 0.980392218f, 1.000000000f ); + public const Color LightSlateGray = .( 0.466666698f, 0.533333361f, 0.600000024f, 1.000000000f ); + public const Color LightSteelBlue = .( 0.690196097f, 0.768627524f, 0.870588303f, 1.000000000f ); + public const Color LightYellow = .( 1.000000000f, 1.000000000f, 0.878431439f, 1.000000000f ); + public const Color Lime = .( 0.000000000f, 1.000000000f, 0.000000000f, 1.000000000f ); + public const Color LimeGreen = .( 0.196078449f, 0.803921640f, 0.196078449f, 1.000000000f ); + public const Color Linen = .( 0.980392218f, 0.941176534f, 0.901960850f, 1.000000000f ); + public const Color Magenta = .( 1.000000000f, 0.000000000f, 1.000000000f, 1.000000000f ); + public const Color Maroon = .( 0.501960814f, 0.000000000f, 0.000000000f, 1.000000000f ); + public const Color MediumAquamarine = .( 0.400000036f, 0.803921640f, 0.666666687f, 1.000000000f ); + public const Color MediumBlue = .( 0.000000000f, 0.000000000f, 0.803921640f, 1.000000000f ); + public const Color MediumOrchid = .( 0.729411781f, 0.333333343f, 0.827451050f, 1.000000000f ); + public const Color MediumPurple = .( 0.576470613f, 0.439215720f, 0.858823597f, 1.000000000f ); + public const Color MediumSeaGreen = .( 0.235294133f, 0.701960802f, 0.443137288f, 1.000000000f ); + public const Color MediumSlateBlue = .( 0.482352972f, 0.407843173f, 0.933333397f, 1.000000000f ); + public const Color MediumSpringGreen = .( 0.000000000f, 0.980392218f, 0.603921592f, 1.000000000f ); + public const Color MediumTurquoise = .( 0.282352954f, 0.819607913f, 0.800000072f, 1.000000000f ); + public const Color MediumVioletRed = .( 0.780392230f, 0.082352944f, 0.521568656f, 1.000000000f ); + public const Color MidnightBlue = .( 0.098039225f, 0.098039225f, 0.439215720f, 1.000000000f ); + public const Color MintCream = .( 0.960784376f, 1.000000000f, 0.980392218f, 1.000000000f ); + public const Color MistyRose = .( 1.000000000f, 0.894117713f, 0.882353008f, 1.000000000f ); + public const Color Moccasin = .( 1.000000000f, 0.894117713f, 0.709803939f, 1.000000000f ); + public const Color NavajoWhite = .( 1.000000000f, 0.870588303f, 0.678431392f, 1.000000000f ); + public const Color Navy = .( 0.000000000f, 0.000000000f, 0.501960814f, 1.000000000f ); + public const Color OldLace = .( 0.992156923f, 0.960784376f, 0.901960850f, 1.000000000f ); + public const Color Olive = .( 0.501960814f, 0.501960814f, 0.000000000f, 1.000000000f ); + public const Color OliveDrab = .( 0.419607878f, 0.556862772f, 0.137254909f, 1.000000000f ); + public const Color Orange = .( 1.000000000f, 0.647058845f, 0.000000000f, 1.000000000f ); + public const Color OrangeRed = .( 1.000000000f, 0.270588249f, 0.000000000f, 1.000000000f ); + public const Color Orchid = .( 0.854902029f, 0.439215720f, 0.839215755f, 1.000000000f ); + public const Color PaleGoldenrod = .( 0.933333397f, 0.909803987f, 0.666666687f, 1.000000000f ); + public const Color PaleGreen = .( 0.596078455f, 0.984313786f, 0.596078455f, 1.000000000f ); + public const Color PaleTurquoise = .( 0.686274529f, 0.933333397f, 0.933333397f, 1.000000000f ); + public const Color PaleVioletRed = .( 0.858823597f, 0.439215720f, 0.576470613f, 1.000000000f ); + public const Color PapayaWhip = .( 1.000000000f, 0.937254965f, 0.835294187f, 1.000000000f ); + public const Color PeachPuff = .( 1.000000000f, 0.854902029f, 0.725490212f, 1.000000000f ); + public const Color Peru = .( 0.803921640f, 0.521568656f, 0.247058839f, 1.000000000f ); + public const Color Pink = .( 1.000000000f, 0.752941251f, 0.796078503f, 1.000000000f ); + public const Color Plum = .( 0.866666734f, 0.627451003f, 0.866666734f, 1.000000000f ); + public const Color PowderBlue = .( 0.690196097f, 0.878431439f, 0.901960850f, 1.000000000f ); + public const Color Purple = .( 0.501960814f, 0.000000000f, 0.501960814f, 1.000000000f ); + public const Color Red = .( 1.000000000f, 0.000000000f, 0.000000000f, 1.000000000f ); + public const Color RosyBrown = .( 0.737254918f, 0.560784340f, 0.560784340f, 1.000000000f ); + public const Color RoyalBlue = .( 0.254901975f, 0.411764741f, 0.882353008f, 1.000000000f ); + public const Color SaddleBrown = .( 0.545098066f, 0.270588249f, 0.074509807f, 1.000000000f ); + public const Color Salmon = .( 0.980392218f, 0.501960814f, 0.447058856f, 1.000000000f ); + public const Color SandyBrown = .( 0.956862807f, 0.643137276f, 0.376470625f, 1.000000000f ); + public const Color SeaGreen = .( 0.180392161f, 0.545098066f, 0.341176480f, 1.000000000f ); + public const Color SeaShell = .( 1.000000000f, 0.960784376f, 0.933333397f, 1.000000000f ); + public const Color Sienna = .( 0.627451003f, 0.321568638f, 0.176470593f, 1.000000000f ); + public const Color Silver = .( 0.752941251f, 0.752941251f, 0.752941251f, 1.000000000f ); + public const Color SkyBlue = .( 0.529411793f, 0.807843208f, 0.921568692f, 1.000000000f ); + public const Color SlateBlue = .( 0.415686309f, 0.352941185f, 0.803921640f, 1.000000000f ); + public const Color SlateGray = .( 0.439215720f, 0.501960814f, 0.564705908f, 1.000000000f ); + public const Color Snow = .( 1.000000000f, 0.980392218f, 0.980392218f, 1.000000000f ); + public const Color SpringGreen = .( 0.000000000f, 1.000000000f, 0.498039246f, 1.000000000f ); + public const Color SteelBlue = .( 0.274509817f, 0.509803951f, 0.705882370f, 1.000000000f ); + public const Color Tan = .( 0.823529482f, 0.705882370f, 0.549019635f, 1.000000000f ); + public const Color Teal = .( 0.000000000f, 0.501960814f, 0.501960814f, 1.000000000f ); + public const Color Thistle = .( 0.847058892f, 0.749019623f, 0.847058892f, 1.000000000f ); + public const Color Tomato = .( 1.000000000f, 0.388235331f, 0.278431386f, 1.000000000f ); + public const Color Transparent = .( 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f ); + public const Color Turquoise = .( 0.250980407f, 0.878431439f, 0.815686345f, 1.000000000f ); + public const Color Violet = .( 0.933333397f, 0.509803951f, 0.933333397f, 1.000000000f ); + public const Color Wheat = .( 0.960784376f, 0.870588303f, 0.701960802f, 1.000000000f ); + public const Color White = .( 1.000000000f, 1.000000000f, 1.000000000f, 1.000000000f ); + public const Color WhiteSmoke = .( 0.960784376f, 0.960784376f, 0.960784376f, 1.000000000f ); + public const Color Yellow = .( 1.000000000f, 1.000000000f, 0.000000000f, 1.000000000f ); + public const Color YellowGreen = .( 0.603921592f, 0.803921640f, 0.196078449f, 1.000000000f ); + + /// The red-component of the color + public uint8 R; + /// The green-component of the color + public uint8 G; + /// The blue-component of the color + public uint8 B; + /// The alpha-component of the color + public uint8 A; + + /// Creates a new instance of Color with all components set to 0. + public this() + { + this = default; + } + + /// Creates a new instance of Color with the RGB-values set to the specified values and alpha set to 255. + public this(uint8 r, uint8 g, uint8 b) + { + R = r; + G = g; + B = b; + A = 255; + } + + /// Creates a new instance of Color with the RGB-values set to the specified values and alpha set to 255. + public this(float r, float g, float b) : this(r, g, b, 1.0f) { } + + /// Creates a new instance of Color with the specified values. + public this(uint8 r, uint8 g, uint8 b, uint8 a) + { + R = r; + G = g; + B = b; + A = a; + } + + const float f = Math.Clamp(12.0f, uint8.MinValue, uint8.MaxValue); + + /// Creates a new instance of Color with the specified values. + public this(float r, float g, float b, float a) + { + R = (uint8)(int)Math.Clamp(r * 255.0f, uint8.MinValue, uint8.MaxValue); + G = (uint8)(int)Math.Clamp(g * 255.0f, uint8.MinValue, uint8.MaxValue); + B = (uint8)(int)Math.Clamp(b * 255.0f, uint8.MinValue, uint8.MaxValue); + A = (uint8)(int)Math.Clamp(a * 255.0f, uint8.MinValue, uint8.MaxValue); + } + + public ref uint8 this[int index] + { + [Unchecked] + get mut + { + return ref (&R)[index]; + } + + [Checked] + get mut + { + Runtime.Assert(index < 0 || index > 3); + return ref (&R)[index]; + } + } + + [Inline] + public uint8* ToPtr() mut + { + return &R; + } + + // Todo: mathematical operations + + public static explicit operator Color(ColorRGBA col) + { + return .(col.R, col.G, col.B, col.A); + } + + public static explicit operator ColorRGBA(Color col) + { + return .(col.R / 255.0f, col.G / 255.0f, col.B / 255.0f, col.A / 255.0f); + } + } +} diff --git a/GlitchyEngine/src/Math/ColorRGB.bf b/GlitchyEngine/src/Math/ColorRGB.bf new file mode 100644 index 0000000..3247c8f --- /dev/null +++ b/GlitchyEngine/src/Math/ColorRGB.bf @@ -0,0 +1,60 @@ +using System; + +using internal GlitchyEngine.Math; + +namespace GlitchyEngine.Math +{ + /// Represents an RGB color. + [CRepr] + public struct ColorRGB + { + /** + * A value representing the color of the red component. + * The range of this value is between 0 and 1. + */ + public float R; + /** + * A value representing the color of the green component. + * The range of this value is between 0 and 1. + */ + public float G; + /** + * A value representing the color of the blue component. + * The range of this value is between 0 and 1. + */ + public float B; + + public this() + { + this = default; + } + + public this(float red, float green, float blue) + { + R = red; + G = green; + B = blue; + } + + public this(ColorRGBA color) + { + R = color.R; + G = color.G; + B = color.B; + } + + // Converts a Color from sRGB color space to Linear color space. + public static ColorRGB SRgbToLinear(ColorRGB sRGB) => ColorRGB(Math.Pow(sRGB.R, srgbToLin), Math.Pow(sRGB.G, srgbToLin), Math.Pow(sRGB.B, srgbToLin)); + + // Converts a Color from linear color space to sRGB color space. + public static ColorRGB LinearToSRGB(ColorRGB linear) => ColorRGB(Math.Pow(linear.R, linToSRGB), Math.Pow(linear.G, linToSRGB), Math.Pow(linear.B, linToSRGB)); + + [Inline] +#unwarn + public static explicit operator Vector3(ColorRGB color) => *(Vector3*)&color; + + [Inline] +#unwarn + public static explicit operator ColorRGB(Vector3 color) => *(ColorRGB*)&color; + } +} diff --git a/GlitchyEngine/src/Math/ColorRGBA.bf b/GlitchyEngine/src/Math/ColorRGBA.bf new file mode 100644 index 0000000..11ced00 --- /dev/null +++ b/GlitchyEngine/src/Math/ColorRGBA.bf @@ -0,0 +1,327 @@ +using System; + +using internal GlitchyEngine.Math; + +namespace GlitchyEngine.Math +{ + static + { + internal const float srgbToLin = 2.2f; + internal const float linToSRGB = (float)(1.0 / 2.2); + } + + /** + Represents a four component floating point color + */ + [CRepr] + struct ColorRGBA + { + // from DirectXColors.h + public const ColorRGBA AliceBlue = .( 0.941176534f, 0.972549081f, 1.000000000f, 1.000000000f ); + public const ColorRGBA AntiqueWhite = .( 0.980392218f, 0.921568692f, 0.843137324f, 1.000000000f ); + public const ColorRGBA Aqua = .( 0.000000000f, 1.000000000f, 1.000000000f, 1.000000000f ); + public const ColorRGBA Aquamarine = .( 0.498039246f, 1.000000000f, 0.831372619f, 1.000000000f ); + public const ColorRGBA Azure = .( 0.941176534f, 1.000000000f, 1.000000000f, 1.000000000f ); + public const ColorRGBA Beige = .( 0.960784376f, 0.960784376f, 0.862745166f, 1.000000000f ); + public const ColorRGBA Bisque = .( 1.000000000f, 0.894117713f, 0.768627524f, 1.000000000f ); + public const ColorRGBA Black = .( 0.000000000f, 0.000000000f, 0.000000000f, 1.000000000f ); + public const ColorRGBA BlanchedAlmond = .( 1.000000000f, 0.921568692f, 0.803921640f, 1.000000000f ); + public const ColorRGBA Blue = .( 0.000000000f, 0.000000000f, 1.000000000f, 1.000000000f ); + public const ColorRGBA BlueViolet = .( 0.541176498f, 0.168627456f, 0.886274576f, 1.000000000f ); + public const ColorRGBA Brown = .( 0.647058845f, 0.164705887f, 0.164705887f, 1.000000000f ); + public const ColorRGBA BurlyWood = .( 0.870588303f, 0.721568644f, 0.529411793f, 1.000000000f ); + public const ColorRGBA CadetBlue = .( 0.372549027f, 0.619607866f, 0.627451003f, 1.000000000f ); + public const ColorRGBA Chartreuse = .( 0.498039246f, 1.000000000f, 0.000000000f, 1.000000000f ); + public const ColorRGBA Chocolate = .( 0.823529482f, 0.411764741f, 0.117647067f, 1.000000000f ); + public const ColorRGBA Coral = .( 1.000000000f, 0.498039246f, 0.313725501f, 1.000000000f ); + public const ColorRGBA CornflowerBlue = .( 0.392156899f, 0.584313750f, 0.929411829f, 1.000000000f ); + public const ColorRGBA Cornsilk = .( 1.000000000f, 0.972549081f, 0.862745166f, 1.000000000f ); + public const ColorRGBA Crimson = .( 0.862745166f, 0.078431375f, 0.235294133f, 1.000000000f ); + public const ColorRGBA Cyan = .( 0.000000000f, 1.000000000f, 1.000000000f, 1.000000000f ); + public const ColorRGBA DarkBlue = .( 0.000000000f, 0.000000000f, 0.545098066f, 1.000000000f ); + public const ColorRGBA DarkCyan = .( 0.000000000f, 0.545098066f, 0.545098066f, 1.000000000f ); + public const ColorRGBA DarkGoldenrod = .( 0.721568644f, 0.525490224f, 0.043137256f, 1.000000000f ); + public const ColorRGBA DarkGray = .( 0.662745118f, 0.662745118f, 0.662745118f, 1.000000000f ); + public const ColorRGBA DarkGreen = .( 0.000000000f, 0.392156899f, 0.000000000f, 1.000000000f ); + public const ColorRGBA DarkKhaki = .( 0.741176486f, 0.717647076f, 0.419607878f, 1.000000000f ); + public const ColorRGBA DarkMagenta = .( 0.545098066f, 0.000000000f, 0.545098066f, 1.000000000f ); + public const ColorRGBA DarkOliveGreen = .( 0.333333343f, 0.419607878f, 0.184313729f, 1.000000000f ); + public const ColorRGBA DarkOrange = .( 1.000000000f, 0.549019635f, 0.000000000f, 1.000000000f ); + public const ColorRGBA DarkOrchid = .( 0.600000024f, 0.196078449f, 0.800000072f, 1.000000000f ); + public const ColorRGBA DarkRed = .( 0.545098066f, 0.000000000f, 0.000000000f, 1.000000000f ); + public const ColorRGBA DarkSalmon = .( 0.913725555f, 0.588235319f, 0.478431404f, 1.000000000f ); + public const ColorRGBA DarkSeaGreen = .( 0.560784340f, 0.737254918f, 0.545098066f, 1.000000000f ); + public const ColorRGBA DarkSlateBlue = .( 0.282352954f, 0.239215702f, 0.545098066f, 1.000000000f ); + public const ColorRGBA DarkSlateGray = .( 0.184313729f, 0.309803933f, 0.309803933f, 1.000000000f ); + public const ColorRGBA DarkTurquoise = .( 0.000000000f, 0.807843208f, 0.819607913f, 1.000000000f ); + public const ColorRGBA DarkViolet = .( 0.580392182f, 0.000000000f, 0.827451050f, 1.000000000f ); + public const ColorRGBA DeepPink = .( 1.000000000f, 0.078431375f, 0.576470613f, 1.000000000f ); + public const ColorRGBA DeepSkyBlue = .( 0.000000000f, 0.749019623f, 1.000000000f, 1.000000000f ); + public const ColorRGBA DimGray = .( 0.411764741f, 0.411764741f, 0.411764741f, 1.000000000f ); + public const ColorRGBA DodgerBlue = .( 0.117647067f, 0.564705908f, 1.000000000f, 1.000000000f ); + public const ColorRGBA Firebrick = .( 0.698039234f, 0.133333340f, 0.133333340f, 1.000000000f ); + public const ColorRGBA FloralWhite = .( 1.000000000f, 0.980392218f, 0.941176534f, 1.000000000f ); + public const ColorRGBA ForestGreen = .( 0.133333340f, 0.545098066f, 0.133333340f, 1.000000000f ); + public const ColorRGBA Fuchsia = .( 1.000000000f, 0.000000000f, 1.000000000f, 1.000000000f ); + public const ColorRGBA Gainsboro = .( 0.862745166f, 0.862745166f, 0.862745166f, 1.000000000f ); + public const ColorRGBA GhostWhite = .( 0.972549081f, 0.972549081f, 1.000000000f, 1.000000000f ); + public const ColorRGBA Gold = .( 1.000000000f, 0.843137324f, 0.000000000f, 1.000000000f ); + public const ColorRGBA Goldenrod = .( 0.854902029f, 0.647058845f, 0.125490203f, 1.000000000f ); + public const ColorRGBA Gray = .( 0.501960814f, 0.501960814f, 0.501960814f, 1.000000000f ); + public const ColorRGBA Green = .( 0.000000000f, 0.501960814f, 0.000000000f, 1.000000000f ); + public const ColorRGBA GreenYellow = .( 0.678431392f, 1.000000000f, 0.184313729f, 1.000000000f ); + public const ColorRGBA Honeydew = .( 0.941176534f, 1.000000000f, 0.941176534f, 1.000000000f ); + public const ColorRGBA HotPink = .( 1.000000000f, 0.411764741f, 0.705882370f, 1.000000000f ); + public const ColorRGBA IndianRed = .( 0.803921640f, 0.360784322f, 0.360784322f, 1.000000000f ); + public const ColorRGBA Indigo = .( 0.294117659f, 0.000000000f, 0.509803951f, 1.000000000f ); + public const ColorRGBA Ivory = .( 1.000000000f, 1.000000000f, 0.941176534f, 1.000000000f ); + public const ColorRGBA Khaki = .( 0.941176534f, 0.901960850f, 0.549019635f, 1.000000000f ); + public const ColorRGBA Lavender = .( 0.901960850f, 0.901960850f, 0.980392218f, 1.000000000f ); + public const ColorRGBA LavenderBlush = .( 1.000000000f, 0.941176534f, 0.960784376f, 1.000000000f ); + public const ColorRGBA LawnGreen = .( 0.486274540f, 0.988235354f, 0.000000000f, 1.000000000f ); + public const ColorRGBA LemonChiffon = .( 1.000000000f, 0.980392218f, 0.803921640f, 1.000000000f ); + public const ColorRGBA LightBlue = .( 0.678431392f, 0.847058892f, 0.901960850f, 1.000000000f ); + public const ColorRGBA LightCoral = .( 0.941176534f, 0.501960814f, 0.501960814f, 1.000000000f ); + public const ColorRGBA LightCyan = .( 0.878431439f, 1.000000000f, 1.000000000f, 1.000000000f ); + public const ColorRGBA LightGoldenrodYellow = .( 0.980392218f, 0.980392218f, 0.823529482f, 1.000000000f ); + public const ColorRGBA LightGreen = .( 0.564705908f, 0.933333397f, 0.564705908f, 1.000000000f ); + public const ColorRGBA LightGray = .( 0.827451050f, 0.827451050f, 0.827451050f, 1.000000000f ); + public const ColorRGBA LightPink = .( 1.000000000f, 0.713725507f, 0.756862819f, 1.000000000f ); + public const ColorRGBA LightSalmon = .( 1.000000000f, 0.627451003f, 0.478431404f, 1.000000000f ); + public const ColorRGBA LightSeaGreen = .( 0.125490203f, 0.698039234f, 0.666666687f, 1.000000000f ); + public const ColorRGBA LightSkyBlue = .( 0.529411793f, 0.807843208f, 0.980392218f, 1.000000000f ); + public const ColorRGBA LightSlateGray = .( 0.466666698f, 0.533333361f, 0.600000024f, 1.000000000f ); + public const ColorRGBA LightSteelBlue = .( 0.690196097f, 0.768627524f, 0.870588303f, 1.000000000f ); + public const ColorRGBA LightYellow = .( 1.000000000f, 1.000000000f, 0.878431439f, 1.000000000f ); + public const ColorRGBA Lime = .( 0.000000000f, 1.000000000f, 0.000000000f, 1.000000000f ); + public const ColorRGBA LimeGreen = .( 0.196078449f, 0.803921640f, 0.196078449f, 1.000000000f ); + public const ColorRGBA Linen = .( 0.980392218f, 0.941176534f, 0.901960850f, 1.000000000f ); + public const ColorRGBA Magenta = .( 1.000000000f, 0.000000000f, 1.000000000f, 1.000000000f ); + public const ColorRGBA Maroon = .( 0.501960814f, 0.000000000f, 0.000000000f, 1.000000000f ); + public const ColorRGBA MediumAquamarine = .( 0.400000036f, 0.803921640f, 0.666666687f, 1.000000000f ); + public const ColorRGBA MediumBlue = .( 0.000000000f, 0.000000000f, 0.803921640f, 1.000000000f ); + public const ColorRGBA MediumOrchid = .( 0.729411781f, 0.333333343f, 0.827451050f, 1.000000000f ); + public const ColorRGBA MediumPurple = .( 0.576470613f, 0.439215720f, 0.858823597f, 1.000000000f ); + public const ColorRGBA MediumSeaGreen = .( 0.235294133f, 0.701960802f, 0.443137288f, 1.000000000f ); + public const ColorRGBA MediumSlateBlue = .( 0.482352972f, 0.407843173f, 0.933333397f, 1.000000000f ); + public const ColorRGBA MediumSpringGreen = .( 0.000000000f, 0.980392218f, 0.603921592f, 1.000000000f ); + public const ColorRGBA MediumTurquoise = .( 0.282352954f, 0.819607913f, 0.800000072f, 1.000000000f ); + public const ColorRGBA MediumVioletRed = .( 0.780392230f, 0.082352944f, 0.521568656f, 1.000000000f ); + public const ColorRGBA MidnightBlue = .( 0.098039225f, 0.098039225f, 0.439215720f, 1.000000000f ); + public const ColorRGBA MintCream = .( 0.960784376f, 1.000000000f, 0.980392218f, 1.000000000f ); + public const ColorRGBA MistyRose = .( 1.000000000f, 0.894117713f, 0.882353008f, 1.000000000f ); + public const ColorRGBA Moccasin = .( 1.000000000f, 0.894117713f, 0.709803939f, 1.000000000f ); + public const ColorRGBA NavajoWhite = .( 1.000000000f, 0.870588303f, 0.678431392f, 1.000000000f ); + public const ColorRGBA Navy = .( 0.000000000f, 0.000000000f, 0.501960814f, 1.000000000f ); + public const ColorRGBA OldLace = .( 0.992156923f, 0.960784376f, 0.901960850f, 1.000000000f ); + public const ColorRGBA Olive = .( 0.501960814f, 0.501960814f, 0.000000000f, 1.000000000f ); + public const ColorRGBA OliveDrab = .( 0.419607878f, 0.556862772f, 0.137254909f, 1.000000000f ); + public const ColorRGBA Orange = .( 1.000000000f, 0.647058845f, 0.000000000f, 1.000000000f ); + public const ColorRGBA OrangeRed = .( 1.000000000f, 0.270588249f, 0.000000000f, 1.000000000f ); + public const ColorRGBA Orchid = .( 0.854902029f, 0.439215720f, 0.839215755f, 1.000000000f ); + public const ColorRGBA PaleGoldenrod = .( 0.933333397f, 0.909803987f, 0.666666687f, 1.000000000f ); + public const ColorRGBA PaleGreen = .( 0.596078455f, 0.984313786f, 0.596078455f, 1.000000000f ); + public const ColorRGBA PaleTurquoise = .( 0.686274529f, 0.933333397f, 0.933333397f, 1.000000000f ); + public const ColorRGBA PaleVioletRed = .( 0.858823597f, 0.439215720f, 0.576470613f, 1.000000000f ); + public const ColorRGBA PapayaWhip = .( 1.000000000f, 0.937254965f, 0.835294187f, 1.000000000f ); + public const ColorRGBA PeachPuff = .( 1.000000000f, 0.854902029f, 0.725490212f, 1.000000000f ); + public const ColorRGBA Peru = .( 0.803921640f, 0.521568656f, 0.247058839f, 1.000000000f ); + public const ColorRGBA Pink = .( 1.000000000f, 0.752941251f, 0.796078503f, 1.000000000f ); + public const ColorRGBA Plum = .( 0.866666734f, 0.627451003f, 0.866666734f, 1.000000000f ); + public const ColorRGBA PowderBlue = .( 0.690196097f, 0.878431439f, 0.901960850f, 1.000000000f ); + public const ColorRGBA Purple = .( 0.501960814f, 0.000000000f, 0.501960814f, 1.000000000f ); + public const ColorRGBA Red = .( 1.000000000f, 0.000000000f, 0.000000000f, 1.000000000f ); + public const ColorRGBA RosyBrown = .( 0.737254918f, 0.560784340f, 0.560784340f, 1.000000000f ); + public const ColorRGBA RoyalBlue = .( 0.254901975f, 0.411764741f, 0.882353008f, 1.000000000f ); + public const ColorRGBA SaddleBrown = .( 0.545098066f, 0.270588249f, 0.074509807f, 1.000000000f ); + public const ColorRGBA Salmon = .( 0.980392218f, 0.501960814f, 0.447058856f, 1.000000000f ); + public const ColorRGBA SandyBrown = .( 0.956862807f, 0.643137276f, 0.376470625f, 1.000000000f ); + public const ColorRGBA SeaGreen = .( 0.180392161f, 0.545098066f, 0.341176480f, 1.000000000f ); + public const ColorRGBA SeaShell = .( 1.000000000f, 0.960784376f, 0.933333397f, 1.000000000f ); + public const ColorRGBA Sienna = .( 0.627451003f, 0.321568638f, 0.176470593f, 1.000000000f ); + public const ColorRGBA Silver = .( 0.752941251f, 0.752941251f, 0.752941251f, 1.000000000f ); + public const ColorRGBA SkyBlue = .( 0.529411793f, 0.807843208f, 0.921568692f, 1.000000000f ); + public const ColorRGBA SlateBlue = .( 0.415686309f, 0.352941185f, 0.803921640f, 1.000000000f ); + public const ColorRGBA SlateGray = .( 0.439215720f, 0.501960814f, 0.564705908f, 1.000000000f ); + public const ColorRGBA Snow = .( 1.000000000f, 0.980392218f, 0.980392218f, 1.000000000f ); + public const ColorRGBA SpringGreen = .( 0.000000000f, 1.000000000f, 0.498039246f, 1.000000000f ); + public const ColorRGBA SteelBlue = .( 0.274509817f, 0.509803951f, 0.705882370f, 1.000000000f ); + public const ColorRGBA Tan = .( 0.823529482f, 0.705882370f, 0.549019635f, 1.000000000f ); + public const ColorRGBA Teal = .( 0.000000000f, 0.501960814f, 0.501960814f, 1.000000000f ); + public const ColorRGBA Thistle = .( 0.847058892f, 0.749019623f, 0.847058892f, 1.000000000f ); + public const ColorRGBA Tomato = .( 1.000000000f, 0.388235331f, 0.278431386f, 1.000000000f ); + public const ColorRGBA Transparent = .( 0.000000000f, 0.000000000f, 0.000000000f, 0.000000000f ); + public const ColorRGBA Turquoise = .( 0.250980407f, 0.878431439f, 0.815686345f, 1.000000000f ); + public const ColorRGBA Violet = .( 0.933333397f, 0.509803951f, 0.933333397f, 1.000000000f ); + public const ColorRGBA Wheat = .( 0.960784376f, 0.870588303f, 0.701960802f, 1.000000000f ); + public const ColorRGBA White = .( 1.000000000f, 1.000000000f, 1.000000000f, 1.000000000f ); + public const ColorRGBA WhiteSmoke = .( 0.960784376f, 0.960784376f, 0.960784376f, 1.000000000f ); + public const ColorRGBA Yellow = .( 1.000000000f, 1.000000000f, 0.000000000f, 1.000000000f ); + public const ColorRGBA YellowGreen = .( 0.603921592f, 0.803921640f, 0.196078449f, 1.000000000f ); + + /// The red-component of the color + public float R; + /// The green-component of the color + public float G; + /// The blue-component of the color + public float B; + /// The alpha-component of the color + public float A; + + /// Creates a new instance of ColorRGBA with all components set to 0. + public this() + { + this = default; + } + + /// Creates a new instance of ColorRGBA with the specified values. + public this(float r, float g, float b, float a = 1.0f) + { + R = r; + G = g; + B = b; + A = a; + } + + /// Creates a new instance of ColorRGBA with the specified values. + public this(ColorRGB color, float a = 1.0f) + { + R = color.R; + G = color.G; + B = color.B; + A = a; + } + + public ref float this[int index] + { + [Unchecked] + get mut + { + return ref (&R)[index]; + } + + [Checked] + get mut + { + Runtime.Assert(index < 0 || index > 3); + return ref (&R)[index]; + } + } + + [Inline] + public float* ToPtr() mut + { + return &R; + } + + /// + /// Addition + /// + + public void operator +=(ColorRGBA value) mut + { + R += value.R; + G += value.G; + B += value.B; + A += value.A; + } + + public static ColorRGBA operator +(ColorRGBA left, ColorRGBA right) + { + return .(left.R + right.R, left.G + right.G, left.B + right.B, left.A + right.A); + } + + /// + /// Subtraction + /// + + public void operator -=(ColorRGBA value) mut + { + R -= value.R; + G -= value.G; + B -= value.B; + A -= value.A; + } + + public static ColorRGBA operator -(ColorRGBA left, ColorRGBA right) + { + return .(left.R - right.R, left.G - right.G, left.B - right.B, left.A - right.A); + } + + /// + /// Multiplication + /// + + public void operator *=(ColorRGBA value) mut + { + R *= value.R; + G *= value.G; + B *= value.B; + A *= value.A; + } + + public void operator *=(float value) mut + { + R *= value; + G *= value; + B *= value; + A *= value; + } + + public static ColorRGBA operator *(ColorRGBA left, ColorRGBA right) + { + return .(left.R * right.R, left.G * right.G, left.B * right.B, left.A * right.A); + } + + public static ColorRGBA operator *(ColorRGBA left, float right) + { + return .(left.R * right, left.G * right, left.B * right, left.A * right); + } + + public static ColorRGBA operator *(float left, ColorRGBA right) + { + return .(left * right.R, left * right.G, left * right.B, left * right.A); + } + + /// + /// Division + /// + + public void operator /=(float value) mut + { + R /= value; + G /= value; + B /= value; + A /= value; + } + + public static ColorRGBA operator /(ColorRGBA left, float right) + { + return .(left.R / right, left.G / right, left.B / right, left.A / right); + } + + public static ColorRGBA operator /(float left, ColorRGBA right) + { + return .(left / right.R, left / right.G, left / right.B, left / right.A); + } + + public static implicit operator ColorRGB(ColorRGBA color) + { + return .(color.R, color.G, color.B); + } + + // Converts a Color from sRGB color space to Linear color space. + public static ColorRGBA SRgbToLinear(ColorRGBA sRGB) => ColorRGBA(Math.Pow(sRGB.R, srgbToLin), Math.Pow(sRGB.G, srgbToLin), Math.Pow(sRGB.B, srgbToLin), sRGB.A); + + // Converts a Color from linear color space to sRGB color space. + public static ColorRGBA LinearToSRGB(ColorRGBA linear) => ColorRGBA(Math.Pow(linear.R, linToSRGB), Math.Pow(linear.G, linToSRGB), Math.Pow(linear.B, linToSRGB), linear.A); + + [Inline] +#unwarn + public static explicit operator Vector4(ColorRGBA color) => *(Vector4*)&color; + + [Inline] +#unwarn + public static explicit operator ColorRGBA(Vector4 color) => *(ColorRGBA*)&color; + } +} diff --git a/GlitchyEngine/src/Math/MathDefs.bf b/GlitchyEngine/src/Math/MathDefs.bf index 40531a0..5afe42a 100644 --- a/GlitchyEngine/src/Math/MathDefs.bf +++ b/GlitchyEngine/src/Math/MathDefs.bf @@ -2,10 +2,6 @@ using System; namespace GlitchyEngine.Math { - typealias Color = DirectX.Color; - typealias ColorRGB = DirectX.ColorRGB; - typealias ColorRGBA = DirectX.ColorRGBA; - typealias Matrix3x3 = DirectX.Math.Matrix3x3; typealias Matrix4x3 = DirectX.Math.Matrix4x3; typealias Matrix = DirectX.Math.Matrix; diff --git a/GlitchyEngine/src/Math/MathHelper.bf b/GlitchyEngine/src/Math/MathHelper.bf index 9b31db3..32b7aec 100644 --- a/GlitchyEngine/src/Math/MathHelper.bf +++ b/GlitchyEngine/src/Math/MathHelper.bf @@ -83,5 +83,20 @@ namespace GlitchyEngine.Math { return .(Math.Cos(angle), Math.Sin(angle)) * radius; } + + public static Vector2 Pow(Vector2 v, float p) + { + return Vector2(Math.Pow(v.X, p), Math.Pow(v.Y, p)); + } + + public static Vector3 Pow(Vector3 v, float p) + { + return Vector3(Math.Pow(v.X, p), Math.Pow(v.Y, p), Math.Pow(v.Z, p)); + } + + public static Vector4 Pow(Vector4 v, float p) + { + return Vector4(Math.Pow(v.X, p), Math.Pow(v.Y, p), Math.Pow(v.Z, p), Math.Pow(v.W, p)); + } } } diff --git a/GlitchyEngine/src/Platform/DX11/Math/Dx11Color.bf b/GlitchyEngine/src/Platform/DX11/Math/Dx11Color.bf new file mode 100644 index 0000000..953b82f --- /dev/null +++ b/GlitchyEngine/src/Platform/DX11/Math/Dx11Color.bf @@ -0,0 +1,29 @@ +#if GE_GRAPHICS_DX11 + +using System; + +namespace GlitchyEngine.Math +{ + extension ColorRGBA + { + [Inline] +#unwarn + public static implicit operator DirectX.ColorRGBA(ColorRGBA self) => *(DirectX.ColorRGBA*)&self; + } + + extension ColorRGB + { + [Inline] +#unwarn + public static implicit operator DirectX.ColorRGB(ColorRGB self) => *(DirectX.ColorRGB*)&self; + } + + extension Color + { + [Inline] +#unwarn + public static implicit operator DirectX.Color(Color self) => *(DirectX.Color*)&self; + } +} + +#endif diff --git a/GlitchyEngine/src/Renderer/ColorHSV.bf b/GlitchyEngine/src/Renderer/ColorHSV.bf index 0764a68..0525b33 100644 --- a/GlitchyEngine/src/Renderer/ColorHSV.bf +++ b/GlitchyEngine/src/Renderer/ColorHSV.bf @@ -38,7 +38,7 @@ namespace GlitchyEngine.Renderer else if (hsv.H < 360.0f) rgb_ = ColorRGB(c, 0, x); - return .(rgb_.Red + m, rgb_.Green + m, rgb_.Blue + m); + return .(rgb_.R + m, rgb_.G + m, rgb_.B + m); } } } \ No newline at end of file diff --git a/GlitchyEngine/src/Renderer/Material.bf b/GlitchyEngine/src/Renderer/Material.bf index 86cd5f2..31c09df 100644 --- a/GlitchyEngine/src/Renderer/Material.bf +++ b/GlitchyEngine/src/Renderer/Material.bf @@ -134,7 +134,7 @@ namespace GlitchyEngine.Renderer public void SetVariable(String name, uint32 value) => SetVariable(name, value); - public void SetVariable(String name, Color value) => SetVariable(name, value); + public void SetVariable(String name, Color value) => SetVariable(name, (ColorRGBA)value); public void SetVariable(String name, ColorRGB value) => SetVariable(name, value); public void SetVariable(String name, ColorRGBA value) => SetVariable(name, value); diff --git a/GlitchyEngine/src/Renderer/Renderer.bf b/GlitchyEngine/src/Renderer/Renderer.bf index 8c5a87b..e4b6db1 100644 --- a/GlitchyEngine/src/Renderer/Renderer.bf +++ b/GlitchyEngine/src/Renderer/Renderer.bf @@ -453,7 +453,7 @@ namespace GlitchyEngine.Renderer */ public static void DrawLine(Vector3 start, Vector3 end, Color color) { - DrawLine(Vector4(start, 1.0f), Vector4(end, 1.0f), color, .Identity); + DrawLine(Vector4(start, 1.0f), Vector4(end, 1.0f), (ColorRGBA)color, .Identity); } /** @brief Draws a line. diff --git a/GlitchyEngine/src/Renderer/Text/Font.bf b/GlitchyEngine/src/Renderer/Text/Font.bf index 4f516f7..7fc929e 100644 --- a/GlitchyEngine/src/Renderer/Text/Font.bf +++ b/GlitchyEngine/src/Renderer/Text/Font.bf @@ -707,9 +707,9 @@ namespace GlitchyEngine.Renderer.Text int index = ((desc.Height - y - 1) * desc.Width + x) * 4; - pixels[index + 0] = ToInt8(pixel.Red); - pixels[index + 1] = ToInt8(pixel.Green); - pixels[index + 2] = ToInt8(pixel.Blue); + pixels[index + 0] = ToInt8(pixel.R); + pixels[index + 1] = ToInt8(pixel.G); + pixels[index + 2] = ToInt8(pixel.B); pixels[index + 3] = Int8.MaxValue; } diff --git a/GlitchyEngine/src/Renderer/Text/FontRenderer.bf b/GlitchyEngine/src/Renderer/Text/FontRenderer.bf index adfa41f..e652cc2 100644 --- a/GlitchyEngine/src/Renderer/Text/FontRenderer.bf +++ b/GlitchyEngine/src/Renderer/Text/FontRenderer.bf @@ -397,7 +397,7 @@ namespace GlitchyEngine.Renderer.Text Matrix glyphTransform = transform * Matrix.Translation(position) * Matrix.Scaling(viewportRect.Z, viewportRect.W, 1.0f); - Renderer2D.DrawQuad(glyphTransform, atlas, glyphColor, texRect); + Renderer2D.DrawQuad(glyphTransform, atlas, (ColorRGBA)glyphColor, texRect); //Renderer2D.DrawQuad(Vector2(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2), .(viewportRect.Z, viewportRect.W), 0, atlas, glyphColor, texRect); // Show pen positions @@ -648,7 +648,7 @@ namespace GlitchyEngine.Renderer.Text texRect /= Vector4(atlasSize, atlasSize); - Renderer2D.DrawQuad(Vector2(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2), .(viewportRect.Z, viewportRect.W), 0, atlas, glyphColor, texRect); + Renderer2D.DrawQuad(Vector2(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2), .(viewportRect.Z, viewportRect.W), 0, atlas, (ColorRGBA)glyphColor, texRect); //renderer.Draw(atlas, viewportRect.X, viewportRect.Y, viewportRect.Z, viewportRect.W, glyphColor, *(float*)(&depthInt), texRect); diff --git a/GlitchyEngine/src/World/Scene.bf b/GlitchyEngine/src/World/Scene.bf index 44d3670..bb47fa9 100644 --- a/GlitchyEngine/src/World/Scene.bf +++ b/GlitchyEngine/src/World/Scene.bf @@ -15,6 +15,8 @@ namespace GlitchyEngine.World private RenderTargetGroup _compositeTarget ~ _.ReleaseRef(); + private Effect _gammaCorrectEffect ~ _.ReleaseRef(); + public Entity ActiveCamera => { Entity cameraEntity = .(); @@ -32,10 +34,10 @@ namespace GlitchyEngine.World public this() { Entity entity = CreateEntity("Green Quad"); - entity.AddComponent(.(ColorRGBA(0.2f, 0.9f, 0.15f))); + entity.AddComponent(.(ColorRGBA.SRgbToLinear(.(0.2f, 0.9f, 0.15f)))); Entity entity2 = CreateEntity("Red Square"); - var v = entity2.AddComponent(.(ColorRGBA(0.95f, 0.1f, 0.3f))); + var v = entity2.AddComponent(.(ColorRGBA.SRgbToLinear(.(0.95f, 0.1f, 0.3f)))); v.Sprite = new Texture2D("Textures/rocket.dds"); v.Sprite.SamplerState = SamplerStateManager.PointClamp; @@ -51,6 +53,8 @@ namespace GlitchyEngine.World .(RenderTargetFormat.R32_UInt) {ClearColor = .UInt(uint32.MaxValue)}), RenderTargetFormat.D24_UNorm_S8_UInt); _compositeTarget = new RenderTargetGroup(desc); + + _gammaCorrectEffect = Application.Get().EffectLibrary.Load("content/Shaders/GammaCorrect.hlsl"); } public ~this() @@ -171,7 +175,18 @@ namespace GlitchyEngine.World Renderer2D.EndScene(); - // TODO: compositeTarget into viewportTarget (gamma correction!) + // Gamma correct composit target and draw it into viewport + { + RenderCommand.UnbindRenderTargets(); + RenderCommand.SetRenderTargetGroup(viewportTarget, false); + RenderCommand.BindRenderTargets(); + + _gammaCorrectEffect.SetTexture("Texture", _compositeTarget, 0); + // TODO: iiihhh + _gammaCorrectEffect.Bind(Application.Get().Window.Context); + + FullscreenQuad.Draw(); + } viewportTarget.ReleaseRef(); }