Exemplo n.º 1
0
        public static byte[] Generate(uint width, uint height, string optFileName, RenderFace side, RenderMethod method)
        {
            var options = new DeviceResourcesOptions
            {
                UseHighestFeatureLevel = false,
                PreferMultisampling    = true
            };

            var deviceResources = new RenderTargetDeviceResources(width, height, D3D11FeatureLevel.FeatureLevel100, options);
            var optComponent    = new OptComponent(optFileName);

            try
            {
                optComponent.CreateDeviceDependentResources(deviceResources);
                optComponent.CreateWindowSizeDependentResources();
                optComponent.Update(null);

                optComponent.WorldMatrix =
                    XMMatrix.Translation(optComponent.OptCenter.X, -optComponent.OptCenter.Z, optComponent.OptCenter.Y)
                    * XMMatrix.ScalingFromVector(XMVector.Replicate(0.95f / optComponent.OptSize));

                switch (side)
                {
                case RenderFace.Top:
                default:
                    optComponent.ViewMatrix = XMMatrix.LookAtRH(new XMFloat3(0, 2, 0), new XMFloat3(0, 0, 0), new XMFloat3(0, 0, 1));
                    break;

                case RenderFace.Front:
                    optComponent.ViewMatrix = XMMatrix.LookAtRH(new XMFloat3(0, 0, 2), new XMFloat3(0, 0, 0), new XMFloat3(0, 1, 0));
                    break;

                case RenderFace.Side:
                    optComponent.ViewMatrix = XMMatrix.LookAtRH(new XMFloat3(2, 0, 0), new XMFloat3(0, 0, 0), new XMFloat3(0, 1, 0));
                    break;
                }

                optComponent.ProjectionMatrix = XMMatrix.OrthographicRH(1, 1, 0, 5);

                var context = deviceResources.D3DContext;
                context.OutputMergerSetRenderTargets(new[] { deviceResources.D3DRenderTargetView }, deviceResources.D3DDepthStencilView);
                context.ClearRenderTargetView(deviceResources.D3DRenderTargetView, new[] { 0.0f, 0.0f, 0.0f, 0.0f });
                context.ClearDepthStencilView(deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth, 1.0f, 0);

                optComponent.Render();
                deviceResources.Present();

                var buffer = deviceResources.GetBackBufferContent();

                for (int i = 0; i < width * height; i++)
                {
                    byte a = buffer[i * 4 + 3];
                    buffer[i * 4 + 3] = a == 0 ? (byte)0 : (byte)255;
                }

                switch (method)
                {
                case RenderMethod.Color:
                default:
                    return(buffer);

                case RenderMethod.GrayScaleLight:
                    return(ConvertToGrayScaleLight(buffer, (int)width, (int)height));

                case RenderMethod.GrayScaleDark:
                    return(ConvertToGrayScaleDark(buffer, (int)width, (int)height));

                case RenderMethod.Blue:
                    return(ConvertToBlue(buffer, (int)width, (int)height));
                }
            }
            finally
            {
                optComponent.ReleaseWindowSizeDependentResources();
                optComponent.ReleaseDeviceDependentResources();
                deviceResources.Release();
            }
        }