Esempio n. 1
0
        public BitmapSource Render(Camera camera)
        {
            // Setup for rendering.
            var rasterizerState = RasterizerState.New(_device, new RasterizerStateDescription
            {
                CullMode                = CullMode.Back,
                FillMode                = FillMode.Solid,
                IsMultisampleEnabled    = true,
                IsFrontCounterClockwise = Options.TriangleWindingOrderReversed
            });

            _device.SetRasterizerState(rasterizerState);
            _device.Clear(_renderTexture, ConversionUtility.ToSharpDXColor(Options.BackgroundColor));
            _device.Clear(_depthStencilTexture, DepthStencilClearFlags.Depth, 1, 0);
            _device.SetRenderTargets(_depthStencilTexture, _renderTexture);

            _effect.LightingEnabled = Options.LightingEnabled;
            _effect.View            = ConversionUtility.ToSharpDXMatrix(camera.GetViewMatrix());
            _effect.Projection      = ConversionUtility.ToSharpDXMatrix(camera.GetProjectionMatrix(_aspectRatio));

            // Render scene.
            _device.SetBlendState(_device.BlendStates.Opaque);
            foreach (WarpMesh mesh in _meshes.Where(m => m.IsOpaque))
            {
                mesh.Draw(_effect);
            }
            _device.SetBlendState(_device.BlendStates.AlphaBlend);
            foreach (WarpMesh mesh in _meshes.Where(m => !m.IsOpaque))
            {
                mesh.Draw(_effect);
            }
            _device.SetBlendState(null);

            // Extract image from render target.
            _device.SetRenderTargets((RenderTargetView)null);
            _device.SetRasterizerState(null);
            rasterizerState.Dispose();

            using (var memoryStream = new MemoryStream())
            {
                _renderTexture.Save(memoryStream, ImageFileType.Png);

                var bi = new BitmapImage();
                bi.BeginInit();
                bi.CacheOption  = BitmapCacheOption.OnLoad;
                bi.StreamSource = memoryStream;
                bi.EndInit();

                return(bi);
            }
        }