• Advertisement
Sign in to follow this  

Drawing a 3D Line in DirectX

This topic is 4021 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement