Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering of projector, color and depth frustums and some smaller changes #38

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions ProCamCalibration/CalibrateEnsemble/CalibrateEnsemble.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
<SharpDXDirectXVersion>DirectX11_2</SharpDXDirectXVersion>
<NuGetPackageImportStamp>1eec9909</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand All @@ -28,7 +29,7 @@
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Prefer32Bit>false</Prefer32Bit>
<UseVSHostingProcess>true</UseVSHostingProcess>
<UseVSHostingProcess>false</UseVSHostingProcess>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -73,23 +74,23 @@
</Reference>
<Reference Include="SharpDX, Version=2.6.3.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\SharpDX.2.6.3\Bin\DirectX11_2-net40\SharpDX.dll</HintPath>
<HintPath>$(SharpDXPackageBinDir)\SharpDX.dll</HintPath>
</Reference>
<Reference Include="SharpDX.D3DCompiler, Version=2.6.3.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\SharpDX.2.6.3\Bin\DirectX11_2-net40\SharpDX.D3DCompiler.dll</HintPath>
<HintPath>$(SharpDXPackageBinDir)\SharpDX.D3DCompiler.dll</HintPath>
</Reference>
<Reference Include="SharpDX.Direct2D1, Version=2.6.3.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\SharpDX.2.6.3\Bin\DirectX11_2-net40\SharpDX.Direct2D1.dll</HintPath>
<HintPath>$(SharpDXPackageBinDir)\SharpDX.Direct2D1.dll</HintPath>
</Reference>
<Reference Include="SharpDX.Direct3D11, Version=2.6.3.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\SharpDX.2.6.3\Bin\DirectX11_2-net40\SharpDX.Direct3D11.dll</HintPath>
<HintPath>$(SharpDXPackageBinDir)\SharpDX.Direct3D11.dll</HintPath>
</Reference>
<Reference Include="SharpDX.DXGI, Version=2.6.3.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\SharpDX.2.6.3\Bin\DirectX11_2-net40\SharpDX.DXGI.dll</HintPath>
<HintPath>$(SharpDXPackageBinDir)\SharpDX.DXGI.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -109,6 +110,7 @@
<Compile Include="ConsoleTextBox.Designer.cs">
<DependentUpon>ConsoleTextBox.cs</DependentUpon>
</Compile>
<Compile Include="Frustum.cs" />
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>
Expand Down Expand Up @@ -154,6 +156,14 @@
<Link>Content\DepthAndColorVS.cso</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\x64\Debug\frustumPS.cso">
<Link>Content\frustumPS.cso</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\x64\Debug\frustumVS.cso">
<Link>Content\frustumVS.cso</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="App.config" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
Expand Down
224 changes: 224 additions & 0 deletions ProCamCalibration/CalibrateEnsemble/Frustum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RoomAliveToolkit
{
using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using SharpDX;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX.D3DCompiler;
using SharpDX.WIC;
using Device = SharpDX.Direct3D11.Device;

public class FrustumShader
{
float maxZ = 0.97f;

struct VertexPosition
{
public SharpDX.Vector4 position;
static public int SizeInBytes { get { return 4 * 4; } }
}


public SharpDX.Direct3D11.Buffer vertexBuffer;
public VertexBufferBinding vertexBufferBinding;

public FrustumShader(Device device)
{
// create single vertex buffer
var stream = new DataStream(24 * VertexPosition.SizeInBytes, true, true);
stream.Write(new Vector4(-1, -1, 0, 1));
stream.Write(new Vector4( 1, -1, 0, 1));
stream.Write(new Vector4( 1, -1, 0, 1));
stream.Write(new Vector4( 1, 1, 0, 1));
stream.Write(new Vector4( 1, 1, 0, 1));
stream.Write(new Vector4(-1, 1, 0, 1));
stream.Write(new Vector4(-1, 1, 0, 1));
stream.Write(new Vector4(-1, -1, 0, 1));

stream.Write(new Vector4(-1, -1, maxZ, 1));
stream.Write(new Vector4( 1, -1, maxZ, 1));
stream.Write(new Vector4( 1, -1, maxZ, 1));
stream.Write(new Vector4( 1, 1, maxZ, 1));
stream.Write(new Vector4( 1, 1, maxZ, 1));
stream.Write(new Vector4(-1, 1, maxZ, 1));
stream.Write(new Vector4(-1, 1, maxZ, 1));
stream.Write(new Vector4(-1, -1, maxZ, 1));

stream.Write(new Vector4(-1, -1, 0, 1));
stream.Write(new Vector4(-1, -1, maxZ, 1));
stream.Write(new Vector4( 1, -1, 0, 1));
stream.Write(new Vector4( 1, -1, maxZ, 1));
stream.Write(new Vector4( 1, 1, 0, 1));
stream.Write(new Vector4( 1, 1, maxZ, 1));
stream.Write(new Vector4(-1, 1, 0, 1));
stream.Write(new Vector4(-1, 1, maxZ, 1));
stream.Position = 0;

var vertexBufferDesc = new BufferDescription()
{
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
Usage = ResourceUsage.Default,
SizeInBytes = 24 * VertexPosition.SizeInBytes,
};
vertexBuffer = new SharpDX.Direct3D11.Buffer(device, stream, vertexBufferDesc);

stream.Dispose();

vertexBufferBinding = new VertexBufferBinding(vertexBuffer, VertexPosition.SizeInBytes, 0);


shaderByteCode = new ShaderBytecode(File.ReadAllBytes("Content/frustumVS.cso"));
frustumVS = new VertexShader(device, shaderByteCode);
frustumPS = new PixelShader(device, new ShaderBytecode(File.ReadAllBytes("Content/frustumPS.cso")));

// depth stencil state
var depthStencilStateDesc = new DepthStencilStateDescription()
{
IsDepthEnabled = true,
DepthWriteMask = DepthWriteMask.All,
DepthComparison = Comparison.LessEqual,
IsStencilEnabled = false,
};
depthStencilState = new DepthStencilState(device, depthStencilStateDesc);

// rasterizer state
var rasterizerStateDesc = new RasterizerStateDescription()
{
CullMode = CullMode.None,
FillMode = FillMode.Wireframe,
IsDepthClipEnabled = true,
IsFrontCounterClockwise = true,
IsMultisampleEnabled = true,
};
rasterizerState = new RasterizerState(device, rasterizerStateDesc);

// constant buffer
var VSConstantBufferDesc = new BufferDescription()
{
Usage = ResourceUsage.Dynamic,
BindFlags = BindFlags.ConstantBuffer,
SizeInBytes = VSConstantBuffer.size,
CpuAccessFlags = CpuAccessFlags.Write,
StructureByteStride = 0,
OptionFlags = 0,
};
vertexShaderConstantBuffer = new SharpDX.Direct3D11.Buffer(device, VSConstantBufferDesc);

// Pixel shader constant buffer
var PSConstantBufferDesc = new BufferDescription()
{
Usage = ResourceUsage.Dynamic,
BindFlags = BindFlags.ConstantBuffer,
SizeInBytes = PSConstantBuffer.size,
CpuAccessFlags = CpuAccessFlags.Write,
StructureByteStride = 0,
OptionFlags = 0,
};
pixelShaderConstantBuffer = new SharpDX.Direct3D11.Buffer(device, PSConstantBufferDesc);

vertexInputLayout = new InputLayout(device, shaderByteCode.Data, new[]
{
new InputElement("SV_POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
});
}

// protip: compile shader with /Fc; output gives exact layout
// hlsl matrices are stored column major
// variables are stored on 4-component boundaries; inc. matrix columns
// size is a multiple of 16
[StructLayout(LayoutKind.Explicit, Size = VSConstantBuffer.size)]
unsafe struct VSConstantBuffer
{
public const int size = 64 + 64 + 64;

[FieldOffset(0)]
public fixed float world[16];
[FieldOffset(64)]
public fixed float viewProjection[16];
[FieldOffset(128)]
public fixed float inverseProjection[16];
};


[StructLayout(LayoutKind.Explicit, Size = PSConstantBuffer.size)]
unsafe struct PSConstantBuffer
{
public const int size = 16;

[FieldOffset(0)]
public fixed float color[3];
};

public unsafe void SetVertexShaderConstants(DeviceContext deviceContext, SharpDX.Matrix world, SharpDX.Matrix viewProjection, SharpDX.Matrix inverseProjection)
{
// hlsl matrices are default column order
var constants = new VSConstantBuffer();
for (int i = 0, col = 0; col < 4; col++)
{
for (int row = 0; row < 4; row++)
{
constants.world[i] = world[row, col];
constants.viewProjection[i] = viewProjection[row, col];
constants.inverseProjection[i] = inverseProjection[row, col];
i++;
}
}

DataStream dataStream;
deviceContext.MapSubresource(vertexShaderConstantBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out dataStream);
dataStream.Write<VSConstantBuffer>(constants);
deviceContext.UnmapSubresource(vertexShaderConstantBuffer, 0);
}

public unsafe void SetPixelShaderConstants(DeviceContext deviceContext, SharpDX.Color3 color)
{
var constants = new PSConstantBuffer();
for (int i = 0; i < 3; i++)
{
constants.color[i] = color[i];
}

DataStream dataStream;
deviceContext.MapSubresource(pixelShaderConstantBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out dataStream);
dataStream.Write<PSConstantBuffer>(constants);
deviceContext.UnmapSubresource(pixelShaderConstantBuffer, 0);
}

public void Render(DeviceContext deviceContext)
{
deviceContext.InputAssembler.InputLayout = vertexInputLayout;
deviceContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.LineList;
deviceContext.InputAssembler.SetVertexBuffers(0, vertexBufferBinding);
deviceContext.Rasterizer.State = rasterizerState;

deviceContext.VertexShader.Set(frustumVS);
deviceContext.VertexShader.SetConstantBuffer(0, vertexShaderConstantBuffer);

deviceContext.GeometryShader.Set(null);

deviceContext.PixelShader.SetConstantBuffer(0, pixelShaderConstantBuffer);
deviceContext.PixelShader.Set(frustumPS);

deviceContext.Draw(24, 0);
}

VertexShader frustumVS;
PixelShader frustumPS;
ShaderBytecode shaderByteCode;
DepthStencilState depthStencilState;
RasterizerState rasterizerState;
SharpDX.Direct3D11.Buffer vertexShaderConstantBuffer;
SharpDX.Direct3D11.Buffer pixelShaderConstantBuffer;
InputLayout vertexInputLayout;
}
}
44 changes: 42 additions & 2 deletions ProCamCalibration/CalibrateEnsemble/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading