Exemplo n.º 1
0
        /// <summary>
        /// Render
        /// </summary>
        /// <param name="setMat">Set matrix</param>
        /// <param name="techniqueName">Technique name</param>
        /// <param name="renderDelegate">Render delegate</param>
        public void RenderCollada(string techniqueName,
                                  RenderDelegate renderDelegate)
        {
            /*will become important later in the book.
             * // Can we do the requested technique?
             * // For graphic cards not supporting ps2.0, fall back to ps1.1
             * if (BaseGame.CanUsePS20 == false &&
             * techniqueName.EndsWith("20"))
             * // Use same technique without the 20 ending!
             * techniqueName = techniqueName.Substring(0, techniqueName.Length - 2);
             */

            // Start shader
            effect.CurrentTechnique = effect.Techniques[techniqueName];
            effect.Begin(SaveStateMode.None);

            // Render all passes (usually just one)
            //foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            for (int num = 0; num < effect.CurrentTechnique.Passes.Count; num++)
            {
                EffectPass pass = effect.CurrentTechnique.Passes[num];

                pass.Begin();
                renderDelegate();
                pass.End();
            } // foreach (pass)

            // End shader
            effect.End();
        } // Render(passName, renderDelegate)
        }         // Update()

        #endregion

        #region Render

        /*unused, always specify a technique name
         * /// <summary>
         * /// Render
         * /// </summary>
         * /// <param name="renderDelegate">Render delegate</param>
         * public void Render(BaseGame.RenderDelegate renderDelegate)
         * {
         *      SetParameters();
         *
         *      //not supported or bug, no multiple techniques possible yet:
         *      //effect.Technique = "SpecularPerPixel";
         *
         *      // Start shader
         *      effect.Begin(EffectStateOptions.Default);
         *      foreach (EffectPass pass in effect.CurrentTechnique.Passes)
         *      {
         *              // Render each pass
         *              pass.Begin();
         *              renderDelegate();
         *              pass.End();
         *      } // foreach (pass)
         *
         *      // End shader
         *      effect.End();
         * } // Render(renderDelegate)
         */

        /*unused, always specify a technique name
         * /// <summary>
         * /// Render
         * /// </summary>
         * /// <param name="setMat">Set matrix</param>
         * /// <param name="renderDelegate">Render delegate</param>
         * public void Render(Material setMat,
         *      BaseGame.RenderDelegate renderDelegate)
         * {
         *      SetParameters(setMat);
         *
         *      //not supported or bug, no multiple techniques possible yet:
         *      //effect.Technique = "SpecularPerPixel";
         *
         *      // Start shader
         *      effect.Begin(EffectStateOptions.Default);
         *      foreach (EffectPass pass in effect.CurrentTechnique.Passes)
         *      {
         *              // Render each pass
         *              pass.Begin();
         *              renderDelegate();
         *              pass.End();
         *      } // foreach (pass)
         *
         *      // End shader
         *      effect.End();
         * } // Render(setMat, renderDelegate)
         */

        /// <summary>
        /// Render
        /// </summary>
        /// <param name="setMat">Set matrix</param>
        /// <param name="passName">Pass name</param>
        /// <param name="renderDelegate">Render delegate</param>
        public void Render(Material setMat,
                           string techniqueName,
                           BaseGame.RenderDelegate renderDelegate)
        {
            SetParameters(setMat);

            // Can we do the requested technique?
            // For graphic cards not supporting ps2.0, fall back to ps1.1
            if (BaseGame.CanUsePS20 == false &&
                techniqueName.EndsWith("20"))
            {
                // Use same technique without the 20 ending!
                techniqueName = techniqueName.Substring(0, techniqueName.Length - 2);
            }

            // Start shader
            effect.CurrentTechnique = effect.Techniques[techniqueName];
            effect.Begin(SaveStateMode.None);

            // Render all passes (usually just one)
            //foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            for (int num = 0; num < effect.CurrentTechnique.Passes.Count; num++)
            {
                EffectPass pass = effect.CurrentTechnique.Passes[num];

                pass.Begin();
                renderDelegate();
                pass.End();
            }             // foreach (pass)

            // End shader
            effect.End();
        }         // Render(setMat, passName, renderDelegate)
Exemplo n.º 3
0
        private void Render()
        {
            if (this.vertexCount > 0)
            {
                if (this.declaration == null || this.declaration.IsDisposed)
                {
                    this.declaration = new VertexDeclaration(device, VertexPositionTexture.VertexElements);
                }

                device.VertexDeclaration = this.declaration;

                Effect effect = this.Effect;
                //  set the only parameter this effect takes.
                effect.Parameters["viewProjection"].SetValue(this.View * this.Projection);

                EffectTechnique technique = effect.CurrentTechnique;
                effect.Begin();
                EffectPassCollection passes = technique.Passes;
                for (int i = 0; i < passes.Count; i++)
                {
                    EffectPass pass = passes[i];
                    pass.Begin();

                    device.DrawUserIndexedPrimitives <VertexPositionTexture>(
                        PrimitiveType.TriangleList, this.vertices, 0, this.vertexCount,
                        this.indices, 0, this.indexCount / 3);

                    pass.End();
                }
                effect.End();

                this.vertexCount = 0;
                this.indexCount  = 0;
            }
        }
        }   // end of TopLevelPalette Update()

        public void Render()
        {
            if (Active)
            {
                GraphicsDevice device = BokuGame.bokuGame.GraphicsDevice;

                device.VertexDeclaration = decl;

                // Render all passes.
                effect.Parameters["FrameTexture"].SetValue(frameTexture);
                effect.Parameters["ScreenTexture"].SetValue(screenTexture);
                effect.Parameters["IconTexture"].SetValue(iconTexture);

                effect.Parameters["Alpha"].SetValue(Alpha);

                effect.CurrentTechnique = effect.Techniques["Normal"];

                effect.Parameters["NumIcons"].SetValue((float)numIcons);

                for (int i = 0; i < numIcons; i++)
                {
                    effect.Parameters["Color" + i.ToString()].SetValue(color[i]);
                }

                effect.Begin();
                for (int i = 0; i < effect.CurrentTechnique.Passes.Count; i++)
                {
                    EffectPass pass = effect.CurrentTechnique.Passes[i];
                    pass.Begin();
                    device.DrawUserPrimitives(PrimitiveType.TriangleStrip, vertices, 0, 6);
                    pass.End();
                }
                effect.End();
            } // end if Active
        }     // end of TopLevelPalette Render()
Exemplo n.º 5
0
            }   // end of RenderObj Init()

            public override void Render(Camera camera)
            {
                Matrix viewMatrix = camera.ViewMatrix;
                Matrix projMatrix = camera.ProjectionMatrix;

                Matrix worldViewProjMatrix = shared.worldMatrix * viewMatrix * projMatrix;

                effect.Parameters["WorldViewProjMatrix"].SetValue(worldViewProjMatrix);
                effect.Parameters["WorldMatrix"].SetValue(shared.worldMatrix);

                effect.Parameters["LightTexture"].SetValue(texture);

                effect.Parameters["Color"].SetValue(diffuse);
                effect.Parameters["TextureOffset"].SetValue(shared.TextureOffset);

                device.VertexDeclaration = decl;
                device.Vertices[0].SetSource(vbuf, 0, stride);
                device.Indices = ibuf;

                // Render all passes.
                effect.CurrentTechnique = effect.Techniques["AdditiveTexturedColorPass"];

                effect.Begin();
                for (int i = 0; i < effect.CurrentTechnique.Passes.Count; i++)
                {
                    EffectPass pass = effect.CurrentTechnique.Passes[i];
                    pass.Begin();
                    device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, numVertices, 0, numTriangles);
                    pass.End();
                }
                effect.End();
            }   // end of RenderObj Render()
        public void Render()
        {
            //game.GraphicsDevice.RenderState.FillMode = FillMode.WireFrame;
            game.GraphicsDevice.RenderState.CullMode = CullMode.None;
            effect.World      = World;
            effect.View       = game.Camera.View;
            effect.Projection = game.Camera.Projection;


            effect.Begin(SaveStateMode.None);

            // Render all passes (usually just one)
            //foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            for (int num = 0; num < effect.CurrentTechnique.Passes.Count; num++)
            {
                EffectPass pass = effect.CurrentTechnique.Passes[num];

                pass.Begin();
                renderPrimitives();
                pass.End();
            } // foreach (pass)

            // End shader
            effect.End();
        }
            }             // Add(addMesh)

            #endregion

            #region Render
            /// <summary>
            /// Render all meshes that use this technique sorted by the materials.
            /// This method is only called if we got any meshes to render,
            /// which is determinated if NumberOfRenderMeshes is greater 0.
            /// </summary>
            /// <param name="effect">Effect</param>
            public void Render(Effect effect)
            {
                // Start effect for this technique
                effect.CurrentTechnique = technique;
                effect.Begin(SaveStateMode.None);

                // Render all pass (we always just have one)
                //obs: foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    EffectPass pass = effect.CurrentTechnique.Passes[0];

                    pass.Begin();
                    // Render all meshes sorted by all materials.
                    //obs: foreach (MeshesPerMaterial list in meshesPerMaterials)
                    for (int listNum = 0; listNum < meshesPerMaterials.Count; listNum++)
                    {
                        MeshesPerMaterial list = meshesPerMaterials[listNum];
                        if (list.NumberOfRenderMatrices > 0)
                        {
                            list.Render();
                        }
                    }             // for (listNum)
                    pass.End();
                }                 // foreach (pass)

                // End shader
                effect.End();
            }             // Render(effect)
Exemplo n.º 8
0
            /// <summary>
            /// Render all meshes that use this technique sorted by the materials.
            /// This method is only called if we got any meshes to render,
            /// which is determinated if NumberOfRenderMeshes is greater 0.
            /// </summary>
            /// <param name="effect">Effect</param>
            public void Render(Effect effect)
            {
                // Start effect for this technique
                effect.CurrentTechnique = technique;
                try
                {
                    effect.Begin(SaveStateMode.None);

                    // Render all pass (we always just have one)
                    EffectPass pass = effect.CurrentTechnique.Passes[0];

                    pass.Begin();
                    // Render all meshes sorted by all materials.
                    for (int listNum = 0; listNum < meshesPerMaterials.Count; listNum++)
                    {
                        MeshesPerMaterial list = meshesPerMaterials[listNum];
                        if (list.NumberOfRenderMatrices > 0)
                        {
                            list.Render();
                        }
                    }
                    pass.End();
                }
                finally
                {
                    // End shader
                    effect.End();
                }
            }
Exemplo n.º 9
0
        /// <summary>
        /// Draw a list of Lines.
        /// </summary>
        public void Draw(List <RoundLine> roundLines, float lineRadius, Color lineColor /*, Matrix viewProjMatrix*/,
                         float time, string techniqueName)
        {
            device.VertexDeclaration = vdecl;
            device.Vertices[0].SetSource(vb, 0, bytesPerVertex);
            device.Indices = ib;

            viewProjMatrixParameter.SetValue(mProjection);
            timeParameter.SetValue(time);
            lineColorParameter.SetValue(lineColor.ToVector4());
            lineRadiusParameter.SetValue(lineRadius);
            blurThresholdParameter.SetValue(BlurThreshold);

            if (techniqueName == null)
            {
                effect.CurrentTechnique = effect.Techniques[0];
            }
            else
            {
                effect.CurrentTechnique = effect.Techniques[techniqueName];
            }
            effect.Begin();
            EffectPass pass = effect.CurrentTechnique.Passes[0];

            pass.Begin();

            int iData = 0;
            int numInstancesThisDraw = 0;

            foreach (RoundLine roundLine in roundLines)
            {
                translationData[iData++] = roundLine.P0.X;
                translationData[iData++] = roundLine.P0.Y;
                translationData[iData++] = roundLine.Rho;
                translationData[iData++] = roundLine.Theta;
                numInstancesThisDraw++;

                if (numInstancesThisDraw == numInstances)
                {
                    instanceDataParameter.SetValue(translationData);
                    effect.CommitChanges();
                    device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, numVertices, 0, numPrimitivesPerInstance * numInstancesThisDraw);
                    NumLinesDrawn       += numInstancesThisDraw;
                    numInstancesThisDraw = 0;
                    iData = 0;
                }
            }
            if (numInstancesThisDraw > 0)
            {
                instanceDataParameter.SetValue(translationData);
                effect.CommitChanges();
                device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, numVertices, 0, numPrimitivesPerInstance * numInstancesThisDraw);
                NumLinesDrawn += numInstancesThisDraw;
            }
            pass.End();

            effect.End();
        }
        public void DrawPrimitives(RenderPrimitivesDelegate renderPrimitives)
        {
            Shader.effect.Begin(SaveStateMode.None);

            EffectPass pass = Shader.effect.CurrentTechnique.Passes[0];

            pass.Begin();
            renderPrimitives();
            pass.End();

            Shader.effect.End();
        }
Exemplo n.º 11
0
        public override void Draw()
        {
            fx.Begin();
            for (int i = 0; i < fx.CurrentTechnique.Passes.Count; i++)
            {
                EffectPass pass = fx.CurrentTechnique.Passes[i];

                pass.Begin();
                device.VertexDeclaration = vertexDeclaration;
                device.DrawUserIndexedPrimitives <VertexPositionColor>
                    (PrimitiveType.TriangleList, vertices, 0, 4, indices, 0, 2);
                pass.End();
            }
            fx.End();
        }
Exemplo n.º 12
0
        void FinishDrawing(PrimitiveType drawtype, Int32 count)
        {
            m_effect.Begin();
            for (Int32 i = 0; i != m_effect.CurrentTechnique.Passes.Count; ++i)
            {
                EffectPass pass = m_effect.CurrentTechnique.Passes[i];

                pass.Begin();
                Device.DrawUserPrimitives <Vertex>(drawtype, m_drawbuffer, 0, count);
                pass.End();
            }
            m_effect.End();

            m_effect.GraphicsDevice.Textures[0] = null;
            m_effect.GraphicsDevice.Textures[1] = null;
        }
Exemplo n.º 13
0
        public static void RenderSphere(BoundingSphere sphere, Color color, ref Matrix view, ref Matrix projection)
        {
            //this null check is here in case we never ran the InitializeBuffers method of this class
            //if this is the case the device will be null
            if (device == null)
            {
                return;
            }

            device.VertexDeclaration = vertexDeclaration;
            device.Vertices[0].SetSource(sphereBuffer, 0, VertexPositionColor.SizeInBytes);

            effect.View       = view;
            effect.Projection = projection;

            Matrix scale       = Matrix.CreateScale(sphere.Radius);
            Matrix translation = Matrix.CreateTranslation(sphere.Center);

            effect.Begin(SaveStateMode.SaveState);

            for (int i = 0; i < effect.CurrentTechnique.Passes.Count; i++)
            {
                EffectPass pass = effect.CurrentTechnique.Passes[i];

                pass.Begin();

                effect.DiffuseColor = color.ToVector3();//Vector3.UnitY;
                effect.World        = scale * translation;
                effect.CommitChanges();
                device.DrawPrimitives(PrimitiveType.LineStrip, 0, numberOfSphereVerts - 1);

                effect.DiffuseColor = color.ToVector3(); //Vector3.UnitZ;
                effect.World        = scale * Matrix.CreateRotationX(MathHelper.PiOver2) * translation;
                effect.CommitChanges();
                device.DrawPrimitives(PrimitiveType.LineStrip, 0, numberOfSphereVerts - 1);

                effect.DiffuseColor = color.ToVector3(); //Vector3.UnitX;
                effect.World        = scale * Matrix.CreateRotationZ(MathHelper.PiOver2) * translation;
                effect.CommitChanges();
                device.DrawPrimitives(PrimitiveType.LineStrip, 0, numberOfSphereVerts - 1);

                pass.End();
            }

            effect.End();
        }
