Solved: XNA to Monogame Migration iusse: Lines primitive ugly

Hello,
I’m trying tomigrate some code from XNA to MG(DX), basically it draw a lines chart using Drawlines primitives.
In XNA it work great, but in monogame I get ugly lines, wider and “segmented”.
I found a topic where changing the rasterizerstate the user solved, but unfortunately this not work for me.
I really want to switch to MG. Any advice?

are you adding a halfpixel offset to the viewProj at some point?
In MG you no longer have to do that.

No, I did not.

I don’t know what DrawLine Priimtive you mean, but I guess it uses SpriteBatch oder creates some quads. Didn’t know that XNA had such thing built in?

wider & segmented sounds like whatever creates the geometry does less steps

In here

There is a file, Primitives2D.cs which extends spritebatch to draw all sorts of things including lines

Grab that

I see, so that basically draws a stretched, rotated sprite to make a line.

As you say, the lines are wider, are you sure you’re drawing onto a non-scaled target or some low resolution?

the only thing affecting the edges of such sprites is multisampling on the backbuffer … there were some times, where multisampling was not working on offscreen rendering but I am pretty sure this is long fixed, otherwise check if multisampling is set on your rendertarget.

Just guessing here, as you didn’t provide a screenshot or further info about where you render to

I use something like that: (in this code I draw only one line only for example, but in the real world app lines can be also 2k.

using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

public class Form1
{
private GraphicsDevice grafix;
private bool InitializeGraphics(ref PictureBox surface)
{
try
{
PresentationParameters pparam = new PresentationParameters();
pparam.DeviceWindowHandle = surface.Handle;
pparam.IsFullScreen = false;

        GraphicsAdapter grafixAdapt = GraphicsAdapter.DefaultAdapter;

        grafix = new GraphicsDevice(grafixAdapt, GraphicsProfile.HiDef, pparam);


        InitializeGraphics = true;
    }
    catch (Exception ex)
    {
        InitializeGraphics = false;
    }
}

private BasicEffect effect;
private bool InitializeEffect(GraphicsDevice graphics)
{
    effect = new BasicEffect(graphics);

    try
    {
        effect.VertexColorEnabled = true;
        effect.Projection = Matrix.CreateOrthographicOffCenter(0, graphics.Viewport.Width, graphics.Viewport.Height, 0, 0, 1);

        InitializeEffect = true;
    }
    catch (Exception ex)
    {
        InitializeEffect = false;
    }
}

private bool quit = false;
private void Form1_Load(System.Object sender, System.EventArgs e)
{
    // Set up the initialize function found above
    if (InitializeGraphics(ref pbGame) && InitializeEffect(grafix))
        BackgroundWorker1.RunWorkerAsync();
    else
    {
        MessageBox.Show("Init. Error");
        this.Close();
    }
}

private void BackgroundWorker1_DoWork(System.Object sender, System.ComponentModel.DoWorkEventArgs e)
{
    while (!quit == true)
    {
        grafix.Clear(Color.CornflowerBlue);

        effect.CurrentTechnique.Passes(0).Apply();

        VertexPositionColor[] newline = Set2dLine(0, 30, 0, 300, 25, 0, Color.Black);
       

        grafix.DrawUserPrimitives(PrimitiveType.LineList, newline, 0, 1);        

        grafix.Present();
    }
}

private VertexPositionColor[] Set2dLine(int x1, int y1, int z1, int x2, int y2, int z2, Color color)
{
    VertexPositionColor vertices1 = new VertexPositionColor(), vertices2 = new VertexPositionColor();

    vertices1.Position = new Vector3(x1, y1, z1);
    vertices1.Color = color;
    vertices2.Position = new Vector3(x2, y2, z2);
    vertices2.Color = color;

    return new[] { vertices1, vertices2 };
}

}

Try the following:
effect.Projection = CreateOrthographicOffCenter(0, graphics.Viewport.Width-1, graphics.Viewport.Height-1, 0, 0, 1);

It works! thanks.