Added Line and Ray drawing

This commit is contained in:
Simon Lübeß
2021-08-20 19:44:26 +02:00
parent 9e06d4d51f
commit e239de52d6
2 changed files with 122 additions and 1 deletions
+91 -1
View File
@@ -23,7 +23,11 @@ namespace GlitchyEngine.Renderer
static SceneConstants _sceneConstants; 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(); _context = context..AddRef();
/* /*
@@ -35,6 +39,32 @@ namespace GlitchyEngine.Renderer
*/ */
RenderCommand.Init(); 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<Vector4>(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) public static void BeginScene(EcsWorld world, Entity cameraEntity)
@@ -88,5 +118,65 @@ namespace GlitchyEngine.Renderer
geometry.Bind(); geometry.Bind();
RenderCommand.DrawIndexed(geometry); 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);
}
} }
} }
+31
View File
@@ -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]