Exemplo n.º 14
0
        public override void Draw(GameTime gameTime)
        {
            //GraphicsDevice.Clear(Color.Black);
            GraphicsDevice.SetRenderTarget(0, target1);
            //GraphicsDevice.SetRenderTarget(0, null);
            GraphicsDevice.Clear(Color.Black);

            // Draw on the target1
            base.Draw(gameTime);

            // Draw post-screen fx
            #region Effects render
            for (int i = 0; i < blur.CurrentTechnique.Passes.Count; i++)
            {
                if (i == 0)
                {
                    GraphicsDevice.SetRenderTarget(0, target2);
                }
                else
                {
                    GraphicsDevice.SetRenderTarget(0, null);
                }

                game.spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);

                blur.Begin();

                EffectPass pass = blur.CurrentTechnique.Passes[i];

                pass.Begin();

                Texture2D tex = (i == 0) ? target1.GetTexture() : target2.GetTexture();

                game.spriteBatch.Draw(tex, new Vector2(0, 0), Color.White);

                pass.End();

                blur.End();

                //if (i == 1)
                //    game.spriteBatch.Draw(target1.GetTexture(), new Vector2(0, 0), Color.White);

                game.spriteBatch.End();
            }
            #endregion
        }
Exemplo n.º 15
0
        private void PostRender()
        {
            //graphics.GraphicsDevice.ResolveRenderTarget(0);
            texturedRenderedTo = renderTarget.GetTexture();

            graphics.GraphicsDevice.SetRenderTarget(0, null);
            graphics.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);

            postEffect.Begin();

            camSpeed = Math.Abs(Editor.camera.speed);

            if (camSpeed != 0f)
            {
                if (effectSpeed < camSpeed * 0.003f)
                {
                    effectSpeed = camSpeed * 0.003f;
                }
            }
            else
            {
                if (effectSpeed > 0.0003f)
                {
                    effectSpeed *= 0.92f;
                }
                else
                {
                    effectSpeed = 0f;
                }
            }

            postEffect.Parameters["speed"].SetValue(effectSpeed);

            spriteBatch.Begin(postBlendMode, SpriteSortMode.Immediate, SaveStateMode.SaveState);

            EffectPass pass = postEffect.CurrentTechnique.Passes[0];

            pass.Begin();

            spriteBatch.Draw(texturedRenderedTo, new Vector2(0, 0), null, Color.White, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1);

            pass.End();
            spriteBatch.End();
            postEffect.End();
        }
Exemplo n.º 16
0
        //* ────________________________________*
        //* methods ───────────────────────────────-*

        //* -----------------------------------------------------------------------*
        /// <summary>1フレーム分の描画処理を実行します。</summary>
        ///
        /// <param name="entity">この状態を適用されているオブジェクト。</param>
        /// <param name="privateMembers">
        /// オブジェクトと状態クラスのみがアクセス可能なフィールド。
        /// </param>
        /// <param name="gameTime">前フレームが開始してからの経過時間。</param>
        public override void draw(CEntity entity, CCursor privateMembers, GameTime gameTime)
        {
            Matrix world = privateMembers.world;

            effect.Parameters["World"].SetValue(world);
            effect.Begin();
            EffectPassCollection passes = effect.CurrentTechnique.Passes;

            for (int i = passes.Count; --i >= 0;)
            {
                EffectPass pass = passes[i];
                pass.Begin();
                device.DrawUserPrimitives <VertexPositionNormalTexture>(
                    PrimitiveType.TriangleStrip, vertex, 0, 2);
                pass.End();
            }
            effect.End();
        }
Exemplo n.º 17
0
        public void RenderSinglePass(Effect effect, params string[] textureParams)
        {
            if (!isVisible)
            {
                return;
            }
            for (int i = 0; i < textureParams.Length; i++)
            {
                effect.Parameters[textureParams[i]].SetValue(textures[i]);
            }
            effect.Begin();
            EffectPass pass = effect.CurrentTechnique.Passes[0];

            pass.Begin();
            Render();
            pass.End();
            effect.End();
        }
Exemplo n.º 18
0
        /// <summary>
        /// Flushes all queued areas to be drawn.
        /// </summary>
        public void Flush()
        {
            if (this.Areas.Count > 0)
            {
                Matrix world;
                Matrix worldViewProjection;

                this.Effect.CurrentTechnique = this.Effect.Techniques[0];
                this.Effect.Begin();

                EffectPass pass = this.Effect.CurrentTechnique.Passes[0];

                this.GraphicsDevice.VertexDeclaration = this.VertexDeclaration;
                this.GraphicsDevice.Vertices[0].SetSource(this.VertexBuffer, 0, this.BytesPerVertices);
                this.GraphicsDevice.Indices = this.IndexBuffer;

                pass.Begin();

                for (int index = 0; index < this.Areas.Count; index++)
                {
                    if (this.Areas[index] != null)
                    {
                        world = this.Areas[index].World;
                        worldViewProjection = world * this.Camera.AreaView * this.Camera.AreaProjection;

                        this.Effect.Parameters["worldViewProj"].SetValue(worldViewProjection);
                        this.Effect.Parameters["color"].SetValue(this.Areas[index].Color.ToVector4());
                        this.Effect.Parameters["blurThreshold"].SetValue(0.95f);

                        this.Effect.CommitChanges();

                        this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, this.VerticesNumber, 0, this.PrimitivesNumber);
                    }
                }

                pass.End();

                this.Effect.End();

                this.Areas.Clear();
            }
        }
        /// <summary>
        /// This method is meant for usage with one shader per meshpart, all parameters preset and using shared parameters.
        /// Technique should have just one pass.
        /// </summary>
        /// <param name="primitive"></param>
        /// <param name="state"></param>
        public void RenderPrimitiveSinglePass(IRenderPrimitives primitives, SaveStateMode state)
        {
            Shader.effect.Begin(state);


            // Render all passes (usually just one)
            //for ( int num = 0; num < Shader.effect.CurrentTechnique.Passes.Count; num++ )
            //{
            //EffectPass pass = Shader.effect.CurrentTechnique.Passes[ num ];
            EffectPass pass = Shader.effect.CurrentTechnique.Passes[0];

            pass.Begin();
            primitives.RenderPrimitives();
            pass.End();
            //}


            // End shader
            Shader.effect.End();
        }
Exemplo n.º 20
0
        /// <summary>
        /// Render using this shader.
        /// </summary>
        /// <param name="setMat">Set matrix</param>
        /// <param name="techniqueName">Technique name</param>
        /// <param name="renderDelegate">Render delegate</param>
        public void RenderMultipass(RenderDelegate renderDelegate)
        {
            //SetCameraParameters( game.Camera );
            // Start shader
            //effect.CurrentTechnique = effect.Techniques[ techniqueName ];
            effect.Begin(SaveStateMode.None);

            // Render all passes (usually just one)
            //foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            for (int num = 0; num < effect.CurrentTechnique.Passes.Count; num++)
            {
                EffectPass pass = effect.CurrentTechnique.Passes[num];

                pass.Begin();
                renderDelegate();
                pass.End();
            } // foreach (pass)

            // End shader
            effect.End();
        } // Render(passName, renderDelegate)
Exemplo n.º 21
0
        //  do the device and effect magic to render a given chunk of geometry
        private void DrawChunk(ref XCamera Camera, ref Matrix world, Chunk ch)
        {
            //  configure the device and actually draw
            X.GraphicsDevice.Indices           = ch.Mesh.IndexBuffer;
            X.GraphicsDevice.VertexDeclaration = ch.Part.VertexDeclaration;
            X.GraphicsDevice.Vertices[0].SetSource(ch.Mesh.VertexBuffer, ch.Part.StreamOffset, ch.Part.VertexStride);
            //  note: calculating the world matrix overrides the previous value, hence the use
            //  of the saved copy of the world transform
            Matrix chworld;

            Matrix.Multiply(ref matrices_[ch.Mesh.ParentBone.Index], ref world, out chworld);

            //ch.Fx.Setup();
            ch.SAS.Projection = Camera.Projection;
            ch.SAS.World      = chworld;
            ch.SAS.View       = Camera.View;

            ch.SAS.ComputeViewAndProjection();
            ch.SAS.ComputeModel();
            ch.SAS.BindEnvironment();

            for (int k = 0; k < ch.Fx.Parameters.Count; k++)
            {
                ch.SAS.SetEffectParameterValue(ch.Fx.Parameters[k]);
            }

            ch.Fx.Begin(SaveStateMode.SaveState); //  be lazy and save state
            EffectTechnique et = ch.Fx.CurrentTechnique;

            //  most my effects are single-pass, but at least transparency is multi-pass
            for (int i = 0, n = et.Passes.Count; i != n; ++i)
            {
                EffectPass ep = et.Passes[i];
                ep.Begin();
                X.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, ch.Part.BaseVertex,
                                                       0, ch.Part.NumVertices, ch.Part.StartIndex, ch.Part.PrimitiveCount);
                ep.End();
            }
            ch.Fx.End();
        }
Exemplo n.º 22
0
        public void Draw()
        {
            if (UIReady == false)
            {
                PrepareRenderer();
            }


            if (UITexture != null && !UITexture.IsDisposed && !UIRectangle.IsEmpty)
            {
                if (this.isBloom == false)
                {
                    UISpriteBatch.Draw(UITexture, UIPosition, UIRectangle, new Color(Color.White, UIAlpha), UIRotation,
                                       new Vector2(0, 0), 1.0f, SpriteEffects.None, 0.0f);
                }
                else
                {
                    UISpriteBatch.End();
                    UIBloom.Parameters["mag"].SetValue(this.UIBPara);
                    UIBloom.Parameters["alpha"].SetValue(UIAlpha);
                    UIBloom.Parameters["hirange"].SetValue(this.UIHiRange);

                    UIBloom.Begin();
                    UISpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate,
                                        SaveStateMode.SaveState);

                    EffectPass pass = UIBloom.CurrentTechnique.Passes[0];
                    pass.Begin();

                    UISpriteBatch.Draw(UITexture, UIPosition, UIRectangle, new Color(Color.White, UIAlpha), UIRotation,
                                       new Vector2(0, 0), 1.0f, SpriteEffects.None, 0.0f);

                    pass.End();
                    UISpriteBatch.End();
                    UIBloom.End();
                    UISpriteBatch.Begin();
                }
            }
        }
Exemplo n.º 23
0
        /// <summary>
        /// Render
        /// </summary>
        /// <param name="setMat">Set matrix</param>
        /// <param name="passName">Pass name</param>
        /// <param name="renderDelegate">Render delegate</param>
        public void Render(Material setMat,
                           string techniqueName,
                           BaseGame.RenderHandler renderCode)
        {
            if (techniqueName == null)
            {
                throw new ArgumentNullException("techniqueName");
            }
            if (renderCode == null)
            {
                throw new ArgumentNullException("renderCode");
            }

            SetParameters(setMat);

            // Start shader
            effect.CurrentTechnique = effect.Techniques[techniqueName];
            try
            {
                effect.Begin(SaveStateMode.None);

                // Render all passes (usually just one)
                //foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                for (int num = 0; num < effect.CurrentTechnique.Passes.Count; num++)
                {
                    EffectPass pass = effect.CurrentTechnique.Passes[num];

                    pass.Begin();
                    renderCode();
                    pass.End();
                }
            }
            finally
            {
                // End shader
                effect.End();
            }
        }
