簡體   English   中英

數學:如何將3D世界轉換為2D屏幕坐標

[英]Math : How to convert a 3D World to 2D Screen coordinate

我正在尋找一種將3D xyz坐標轉換為2D xy(像素)坐標的方法。 我正在獲取需要在二維平面上繪制的坐標列表。

平面將始終是自頂向下的視圖,其寬度為以下尺寸:寬度:800像素,高度:400像素

3D世界坐標也可能包含負值,范圍從-4000到4000。我已經在Wikipedia上閱讀了一些轉換文章和一些SO線程,但是它們要么不能滿足我的需要,要么對於我有限的數學知識來說太復雜了。

我希望有一個人可以幫助我。 感謝您的時間。

問候,馬克

您可以使用[[x / z),(y / z)]之類的東西將3d投影到2d-我相信這是一種相當粗糙的方法,我認為3d到2d Googlings會返回一些相當標准的算法

Rob或多或少是正確的,只是通常需要使用比例因子(即[k *(x / z),k *(y / z)])。 如果您從不改變觀點或方向,則要完全理解其工作原理所需的所有數學就是截距定理。

我認為這的標准實現使用所謂的同質坐標,這要復雜一些。 但是對於快速而骯臟的實現,僅使用“常規” 3D坐標就可以正常工作。

在處理視點背后的坐標時,您還需要小心一點。 實際上,這是我發現(基於多邊形的)3D圖形中最難看的部分。

您可能會發現這很有趣: C#中的3D繪圖庫

可能有幫助的地方:我正在處理的某些代碼...

// Location in 3D space (x,y,z), w = 1 used for affine matrix transformations...
public class Location3d : Vertex4d
{
    // Default constructor
    public Location3d()
    {
        this.x = 0;
        this.y = 0;
        this.z = 0;
        this.w = 1; // w = 1 used for affine matrix transformations...
    }
    // Initiated constructor(dx,dy,dz)
    public Location3d(double dx, double dy, double dz)
    {
        this.x = dx;
        this.y = dy;
        this.z = dz;
        this.w = 1;     // w = 1 used for affine matrix transformations...
    }
}

// Point in 2d space(x,y) , screen coordinate system?
public class Point2d
{
    public int x { get; set; }              // 2D space x,y
    public int y { get; set; }

    // Default constructor
    public point2d()
    {
        this.x = 0;
        this.y = 0;
    }
}

// Check if a normal  vertex4d of a plane is pointing away?
// z = looking toward the screen +1 to -1   
public bool Checkvisible(Vertex4d v)
{
    if(v.z <= 0)
    {
        return false;       // pointing away, thus invisible
    }
    else
    {
        return true;
    }
}

// Check if a vertex4d is behind you, out of view(behinde de camera?)
// z = looking toward the screen +1 to -1
public bool CheckIsInFront(Vertex4d v)
{
    if(v.z < 0)
    {
        return false;       // some distans from the camera
    }
    else
    {
        return true;
    }
}

如果頂點在屏幕區域之外,則需要進行一些裁剪!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM