2011-04-22 109 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 

希望這可以幫助。我想它是你在哪裏尋找的。對不起,格式化(新的)