Exemplo n.º 24
0
        //  do the device and effect magic to render a given chunk of geometry
        private void DrawChunk(DrawDetails dd, Chunk ch)
        {
            //  configure the device and actually draw
            dd.dev.Indices           = ch.Mesh.IndexBuffer;
            dd.dev.VertexDeclaration = ch.Part.VertexDeclaration;
            dd.dev.Vertices[0].SetSource(ch.Mesh.VertexBuffer, ch.Part.StreamOffset, ch.Part.VertexStride);
            //  note: calculating the world matrix overrides the previous value, hence the use
            //  of the saved copy of the world transform
            Matrix.Multiply(ref matrices_[ch.Mesh.ParentBone.Index], ref world_, out dd.world);
            ch.Fx.Setup(dd);
            ch.Fx.FX.Begin(); //  be lazy and save state
            EffectTechnique et = ch.Fx.FX.CurrentTechnique;

            //  most my effects are single-pass, but at least transparency is multi-pass
            for (int i = 0, n = et.Passes.Count; i != n; ++i)
            {
                EffectPass ep = et.Passes[i];
                ep.Begin();
                dd.dev.DrawIndexedPrimitives(PrimitiveType.TriangleList, ch.Part.BaseVertex,
                                             0, ch.Part.NumVertices, ch.Part.StartIndex, ch.Part.PrimitiveCount);
                ep.End();
            }
            ch.Fx.FX.End();
        }
Exemplo n.º 25
0
        public static void RenderBox(Vector3 pos, Matrix orient, Vector3 sideLengths, Color color, ref Matrix view, ref Matrix projection)
        {
            //this null check is here in case we never ran the InitializeBuffers method of this class
            //if this is the case the device will be null
            if (device == null)
            {
                return;
            }

            device.VertexDeclaration = vertexDeclaration;
            device.Vertices[0].SetSource(boxBuffer, 0, VertexPositionColor.SizeInBytes);

            effect.View       = view;
            effect.Projection = projection;

            Matrix scale       = Matrix.CreateScale(sideLengths);
            Matrix translation = Matrix.CreateTranslation(pos);

            effect.Begin(SaveStateMode.SaveState);

            for (int i = 0; i < effect.CurrentTechnique.Passes.Count; i++)
            {
                EffectPass pass = effect.CurrentTechnique.Passes[i];

                pass.Begin();

                effect.DiffuseColor = color.ToVector3();
                effect.World        = scale * translation * orient;
                effect.CommitChanges();
                device.DrawPrimitives(PrimitiveType.LineStrip, 0, numberOfBoxVerts - 1);

                pass.End();
            }

            effect.End();
        }
Exemplo n.º 26
0
        internal static void BloomEnd()
        {
            try
            {
                GameBase.graphics.GraphicsDevice.ResolveRenderTarget(0);
                GameBase.graphics.GraphicsDevice.SetRenderTarget(0, null);
                GameBase.graphics.GraphicsDevice.Clear(Color.Black);

                Texture2D tex = rTarg.GetTexture();

                GameBase.spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate,
                                           SaveStateMode.None);
                GameBase.spriteBatch.GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = true;
                GameBase.spriteBatch.GraphicsDevice.RenderState.AlphaSourceBlend          = Blend.One;
                GameBase.spriteBatch.GraphicsDevice.RenderState.AlphaDestinationBlend     = Blend.InverseSourceAlpha;
                GameBase.spriteBatch.Draw(tex,
                                          new Rectangle(0, 0, GameBase.WindowWidth,
                                                        GameBase.WindowHeight + GameBase.WindowVOffset),
                                          new Rectangle(0, 0, GameBase.WindowWidth,
                                                        GameBase.WindowHeight + GameBase.WindowVOffset), Color.White);
                GameBase.spriteBatch.End();

                bloomatic.Parameters["mag"].SetValue(0.004f);
                bloomatic.Parameters["alpha"].SetValue(0.15f);
                bloomatic.Parameters["hirange"].SetValue(false);
                bloomatic.Parameters["redtint"].SetValue(0);

                bloomatic.Begin();

                GameBase.spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate,
                                           SaveStateMode.None);
                EffectPass pass = bloomatic.CurrentTechnique.Passes[0];
                pass.Begin();
                GameBase.spriteBatch.Draw(tex,
                                          new Rectangle(0, 0, GameBase.WindowWidth,
                                                        GameBase.WindowHeight + GameBase.WindowVOffset),
                                          new Rectangle(0, 0, GameBase.WindowWidth,
                                                        GameBase.WindowHeight + GameBase.WindowVOffset), Color.White);
                pass.End();
                GameBase.spriteBatch.End();

                if (ExtraPass)
                {
                    bloomatic.Parameters["mag"].SetValue(Magnitude);
                    bloomatic.Parameters["alpha"].SetValue(Alpha);
                    bloomatic.Parameters["redtint"].SetValue(RedTint);
                    bloomatic.Parameters["hirange"].SetValue(HiRange);

                    GameBase.spriteBatch.Begin(Additive ? SpriteBlendMode.Additive : SpriteBlendMode.AlphaBlend,
                                               SpriteSortMode.Immediate,
                                               SaveStateMode.None);

                    pass.Begin();
                    GameBase.spriteBatch.Draw(tex,
                                              new Rectangle(0, 0, GameBase.WindowWidth,
                                                            GameBase.WindowHeight + GameBase.WindowVOffset),
                                              new Rectangle(0, 0, GameBase.WindowWidth,
                                                            GameBase.WindowHeight + GameBase.WindowVOffset), Colour);
                    pass.End();
                    GameBase.spriteBatch.End();
                }
                bloomatic.End();
            }
            catch
            {
                ConfigManager.sBloom = false;
                GameBase.ShowMessage("No shader support - disabling bloom effects.");
            }
        }
Exemplo n.º 27
0
        /// <summary>
        /// Show shadows with help of our blur map shader
        /// </summary>
        public void ShowShadows()
        {
            // Only apply post screen blur if texture is valid and effect are valid
            if (blurMapTexture == null ||
                Valid == false ||
                // If the shadow scene map is not yet filled, there is no point
                // continuing here ...
                blurMapTexture.XnaTexture == null)
            {
                return;
            }

            // Don't use or write to the z buffer
            BaseGame.Device.RenderState.DepthBufferEnable      = false;
            BaseGame.Device.RenderState.DepthBufferWriteEnable = false;

            // Make sure we clamp everything to 0-1
            BaseGame.Device.SamplerStates[0].AddressU = TextureAddressMode.Clamp;
            BaseGame.Device.SamplerStates[0].AddressV = TextureAddressMode.Clamp;
            // Restore back buffer as render target
            //not required: BaseGame.ResetRenderTarget(false);

            if (blurMap != null)
            {
                blurMap.SetValue(blurMapTexture.XnaTexture);
            }

            effect.CurrentTechnique = effect.Techniques["ScreenAdvancedBlur20"];

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

            // Render second pass
            try
            {
                effect.Begin(SaveStateMode.None);

                // Use ZeroSourceBlend alpha mode for the final result
                BaseGame.Device.RenderState.AlphaBlendEnable    = true;
                BaseGame.Device.RenderState.AlphaBlendOperation = BlendFunction.Add;
                BaseGame.Device.RenderState.SourceBlend         = Blend.Zero;
                BaseGame.Device.RenderState.DestinationBlend    = Blend.SourceColor;

                EffectPass effectPass = effect.CurrentTechnique.Passes[1];
                effectPass.Begin();
                VBScreenHelper.Render();
                effectPass.End();
            }
            finally
            {
                effect.End();
            }

            // Restore z buffer state
            BaseGame.Device.RenderState.DepthBufferEnable      = true;
            BaseGame.Device.RenderState.DepthBufferWriteEnable = true;
            // Set u/v addressing back to wrap
            BaseGame.Device.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
            BaseGame.Device.SamplerStates[0].AddressV = TextureAddressMode.Wrap;
            // Restore normal alpha blending
            //BaseGame.Device.RenderState.BlendFunction = BlendFunction.Add;
            BaseGame.SetCurrentAlphaMode(BaseGame.AlphaMode.Default);
        }
Exemplo n.º 28
0
        /// <summary>
        /// Show shadows with help of our blur map shader
        /// </summary>
        public void RenderShadows()
        {
            // Only apply post screen blur if texture is valid and effect are valid
            if (sceneMapTexture == null ||
                Valid == false ||
                // If the shadow scene map is not yet filled, there is no point
                // continuing here ...
                sceneMapTexture.XnaTexture == null)
            {
                return;
            }

            // Don't use or write to the z buffer
            BaseGame.Device.RenderState.DepthBufferEnable      = false;
            BaseGame.Device.RenderState.DepthBufferWriteEnable = false;
            // Disable alpha for the first pass
            BaseGame.Device.RenderState.AlphaBlendEnable = false;

            if (windowSize != null)
            {
                windowSize.SetValue(
                    new float[] { sceneMapTexture.Width, sceneMapTexture.Height });
            }
            if (sceneMap != null)
            {
                sceneMap.SetValue(sceneMapTexture.XnaTexture);
            }

            effect.CurrentTechnique = effect.Techniques["ScreenAdvancedBlur20"];

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

            // Just start pass 0
            try
            {
                effect.Begin(SaveStateMode.None);
                blurMapTexture.SetRenderTarget();

                EffectPass effectPass = effect.CurrentTechnique.Passes[0];
                effectPass.Begin();
                VBScreenHelper.Render();
                effectPass.End();
            }
            finally
            {
                effect.End();
            }

            blurMapTexture.Resolve();
            BaseGame.ResetRenderTarget(false);

            // Restore z buffer state
            BaseGame.Device.RenderState.DepthBufferEnable      = true;
            BaseGame.Device.RenderState.DepthBufferWriteEnable = true;
            // Set u/v addressing back to wrap
            BaseGame.Device.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
            BaseGame.Device.SamplerStates[0].AddressV = TextureAddressMode.Wrap;
            // Restore normal alpha blending
            //BaseGame.Device.RenderState.BlendFunction = BlendFunction.Add;
            BaseGame.SetCurrentAlphaMode(BaseGame.AlphaMode.Default);
        }
