This project is read-only.

Issues while drawing coloured line list on top of Textured rectangle

Dec 6, 2012 at 1:53 PM

Hi there,

    In my application, I am trying to draw line list on top of a Rectangle. But irrespective of the 'Z' value I used in the vector to draw the lines, every time i get the rectangle drawn on top of the lines. Can anyone tell me how to overcome this issue? Any help would be greatly appreciated. Thanks in advance. The source code as shown below...

class RectangleSceneTest : ITestGame
    {
        public Scene CreateTestScene(GraphicsDevice graphics, ContentManager content)
        {
            var scene = new Scene();
            float angle = 0;

            Sprite rectangle = new Sprite(graphics);
            rectangle.Position = new Vector2(0, 0);
            rectangle.Rotation = angle;
            rectangle.Size = new Vector2(7, 7);
            rectangle.Scale = new Vector2(1, 1);
            var texture = content.Load<Texture2D>("Square");
            rectangle.Texture = texture;
            scene.Add(rectangle);


            float radian = (float)((angle * Math.PI) / 180);
            var rotation = Matrix.CreateRotationZ(radian);
            var dynamicPrimitive = new DynamicPrimitive(graphics) { DepthBias = 0.0002f };            
            dynamicPrimitive.AddLine(new Vector3(0, 0, 0), new Vector3(5, 0, 0), rotation, Color.Red, 1.0f);      
            scene.Add(dynamicPrimitive);

            return scene;
        }
    }

Dec 6, 2012 at 2:16 PM

You are mixing 2d rendering with 3d rendering (DynamicPrimitive is 3d !!). In the default setting, 2d sprites is always rendered on top of 3d objects. I think you can change this order by setting the Pass.Order property, the following is the psedo code, I haven't test it:

scene.GetDrawContext(graphics).Passes.OfType<SpritePass>().First().Order = 1;

scene.GetDrawContext(graphics).Passes.OfType<DrawingPass>().First().Order = 2;

The downside is that anything rendered with the DynamicPrimitive is alway in front of the sprites.

If this doesn't work for you, I'll be looking into it tomorrow.

Dec 6, 2012 at 2:29 PM

Hello yufeih,

  Thanks for your quick response. But When make this changes and ran the application, I got an error - "Not supported Exception" saying "XNA Framework Reach profile requires TextureAddressMode to be Clamp when using texture sizes that are not powers of two." at the 'Draw()' method of the Game.

Is there any other 2D options to draw colored Line lists with in the scene to avoid the 2D-3D conflict? Thanks.

Dec 7, 2012 at 2:51 AM

Implement ISprite interface to support custom 2d rendering. The following code shows how to turn a 3d dynamic primitive into a 2d sprite.

    public class CustomSprite : ISprite
    {
        public bool Visible { get; set; }
        public int ZOrder { get; set; }
        public Material Material { get; private set; }
        public DynamicPrimitive Primitive { get; private set; }

        public SamplerState SamplerState
        {
            get { return null; }
        }

        public BlendState BlendState
        {
            get { return null; }
        }

        public CustomSprite(GraphicsDevice graphics)
        {
            Visible = true;
            Material = new BasicMaterial(graphics);
            Primitive = new DynamicPrimitive(graphics);
        }

        public void Draw(DrawingContext context, Material material)
        {
            Primitive.Draw(context, material);
        }

        public void Draw(DrawingContext context, SpriteBatch spriteBatch)
        {
            throw new InvalidOperationException();
        }
    }


    public class CustomSpriteTest : ITestGame
    {
        public Scene CreateTestScene(GraphicsDevice graphics, ContentManager content)
        {
            var scene = new Scene();
            float angle = 0;

            Sprite rectangle = new Sprite(graphics);
            rectangle.Position = new Vector2(0, 0);
            rectangle.Rotation = angle;
            rectangle.Size = new Vector2(7, 7);
            rectangle.Scale = new Vector2(1, 1);
            var texture = content.Load<Texture2D>("Textures/Butterfly");
            rectangle.Texture = texture;
            scene.Add(rectangle);


            float radian = (float)((angle * Math.PI) / 180);
            var rotation = Matrix.CreateRotationZ(radian);
            var sprite = new CustomSprite(graphics);
            sprite.Primitive.AddLine(new Vector3(0, 0, 0), new Vector3(5, 0, 0), rotation, Color.Red, 1.0f);
            scene.Add(sprite);

            return scene;
        }
    }

 


Dec 7, 2012 at 6:15 AM

Thank you yufeih so much for your timely help!!! Your solution worked... :)