Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

2D Vertex Transformations Help?

$
0
0

@Tholok wrote:

Hi Monogame,

I am working on a simple 2D Graphics Library for games. It has worked in previous versions but I am currently having a subtle bug problem.

I want points specified in drawing methods to correspond directly to traditional screen pixel coordinates. So, let's say a screen is 1024 x 768. I want my DrawBox method to take a Rectangle as a parameter (e.g. 0, 0, 1024, 768) and the box to be drawn around the edge pixels of the screen.

My drawbox method uses a linestrip to draw the box, and the results seem to be slightly off. In 1920 x 1080 resolution, for example, the origin 0, 0 is off the screen, and point 1919, 1079 is not the bottom right. So a box the size of the screen is shown missing the top and left boundary lines.

This is the code that sets up the standard BasicEffect used for the transformations -

        // Create a convenient coordinate transformation matrix.
        screenTransformMatrix = Matrix.CreateScale (2f / (float) surfaceWidth, -2f / (float) surfaceHeight, 0) * Matrix.CreateTranslation (-1, 1, 0);
        surfaceTransformMatrix = screenTransformMatrix;

[...]

        // This is a 2 dimensional graphics engine. No matrix transformations on points are desirable.
        standardEffect.Projection = Matrix.Identity;
        // Automatically transform surface points to render points.
        standardEffect.View = surfaceTransformMatrix;
        standardEffect.World = Matrix.Identity;

        // Standard effect basic settings.
        standardEffect.Alpha = 1;
        standardEffect.TextureEnabled = false;
        standardEffect.VertexColorEnabled = true;

I am worried it is slightly incorrect?

This is the code that draws the box -

        boxArray [0] = new VertexPositionColorTexture (new Vector3 (box.Left, box.Top, 0), lineColour, new Vector2 (0, 0));
        boxArray [1] = new VertexPositionColorTexture (new Vector3 (box.Right - 1, box.Top, 0), lineColour, new Vector2 (0, 0));
        boxArray [2] = new VertexPositionColorTexture (new Vector3 (box.Right - 1, box.Bottom - 1, 0), lineColour, new Vector2 (0, 0));
        boxArray [3] = new VertexPositionColorTexture (new Vector3 (box.Left, box.Bottom - 1, 0), lineColour, new Vector2 (0, 0));
        boxArray [4] = boxArray [0];

        lock (synchronisationObject)
        {
            // Set texture addressing to clamp mode.
            outputDevice.BlendState = BlendState.AlphaBlend;
            outputDevice.SamplerStates [0] = SamplerState.PointClamp;

            // Render the box.
            standardEffect.Alpha = (float) lineColour.A / 255f;
            standardEffect.TextureEnabled = false;
            standardEffect.CurrentTechnique.Passes [0].Apply ();
            outputDevice.DrawUserPrimitives<VertexPositionColorTexture> (PrimitiveType.LineStrip, boxArray, 0, 4);
        }

I get the impression the problem is with the translation. The resulting boxes are also missing the top left pixel when looked at on screen..

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles