简体   繁体   中英

Perspective on 3D objects

I am updating one of our older apps from vb6 to c# and in the process have to recreate a custom control that the original programmer designed. The control simply took the dimensions of an object, rectangular or conical, and placed an outline sketch of the object in 3D (2.5D technically I think). Of course, the code for the control or the algorithim is nowhere to be had.

Knowing nothing about this before hand I have gotten pretty much everything replicated except the perspective. I am using this code that I found on another answer here.

        }
        double w = 400;
        double h = 250;
        double t = 0.6; // tilt angle   
        double X = w / 2 - x;
        double Y = h / 2 - y;
        double a = h / (h + Y * Math.Sin(t));
        double u = a * X + w / 2;
        double v = a * Y * Math.Cos(t) + h / 2;
        }

The last piece I need help with though is turning the perspective about 30 degrees left-to-right so I'm not looking at straight on.

Thanks for any help.

As the commenter says: You should use matrices to make your live easy.

Rotation could be easily done by multiplying the 2 matrices, a rotation matrix and a perspective matrix this way:

// We don't have a view matrix here
Matrix4x4 modelProjection = Matrix4x4.Perspective(400, 250, Math.PI / 4) * Matrix4x4.RotationX(degree);
// Get a specifics point position, use x and y to determine the screen position and z for the z-order
Vector3 screenPosition = modelProjection * myPosition; // myPosition is a Vector3

For running the code you have to do some things: Implement a C# matrix, or get it from anywhere else. Here is a excellent source for implementing matrices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM