Exemplo 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);
            }
        }
Exemplo n.º 2
0
        public void Draw(BasicEffect effect)
        {
            if (!_initialized)
            {
                throw new InvalidOperationException("Initialize must be called before Draw");
            }

            _device.SetVertexBuffer(_vertexBuffer);
            _device.SetIndexBuffer(_indexBuffer, false);

            effect.World         = ConversionUtility.ToSharpDXMatrix(_sourceMesh.Transform.Value);
            effect.DiffuseColor  = ConversionUtility.ToSharpDXVector3(_sourceMesh.Material.DiffuseColor);
            effect.SpecularColor = ConversionUtility.ToSharpDXVector3(_sourceMesh.Material.SpecularColor);
            effect.SpecularPower = _sourceMesh.Material.Shininess;

            if (!string.IsNullOrEmpty(_sourceMesh.Material.DiffuseTextureName))
            {
                effect.TextureEnabled = true;
                effect.Texture        = _texture;
            }
            else
            {
                effect.TextureEnabled = false;
                effect.Texture        = null;
            }

            effect.Alpha = _sourceMesh.Material.Transparency;

            foreach (var pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                _device.DrawIndexed(PrimitiveType.TriangleList, _sourceMesh.Indices.Count);
            }

            _device.SetIndexBuffer(null, false);
            _device.SetVertexBuffer((Buffer <VertexPositionNormalTexture>)null);
        }