示例#1
0
        /// <summary>
        /// Performs a post-processing step using multiple source textures
        /// </summary>
        /// <param name="sources">The source textures</param>
        /// <param name="result">The output render target</param>
        /// <param name="effect">The effect to use</param>
        private void PostProcess(RenderTarget2D[] sources, RenderTarget2D result, Effect effect)
        {
            device.SetRenderTarget(result);
            device.Clear(Color.Black);


            for (int i = 0; i < sources.Length; i++)
            {
                effect.Parameters["SourceTexture" + Convert.ToString(i)].SetValue(sources[i]);
            }
            effect.Parameters["g_vSourceDimensions"].SetValue(new Vector2(sources[0].Width, sources[0].Height));
            if (result == null)
            {
                effect.Parameters["g_vDestinationDimensions"].SetValue(new Vector2(device.PresentationParameters.BackBufferWidth, device.PresentationParameters.BackBufferHeight));
            }
            else
            {
                effect.Parameters["g_vDestinationDimensions"].SetValue(new Vector2(result.Width, result.Height));
            }

            // Begin effect
            effect.CurrentTechnique.Passes[0].Apply();


            PrimitiveBatch primitivebatch = PrimitiveBatch.GetInstance(device);

            primitivebatch.Begin(Primitive.Quad);
            primitivebatch.AddVertex(-1, 1, 1, 0, 0);
            primitivebatch.AddVertex(-1, -1, 1, 0, 1);
            primitivebatch.AddVertex(1, -1, 1, 1, 1);
            primitivebatch.AddVertex(1, 1, 1, 1, 0);
            primitivebatch.End();
        }
示例#2
0
 private void Warm(GraphicsDevice device)
 {
     primitivebatch = new PrimitiveBatch(device);
     view           = Matrix.Identity;
     proj           = Matrix.CreateTranslation(-0.5f, -0.5f, 0) * Matrix.CreateOrthographicOffCenter(0, device.Viewport.Width, device.Viewport.Height, 0, 0, 1);
     material       = Resources.spriterenderer_material;
 }
示例#3
0
 public static PrimitiveBatch GetInstance(GraphicsDevice device)
 {
     if (instance == null)
     {
         instance = new PrimitiveBatch(device);
     }
     return(instance);
 }
示例#4
0
        public void Draw(Physics physics, Camera camera, bool shapes, bool outlines, bool normals, bool points, bool chains, bool tags)
        {
            material.Projection = camera.projection;
            material.View       = camera.view;
            material.World      = Matrix.Identity;
            material.CurrentTechnique.Passes[0].Apply();

            PrimitiveBatch instance = PrimitiveBatch.GetInstance(device);

            instance.Begin(Primitive.Line);

            for (int i = 0; i < physics.body_list.Count; i++)
            {
                Body body = physics.body_list[i];

                if (shapes)
                {
                    instance.SetColor(Color.Yellow);
                    for (int p = 1; p < body.curr_shape.count; p++)
                    {
                        instance.AddVertex(body.curr_shape.points[p - 1]);
                        instance.AddVertex(body.curr_shape.points[p - 0]);
                    }

                    instance.AddVertex(body.curr_shape.points[body.curr_shape.count - 1]);
                    instance.AddVertex(body.curr_shape.points[0]);
                }

                if (outlines)
                {
                    instance.SetColor(Color.White);
                    for (int p = 1; p < body.pointmass_list.Length; p++)
                    {
                        instance.AddVertex(body.pointmass_list[p - 1].position);
                        instance.AddVertex(body.pointmass_list[p - 0].position);
                    }

                    instance.AddVertex(body.pointmass_list[body.pointmass_list.Length - 1].position);
                    instance.AddVertex(body.pointmass_list[0].position);
                }


                if (normals)
                {
                    instance.SetColor(Color.Purple);
                    for (int p = 0; p < body.pointmass_list.Length; p++)
                    {
                        Vector2 pt = body.pointmass_list[p].position;

                        int prevPt = (p > 0) ? p - 1 : body.pointmass_list.Length - 1;
                        int nextPt = (p < body.pointmass_list.Length - 1) ? p + 1 : 0;

                        Vector2 prev = body.pointmass_list[prevPt].position;
                        Vector2 next = body.pointmass_list[nextPt].position;

                        Vector2 fromPrev = new Vector2();
                        fromPrev.X = pt.X - prev.X;
                        fromPrev.Y = pt.Y - prev.Y;

                        Vector2 toNext = new Vector2();
                        toNext.X = next.X - pt.X;
                        toNext.Y = next.Y - pt.Y;

                        Vector2 ptNorm = new Vector2();
                        ptNorm.X = fromPrev.X + toNext.X;
                        ptNorm.Y = fromPrev.Y + toNext.Y;
                        VectorHelper.Perpendicular(ref ptNorm);

                        ptNorm = Vector2.Normalize(ptNorm) * (camera.position.Z * 0.03f);

                        instance.AddVertex(pt);
                        instance.AddVertex(pt + ptNorm);
                    }
                }


                /*
                 * if (body is SpringBody)
                 * {
                 *  SpringBody sbody = body as SpringBody;
                 *
                 *  instance.SetColor(Color.Green);
                 *  for (int s = 0; s < sbody.spring_list.Count; s++)
                 *  {
                 *
                 *      instance.AddVertex(sbody.spring_list[s].pointmass_a.position);
                 *      instance.AddVertex(sbody.spring_list[s].pointmass_b.position);
                 *  }
                 * }*/
            }

            if (chains)
            {
                for (int i = 0; i < physics.chain_list.Count; i++)
                {
                    Chain chain = physics.chain_list[i];

                    instance.SetColor(Color.Green);
                    for (int s = 0; s < chain.spring_list.Count; s++)
                    {
                        instance.AddVertex(chain.spring_list[s].pointmass_a.position);
                        instance.AddVertex(chain.spring_list[s].pointmass_b.position);
                    }
                }
            }

            instance.End();


            if (points)
            {
                instance.Begin(Primitive.Quad);
                instance.SetColor(Color.Red);

                float size = 0.015f;
                for (int i = 0; i < physics.body_list.Count; i++)
                {
                    Body body = physics.body_list[i];

                    for (int p = 0; p < body.pointmass_list.Length; p++)
                    {
                        PointMass pm = body.pointmass_list[p];

                        instance.AddVertex(pm.position.X + size, pm.position.Y - size, 0);
                        instance.AddVertex(pm.position.X + size, pm.position.Y + size, 0);
                        instance.AddVertex(pm.position.X - size, pm.position.Y + size, 0);
                        instance.AddVertex(pm.position.X - size, pm.position.Y - size, 0);
                    }
                }

                for (int i = 0; i < physics.chain_list.Count; i++)
                {
                    Chain chain = physics.chain_list[i];

                    for (int p = 0; p < chain.pointmass_list.Count; p++)
                    {
                        PointMass pm = chain.pointmass_list[p];

                        instance.AddVertex(pm.position.X + size, pm.position.Y - size, 0);
                        instance.AddVertex(pm.position.X + size, pm.position.Y + size, 0);
                        instance.AddVertex(pm.position.X - size, pm.position.Y + size, 0);
                        instance.AddVertex(pm.position.X - size, pm.position.Y - size, 0);
                    }
                }

                instance.End();
            }

            if (tags)
            {
                SpriteRenderer spriterenderer = SpriteRenderer.GetInstance(device);
                spriterenderer.Begin(null);

                for (int i = 0; i < physics.body_list.Count; i++)
                {
                    Body    body = physics.body_list[i];
                    Vector3 proj = camera.Project(new Vector3(body.position, 0));
                    spriterenderer.AddString(Resources.arial10px_font, string.Format("[id:{0} body:{1}", i, body.ToStringSimple()), proj.X, proj.Y);
                }

                spriterenderer.End();
            }
        }
示例#5
0
        public void Draw(Body body, Camera camera, bool shapes, bool outlines, bool normals, bool points, bool chains, bool tags)
        {
            material.Projection = camera.projection;
            material.View       = camera.view;
            material.World      = Matrix.Identity;
            material.CurrentTechnique.Passes[0].Apply();

            PrimitiveBatch instance = PrimitiveBatch.GetInstance(device);

            instance.Begin(Primitive.Line);

            if (shapes)
            {
                instance.SetColor(Color.Purple);
                for (int p = 1; p < body.curr_shape.count; p++)
                {
                    instance.AddVertex(body.curr_shape.points[p - 1]);
                    instance.AddVertex(body.curr_shape.points[p - 0]);
                }

                instance.AddVertex(body.curr_shape.points[body.curr_shape.count - 1]);
                instance.AddVertex(body.curr_shape.points[0]);
            }

            if (outlines)
            {
                instance.SetColor(Color.White);
                for (int p = 1; p < body.pointmass_list.Length; p++)
                {
                    instance.AddVertex(body.pointmass_list[p - 1].position);
                    instance.AddVertex(body.pointmass_list[p - 0].position);
                }

                instance.AddVertex(body.pointmass_list[body.pointmass_list.Length - 1].position);
                instance.AddVertex(body.pointmass_list[0].position);
            }

            if (normals)
            {
                instance.SetColor(Color.Purple);
                for (int p = 0; p < body.pointmass_list.Length; p++)
                {
                    Vector2 pt = body.pointmass_list[p].position;

                    int prevPt = (p > 0) ? p - 1 : body.pointmass_list.Length - 1;
                    int nextPt = (p < body.pointmass_list.Length - 1) ? p + 1 : 0;

                    Vector2 prev = body.pointmass_list[prevPt].position;
                    Vector2 next = body.pointmass_list[nextPt].position;

                    Vector2 fromPrev = new Vector2();
                    fromPrev.X = pt.X - prev.X;
                    fromPrev.Y = pt.Y - prev.Y;

                    Vector2 toNext = new Vector2();
                    toNext.X = next.X - pt.X;
                    toNext.Y = next.Y - pt.Y;

                    Vector2 ptNorm = new Vector2();
                    ptNorm.X = fromPrev.X + toNext.X;
                    ptNorm.Y = fromPrev.Y + toNext.Y;
                    VectorHelper.Perpendicular(ref ptNorm);

                    ptNorm = Vector2.Normalize(ptNorm) * (camera.position.Z * 0.03f);;

                    instance.AddVertex(pt);
                    instance.AddVertex(pt + ptNorm);
                }
            }
            instance.End();


            if (points)
            {
                instance.Begin(Primitive.Quad);
                instance.SetColor(Color.Red);

                float size = 0.015f;

                for (int p = 0; p < body.pointmass_list.Length; p++)
                {
                    PointMass pm = body.pointmass_list[p];

                    instance.AddVertex(pm.position.X + size, pm.position.Y - size, 0);
                    instance.AddVertex(pm.position.X + size, pm.position.Y + size, 0);
                    instance.AddVertex(pm.position.X - size, pm.position.Y + size, 0);
                    instance.AddVertex(pm.position.X - size, pm.position.Y - size, 0);
                }

                instance.End();
            }

            if (tags)
            {
                SpriteRenderer spriterenderer = SpriteRenderer.GetInstance(device);
                spriterenderer.Begin(null);

                Vector3 proj = camera.Project(new Vector3(body.position, 0));
                spriterenderer.AddString(Resources.arial10px_font, body.ToStringSimple(), proj.X, proj.Y);


                spriterenderer.End();
            }
        }