From e239de52d68a1ab51003204109e884dc1e14ac05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Fri, 20 Aug 2021 19:44:26 +0200 Subject: [PATCH] Added Line and Ray drawing --- GlitchyEngine/src/Renderer/Renderer.bf | 92 ++++++++++++++++++++++++- Sandbox/content/Shaders/lineShader.hlsl | 31 +++++++++ 2 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 Sandbox/content/Shaders/lineShader.hlsl diff --git a/GlitchyEngine/src/Renderer/Renderer.bf b/GlitchyEngine/src/Renderer/Renderer.bf index 5f1e4ca..4738b04 100644 --- a/GlitchyEngine/src/Renderer/Renderer.bf +++ b/GlitchyEngine/src/Renderer/Renderer.bf @@ -23,7 +23,11 @@ namespace GlitchyEngine.Renderer static SceneConstants _sceneConstants; - public static void Init(GraphicsContext context) + static Effect LineEffect ~ _?.ReleaseRef(); + static VertexBuffer LineVertices ~ _?.ReleaseRef(); + static GeometryBinding LineGeometry ~ _?.ReleaseRef(); + + public static void Init(GraphicsContext context, EffectLibrary effectLibrary) { _context = context..AddRef(); /* @@ -35,6 +39,32 @@ namespace GlitchyEngine.Renderer */ RenderCommand.Init(); + + InitLineRenderer(effectLibrary); + } + + static void InitLineRenderer(EffectLibrary effectLibrary) + { + LineEffect = effectLibrary.Load("content\\Shaders\\lineShader.hlsl"); + + LineGeometry = new GeometryBinding(_context); + LineGeometry.SetPrimitiveTopology(.LineList); + + Vector4[2] vertices = .(.Zero, .Zero); + LineVertices = new VertexBuffer(_context, sizeof(Vector4), 2, .Dynamic, .Write); + LineVertices.SetData(vertices, 0, .WriteDiscard); + LineGeometry.SetVertexBufferSlot(LineVertices, 0); + + uint16[2] indices = .(0, 1); + IndexBuffer indexBuffer = new IndexBuffer(_context, 2, .Immutable); + indexBuffer.SetData(indices); + LineGeometry.SetIndexBuffer(indexBuffer); + indexBuffer.ReleaseRef(); + + VertexElement[] vertexElements = new VertexElement[1]; + vertexElements[0] = .(.R32G32B32_Float, "POSITION"); + VertexLayout layout = new VertexLayout(_context, vertexElements, true, LineEffect.VertexShader); + LineGeometry.SetVertexLayout(layout..ReleaseRefNoDelete()); } public static void BeginScene(EcsWorld world, Entity cameraEntity) @@ -88,5 +118,65 @@ namespace GlitchyEngine.Renderer geometry.Bind(); RenderCommand.DrawIndexed(geometry); } + + /** @brief Draws a line. + * @param start The start point of the line. + * @param end The end point of the line. + * @param color The color of the line. + */ + public static void DrawLine(Vector3 start, Vector3 end, Color color) + { + DrawLine(Vector4(start, 1.0f), Vector4(end, 1.0f), color, .Identity); + } + + /** @brief Draws a line. + * @param start The start point of the line. + * @param end The end point of the line. + * @param color The color of the line. + * @param transform A transform matrix transforming the line. + */ + public static void DrawLine(Vector3 start, Vector3 end, Color color, Matrix transform) + { + DrawLine(Vector4(start, 1.0f), Vector4(end, 1.0f), color, transform); + } + + /** @brief Draws a ray. + * @param start The start point of the ray. + * @param direction The direction of the ray. + * @param color The color of the ray. + */ + public static void DrawRay(Vector3 start, Vector3 direction, Color color) + { + DrawLine(Vector4(start, 1.0f), Vector4(direction, 0.0f), color, .Identity); + } + + /** @brief Draws a ray. + * @param start The start point of the ray. + * @param direction The direction of the ray. + * @param color The color of the ray. + * @param transform A transform matrix transforming the ray. + */ + public static void DrawRay(Vector3 start, Vector3 direction, Color color, Matrix transform) + { + DrawLine(Vector4(start, 1.0f), Vector4(direction, 0.0f), color, transform); + } + + /** @brief Draws a line. + * @param start The start point of the line. + * @param end The end point of the line. + * @param color The color of the line. + * @param transform A transform matrix transforming the line. + */ + public static void DrawLine(Vector4 start, Vector4 end, Color color, Matrix transform) + { + LineVertices.SetData(Vector4[2](start, end), 0, .WriteDiscard); + LineEffect.Variables["ViewProjection"].SetData(_sceneConstants.ViewProjection * transform); + LineEffect.Variables["Color"].SetData(color); + + LineEffect.Bind(_context); + + LineGeometry.Bind(); + RenderCommand.DrawIndexed(LineGeometry); + } } } diff --git a/Sandbox/content/Shaders/lineShader.hlsl b/Sandbox/content/Shaders/lineShader.hlsl new file mode 100644 index 0000000..1353f92 --- /dev/null +++ b/Sandbox/content/Shaders/lineShader.hlsl @@ -0,0 +1,31 @@ +cbuffer Constants : register(b0) +{ + float4x4 ViewProjection; + float4 Color; +}; + +struct VS_Input +{ + float4 Position : POSITION; +}; + +struct PS_Input +{ + float4 Position : SV_Position; +}; + +PS_Input VS(VS_Input input) +{ + PS_Input output; + + output.Position = mul(ViewProjection, input.Position); + + return output; +} + +float4 PS(PS_Input input) : SV_Target0 +{ + return Color; +} + +#effect[VS=VS, PS=PS]