簡體   English   中英

如何在DirectX中繪制3D對象周圍的2D幾何圖形? (D3D9)

[英]How can I draw 2D geometry benind 3D objects in DirectX? (D3D9)

我正在使用D3DXVec3Project函數來獲取3D點的屏幕坐標並繪制看似3D的2D線。 顯而易見的結果是,即使假定對象在線條的前面,繪制的線條也將始終在任何3D對象的頂部。

這是我用來畫線的代碼:

 void DrawLine(float Xa, float Ya, float Xb, float Yb, float dwWidth, D3DCOLOR Color)
 {
    if (!g_pLine)
        D3DXCreateLine(d3ddev, &g_pLine);

    D3DXVECTOR2 vLine[2]; // Two points
    g_pLine->SetAntialias(0); // To smooth edges


    g_pLine->SetWidth(dwWidth); // Width of the line
    g_pLine->Begin();

    vLine[0][0] = Xa; // Set points into array
    vLine[0][1] = Ya;
    vLine[1][0] = Xb;
    vLine[1][1] = Yb;

    g_pLine->Draw(vLine, 2, Color); // Draw with Line, number of lines, and color
    g_pLine->End(); // finish
 }

例如,我將地球作為3D球體,將黃道面作為2D線的網格,地球的一半在黃道的頂部,因此應將地球的一半繪制在黃道的頂部,我使用的整個網格代碼始終位於地球的頂部,因此看起來整個星球都在網格之下。

這是屏幕截圖: http : //s13.postimg.org/3wok97q7r/screenshot.png

如何在3D對象后面畫線?

好的,我現在可以正常工作了,這是在D3D9中繪制3D線的代碼:

LPD3DXLINE      g_pLine;

void Draw3DLine(float Xa, float Ya, float Za,
                float Xb, float Yb, float Zb,
                float dwWidth, D3DCOLOR Color)
{
    D3DXVECTOR3 vertexList[2];
    vertexList[0].x = Xa;
    vertexList[0].y = Ya;
    vertexList[0].z = Za;

    vertexList[1].x = Xb;
    vertexList[1].y = Yb;
    vertexList[1].z = Zb;

    static D3DXMATRIX   m_mxProjection, m_mxView;

    d3ddev->GetTransform(D3DTS_VIEW, &m_mxView);
    d3ddev->GetTransform(D3DTS_PROJECTION, &m_mxProjection);

    // Draw the line.
    if (!g_pLine)
    D3DXCreateLine(d3ddev, &g_pLine);
    D3DXMATRIX tempFinal = m_mxView * m_mxProjection;
    g_pLine->SetWidth(dwWidth);
    g_pLine->Begin();
    g_pLine->DrawTransform(vertexList, 2, &tempFinal, Color);
    g_pLine->End();
}

與傳統的Direct3D 9,你只需要使用標准IDirect3DDevice9::DrawPrimitive方法與D3DPRIMITIVETYPED3DPT_LINELISTD3DPT_LINESTRIP 您可能會使用DrawPrimitiveUPDrawIndexedPrimitiveUP ,盡管將非UP版本與DYNAMIC Vertex Buffer一起使用效率更高。

舊版D3DX9實用程序庫中的D3DXCreateLine適用於類似CAD場景的​​樣式化線。 在現代DirectX 11中, D3DXCreateLine基本上類似於Direct2D,用於2D渲染。

在Direct3D 11中,使用ID3D11DeviceContext::IASetPrimitiveTopology將其設置為D3D11_PRIMITIVE_TOPOLOGY_LINELIST11_PRIMITIVE_TOPOLOGY_LINESTRIP模式后,將使用ID3D11DeviceContext::DrawID3D11DeviceContext::DrawIndexed 有關“ UP”樣式的圖形,請參見DirectX Tool Kit中的PrimitiveBatch 實際上,我使用PrimitiveBatch在DirectX工具包簡單樣本中將3D線網格繪制到3D場景中。

暫無
暫無

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

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