3D 物体的透视

发布于 2024-12-02 15:46:30 字数 574 浏览 1 评论 0原文

我正在将我们的一个旧应用程序从 vb6 更新为 c#,在此过程中必须重新创建原始程序员设计的自定义控件。该控件只是获取一个对象(矩形或圆锥形)的尺寸,并以 3D 形式放置该对象的轮廓草图(我认为技术上是 2.5D)。当然,控制或算法的代码无处可寻。

事先对此一无所知,除了视角之外,我几乎复制了所有内容。我正在使用我在另一个答案中找到的代码。

        }
        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;
        }

我需要帮助的最后一个部分是将视角从左向右旋转约 30 度,这样我就不会直视。

感谢您的任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2024-12-09 15:46:30

正如评论者所说:你应该使用矩阵来让你的生活变得轻松。

通过将两个矩阵(一个旋转矩阵和一个透视矩阵)相乘可以轻松完成旋转:

// 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

用于运行代码你必须做一些事情:
实现一个 C# 矩阵,或者从其他地方获取它。 这里是一个很好的实现矩阵的来源。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文