Drawing a 3D Line in DirectX

Started by
5 comments, last by AberAber 17 years, 3 months ago
Are there any built-in or good snippets of code for drawing a 3d line in DirectX? I want to draw a line in space, for example (1, 2, 3) to (10, 30, 4). There was a built-in 3d line function I saw, but it seemed to always just display on the screen and not actually be in the world. Code snippets would be appreciated, thanks!
Advertisement
You can simply use D3DPT_LINELIST or D3DPT_LINESTRIP with DrawPrimitive and provide the vertices in the buffer.

Note that the line is affected by lighting and texture as well.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Sorry can you explain it a little farther please.

According to msdn:
D3DPT_LINELIST
Renders the vertices as a list of isolated straight line segments.
D3DPT_LINESTRIP
Renders the vertices as a single polyline.

Is it like wireframe mode then? Does it have to be triangles or are they not triangles?
For rendering triangles as triangle list you provide the three vertices of each face in the buffer.

For rendering lines as line list you just provide the two end points of a line inside the buffer. With a line strip the previous second vertex will be used as the first vertex of the next line.


If you want a wireframe rendering of a "normal" model consisting of triangles you could use the renderstate D3DRS_FILLMODE with D3DFILL_WIREFRAME.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Is there any way to add 3d thickness to these lines?
Here's how you do it in c# with MDX:

Vector3[] startToEnd;
/*Fill out the points of the line in startToEnd*/
System.Drawing.Color color = Color.Red;
Line line = new Line(D3DDevice);
line.Antialias = true;
line.Width = 3.0f;
this.line.DrawTransform(startToEnd,D3DDevice.Transform.World*D3DDevice.Transform.View*D3DDevice.Transform.Projection,
color);
Great. Got it working!

D3DXVECTOR3 vertexList[2];
vertexList[0].x = 778;
vertexList[0].y = -55;
vertexList[0].z = -619;
vertexList[1].x = 507;
vertexList[1].y = -55;
vertexList[1].z = -619;

// Draw the line.
LPD3DXLINE _line;
D3DXCreateLine(g_pd3dDevice, &_line);
D3DXMATRIX tempFinal = TheCamera.m_matView * matProj;
_line->SetWidth(10.0f);
_line->Begin();
_line->DrawTransform(vertexList, 2, &tempFinal, 0xff408010);
_line->End();

[Edited by - AberAber on January 18, 2007 1:02:12 AM]

This topic is closed to new replies.

Advertisement