Transformation works fine - but why is rectangle half?

Hello everyone,

I am sorry for disturbation!

I really don’t understand why does my rect make half than it happens like crazy. How do I fix if I want make

But for me only vertices…

// EDIT:
My result:

This is Maths.cs

using Microsoft.Xna.Framework;
    namespace MyTransformation
    {
        public class Maths
        {
            public static Matrix CreateTransformationMatrix(Vector3 position, float rotX, float rotY, float rotZ, float rescale)
            {
                Matrix translation = Matrix.CreateTranslation(position);
                Matrix rotationX = Matrix.CreateRotationX(rotX);
                Matrix rotationY = Matrix.CreateRotationY(rotY);
                Matrix rotationZ = Matrix.CreateRotationZ(rotZ);
                Matrix scale = Matrix.CreateScale(rescale);
                Matrix transformation = translation * rotationX * rotationY * rotationZ * scale;
                return transformation;
            }
        }
    }

This is Entity.cs

using Microsoft.Xna.Framework;

namespace MyTransformation
{
    public class Entity : GameComponent
    {
        private Game game;
        private Vector3 position;
        private float rotX;
        private float rotY;
        private float rotZ;
        private float scale;

        public Entity(Game game, Vector3 position, float rotX, float rotY, float rotZ, float scale) : base(game)
        {
            this.game = game;
            this.position = position;
            this.rotX = rotX;
            this.rotY = rotY;
            this.rotZ = rotZ;
            this.scale = scale;
        }

        public void IncreasePosition(float distanceX, float distanceY, float distanceZ)
        {
            position.X += distanceX;
            position.Y += distanceY;
            position.Z += distanceZ;
        }

        public void IncreaseRotation(float distRotX, float distRotY, float distRotZ)
        {
            rotX += distRotX;
            rotY += distRotY;
            rotZ += distRotZ;
        }

        public void IncreaseScale(float distScale)
        {
            scale += distScale;
        }

        public virtual Game Instance
        {
            get
            {
                return game;
            }

            set
            {
                game = value;
            }
        }

        public virtual Vector3 Position
        {
            get
            {
                return position;
            }

            set
            {
                position = value;
            }
        }

        public virtual float RotX
        {
            get
            {
                return rotX;
            }

            set
            {
                rotX = value;
            }
        }

        public virtual float RotY
        {
            get
            {
                return rotY;
            }

            set
            {
                rotY = value;
            }
        }

        public virtual float RotZ
        {
            get
            {
                return rotZ;
            }

            set
            {
                rotZ = value;
            }
        }

        public virtual float Scale
        {
            get
            {
                return scale;
            }

            set
            {
                scale = value;
            }
        }
    }
}

Game1.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MyTransformation
{
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        VertexBuffer vertexBuffer;
        IndexBuffer indexBuffer;
        VertexPositionTexture[] vertices;
        BasicEffect basicEffect;
        Effect effect;
        Texture2D exampleTex;

        Entity entity;
        Matrix transform;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            Window.Title = "Transformating triangles";

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            basicEffect = new BasicEffect(GraphicsDevice);

            effect = Content.Load<Effect>("Shader");

            VertexPositionTexture[] vertices = new VertexPositionTexture[4];

            vertices[0] = new VertexPositionTexture(new Vector3(-0.5f, 0.5f, 0f), new Vector2(0, 0));
            vertices[1] = new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0f), new Vector2(0, 1));
            vertices[2] = new VertexPositionTexture(new Vector3(0.5f, -0.5f, 0f), new Vector2(1, 1));
            vertices[3] = new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0f), new Vector2(1, 0));

            vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), 6, BufferUsage.WriteOnly);
            vertexBuffer.SetData<VertexPositionTexture>(vertices);

            exampleTex = Content.Load<Texture2D>("example");
            entity = new Entity(this, new Vector3(0, 0, 0), 0, 0, 0, 1);

            short[] indices = new short[6];
            indices[0] = 0; indices[1] = 1; indices[2] = 2;
            indices[3] = 2; indices[4] = 3; indices[5] = 0;

            indexBuffer = new IndexBuffer(graphics.GraphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly);
            indexBuffer.SetData(indices);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            transform = Matrix.Identity;
            transform = Maths.CreateTransformationMatrix(entity.Position, entity.RotX, entity.RotY, entity.RotZ, entity.Scale);
            //entity.IncreasePosition(0.002f, 0, 0);
            entity.IncreaseRotation(0, 0.01f, 0);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            basicEffect.Projection = Matrix.Identity;
            basicEffect.View = Matrix.Identity;

            GraphicsDevice.SetVertexBuffer(vertexBuffer);
            GraphicsDevice.Indices = indexBuffer;

            RasterizerState rasterizerState = new RasterizerState();
            rasterizerState.CullMode = CullMode.None;
            GraphicsDevice.RasterizerState = rasterizerState;

            effect.CurrentTechnique = effect.Techniques["ShaderTesxture"];
            effect.Parameters["WorldViewProjection"].SetValue(Matrix.Identity * transform);
            effect.Parameters["xTexture"].SetValue(exampleTex);

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indexBuffer.IndexCount / 3);
            }

            base.Draw(gameTime);
        }
    }
}

