2011-04-22 107 views
2

我搜索左右,但我实在不明白这一点。其他问题没有帮助,或者我不理解他们。映射一个3D矩形2D屏幕

的问题是,我有一个3D图像一堆点。这些点是用于矩形的,因为透视从3D相机的视图看起来不像矩形。任务是将这个矩形的点映射到屏幕上。我见过一些方面有人称之为“四到四转变”,这却大多是二维四边形映射到另一个。但是,我有X,Y 和Z坐标在现实世界中的矩形的,所以我在寻找一些更简单的方法。有没有人知道这样做的实用算法或方法?

如果有帮助,我的3D相机实际上是一个Kinect的设备与OpenNI和NITE中间件,和我使用WPF。

在此先感谢。

编辑: 我还发现,使用角度和余弦但是这似乎是一个艰难的方式(找到的3D图像的角度),我不知道这是否是真正的维基百科上的3D投影页解决与否。

+0

你想*投影到2D平面上吗?没有透视失真? – Blender 2011-04-22 17:36:46

+0

我想映射它,以便它看起来像是直视对象,如果这就是您的投影所表达的意思。 – Auxiliary 2011-04-22 17:39:36

回答

2

你可能想看看projection matrices

这是任何3D光栅化如何“变平”二维屏幕上实现三维体积。

+1

+1但是记住投影矩阵(和大多数图形相关的矩阵)不适合轻松的人,或者那些在数学中没有注意的人。他们是非常严肃的东西。我会继续睁大眼睛寻找可以使用为您完成这项工作的图书馆。 (OpenGL出现在脑海中,但是我记得是否需要手动或者不用手动来做矩阵......)编辑:关注这个链接,这个恐怖回归到如火如荼的生活中。哈啊! – corsiKa 2011-04-22 17:35:49

+0

我无法忍受矩阵。一个真正的绅士从不选择他的基础;) – Blender 2011-04-22 17:37:53

+1

实际上,构建投影矩阵非常简单。如果你想了解他们的工作方式,请查看[this](http://www.codeguru.com/cpp/misc/misc/math/article.php/c10123__1/Deriving-Projection-Matrices.htm)。如果你只是想实现,跳到第2页。 – 2011-04-22 17:39:43

0

在3D世界映射指向2D屏幕就像是OpenGL和Direct3D框架的工作的一部分。像Heandel所说的,它被称为光栅化。也许你可以使用Direct3d?

+0

所以,你是说,通过给现实世界中的矩形的四个坐标或Direct3D的这种输入,它可以映射我给它的任何其他点?我会试一试。 – Auxiliary 2011-04-22 17:42:00

1

你可以做一个基本的正投影(我想在光线追踪方面,所以你在做什么,这可能并不适用于):

enter image description here

的代码是很直观:

for y in image.height: 
    for x in image.width: 
    ray = new Ray(x, 0, z, Vector(0, 1, 0)) # Pointing forward 
    intersection = prism.intersection(ray) # Since you aren't shading, you can check only for intersections. 

    image.setPixel(x, y, intersection) # Returns black and white image of prism mapped to plane 

你只需将方向为(0, 1, 0)的矢量直接射入空间并记录下哪些命中。

2

See this code得到投影矩阵对于给定的WPF相机:

private static Matrix3D GetProjectionMatrix(OrthographicCamera camera, double aspectRatio) 
{ 
    // This math is identical to what you find documented for 
    // D3DXMatrixOrthoRH with the exception that in WPF only 
    // the camera's width is specified. Height is calculated 
    // from width and the aspect ratio. 
    double w = camera.Width; 
    double h = w/aspectRatio; 
    double zn = camera.NearPlaneDistance; 
    double zf = camera.FarPlaneDistance; 
    double m33 = 1/(zn - zf); 
    double m43 = zn * m33; 
    return new Matrix3D(
     2/w, 0, 0, 0, 
     0, 2/h, 0, 0, 
     0, 0, m33, 0, 
     0, 0, m43, 1); 
} 

private static Matrix3D GetProjectionMatrix(PerspectiveCamera camera, double aspectRatio) 
{ 
    // This math is identical to what you find documented for 
    // D3DXMatrixPerspectiveFovRH with the exception that in 
    // WPF the camera's horizontal rather the vertical 
    // field-of-view is specified. 
    double hFoV = MathUtils.DegreesToRadians(camera.FieldOfView); 
    double zn = camera.NearPlaneDistance; 
    double zf = camera.FarPlaneDistance; 
    double xScale = 1/Math.Tan(hFoV/2); 
    double yScale = aspectRatio * xScale; 
    double m33 = (zf == double.PositiveInfinity) ? -1 : (zf/(zn - zf)); 
    double m43 = zn * m33; 
    return new Matrix3D(
     xScale, 0, 0, 0, 
     0, yScale, 0, 0, 
     0, 0, m33, -1, 
     0, 0, m43, 0); 
} 

/// <summary> 
///  Computes the effective projection matrix for the given 
///  camera. 
/// </summary> 
public static Matrix3D GetProjectionMatrix(Camera camera, double aspectRatio) 
{ 
    if (camera == null) 
    { 
     throw new ArgumentNullException("camera"); 
    } 
    PerspectiveCamera perspectiveCamera = camera as PerspectiveCamera; 
    if (perspectiveCamera != null) 
    { 
     return GetProjectionMatrix(perspectiveCamera, aspectRatio); 
    } 
    OrthographicCamera orthographicCamera = camera as OrthographicCamera; 
    if (orthographicCamera != null) 
    { 
     return GetProjectionMatrix(orthographicCamera, aspectRatio); 
    } 
    MatrixCamera matrixCamera = camera as MatrixCamera; 
    if (matrixCamera != null) 
    { 
     return matrixCamera.ProjectionMatrix; 
    } 
    throw new ArgumentException(String.Format("Unsupported camera type '{0}'.", camera.GetType().FullName), "camera"); 
} 
1

我发现this。使用直接数学而不是基础。

这被称为透视投影从3D顶点转换到2D屏幕顶点。我用它来帮助我制作我的3D程序。

HorizontalFactor = ScreenWidth/Tan(PI/4) 
VerticalFactor = ScreenHeight/Tan(PI/4) 

ScreenX = ((X * HorizontalFactor)/Y) + HalfWidth 
ScreenY = ((Z * VerticalFactor)/Y) + HalfHeight 

希望这可以帮助。我想它是你在哪里寻找的。对不起,格式化(新的)