如何在Matrix3D中设置这种透视变换?
我有一个图像,并且有一些值可以使其成为 Silverlight 中的透视图,但无法完全弄清楚我需要在数学上做什么才能实现它。最重要的是我有一个称为“视野”(FOV)的角度。
这是正常的图片:
例如:
X = 30° X = 30° X = 30° FOV = 30° FOV = 60° FOV = 120° X = 60° X = 60° X = 60° FOV = 30° FOV = 60° FOV = 120°
如果有任何帮助,请帮助我完成数学计算在 Silverlight 中重现这些。
I have an image with and have a few values to make it a perspective in Silverlight, but can't quite figure out what I need to do mathmatically to make it happen. The most important thing is I have an angle called a "Field of View" (FOV).
This is the normal picture:
For example:
X = 30° X = 30° X = 30° FOV = 30° FOV = 60° FOV = 120° X = 60° X = 60° X = 60° FOV = 30° FOV = 60° FOV = 120°
Any help would be appreciated to walk me through the math to reproduce these in Silverlight.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为每个人都遇到的问题是随着透视变换需要视口移动。
尝试一下:
这将创建适当的视角转换并与 PowerPoint 生成的内容相匹配。
此代码改编自 MSDN。
I think the problem everyone is encountering is that there needs to be a viewport shift along with the perspective transformation.
Try this out:
This will create the appropriate perspective shift and match what PowerPoint is producing.
This code was adapted from MSDN.
经过大量的尝试后,我实际上同意“Ladislav Mrnka”的矩阵答案作为最简单的解决方案,并且对他们的答案进行了投票。
只需留下下面的示例即可为您提供一些可以使用的东西,但您需要通过 Matrix3DProjection 来更新它。
看起来您正在将源图片视为具有几种可能的视野之一,例如,就像使用 120° 的广角镜头或 30° 的变焦镜头拍摄的一样。然后,您尝试在显示时再现原始场景的纵横比。这是正确的吗?
如果是这样,您实际上希望在使用透视变换旋转图片之前水平拉伸图片以恢复隐式宽度。这意味着您实际上正在尝试解决两个独立的(更简单的)数学问题,例如:
我遇到的困难是示例照片没有表明有关显示的任何具体规则。显示宽度各不相同,因此我无法计算出您的最终结果是什么。如果您可以提供更多信息,我应该能够提供具体的计算。
好的,根据您对 PowerPoint 中透视设置的使用,所需的 2 个步骤确实是:
第一个计算非常简单简单的。您需要将比例设置为余弦(X 角度)。
第二个是估计值,因为 Powerpoint 透视角度似乎与旋转无关。
我在下面提供了完整的示例 XAML 和代码隐藏来生成所示的应用程序*。
***注意:存在一个严重缺陷,即投影变换无法将图像扭曲到一定程度你需要。我正在尝试 Matrix3DProjection,解决方案将遵循 **
后面的代码:
这应该为您提供一个处理测试框架来尝试变化。我重新考虑了计算步骤,使其更加明显。
After lots of playing with this I actually concur with "Ladislav Mrnka"'s matrix answer as being the simplest solution and have up-voted their answer.
Just leaving the sample below to give you something to play with but you will need to update it via a Matrix3DProjection.
It looks like you are treating your source picture as having one of several possible fields of view, e.g. as if taken with a wide-angle lens for the 120° or a zoom lens for the 30°. You are then trying to reproduce the aspect ratio of the original scene when displayed. Is this correct?
If so the procedure you actually want to stretch the picture horizontally to restore the implicit width, before rotating it with the perspective transform. That would mean you are actually trying to solve 2 separate (simpler) maths problems here e.g.:
The difficulty I have is that the example photos do not indicate any specific rules about the display. The display widths all vary so I cannot work out what your end result is intended to be. If you can provide more information I should be able to provide specific calculations.
Ok, based on your use of the Perspective settings in PowerPoint, the 2 required steps are indeed:
The first calculation is very simple. You need to set the scale to Cosine(X-angle).
The second is an estimate as the Powerpoint perspective angle does not seem to relate to rotation.
I have provided a full sample XAML and code-behind below to generate the app shown*.
***Note: there is a serious flaw in that Projection Transforms are not able to distort the image to the degree you require. I am trying Matrix3DProjection instead, solution will follow **
Code behind:
This should give you a handle test framework to try out variations. I re-factored the calculation steps to make it more obvious.
使用 Matrix3DProjection 并根据 此函数来自 Direct3D。您需要以弧度为单位的视场、屏幕的长宽比和两个剪辑距离(您正在定义有限平截头体)。如果您正在寻找进一步的解释为什么这样设置,您应该阅读一些有关计算机图形学的书。通常,投影变换矩阵仅设置视锥体。绕 X 轴旋转对象是通过单独的转换来执行的,但这是计算机图形学的一般做法,我不确定它在 Silverlight 中是否同样有效。
编辑:
编辑2:
我之前的建议不起作用。用于计算的第一个矩阵不正确,因为 Silverlight 使用转置版本。不使用第二个平移图像到中心和视口转换。我合并了艾莉森提出的代码(也可以找到此处)进行修改以拥有 FovX 和 HiTech Magic 的 Silverlight 应用程序。我以前从未编写过 Silverlight 应用程序...这是工作示例:
Use Matrix3DProjection and set transformation based on matrix provided at the end of this function from Direct3D. You need your FOV in radians, Aspect ratio for screen and two distances for clipping (you are defining finite frustrum). If are you looking for futher explanation why it is set this way you should get some book about computer graphics. Also it is usual that matrix for projection transformation sets only view frustrum. Rotatin objects around X axis is performed by separate transformation but it is general practice from computer graphics and I'm not sure if it works same in Silverlight.
Edit:
Edit2:
My previous suggestion doesn't work. First matrices used for computation are incorrect because Silverlight uses transposed versions. Second translation image to center and viewport transformation is not used. I have combined Alison's proposed code (also can be found here) with modification to have FovX and HiTech Magic's Silverlight application. I have never written Silverlight app before ... Here is working sample:
这似乎准确地解释了您的情况
This seems to explain exactly your scenario
我的线性代数有点生疏,有信心帮助您,但是这篇文章 看起来这对您来说可能是一个很好的起点
My linear algebra is a little rusty be confident in helping, but this article looks like it could be a good starting place for you
好的。我已将 Ladislav Mrnka 的矩阵变换答案与我之前的示例应用程序结合起来,但他们的 3D 矩阵示例中似乎存在一些拼写错误,而且我对 3D 矩阵数学的了解不够强,无法纠正它。最终结果是图像应该是空白的显示:(
为了让这个聚会开始,我提供了一个带有 Xaml 的完整交互式测试应用程序(如下),以便 Ladislav Mrnka(或其他具有更好 3D 数学知识的人)可以纠正问题。
隐藏代码:
Okay. I have combined Ladislav Mrnka's Matrix transform answer with my previous sample app, but there appear to have been some typos in their 3D Matrix example and I am not strong enough with 3D Matrix math to correct it. The end result is a blank display where the image should be :(
Instead to get this party started I have provide a full interactive test app (below) with Xaml so that Ladislav Mrnka (or someone else with better 3D maths) can correct the problem.
Code-behind: