如何在 DirectX 中绘制 2D 几何体和 3D 对象?(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
方法,D3DPRIMITIVETYPE
为 D3DPT_LINELIST
或 D3DPT_LINESTRIP
。您可能会使用 DrawPrimitiveUP
或 DrawIndexedPrimitiveUP
,尽管使用带有动态顶点缓冲区的非 UP 版本效率要高得多。
旧版 D3DX9 实用程序库中的D3DXCreateLine
适用于类似 CAD 的方案的样式化线条。 在现代DirectX 11的说法中,D3DXCreateLine
基本上就像用于2D渲染的Direct2D一样。
Direct3D 11,在使用 ID3D11DeviceContext::IASetPrimitiveTopology
将其设置为D3D11_PRIMITIVE_TOPOLOGY_LINELIST
或11_PRIMITIVE_TOPOLOGY_LINESTRIP
模式后,你将使用 ID3D11DeviceContext::Draw
或 ID3D11DeviceContext::DrawIndexed
。有关"UP"样式绘图,请参阅 DirectX 工具包中的PrimitiveBatch
。实际上,我使用 PrimitiveBatch 在 DirectX 工具包的简单示例中将线条的 3D 网格绘制成 3D 场景。
- CPU 瓶颈;处理具有许多非静态对象的 3D 场景渲染的简单方法
- 如何从 3d 对象文件中获取 3d 坐标
- 如何在3D世界中获得对象相对于父母的绝对位置
- 根据 3D 对象的点和边生成其面
- 将每个对象渲染到空间中,并在3D场景上渲染2D场景
- OPENCV(C ) - 从已知的3D对象和相机位置计算图像的2D坐标
- 我的3D OpenGL对象围绕世界来源,而不是本地空间来源.我在做什么错或误会
- 如何将3D .OBJ对象表示为3D数组
- Android OpenCV - 每次旋转是否有可能捕捉到不同的3D对象点击?
- 找出3D空间中两个对象的公式
- 想要在从搅拌机导入的实体3D对象上绘制线框网格
- 如何将纹理映射到 3D 对象
- 如何在 OpenGL 中为 3D 对象设置原点
- C++通过引用将 3D 数组传递给第二个对象,无法操作
- 如何在 3D 场景中旋转单个对象
- 如何在 DirectX 中绘制 2D 几何体和 3D 对象?(D3D9)
- 基于对应分组参数的PCL 3D对象识别
- 计算 3D 对象与点之间的角度
- 3d对象覆盖-增强现实irrlight + opencv
- 在3D对象数组中标记连续的部分