示例#1
0
            // encapsulates d3d resources for a camera
            public CameraDeviceResource(SharpDX.Direct3D11.Device device, ProjectorCameraEnsemble.Camera camera, Object renderLock, string directory)
            {
                this.device     = device;
                this.camera     = camera;
                this.renderLock = renderLock;

                // Kinect depth image
                var depthImageTextureDesc = new Texture2DDescription()
                {
                    Width             = Kinect2Calibration.depthImageWidth,
                    Height            = Kinect2Calibration.depthImageHeight,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    Format            = SharpDX.DXGI.Format.R16_UInt,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                    Usage             = ResourceUsage.Dynamic,
                    BindFlags         = BindFlags.ShaderResource,
                    CpuAccessFlags    = CpuAccessFlags.Write,
                };

                depthImageTexture   = new Texture2D(device, depthImageTextureDesc);
                depthImageTextureRV = new ShaderResourceView(device, depthImageTexture);

                var floatDepthImageTextureDesc = new Texture2DDescription()
                {
                    Width             = Kinect2Calibration.depthImageWidth,
                    Height            = Kinect2Calibration.depthImageHeight,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    Format            = SharpDX.DXGI.Format.R32_Float,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                    Usage             = ResourceUsage.Default,
                    BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                    CpuAccessFlags    = CpuAccessFlags.None,
                };

                floatDepthImageTexture          = new Texture2D(device, floatDepthImageTextureDesc);
                floatDepthImageRV               = new ShaderResourceView(device, floatDepthImageTexture);
                floatDepthImageRenderTargetView = new RenderTargetView(device, floatDepthImageTexture);

                floatDepthImageTexture2          = new Texture2D(device, floatDepthImageTextureDesc);
                floatDepthImageRV2               = new ShaderResourceView(device, floatDepthImageTexture2);
                floatDepthImageRenderTargetView2 = new RenderTargetView(device, floatDepthImageTexture2);

                // Kinect color image
                var colorImageStagingTextureDesc = new Texture2DDescription()
                {
                    Width             = Kinect2Calibration.colorImageWidth,
                    Height            = Kinect2Calibration.colorImageHeight,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                    Usage             = ResourceUsage.Dynamic,
                    BindFlags         = BindFlags.ShaderResource,
                    CpuAccessFlags    = CpuAccessFlags.Write
                };

                colorImageStagingTexture = new Texture2D(device, colorImageStagingTextureDesc);

                var colorImageTextureDesc = new Texture2DDescription()
                {
                    Width             = Kinect2Calibration.colorImageWidth,
                    Height            = Kinect2Calibration.colorImageHeight,
                    MipLevels         = 0,
                    ArraySize         = 1,
                    Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                    Usage             = ResourceUsage.Default,
                    BindFlags         = BindFlags.ShaderResource | BindFlags.RenderTarget,
                    CpuAccessFlags    = CpuAccessFlags.None,
                    OptionFlags       = ResourceOptionFlags.GenerateMipMaps
                };

                colorImageTexture   = new Texture2D(device, colorImageTextureDesc);
                colorImageTextureRV = new ShaderResourceView(device, colorImageTexture);

                // vertex buffer
                var table       = camera.calibration.ComputeDepthFrameToCameraSpaceTable();
                int numVertices = 6 * (Kinect2Calibration.depthImageWidth - 1) * (Kinect2Calibration.depthImageHeight - 1);
                var vertices    = new VertexPosition[numVertices];

                Int3[] quadOffsets = new Int3[]
                {
                    new Int3(0, 0, 0),
                    new Int3(1, 0, 0),
                    new Int3(0, 1, 0),
                    new Int3(1, 0, 0),
                    new Int3(1, 1, 0),
                    new Int3(0, 1, 0),
                };

                int vertexIndex = 0;

                for (int y = 0; y < Kinect2Calibration.depthImageHeight - 1; y++)
                {
                    for (int x = 0; x < Kinect2Calibration.depthImageWidth - 1; x++)
                    {
                        for (int i = 0; i < 6; i++)
                        {
                            int vertexX = x + quadOffsets[i].X;
                            int vertexY = y + quadOffsets[i].Y;

                            var point = table[Kinect2Calibration.depthImageWidth * vertexY + vertexX];

                            var vertex = new VertexPosition();
                            vertex.position         = new SharpDX.Vector4(point.X, point.Y, vertexX, vertexY);
                            vertices[vertexIndex++] = vertex;
                        }
                    }
                }

                var stream = new DataStream(numVertices * VertexPosition.SizeInBytes, true, true);

                stream.WriteRange(vertices);
                stream.Position = 0;

                var vertexBufferDesc = new BufferDescription()
                {
                    BindFlags      = BindFlags.VertexBuffer,
                    CpuAccessFlags = CpuAccessFlags.None,
                    Usage          = ResourceUsage.Default,
                    SizeInBytes    = numVertices * VertexPosition.SizeInBytes,
                };

                vertexBuffer = new SharpDX.Direct3D11.Buffer(device, stream, vertexBufferDesc);

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

                stream.Dispose();

                var colorImage = new RoomAliveToolkit.ARGBImage(Kinect2Calibration.colorImageWidth, Kinect2Calibration.colorImageHeight);

                ProjectorCameraEnsemble.LoadFromTiff(imagingFactory, colorImage, directory + "/camera" + camera.name + "/color.tiff");

                var depthImage = new RoomAliveToolkit.ShortImage(Kinect2Calibration.depthImageWidth, Kinect2Calibration.depthImageHeight);

                ProjectorCameraEnsemble.LoadFromTiff(imagingFactory, depthImage, directory + "/camera" + camera.name + "/mean.tiff");

                lock (renderLock) // necessary?
                {
                    UpdateColorImage(device.ImmediateContext, colorImage.DataIntPtr);
                    UpdateDepthImage(device.ImmediateContext, depthImage.DataIntPtr);
                }

                colorImage.Dispose();
                depthImage.Dispose();
            }
        protected override void LoadContent()
        {
            _spriteBatch    = ToDisposeContent(new SpriteBatch(GraphicsDevice));
            _depthRectangle = new Rectangle(0, 0, RoomAliveToolkit.Kinect2Calibration.depthImageWidth, RoomAliveToolkit.Kinect2Calibration.depthImageHeight);

            int depthWidth  = RoomAliveToolkit.Kinect2Calibration.depthImageWidth;
            int depthHeight = RoomAliveToolkit.Kinect2Calibration.depthImageHeight;

            _roomAliveEffect = Content.Load <Effect>("Holographic");
            _fromUintEffect  = Content.Load <Effect>("FromUint");
            _gaussianEffect  = Content.Load <Effect>("BilateralFilter");

            _colorSamplerState = ToDisposeContent(SharpDX.Toolkit.Graphics.SamplerState.New(this.GraphicsDevice, new SamplerStateDescription()
            {
                Filter             = Filter.MinMagMipLinear,
                AddressU           = TextureAddressMode.Border,
                AddressV           = TextureAddressMode.Border,
                AddressW           = TextureAddressMode.Border,
                BorderColor        = new SharpDX.Color4(0.5f, 0.5f, 0.5f, 1.0f),
                ComparisonFunction = Comparison.Never,
                MaximumAnisotropy  = 16,
                MipLodBias         = 0,
                MinimumLod         = -float.MaxValue,
                MaximumLod         = float.MaxValue
            }));

            List <VertexPosition> vertices = new List <VertexPosition>();
            List <int>            indices  = new List <int>();

            // vertex buffer
            if (_camera.calibration != null)
            {
                var table = _camera.calibration.ComputeDepthFrameToCameraSpaceTable();
                for (int i = 0; i < depthHeight; i++)
                {
                    for (int j = 0; j < depthWidth; j++)
                    {
                        var point = table[RoomAliveToolkit.Kinect2Calibration.depthImageWidth * i + j];

                        vertices.Add(new VertexPosition(new SharpDX.Vector4(point.X, point.Y, j, i)));
                    }
                }


                for (int i = 0; i < depthHeight - 1; i++)
                {
                    for (int j = 0; j < depthWidth - 1; j++)
                    {
                        int baseIndex = depthWidth * i + j;
                        indices.Add(baseIndex);
                        indices.Add(baseIndex + depthWidth + 1);
                        indices.Add(baseIndex + 1);

                        indices.Add(baseIndex);
                        indices.Add(baseIndex + depthWidth);
                        indices.Add(baseIndex + depthWidth + 1);
                    }
                }

                _vertexArray = vertices.ToArray();
                _indexArray  = indices.ToArray();

                // build the plane geometry of the specified size and subdivision segments
                _geometry = ToDisposeContent(new GeometricPrimitive <VertexPosition>(GraphicsDevice, _vertexArray, _indexArray, true));
            }
            else
            {
                Console.WriteLine("Camera '{0}' is missing calibration", _camera.name);
                Visible = false;
                Enabled = false;
            }

            if (!string.IsNullOrEmpty(_colorImageFilePath))
            {
                _tex2DColorImage1 = SharpDX.Toolkit.Graphics.Texture2D.Load(GraphicsDevice, _colorImageFilePath, TextureFlags.ShaderResource, ResourceUsage.Dynamic);
                _tex2DColorImage2 = SharpDX.Toolkit.Graphics.Texture2D.Load(GraphicsDevice, _colorImageFilePath, TextureFlags.ShaderResource, ResourceUsage.Dynamic);
            }

            _imagingFactory = new SharpDX.WIC.ImagingFactory();
            if (!string.IsNullOrEmpty(_depthImageFilePath))
            {
                var depthImage = new RoomAliveToolkit.ShortImage(RoomAliveToolkit.Kinect2Calibration.depthImageWidth, RoomAliveToolkit.Kinect2Calibration.depthImageHeight);
                RoomAliveToolkit.ProjectorCameraEnsemble.LoadFromTiff(_imagingFactory, depthImage, _depthImageFilePath);

                UpdateDepthImage(this.GraphicsDevice, depthImage.DataIntPtr);
            }

            var floatDepthImageTextureDesc = new Texture2DDescription()
            {
                Width             = depthWidth,
                Height            = depthHeight,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = SharpDX.DXGI.Format.R32_Float,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
            };

            _floatDepthImageFinal   = ToDisposeContent(RenderTarget2D.New(GraphicsDevice, floatDepthImageTextureDesc));
            _floatDepthImageSubpass = ToDisposeContent(RenderTarget2D.New(GraphicsDevice, floatDepthImageTextureDesc));

            base.LoadContent();
        }