MoonWorksGraphicsTests/CullFace/CullFaceGame.cs

142 lines
4.8 KiB
C#
Raw Normal View History

2022-11-09 19:54:42 +00:00
using MoonWorks;
using MoonWorks.Graphics;
using MoonWorks.Math.Float;
namespace MoonWorks.Test
{
class CullFaceGame : Game
2022-11-09 19:54:42 +00:00
{
private GraphicsPipeline CW_CullNonePipeline;
private GraphicsPipeline CW_CullFrontPipeline;
private GraphicsPipeline CW_CullBackPipeline;
private GraphicsPipeline CCW_CullNonePipeline;
private GraphicsPipeline CCW_CullFrontPipeline;
private GraphicsPipeline CCW_CullBackPipeline;
2024-02-23 20:42:01 +00:00
private GpuBuffer cwVertexBuffer;
private GpuBuffer ccwVertexBuffer;
2022-11-09 19:54:42 +00:00
private bool useClockwiseWinding;
public CullFaceGame() : base(TestUtils.GetStandardWindowCreateInfo(), TestUtils.GetStandardFrameLimiterSettings(), TestUtils.Backend, 60, true)
2022-11-09 19:54:42 +00:00
{
Logger.LogInfo("Press Down to toggle the winding order of the triangles (default is counter-clockwise)");
2022-11-09 19:54:42 +00:00
// Load the shaders
ShaderModule vertShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("PositionColor.vert"));
ShaderModule fragShaderModule = new ShaderModule(GraphicsDevice, TestUtils.GetShaderPath("SolidColor.frag"));
2022-11-09 19:54:42 +00:00
// Create the graphics pipelines
GraphicsPipelineCreateInfo pipelineCreateInfo = TestUtils.GetStandardGraphicsPipelineCreateInfo(
2022-11-17 20:33:58 +00:00
MainWindow.SwapchainFormat,
vertShaderModule,
fragShaderModule
);
2023-01-06 20:38:30 +00:00
pipelineCreateInfo.VertexInputState = VertexInputState.CreateSingleBinding<PositionColorVertex>();
2022-11-09 19:54:42 +00:00
pipelineCreateInfo.RasterizerState = RasterizerState.CW_CullNone;
CW_CullNonePipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
pipelineCreateInfo.RasterizerState = RasterizerState.CW_CullFront;
CW_CullFrontPipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
pipelineCreateInfo.RasterizerState = RasterizerState.CW_CullBack;
CW_CullBackPipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
pipelineCreateInfo.RasterizerState = RasterizerState.CCW_CullNone;
CCW_CullNonePipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
pipelineCreateInfo.RasterizerState = RasterizerState.CCW_CullFront;
CCW_CullFrontPipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
pipelineCreateInfo.RasterizerState = RasterizerState.CCW_CullBack;
CCW_CullBackPipeline = new GraphicsPipeline(GraphicsDevice, pipelineCreateInfo);
// Create and populate the vertex buffers
var resourceUploader = new ResourceUploader(GraphicsDevice);
2022-11-09 19:54:42 +00:00
cwVertexBuffer = resourceUploader.CreateBuffer(
2024-02-23 20:42:01 +00:00
[
2022-11-09 19:54:42 +00:00
new PositionColorVertex(new Vector3(0, -1, 0), Color.Blue),
new PositionColorVertex(new Vector3(1, 1, 0), Color.Green),
new PositionColorVertex(new Vector3(-1, 1, 0), Color.Red),
2024-02-23 20:42:01 +00:00
],
BufferUsageFlags.Vertex
2022-11-09 19:54:42 +00:00
);
2024-02-23 20:42:01 +00:00
ccwVertexBuffer = resourceUploader.CreateBuffer(
2024-02-23 20:42:01 +00:00
[
2022-11-09 19:54:42 +00:00
new PositionColorVertex(new Vector3(-1, 1, 0), Color.Red),
new PositionColorVertex(new Vector3(1, 1, 0), Color.Green),
2024-02-23 20:42:01 +00:00
new PositionColorVertex(new Vector3(0, -1, 0), Color.Blue)
],
BufferUsageFlags.Vertex
2022-11-09 19:54:42 +00:00
);
2024-02-23 20:42:01 +00:00
resourceUploader.Upload();
resourceUploader.Dispose();
2022-11-09 19:54:42 +00:00
}
protected override void Update(System.TimeSpan delta)
{
if (TestUtils.CheckButtonPressed(Inputs, TestUtils.ButtonType.Bottom))
2022-11-09 19:54:42 +00:00
{
useClockwiseWinding = !useClockwiseWinding;
Logger.LogInfo("Using clockwise winding: " + useClockwiseWinding);
}
}
protected override void Draw(double alpha)
{
CommandBuffer cmdbuf = GraphicsDevice.AcquireCommandBuffer();
Texture? backbuffer = cmdbuf.AcquireSwapchainTexture(MainWindow);
if (backbuffer != null)
{
2024-03-01 23:03:29 +00:00
cmdbuf.BeginRenderPass(new ColorAttachmentInfo(backbuffer, WriteOptions.SafeDiscard, Color.Black));
2022-11-09 19:54:42 +00:00
// Need to bind a pipeline before binding vertex buffers
cmdbuf.BindGraphicsPipeline(CW_CullNonePipeline);
2022-11-09 19:54:42 +00:00
if (useClockwiseWinding)
{
cmdbuf.BindVertexBuffers(cwVertexBuffer);
}
else
{
cmdbuf.BindVertexBuffers(ccwVertexBuffer);
}
cmdbuf.SetViewport(new Viewport(0, 0, 213, 240));
2024-02-23 20:42:01 +00:00
cmdbuf.DrawPrimitives(0, 1);
2022-11-09 19:54:42 +00:00
cmdbuf.SetViewport(new Viewport(213, 0, 213, 240));
cmdbuf.BindGraphicsPipeline(CW_CullFrontPipeline);
2024-02-23 20:42:01 +00:00
cmdbuf.DrawPrimitives(0, 1);
2022-11-09 19:54:42 +00:00
cmdbuf.SetViewport(new Viewport(426, 0, 213, 240));
cmdbuf.BindGraphicsPipeline(CW_CullBackPipeline);
2024-02-23 20:42:01 +00:00
cmdbuf.DrawPrimitives(0, 1);
2022-11-09 19:54:42 +00:00
cmdbuf.SetViewport(new Viewport(0, 240, 213, 240));
cmdbuf.BindGraphicsPipeline(CCW_CullNonePipeline);
2024-02-23 20:42:01 +00:00
cmdbuf.DrawPrimitives(0, 1);
2022-11-09 19:54:42 +00:00
cmdbuf.SetViewport(new Viewport(213, 240, 213, 240));
cmdbuf.BindGraphicsPipeline(CCW_CullFrontPipeline);
2024-02-23 20:42:01 +00:00
cmdbuf.DrawPrimitives(0, 1);
2022-11-09 19:54:42 +00:00
cmdbuf.SetViewport(new Viewport(426, 240, 213, 240));
cmdbuf.BindGraphicsPipeline(CCW_CullBackPipeline);
2024-02-23 20:42:01 +00:00
cmdbuf.DrawPrimitives(0, 1);
2022-11-09 19:54:42 +00:00
cmdbuf.EndRenderPass();
}
GraphicsDevice.Submit(cmdbuf);
}
public static void Main(string[] args)
{
CullFaceGame game = new CullFaceGame();
game.Run();
}
}
}