result: it looks like crazy

Do you remember that original texture is 8 x 8 tiles black / white
like this

Since my monogame with transformation looks like 4 x 8 like it is wrong. and it happens “replying mode” why does matrix not work like limited matrix?

How do I fix if I have already made normal texture length of width and height ( just same in vertices ) But it happens half of rectangle. Why does it happen? What is it wrong?

Edit I have fixed add transformationMat in Shader.fx than it won’t move translate only rotate???

Why does it not work if I use add float4x4 transformationMat

in VS-Function
float4x4 together = worldviewprojection * transformationMat;

Is it correct or wrong?`What do I fix? I need add more for shader or do not need shader?

I try with

float4x4 worldviewprojection =
{
 { 1, 0, 0, 0},
 { 0, 1, 0, 0},
 { 0, 0, 1, 0},
 { 0, 0, 0, 1}
}

And it looks maniac :frowning:

WHY IS RECTANGLE HALF???

Thank you for help and I appreciate that.

This looks wrong because the worldviewprojection matrix already contains the transformation, so you don’t need to apply it in the shader again.

Do you see the whole rectangle if you set transform = Matrix.Identity?

1 Like

I have already tried

no success…

transform = Matrix.Identity; in Update(gametime);

I think MonoGame has bug or did somebody resolve?

PS:

I am using MonoGame 3.6. Do I need upgrade to MonoGame 3.7?

Can you share the entire project?

clipping…

push it a bit…
entity.Position = new Vector3(0, 0, 0.5f);

It looks good but it cannot from left edge to right edge :confused:

@markus - Solved by great guy @PumpkinPudding!!!

@PumpkinPudding and @markus sorry for long wait.

I am busy from my company. Thanks!

Yeah thank you for help me, @PumpkinPudding!!

I found solution in Maths.CreateTransformation()

return rotX * rotY * rotZ * scale * translate * Matrix.CreateTranslate(new Vector(0, 0, 0.5f));

It works fine like chaim! Why does MonoGame need 0.5f in Z of Vector3
Solved!

Result:

Wow why does MonoGame have 0.5f in Z?

no…
it because you not use any matrix to change(simulate) depth(projection matrix…) and default viewport’s depth is 0-1

1 Like

Understand thanks I will save in my head. I do not forget everything. I thought other Frameworks like OpenGL-Wrappers don’t need use 0.5f in Matrix. That is why I understand.

Thanks :slight_smile: I am very excited next serie creating cube wow.

// Now I have problem It can’t make normal like world, view and projections and it looks ugly.

I have created Camera.cs and add CreateViewMatrix(Camera) in Maths.cs

It happens common because cube looks like still 2D not real 3D.

How do I fix? I forget what do I add? I already push with basicEffect.view, basicEffect.projection and basicEffect.world .

Game1.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MyCube
{
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        VertexBuffer vertexBuffer;
        IndexBuffer indexBuffer;
        VertexPositionTexture[] vertices;
        BasicEffect basicEffect;
        Effect effect;
        Texture2D exampleTex;

        Entity entity;
        Matrix transform;
        Camera camera;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            Window.Title = "Making cube in real world";

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            basicEffect = new BasicEffect(GraphicsDevice);

            effect = Content.Load<Effect>("Shader");

            vertices = new VertexPositionTexture[24];

            vertices[0] = new VertexPositionTexture(new Vector3(-0.5f, 0.5f, -0.5f), new Vector2(0, 0));
            vertices[1] = new VertexPositionTexture(new Vector3(-0.5f, -0.5f, -0.5f), new Vector2(0, 1));
            vertices[2] = new VertexPositionTexture(new Vector3(0.5f, -0.5f, -0.5f), new Vector2(1, 1));
            vertices[3] = new VertexPositionTexture(new Vector3(0.5f, 0.5f, -0.5f), new Vector2(1, 0));

            vertices[4] = new VertexPositionTexture(new Vector3(-0.5f, 0.5f, 0.5f), new Vector2(0, 0));
            vertices[5] = new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0.5f), new Vector2(0, 1));
            vertices[6] = new VertexPositionTexture(new Vector3(0.5f, -0.5f, 0.5f), new Vector2(1, 1));
            vertices[7] = new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0.5f), new Vector2(1, 0));

            vertices[8] = new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0.5f), new Vector2(0, 0));
            vertices[9] = new VertexPositionTexture(new Vector3(0.5f, -0.5f, -0.5f), new Vector2(0, 1));
            vertices[10] = new VertexPositionTexture(new Vector3(0.5f, 0.5f, -0.5f), new Vector2(1, 1));
            vertices[11] = new VertexPositionTexture(new Vector3(0.5f, -0.5f, 0.5f), new Vector2(1, 0));

            vertices[12] = new VertexPositionTexture(new Vector3(-0.5f, 0.5f, -0.5f), new Vector2(0, 0));
            vertices[13] = new VertexPositionTexture(new Vector3(-0.5f, -0.5f, -0.5f), new Vector2(0, 1));
            vertices[14] = new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0.5f), new Vector2(1, 1));
            vertices[15] = new VertexPositionTexture(new Vector3(-0.5f, 0.5f, 0.5f), new Vector2(1, 0));

            vertices[16] = new VertexPositionTexture(new Vector3(-0.5f, 0.5f, 0.5f), new Vector2(0, 0));
            vertices[17] = new VertexPositionTexture(new Vector3(-0.5f, 0.5f, -0.5f), new Vector2(0, 1));
            vertices[18] = new VertexPositionTexture(new Vector3(0.5f, 0.5f, -0.5f), new Vector2(1, 1));
            vertices[19] = new VertexPositionTexture(new Vector3(0.5f, 0.5f, 0.5f), new Vector2(1, 0));

            vertices[20] = new VertexPositionTexture(new Vector3(-0.5f, -0.5f, 0.5f), new Vector2(0, 0));
            vertices[21] = new VertexPositionTexture(new Vector3(-0.5f, -0.5f, -0.5f), new Vector2(0, 1));
            vertices[22] = new VertexPositionTexture(new Vector3(0.5f, -0.5f, -0.5f), new Vector2(1, 1));
            vertices[23] = new VertexPositionTexture(new Vector3(0.5f, -0.5f, 0.5f), new Vector2(1, 0));

            vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), 24, BufferUsage.WriteOnly);
            vertexBuffer.SetData<VertexPositionTexture>(vertices);

            exampleTex = Content.Load<Texture2D>("example");

            short[] indices = new short[36];

            indices[0] = 0; indices[1] = 1; indices[2] = 3;
            indices[3] = 3; indices[4] = 1; indices[5] = 2;

            indices[6] = 4; indices[7] = 5; indices[8] = 7;
            indices[9] = 7; indices[10] = 5; indices[11] = 6;

            indices[12] = 8; indices[13] = 9; indices[14] = 11;
            indices[15] = 11; indices[16] = 9; indices[17] = 10;

            indices[18] = 12; indices[19] = 13; indices[20] = 15;
            indices[21] = 15; indices[22] = 13; indices[23] = 14;

            indices[24] = 16; indices[25] = 17; indices[26] = 19;
            indices[27] = 19; indices[28] = 17; indices[29] = 18;

            indices[30] = 20; indices[31] = 21; indices[32] = 23;
            indices[33] = 32; indices[34] = 21; indices[35] = 22;

            indexBuffer = new IndexBuffer(graphics.GraphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly);
            indexBuffer.SetData(indices);

            entity = new Entity(new Vector3(0, 0, -1), 0, 0, 0, 1);
            camera = new Camera();
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            camera.UpdateMove();

            transform = Matrix.Identity;
            transform = Maths.CreateTransformationMatrix(entity.Position, entity.RotX, entity.RotY, entity.RotZ, entity.Scale);

            entity.IncreasePosition(0, 0, 0);
            entity.IncreaseRotation(0.01f, 0.01f, 0);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            basicEffect.World = Matrix.Identity;
            basicEffect.View = Matrix.CreateLookAt(new Vector3(-camera.Position.X, -camera.Position.Y, -camera.Position.Z), new Vector3(0, 0, 0), Vector3.Up);
            basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 0.1f, 100000.0f);

            GraphicsDevice.SetVertexBuffer(vertexBuffer);
            GraphicsDevice.Indices = indexBuffer;

            RasterizerState rasterizerState = new RasterizerState();
            rasterizerState.CullMode = CullMode.None;
            GraphicsDevice.RasterizerState = rasterizerState;

            GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;

            effect.CurrentTechnique = effect.Techniques["ShaderTesxture"];
            effect.Parameters["WorldViewProjection"].SetValue(transform*Maths.CreateViewMatrix(camera));

            effect.Parameters["xTexture"].SetValue(exampleTex);

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indexBuffer.IndexCount / 3);
            }

            base.Draw(gameTime);
        }
    }
}

Maths.cs

using Microsoft.Xna.Framework;

namespace MyCube
{
    public class Maths
    {
        private static Matrix transformation;
        public static Matrix CreateTransformationMatrix( Vector3 position, float rotX, float rotY, float rotZ, float rescale)
        {
            transformation = Matrix.Identity;
            Matrix translation = Matrix.CreateTranslation(position);
            Matrix rotationX = Matrix.CreateRotationX(rotX);
            Matrix rotationY = Matrix.CreateRotationY(rotY);
            Matrix rotationZ = Matrix.CreateRotationZ(rotZ);
            Matrix scale = Matrix.CreateScale(rescale);

            return transformation = rotationX * rotationY * rotationZ * scale * translation * Matrix.CreateTranslation(new Vector3(0, 0, 1f));
        }

        private static Matrix view;
        public static Matrix CreateViewMatrix(Camera camera)
        {
            view = Matrix.Identity;
            var rotateX = Matrix.CreateRotationX(camera.Pitch);
            var rotateY = Matrix.CreateRotationY(camera.Yaw);
            var translate = Matrix.CreateTranslation(new Vector3(-camera.Position.X, -camera.Position.Y, -camera.Position.Z));
            view = rotateX * rotateY * translate;
            return view;
        }
    }
}

Entity.cs

using Microsoft.Xna.Framework;

namespace MyCube
{
    public class Entity
    {
        private Vector3 position;
        private float rotX;
        private float rotY;
        private float rotZ;
        private float scale;

        public Entity(Vector3 position, float rotX, float rotY, float rotZ, float scale)
        {
            this.position = position;
            this.rotX = rotX;
            this.rotY = rotY;
            this.rotZ = rotZ;
            this.scale = scale;
        }

        public void IncreasePosition(float distanceX, float distanceY, float distanceZ)
        {
            position.X += distanceX;
            position.Y += distanceY;
            position.Z += distanceZ;
        }

        public void IncreaseRotation(float distRotX, float distRotY, float distRotZ)
        {
            rotX += distRotX;
            rotY += distRotY;
            rotZ += distRotZ;
        }

        public void IncreaseScale(float distScale)
        {
            scale += distScale;
        }

        public virtual Vector3 Position
        {
            get
            {

                return position;
            }

            set
            {
                position = value;
            }
        }

        public virtual float RotX
        {
            get
            {
                return rotX;
            }

            set
            {
                rotX = value;
            }
        }

        public virtual float RotY
        {
            get
            {
                return rotY;
            }

            set
            {
                rotY = value;
            }
        }

        public virtual float RotZ
        {
            get
            {
                return rotZ;
            }

            set
            {
                rotZ = value;
            }
        }

        public virtual float Scale
        {
            get
            {
                return scale;
            }

            set
            {
                scale = value;
            }
        }
    }
}

Camera.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace MyCube
{
    public class Camera
    {
        private Vector3 position = new Vector3(0, 0, 0);
        private KeyboardState keyState;

        public Camera()
        {
        }

        public void UpdateMove()
        {
            keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Keys.W))
            {
                position.Z -= 0.02f;
            }
            if (keyState.IsKeyDown(Keys.S))
            {
                position.Z += 0.02f;
            }
            if (keyState.IsKeyDown(Keys.D))
            {
                position.X += 0.02f;
            }
            if (keyState.IsKeyDown(Keys.A))
            {
                position.X -= 0.02f;
            }
        }

        public virtual Vector3 Position
        {
            get
            {
                return position;
            }
            set
            {
                position = value;
            }
        }

        public float Pitch { get; }

        public float Yaw { get; }

        public float Roll { get; }
    }
}

Shader.fx:

#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif

float4x4 WorldViewProjection;

Texture xTexture;

sampler TextureSampler = sampler_state { texture = <xTexture>; magfilter = LINEAR; minfilter = LINEAR; mipfilter = LINEAR; AddressU = mirror; AddressV = mirror; };
struct VertexToPixel
{
	float4 Position     : POSITION;
	float2 TexCoords    : TEXCOORD0;
};


struct PixelToFrame
{
	float4 Color        : COLOR0;
};


VertexToPixel SimplestVertexShader(float4 inPos : POSITION, float2 inTexCoords : TEXCOORD0)
{
	VertexToPixel Output = (VertexToPixel)0;

	Output.Position = mul(inPos, WorldViewProjection);
	Output.TexCoords = inTexCoords;

	return Output;
}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)
{
	PixelToFrame Output = (PixelToFrame)0;

	Output.Color = tex2D(TextureSampler, PSIn.TexCoords);


	return Output;
}

technique ShaderTesxture
{
	pass Pass0
	{
		VertexShader = compile vs_2_0 SimplestVertexShader();
		PixelShader = compile ps_2_0 OurFirstPixelShader();
	}
}

Result:


Why does MonoGame mess up to my accuracy?
Same correct vertices ,texturecoordinates and indices but they are not wrong. Why does it happen cube like damaged cube?

I have updated from Github by @Jonashw

and I want add with advanced like Transformation, Maths ,Entity and Camera = it looks like still problematic why does MonoGame not work if I use advanced Matrix-Features like other has with advanced Matrix features like ThinMatrix with Tutorial Transformation and View-Projection and cube looks like normal but MonoGame can’t why does it happen? How do I find? I really like 3 important scripts ( Maths, Entity and Camera ) because they can make powerful like other made same.

Why does cube not have indices? Okay They care only Normal.

Can I have resolve with cube with GraphicsDevice.DepthStencilState = DepthStencilState.Default;

Than it doesn’t like normal :frowning: How do I fix? Thanks!

PS: I am not using Gitter.im because they are spying and watching only me. PS: Do not share to Facebook! I will get ransom from Facebook because I don’t feel because MonoGame in Gitter.im are many spyers and watchers and add to Facebook. I feel that. Because I am not stupid. Thank you for understanding!

No solutions? What do I fix? But another example of MonoGame without advanced transformation looks very weak. With transformation looks better but why does MonoGame not work with advanced matrix?

I hope you have to resolve my help. I will appreciate.

I honestly think this is too much for you to start out with.

If your not going to read any tutorials or listen to advice.
Its pointless for you to continue using this framework.

What you should be doing is asking for good tutorials.
Not posting videos that the framework is broken,
Seriously thats some balls after the other day.

Or spaming chat that monogame is bugged when you never wrote a shader ?
When you don’t even understand the basics ? you should be learning the basics !

Worse your blaming it on the api.

Cubes work the matrices work.

If not a single thing is loaded in the image not even the textures, if its all broken how am i doing it.

Monogame isn’t “broken”, there are broken things in the above image which is seriously bugged, but none of those bugs are monogames bugs, they are mine. They are going to take time to fix.

Im not trying to be mean but straight up with you when i say.
Its not monogame its you.

It seems to me like you want and probably should use something more high level.
I think you should try unity or a full fledged game engine.

Hi @willmotil you’re right. But I am hostile to Unity3D because many bad people don’t care my deafness that is why I don’t want learn with Unity3D. Only MonoGame and OpenTK are good for me.

I will try to exams with MonoGame and OpenTK. I swear that I will learn more.

Kind regards!

Start by reading this.

http://www.codinglabs.net/article_world_view_projection_matrix.aspx