🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Rotating a wireframe cube in 3D space with matrices (C# & Windows GDI)

Started by
3 comments, last by scott.tunstall@ntlworld.com 2 years, 6 months ago

I have code to place a camera “looking at” a rotating object, which is a 3D model. It works.

ScottTunstall/3dCamera (github.com)

I'd like to replace the 3D model with a rotating wireframe cube comprised of 3D points joined by lines. But it's gone wrong:

ScottTunstall/GoneHorriblyWrong: Rotating 3D points using matrixes. Doesn't work. Its gone badly somewhere. (github.com)

I want to do this in GDI with primitives just to get the concepts right. I don't want to use Unity, or 3D Model assets - not yet.

Could someone put me out of my misery and tell me - without heavy math jargon - where I have gone wrong? Or even better, tell me what code I should replace?

Believe me when I say I have searched for examples in C# and can't find anything. I am not from a mathematical background, but I work as a dev in my day job.

Again let me repeat - this is only for understanding the concepts - I will not be creating a game.

Essentials: should have a LookAt camera position and target that I can update.

Thanks in advance,

Scott

Advertisement

Just getting 404 error with that first link… https://github.com/ScottTunstall/3dCamera

maybe a private project?

Never mind,. On your second problem project I changed the following and got a spinning wireframe cube.

Inside your for loop in 'WireframeCubeForm_Paint' , I basically changed it to this :-

  			var projected = new Vector2[8];
            for (var i = 0; i < _vertices.Length; i++)
            {
                var vertex = _vertices[i];
                var matrix = _worldMatrix *
                             _viewMatrix *
                             _projectionMatrix;

                var transformed = Vector4.Transform(vertex, matrix);

                // normalize if w is different than 1 (convert from homogeneous to Cartesian coordinates)
                if (transformed.W != 1)
                {
                    transformed.X /= transformed.W;
                    transformed.Y /= transformed.W;
                }

                projected[i] = new Vector2(transformed.X, transformed.Y);
                projected[i] *= new Vector2(1000, 1000 * Height / Width);
                projected[i] += new Vector2(Width / 2, Height / 2);
            }
  • changed it to W*V*P instead of P*V*W (just the way your matrix library works)
  • Used Vecrtor4.Transform instead of Vector3.Transform on your vertices
  • Normalized the transformed positions and changed your projected array to Vector2
  • Scaled and moved the points (The Graphics is 0,0 top-left not center)

Hope your misery is a bit less…

Thank you for your assistance. I will take and implement the suggestions and give you due credit.

I really appreciate your help.

Scott

This topic is closed to new replies.

Advertisement