Esempio n. 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();
        }
Esempio n. 2
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();
            }
        }
Esempio n. 3
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();
            }
        }
Esempio n. 4
0
        public void End()
        {
            if (!has_begun)
            {
                throw new InvalidOperationException("Begin must be called before End can be called.");
            }

            if (primitivebatch == null || material == null)
            {
                Warm(device);
            }

            material.Parameters["TextureEnabled"].SetValue(true);
            material.Parameters["World"].SetValue(Matrix.Identity);

            if (is_ortho)
            {
                material.Parameters["View"].SetValue(view);
                material.Parameters["Projection"].SetValue(proj);
            }
            else
            {
                material.Parameters["View"].SetValue(camera.view);
                material.Parameters["Projection"].SetValue(camera.projection);
            }

            Matrix matrix;
            float  a, b, c, d, e, f;

            foreach (Texture2D texture in sprites.Keys)
            {
                List <SpriteInfo> list = sprites[texture];

                if (list.Count <= 0)
                {
                    continue;
                }

                material.Parameters["Texture"].SetValue(texture);
                material.CurrentTechnique.Passes[0].Apply();

                primitivebatch.Begin(Primitive.Quad);
                float texture_pixel_width  = 1.0f / texture.Width;
                float texture_pixel_height = 1.0f / texture.Height;

                for (int i = 0; i < list.Count; i++)
                {
                    SpriteInfo    sprite = list[i];
                    TextureRegion region = sprite.region;

                    float index = 0;
                    if (is_ortho)
                    {
                        index  = 1 - (((float)(sprite.index)) / count);
                        index *= -0.09f;
                    }

                    matrix = Matrix.Identity;
                    if (sprite.rotation_z != 0 || sprite.rotation_x != 0 || sprite.rotation_y != 0)
                    {
                        a = (float)Math.Cos(sprite.rotation_x);
                        b = (float)Math.Sin(sprite.rotation_x);

                        c = (float)Math.Cos(sprite.rotation_y);
                        d = (float)Math.Sin(sprite.rotation_y);

                        e = (float)Math.Cos(sprite.rotation_z);
                        f = (float)Math.Sin(sprite.rotation_z);

                        matrix.M11 = (c * e);
                        matrix.M12 = (c * f);
                        matrix.M13 = -d;
                        matrix.M21 = (e * b * d - a * f);
                        matrix.M22 = ((e * a) + (f * b * d));
                        matrix.M23 = (b * c);
                        matrix.M31 = (e * a * d + b * f);
                        matrix.M33 = (a * c);
                        matrix.M32 = -(b * e - f * a * d);
                    }

                    matrix.M41 = sprite.x;
                    matrix.M42 = sprite.y;
                    matrix.M43 = sprite.z + index;

                    primitivebatch.SetTransform(ref matrix);
                    primitivebatch.SetColor(sprite.color);
                    primitivebatch.SetTextureCoords(region.u * texture_pixel_width, region.v * texture_pixel_height);

                    if (is_ortho)                     //in ortho the view is upside down
                    {
                        primitivebatch.AddVertex(0, 0, 0, 0, 0);
                        primitivebatch.AddVertex(0, 0 + sprite.height, 0, 0, 0 + region.height * texture_pixel_height);
                        primitivebatch.AddVertex(0 + sprite.width, 0 + sprite.height, 0, 0 + region.width * texture_pixel_width, 0 + region.height * texture_pixel_height);
                        primitivebatch.AddVertex(0 + sprite.width, 0, 0, 0 + region.width * texture_pixel_width, 0);
                    }
                    else
                    {
                        primitivebatch.AddVertex(0, 0 + sprite.height, 0, 0, 0);
                        primitivebatch.AddVertex(0, 0, 0, 0, 0 + region.height * texture_pixel_height);
                        primitivebatch.AddVertex(0 + sprite.width, 0, 0, 0 + region.width * texture_pixel_width, 0 + region.height * texture_pixel_height);
                        primitivebatch.AddVertex(0 + sprite.width, 0 + sprite.height, 0, 0 + region.width * texture_pixel_width, 0);
                    }
                }

                primitivebatch.End();

                list.Clear();
            }

            has_begun = false;
        }