예제 #1
0
        }         // Start()

        #endregion

        #region Show
        /// <summary>
        /// Execute shaders and show result on screen, Start(..) must have been
        /// called before and the scene should be rendered to sceneMapTexture.
        /// </summary>
        public void Show()
        {
            // Only apply post screen glow if texture is valid and effect is valid
            if (sceneMapTexture == null ||
                Valid == false ||
                started == false)
            {
                return;
            }

            started = false;

            // Resolve sceneMapTexture render target for Xbox360 support
            sceneMapTexture.Resolve(true);

            try
            {
                // Don't use or write to the z buffer
                BaseGame.Device.RenderState.DepthBufferEnable      = false;
                BaseGame.Device.RenderState.DepthBufferWriteEnable = false;
                // Also don't use any kind of blending.
                BaseGame.Device.RenderState.AlphaBlendEnable = false;
                //unused: BaseGame.Device.RenderState.Lighting = false;

                if (windowSize != null)
                {
                    windowSize.SetValue(new float[]
                                        { sceneMapTexture.Width, sceneMapTexture.Height });
                }
                if (sceneMap != null)
                {
                    sceneMap.SetValue(sceneMapTexture.XnaTexture);
                }
                if (downsampleMap != null)
                {
                    downsampleMap.SetValue(downsampleMapTexture.XnaTexture);
                }
                if (blurMap1 != null)
                {
                    blurMap1.SetValue(blurMap1Texture.XnaTexture);
                }
                if (blurMap2 != null)
                {
                    blurMap2.SetValue(blurMap2Texture.XnaTexture);
                }
                if (radialSceneMap != null)
                {
                    radialSceneMap.SetValue(radialSceneMapTexture.XnaTexture);
                }

                RadialBlurScaleFactor = //-0.0125f;
                                        // Warning: To big values will make the motion blur look to
                                        // stepy (we see each step and thats not good). -0.02 should be max.
                                        -(0.005f + Player.Speed * 0.015f);

                if (BaseGame.UsePS20)
                {
                    effect.CurrentTechnique = effect.Techniques["ScreenGlow20"];
                }
                else
                {
                    effect.CurrentTechnique = effect.Techniques["ScreenGlow"];
                }

                // We must have exactly 5 passes!
                if (effect.CurrentTechnique.Passes.Count != 5)
                {
                    throw new Exception("This shader should have exactly 5 passes!");
                }

                effect.Begin();                //SaveStateMode.None);
                for (int pass = 0; pass < effect.CurrentTechnique.Passes.Count;
                     pass++)
                {
                    if (pass == 0)
                    {
                        radialSceneMapTexture.SetRenderTarget();
                    }
                    else if (pass == 1)
                    {
                        downsampleMapTexture.SetRenderTarget();
                    }
                    else if (pass == 2)
                    {
                        blurMap1Texture.SetRenderTarget();
                    }
                    else if (pass == 3)
                    {
                        blurMap2Texture.SetRenderTarget();
                    }
                    else
                    {
                        // Do a full reset back to the back buffer
                        RenderToTexture.ResetRenderTarget(true);
                    }

                    EffectPass effectPass = effect.CurrentTechnique.Passes[pass];
                    effectPass.Begin();
                    // For first effect we use radial blur, draw it with a grid
                    // to get cooler results (more blur at borders than in middle).
                    if (pass == 0)
                    {
                        VBScreenHelper.Render10x10Grid();
                    }
                    else
                    {
                        VBScreenHelper.Render();
                    }
                    effectPass.End();

                    if (pass == 0)
                    {
                        radialSceneMapTexture.Resolve(false);
                        if (radialSceneMap != null)
                        {
                            radialSceneMap.SetValue(radialSceneMapTexture.XnaTexture);
                        }
                        effect.CommitChanges();
                    }                     // if
                    else if (pass == 1)
                    {
                        downsampleMapTexture.Resolve(false);
                        if (downsampleMap != null)
                        {
                            downsampleMap.SetValue(downsampleMapTexture.XnaTexture);
                        }
                        effect.CommitChanges();
                    }                     // if
                    else if (pass == 2)
                    {
                        blurMap1Texture.Resolve(false);
                        if (blurMap1 != null)
                        {
                            blurMap1.SetValue(blurMap1Texture.XnaTexture);
                        }
                        effect.CommitChanges();
                    }                     // else if
                    else if (pass == 3)
                    {
                        blurMap2Texture.Resolve(false);
                        if (blurMap2 != null)
                        {
                            blurMap2.SetValue(blurMap2Texture.XnaTexture);
                        }
                        effect.CommitChanges();
                    }     // else if
                }         // for (pass, <, ++)
            }             // try
            catch (Exception ex)
            {
                // Make effect invalid, continue rendering without this
                // post screen shader.
                effect = null;
                RenderToTexture.ResetRenderTarget(true);
#if DEBUG
                throw ex;
#else
                Log.Write("Failed to render post screen shader " + Filename + ": " +
                          ex.ToString());
#endif
            }             // catch
            finally
            {
                if (effect != null)
                {
                    effect.End();
                }

                // Restore z buffer state
                BaseGame.Device.RenderState.DepthBufferEnable      = true;
                BaseGame.Device.RenderState.DepthBufferWriteEnable = true;
            }     // finally
        }         // Show()
        //*/

        /// <summary>
        /// Test create render to texture
        /// </summary>
        public static void TestCreateRenderToTexture()
        {
            Model testModel = null;
            RenderToTexture renderToTexture = null;

            TestGame.Start(
                "TestCreateRenderToTexture",
                delegate
                {
                    testModel = new Model("asteroid1");
                    renderToTexture = new RenderToTexture(
                        //SizeType.QuarterScreen);
                        SizeType.HalfScreen);
                        //SizeType.HalfScreenWithZBuffer);
                        //SizeType.FullScreen);
                        //SizeType.ShadowMap);
                },
                delegate
                {
                    bool renderToTextureWay =
                        Input.Keyboard.IsKeyUp(Keys.Space) &&
                        Input.GamePadAPressed == false;
                    BaseGame.Device.RenderState.DepthBufferEnable = true;

                    if (renderToTextureWay)
                    {
                        // Set render target to our texture
                        renderToTexture.SetRenderTarget();

                        // Clear background
                        renderToTexture.Clear(Color.Blue);

                        // Draw background lines
                        //Line.DrawGrid();
                        //Ui.LineManager.RenderAll3DLines();

                        // And draw object
                        testModel.Render(Matrix.CreateScale(7.5f));
                        //BaseGame.RenderManager.RenderAllMeshes();

                        // Do we need to resolve?
                        renderToTexture.Resolve(true);
                        //BaseGame.Device.ResolveRenderTarget(0);

                        // Reset background buffer
                        //not longer required, done in Resolve now:
                        //RenderToTexture.ResetRenderTarget(true);
                    } // if (renderToTextureWay)
                    else
                    {
                        // Copy backbuffer way, render stuff normally first
                        // Clear background
                        BaseGame.Device.Clear(Color.Blue);

                        // Draw background lines
                        //Line.DrawGrid();
                        //Ui.LineManager.RenderAll3DLines();

                        // And draw object
                        testModel.Render(Matrix.CreateScale(7.5f));
                        //BaseGame.RenderManager.RenderAllMeshes();
                    } // else

                    // Show render target in a rectangle on our screen
                    renderToTexture.RenderOnScreen(
                        //tst:
                        new Rectangle(100, 100, 256, 256));
                    //BaseGame.ScreenRectangle);
                    //no need: BaseGame.UI.FlushUI();

                    TextureFont.WriteText(2, 0,
                        "               Press Space to toogle full screen rendering");
                    TextureFont.WriteText(2, 30,
                        "renderToTexture.Width=" + renderToTexture.Width);
                    TextureFont.WriteText(2, 60,
                        "renderToTexture.Height=" + renderToTexture.Height);
                    TextureFont.WriteText(2, 90,
                        "renderToTexture.Valid=" + renderToTexture.IsValid);
                    TextureFont.WriteText(2, 120,
                        "renderToTexture.XnaTexture=" + renderToTexture.XnaTexture);
                    TextureFont.WriteText(2, 150,
                        "renderToTexture.ZBufferSurface=" + renderToTexture.ZBufferSurface);
                    TextureFont.WriteText(2, 180,
                        "renderToTexture.Filename=" + renderToTexture.Filename);
                });
        }
        }         // ResetRenderTarget(fullResetToBackBuffer)

        #endregion

        #region Unit Testing
#if DEBUG
        /// <summary>
        /// Test create render to texture
        /// </summary>
        static public void TestCreateRenderToTexture()
        {
            Model           testModel       = null;
            RenderToTexture renderToTexture = null;

            TestGame.Start(
                "TestCreateRenderToTexture",
                delegate
            {
                testModel       = new Model("asteroid1");
                renderToTexture = new RenderToTexture(
                    //SizeType.QuarterScreen);
                    SizeType.HalfScreen);
                //SizeType.HalfScreenWithZBuffer);
                //SizeType.FullScreen);
                //SizeType.ShadowMap);
            },
                delegate
            {
                bool renderToTextureWay =
                    Input.Keyboard.IsKeyUp(Keys.Space) &&
                    Input.GamePadAPressed == false;
                BaseGame.Device.RenderState.DepthBufferEnable = true;

                if (renderToTextureWay)
                {
                    // Set render target to our texture
                    renderToTexture.SetRenderTarget();

                    // Clear background
                    renderToTexture.Clear(Color.Blue);

                    // Draw background lines
                    //Line.DrawGrid();
                    //Ui.LineManager.RenderAll3DLines();

                    // And draw object
                    testModel.Render(Matrix.CreateScale(7.5f));
                    //BaseGame.RenderManager.RenderAllMeshes();

                    // Do we need to resolve?
                    renderToTexture.Resolve(true);
                    //BaseGame.Device.ResolveRenderTarget(0);

                    // Reset background buffer
                    //not longer required, done in Resolve now:
                    //RenderToTexture.ResetRenderTarget(true);
                }                         // if (renderToTextureWay)
                else
                {
                    // Copy backbuffer way, render stuff normally first
                    // Clear background
                    BaseGame.Device.Clear(Color.Blue);

                    // Draw background lines
                    //Line.DrawGrid();
                    //Ui.LineManager.RenderAll3DLines();

                    // And draw object
                    testModel.Render(Matrix.CreateScale(7.5f));
                    //BaseGame.RenderManager.RenderAllMeshes();
                }                         // else

                // Show render target in a rectangle on our screen
                renderToTexture.RenderOnScreen(
                    //tst:
                    new Rectangle(100, 100, 256, 256));
                //BaseGame.ScreenRectangle);
                //no need: BaseGame.UI.FlushUI();

                TextureFont.WriteText(2, 0,
                                      "               Press Space to toogle full screen rendering");
                TextureFont.WriteText(2, 30,
                                      "renderToTexture.Width=" + renderToTexture.Width);
                TextureFont.WriteText(2, 60,
                                      "renderToTexture.Height=" + renderToTexture.Height);
                TextureFont.WriteText(2, 90,
                                      "renderToTexture.Valid=" + renderToTexture.IsValid);
                TextureFont.WriteText(2, 120,
                                      "renderToTexture.XnaTexture=" + renderToTexture.XnaTexture);
                TextureFont.WriteText(2, 150,
                                      "renderToTexture.ZBufferSurface=" + renderToTexture.ZBufferSurface);
                TextureFont.WriteText(2, 180,
                                      "renderToTexture.Filename=" + renderToTexture.Filename);
            });
        }         // TestCreateRenderToTexture()