简体   繁体   English

如何在DirectX中绘制3D对象周围的2D几何图形? (D3D9)

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

I'm using the D3DXVec3Project function to get screen coordinates of 3D points and draw 2D lines that appear to be 3D. 我正在使用D3DXVec3Project函数来获取3D点的屏幕坐标并绘制看似3D的2D线。 The obvious result is that the lines drawn will always be on top of any 3D object even if the object is supposed to be in front of the lines. 显而易见的结果是,即使假定对象在线条的前面,绘制的线条也将始终在任何3D对象的顶部。

This is the code I use to draw lines: 这是我用来画线的代码:

 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
 }

As an example I have the earth as a 3D sphere and the ecliptic plane as a grid of 2D lines, half of the earth is on top of the ecliptic, so half of the earth should be drawn on top of the ecliptic grid, with the code I'm using the whole grid is always on top of the earth so it looks like the entire planet is under the grid. 例如,我将地球作为3D球体,将黄道面作为2D线的网格,地球的一半在黄道的顶部,因此应将地球的一半绘制在黄道的顶部,我使用的整个网格代码始终位于地球的顶部,因此看起来整个星球都在网格之下。

Here is a screenshot: http://s13.postimg.org/3wok97q7r/screenshot.png 这是屏幕截图: http : //s13.postimg.org/3wok97q7r/screenshot.png

How can I draw the lines behind 3D objects? 如何在3D对象后面画线?

Ok, I got it working now, this is the code that rocks for drawing 3D lines in D3D9: 好的,我现在可以正常工作了,这是在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();
}

With legacy Direct3D 9, you just use the standard IDirect3DDevice9::DrawPrimitive methods with a D3DPRIMITIVETYPE of D3DPT_LINELIST or D3DPT_LINESTRIP . 与传统的Direct3D 9,你只需要使用标准IDirect3DDevice9::DrawPrimitive方法与D3DPRIMITIVETYPED3DPT_LINELISTD3DPT_LINESTRIP You will likely use DrawPrimitiveUP or DrawIndexedPrimitiveUP , although using the non-UP versions with a DYNAMIC Vertex Buffer is much more efficient. 您可能会使用DrawPrimitiveUPDrawIndexedPrimitiveUP ,尽管将非UP版本与DYNAMIC Vertex Buffer一起使用效率更高。

D3DXCreateLine in the legacy D3DX9 utility library is intended for styled lines for CAD-like scenarios. 旧版D3DX9实用程序库中的D3DXCreateLine适用于类似CAD场景的​​样式化线。 In modern DirectX 11 parlance, D3DXCreateLine is basically like Direct2D which is for 2D rendering. 在现代DirectX 11中, D3DXCreateLine基本上类似于Direct2D,用于2D渲染。

With Direct3D 11, you'd use ID3D11DeviceContext::Draw or ID3D11DeviceContext::DrawIndexed , after having used ID3D11DeviceContext::IASetPrimitiveTopology to set it to D3D11_PRIMITIVE_TOPOLOGY_LINELIST or 11_PRIMITIVE_TOPOLOGY_LINESTRIP mode. 在Direct3D 11中,使用ID3D11DeviceContext::IASetPrimitiveTopology将其设置为D3D11_PRIMITIVE_TOPOLOGY_LINELIST11_PRIMITIVE_TOPOLOGY_LINESTRIP模式后,将使用ID3D11DeviceContext::DrawID3D11DeviceContext::DrawIndexed For "UP" style drawing, see PrimitiveBatch in the DirectX Tool Kit . 有关“ UP”样式的图形,请参见DirectX Tool Kit中的PrimitiveBatch I in fact use PrimitiveBatch to draw a 3D grid of lines into a 3D scene in the Simple Sample for DirectX Tool Kit . 实际上,我使用PrimitiveBatch在DirectX工具包简单样本中将3D线网格绘制到3D场景中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM