Example #1
0
        private void DrawWithCombineMaterial(DrawingContext context, IList <IDrawableObject> drawables)
        {
            RenderTargetPool.Lock(InputTexture as RenderTarget2D);

            int i, p;

            for (p = 0; p < passes.Count; p++)
            {
                passes[p].GetActivePasses(workingPasses);

                RenderTarget2D intermediate = InputTexture as RenderTarget2D;
                for (i = 0; i < workingPasses.Count; ++i)
                {
                    var workingPass = (PostEffect)workingPasses[i];

                    RenderTarget2D previous = intermediate;
                    RenderTargetPool.Lock(previous);
                    intermediate = workingPass.PrepareRenderTarget(context.graphics, intermediate, null);
                    intermediate.Begin();

                    workingPass.InputTexture = previous;
                    workingPass.Draw(context, drawables);

                    intermediate.End();
                    RenderTargetPool.Unlock(previous);
                }

                passResults.Add(intermediate);
                workingPasses.Clear();
            }

            RenderTargetPool.Unlock(InputTexture as RenderTarget2D);

            if (fullScreenQuad == null)
            {
                fullScreenQuad = new FullScreenQuad(context.graphics);
            }

            context.graphics.BlendState = BlendState.Opaque;
            Material.texture            = InputTexture;

            for (i = 0, p = 0; p < passes.Count; p++)
            {
                if (passes[p].Enabled)
                {
                    Material.SetTexture(passes[p].TextureUsage, passResults[i]);
                    i++;
                }
            }

            fullScreenQuad.Draw(context, Material);

            lastEffects.Clear();
            passResults.Clear();
        }
Example #2
0
        public override void Draw(DrawingContext context, IList <IDrawableObject> drawables)
        {
            if (Material == null)
            {
                Material = adoptionMaterial = new AdaptionMaterial(context.graphics);
            }
            else if (Material != adoptionMaterial)
            {
                throw new InvalidOperationException();
            }

            bool needLocalTexture = false;

            if (needLocalTexture = (currentFrame == null))
            {
                PrepareRenderTarget(context.graphics, InputTexture, null);
                currentFrame.Begin();
            }

            // Disable the adoption effect when we don't have a valid last frame texture
            // or when the adoption effect has been suspended for several frames.
#if SILVERLIGHT
            if (lastFrame == null || lastFrame.IsDisposed)
#else
            if (lastFrame == null || lastFrame.IsDisposed || lastFrame.IsContentLost)
#endif
            {
                CopyToScreen(context, drawables);
            }
            else
            {
                var graphics = context.graphics;
                graphics.Textures[0]      = adoptionMaterial.texture;
                graphics.Textures[1]      = lastFrame;
                graphics.SamplerStates[0] = graphics.SamplerStates[1] = SamplerState.PointClamp;
                adoptionMaterial.effect.Delta.SetValue((1 - (float)Math.Pow(0.98f, 30 * context.elapsedTime)) * Speed);

                base.Draw(context, drawables);

                graphics.SamplerStates[1] = context.SamplerState;
            }

            if (needLocalTexture)
            {
                currentFrame.End();

                InputTexture = currentFrame;
                CopyToScreen(context, drawables);
            }

            RenderTargetPool.Unlock(lastFrame);
            lastFrame    = currentFrame;
            currentFrame = null;
        }
Example #3
0
        /// <summary>
        /// Draws this pass using the specified drawing context.
        /// </summary>
        public override void Draw(DrawingContext context, IList <IDrawableObject> drawables)
        {
            RenderTargetPool.Lock(InputTexture as RenderTarget2D);

            if (fullScreenQuad == null)
            {
                fullScreenQuad = new FullScreenQuad(context.graphics);
            }

            context.graphics.BlendState        = BlendState;
            context.graphics.DepthStencilState = DepthStencilState.None;

            Material.texture = InputTexture;
            fullScreenQuad.Draw(context, Material);

            RenderTargetPool.Unlock(InputTexture as RenderTarget2D);
        }
Example #4
0
        /// <summary>
        /// Draws the specified scene.
        /// </summary>
        public void Draw(float elapsedTime, Matrix view, Matrix projection)
        {
            if (isDrawing)
            {
                throw new InvalidOperationException("Cannot trigger another drawing of the scene while it's still been drawn");
            }

            this.matrices.View          = view;
            this.matrices.Projection    = projection;
            this.isDrawing              = true;
            this.VertexOffset           = 0;
            this.VertexBuffer           = null;
            this.PreviousMaterial       = null;
            this.elapsedTime            = elapsedTime;
            this.totalTime             += TimeSpan.FromSeconds(elapsedTime);
            this.totalSeconds           = (float)totalTime.TotalSeconds;
            this.boundingBoxNeedsUpdate = true;

            if (rootPass == null)
            {
                return;
            }

            graphics.SetVertexBuffer(null);
            graphics.Textures[0] = null;

            UpdateDefaultSamplerStates();

            AddDrawablesToView(matrices.ViewFrustum);

            UpdatePassGraph();
            UpdateActivePasses();

            RenderTarget2D lastRenderTarget            = null;
            RenderTarget2D intermediate                = null;
            bool           overrideViewFrustumLastPass = false;

            for (int i = 0; i < activePasses.Count; ++i)
            {
                var pass = activePasses[i];
                var overrideViewFrustum = false;

                // Query the drawables in the current view frustum only when the view frustum changed
                // or the pass overrides the frustum.
                Matrix passView, passProjection;
                if (pass.TryGetViewFrustum(out passView, out passProjection))
                {
                    matrices.View       = passView;
                    matrices.Projection = passProjection;
                    overrideViewFrustum = true;
                }

                if (overrideViewFrustum || overrideViewFrustumLastPass)
                {
                    AddDrawablesToView(matrices.ViewFrustum);
                    overrideViewFrustumLastPass = overrideViewFrustum;
                }

                if ((pass.PassOperation & PassOperation.EndRenderTarget) != 0)
                {
                    intermediate.End();
                    lastRenderTarget = intermediate;
                }

                if ((pass.PassOperation & PassOperation.BeginRenderTarget) != 0)
                {
                    intermediate = pass.PrepareRenderTarget(graphics, intermediate, pass.PassFormat);
                    intermediate.Begin();
                    RenderTargetPool.Lock(intermediate);
                }

                var postEffect = pass as IPostEffect;
                if (postEffect != null)
                {
                    postEffect.InputTexture = lastRenderTarget;
                    pass.Draw(this, drawablesInViewFrustum);
                    RenderTargetPool.Unlock(lastRenderTarget);
                }
                else
                {
                    pass.Draw(this, drawablesInViewFrustum);
                }
            }

            currentFrame++;
            isDrawing = false;
        }
Example #5
0
        private void DrawWithoutCombineMaterial(DrawingContext context, IList <IDrawableObject> drawables)
        {
            RenderTargetPool.Lock(InputTexture as RenderTarget2D);

            int i, p;

            for (p = 0; p < passes.Count; p++)
            {
                passes[p].GetActivePasses(workingPasses);

                RenderTarget2D intermediate = InputTexture as RenderTarget2D;
                for (i = 0; i < workingPasses.Count - 1; ++i)
                {
                    var workingPass = (PostEffect)workingPasses[i];

                    RenderTarget2D previous = intermediate;
                    RenderTargetPool.Lock(previous);
                    intermediate = workingPass.PrepareRenderTarget(context.graphics, intermediate, null);
                    intermediate.Begin();

                    workingPass.InputTexture = previous;
                    workingPass.Draw(context, drawables);

                    intermediate.End();
                    RenderTargetPool.Unlock(previous);
                }

                PostEffect lastEffect;
                RenderTargetPool.Lock(intermediate);

                if (workingPasses.Count > 0)
                {
                    lastEffect = (PostEffect)workingPasses[workingPasses.Count - 1];
                }
                else
                {
                    if (basicPostEffect == null)
                    {
                        basicPostEffect          = new PostEffect();
                        basicPostEffect.Material = new TextureMaterial(context.graphics);
                    }
                    lastEffect = basicPostEffect;
                }

                lastEffects.Add(lastEffect);
                passResults.Add(intermediate);

                workingPasses.Clear();
            }

            RenderTargetPool.Unlock(InputTexture as RenderTarget2D);

            for (i = 0, p = 0; p < passes.Count; p++)
            {
                if (passes[p].Enabled)
                {
                    lastEffects[i].BlendState   = passes[p].BlendState;
                    lastEffects[i].InputTexture = passResults[i];
                    lastEffects[i].Draw(context, drawables);

                    RenderTargetPool.Unlock(passResults[i]);
                    i++;
                }
            }

            lastEffects.Clear();
            passResults.Clear();
        }