Rendering holographic labels

If you have been following along, your project should now be successfully capturing and analyzing frames. Our next task is to create something to render the results on. We will simply do this using a quad and texture, both of which have been included in the starter project.

Start by declaring the variables to hold references to the classes TextRenderer and QuadRenderer; add the following lines to your FaceTagMain class:

 TextRenderer textRenderer;
QuadRenderer quadRenderer;

The TextRenderer creates and holds references to an in-memory texture; we can write to this texture via the RenderTextOffscreen method, passing the text we would like rendered. The QuadRenderer uses this texture to render. As these are DirectX resources, we are responsible for the creation and disposal of them; make the following amendments to each of the following methods, with the amendments in bold.

In this first snippet, we create the variables textRenderer and quadRenderer; as mentioned earlier, the textRenderer (instance of TextRenderer) is responsible for creating and updating the texture (which will display the recognized persons name/alias), while the quadRenderer (instance of QuardRenderer) is responsible for rendering that texture to a quad in the real world:

 public void SetHolographicSpace(HolographicSpace holographicSpace)
{
// ...
textRenderer = new TextRenderer(deviceResources, 512, 512);
textRenderer.RenderTextOffscreen("No faces detected");

quadRenderer = new QuadRenderer(deviceResources);
}

Also, anytime you create something, we need to dispose of it. In the following code snippet, we add the code responsible for tidying up the resources created by the textRenderer and quadRenderer:

 public void Dispose()
{
if(textRenderer != null)
{
textRenderer.Dispose();
textRenderer = null;
}

if(quadRenderer != null)
{
quadRenderer.Dispose();
quadRenderer = null;
}
}

In the next code snippet, we handle cases for when the device resources are lost and restored. Here, we essentially release any created resources when lost and recreate them once restored:

 public void OnDeviceLost(Object sender, EventArgs e)
{
textRenderer.ReleaseDeviceDependentResources();
quadRenderer.ReleaseDeviceDependentResources();
}

public void OnDeviceRestored(Object sender, EventArgs e)
{
textRenderer.CreateDeviceDependentResources();
quadRenderer.CreateDeviceDependentResourcesAsync();
}

At this stage, we are doing nothing with them; let's rectify this. Jump into the Render method and have it render out QuadRenderer:

 public bool Render(ref HolographicFrame holographicFrame)
{
if (timer.FrameCount == 0)
{
return false;
}

holographicFrame.UpdateCurrentPrediction();
HolographicFramePrediction prediction =
holographicFrame.CurrentPrediction;

return deviceResources.UseHolographicCameraResources(
(Dictionary<uint, CameraResources> cameraResourceDictionary) =>
{
bool atLeastOneCameraRendered = false;

foreach (var cameraPose in prediction.CameraPoses)
{
CameraResources cameraResources =
cameraResourceDictionary[cameraPose.HolographicCamera.Id];

var context = deviceResources.D3DDeviceContext;
var renderTargetView = cameraResources.BackBufferRenderTargetView;
var depthStencilView = cameraResources.DepthStencilView;

context.OutputMerger.SetRenderTargets(depthStencilView,
renderTargetView);

SharpDX.Mathematics.Interop.RawColor4 transparent = new
SharpDX.Mathematics.Interop.RawColor4(0.0f, 0.0f, 0.0f, 0.0f);
context.ClearRenderTargetView(renderTargetView, transparent);
context.ClearDepthStencilView(
depthStencilView,
SharpDX.Direct3D11.DepthStencilClearFlags.Depth |
SharpDX.Direct3D11.DepthStencilClearFlags.Stencil,
1.0f,
0);

cameraResources.UpdateViewProjectionBuffer(deviceResources,
cameraPose, referenceFrame.CoordinateSystem);

bool cameraActive =
cameraResources.AttachViewProjectionBuffer(deviceResources);

if (cameraActive)
{
quadRenderer.RenderRGB(textRenderer.Texture);
}
atLeastOneCameraRendered = true;
}

return atLeastOneCameraRendered;
});
}

Despite all the code; we have simply added three lines under the bool cameraActive = cameraResources.AttachViewProjectionBuffer(deviceResources) statement. So for each active camera, we call quadRenderer.RenderRGB(textRenderer.Texture), which puts it on the list of commands to be rendered.

Our last task is to select and position the label, which we will do next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.113.188