Exemplo n.º 29
0
        /// <summary>
        /// the mesh is drawn by using the vertexData.
        /// When the userPrimitive member is set to true,
        /// it is drawn without using the vertex buffer and the index buffer.
        /// </summary>
        /// <param name="renderTracer"></param>
        protected override void OnDraw(RenderTracer renderTracer)
        {
            GraphicsDevice device      = renderTracer.Device;
            RenderState    renderState = device.RenderState;

            basicEffect.Texture         = this.textureResource;
            basicEffect.World           = this.TransformedMatrix;
            basicEffect.View            = renderTracer.View;
            basicEffect.Projection      = renderTracer.Projection;
            basicEffect.LightingEnabled = false;

            device.VertexDeclaration          = vertexDeclaration;
            device.SamplerStates[0].AddressU  = TextureAddressMode.Wrap;
            device.SamplerStates[0].AddressV  = TextureAddressMode.Wrap;
            device.SamplerStates[0].MinFilter = TextureFilter.Linear;
            device.SamplerStates[0].MagFilter = TextureFilter.Linear;
            device.SamplerStates[0].MipFilter = TextureFilter.Point;

            renderState.AlphaTestEnable  = alphaTestEnable;
            renderState.AlphaBlendEnable = alphaBlendEnable;
            renderState.AlphaFunction    = alphaFunction;

            renderState.SourceBlend      = sourceBlend;
            renderState.DestinationBlend = destinationBlend;
            renderState.BlendFunction    = blendFunction;

            renderState.ReferenceAlpha         = referenceAlpha;
            renderState.DepthBufferEnable      = depthBufferEnable;
            renderState.DepthBufferWriteEnable = depthBufferWriteEnable;
            renderState.DepthBufferFunction    = depthBufferFunction;
            renderState.CullMode = cullMode;

            basicEffect.Begin();

            for (int i = 0; i < basicEffect.CurrentTechnique.Passes.Count; i++)
            {
                EffectPass pass = basicEffect.CurrentTechnique.Passes[i];

                pass.Begin();

                if (userPrimitive)
                {
                    //  Use index?
                    if (indexData != null)
                    {
                        // only use vertex and index data
                        device.DrawUserIndexedPrimitives <VertexPositionColorTexture>(
                            PrimitiveType.TriangleList,
                            vertexData,
                            0,
                            vertexData.Length,
                            indexData,
                            0,
                            this.primitiveCount);
                    }
                    else
                    {
                        device.DrawUserPrimitives <VertexPositionColorTexture>(
                            PrimitiveType.TriangleList,
                            vertexData,
                            0,
                            this.primitiveCount);
                    }
                }
                else
                {
                    //  Use vertex buffer
                    device.Vertices[0].SetSource(vertexBuffer,
                                                 0,
                                                 VertexPositionColorTexture.SizeInBytes);

                    //  Use index?
                    if (indexBuffer != null)
                    {
                        //  Use index buffer
                        device.Indices = indexBuffer;

                        device.DrawIndexedPrimitives(PrimitiveType.TriangleList,
                                                     0,
                                                     0,
                                                     updateVertexCount,
                                                     0,
                                                     this.primitiveCount);
                    }
                    else
                    {
                        device.DrawPrimitives(PrimitiveType.TriangleList,
                                              0,
                                              this.primitiveCount);
                    }
                }

                pass.End();
            }

            basicEffect.End();

            device.RenderState.DepthBufferWriteEnable = true;
        }
Exemplo n.º 30
0
        /// <summary>
        /// Draws the current frame
        /// </summary>
        /// <param name="gameTime">The game time</param>
        public override void Draw(GameTime gameTime)
        {
            try
            {
                int index = 0;
                // Update all the effects with the palette and world and draw the meshes
                for (int i = 0; i < numMeshes; i++)
                {
                    ModelMesh mesh = model.Meshes[i];
                    // The starting index for the modelEffects array
                    int effectStartIndex = index;
                    if (matrixPaletteParams[index] != null)
                    {
                        foreach (Effect effect in mesh.Effects)
                        {
                            worldParams[index].SetValue(

                                world);


                            matrixPaletteParams[index].SetValue(palette[i]);
                            index++;
                        }
                    }
                    else
                    {
                        foreach (Effect effect in mesh.Effects)
                        {
                            worldParams[index].SetValue(pose[mesh.ParentBone.Index] * world);
                            index++;
                        }
                    }
                    int            numParts = mesh.MeshParts.Count;
                    GraphicsDevice device   = mesh.VertexBuffer.GraphicsDevice;
                    device.Indices = mesh.IndexBuffer;
                    for (int j = 0; j < numParts; j++)
                    {
                        ModelMeshPart currentPart = mesh.MeshParts[j];
                        if (currentPart.NumVertices == 0 || currentPart.PrimitiveCount == 0)
                        {
                            continue;
                        }
                        Effect currentEffect = modelEffects[effectStartIndex + j];


                        device.VertexDeclaration = currentPart.VertexDeclaration;
                        device.Vertices[0].SetSource(mesh.VertexBuffer, currentPart.StreamOffset,
                                                     currentPart.VertexStride);

                        currentEffect.Begin();
                        EffectPassCollection passes = currentEffect.CurrentTechnique.Passes;
                        int numPasses = passes.Count;
                        for (int k = 0; k < numPasses; k++)
                        {
                            EffectPass pass = passes[k];
                            pass.Begin();
                            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, currentPart.BaseVertex,
                                                         0, currentPart.NumVertices, currentPart.StartIndex, currentPart.PrimitiveCount);
                            pass.End();
                        }

                        currentEffect.End();
                    }
                }
            }
            catch (NullReferenceException)
            {
                throw new InvalidOperationException("The effects on the model for a " +
                                                    "ModelAnimator were changed without calling ModelAnimator.InitializeEffectParams().");
            }
            catch (InvalidCastException)
            {
                throw new InvalidCastException("ModelAnimator has thrown an InvalidCastException.  This is " +
                                               "likely because the model uses too many bones for the matrix palette.  The default palette size "
                                               + "is 56 for windows and 40 for Xbox.");
            }
        }