• Advertisement
Sign in to follow this  

Managed DirectX, 2D, lines

This topic is 4179 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

I'm looking for information on how to draw simple colored lines, maybe curved lines between specific coordinates using managed directx. I'm doing a school project involving top-down, 2D world representation of a car driving on roads. I'd like to map the roads using a pixel coordinate system, the standard starting at 0,0 on the top left of the GUI screen. I've just been having difficulty finding 2D tutorials for managed directx. Any help on drawing lines using a 2D coordinate system would be much appreciated. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
I'd just use the normal way to draw Lines (with a simple VertexBuffer) and leave the Z-Coordinate zero. There's a load of tutorials how to do that.
As for the Color use CustomVertex.PositionColored for your vertices.

Small example:

// some colour
int color = System.Drawing.Color.Red.ToArgb();

// Array of Vertices
CustomVertex.PositionColored[] vertices = new CustomVertex.PositionColored[6];

// VertexBuffer containing the geometry
VertexBuffer buffer = new VertexBuffer(typeof(CustomVertex.PositionColored),
6, device, Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Managed);

// Fill in some points to draw lines in between
vertices[0].Position = new Vector3(0f, 0f, 0f);
vertices[0].Color = color;
vertices[1].Position = new Vector3(100f, 20f, 0f);
vertices[1].Color = color;
[etc...(for a LineList a line is always a pair of two vertices)]

// write the vertex data to the VertexBuffer
buffer.SetData(0,vertices,0);


I guess you know how to draw that stuff...
If you want to draw curves, look up some algorithms how to rasterize the points on the curves and do the above.
Shouldn't be that hard to find.
for your top-down view use a camera that only moves on the xy-plane.
I don't know, whether this covers what you wanted to know, but that's what I have to say about it ;)

If you have some questions regarding this you can email/pm me.

Good luck,
Martin

Share this post


Link to post
Share on other sites
I ended up using this to define the vertices:

VertexBuffer m_vb4 = new VertexBuffer(device, 2 * PositionColored.StrideSize, Usage.WriteOnly, PositionColored.Format, Pool.Managed, null);
GraphicsBuffer<PositionColored> buffer = m_vb4.Lock<PositionColored>(0, 0, LockFlags.None);
buffer.Write(new PositionColored(0.0f, 0.0f, 5.0f, Color.Red)); // 0
buffer.Write(new PositionColored(0.0f, 2.8f, 5.0f, Color.Red)); // 1
//buffer.Write(new PositionColored(1.0f, 2.7f, 5.0f, Color.Red)); // 2
//buffer.Write(new PositionColored(1.0f, 1.0f, 5.0f, Color.Blue)); // 3
m_vb4.Unlock();
buffer.Dispose();

and this to draw them:

device.VertexFormat = PositionColored.Format;
device.SetStreamSource(0, m_vb4, 0, PositionColored.StrideSize);
device.DrawPrimitives(PrimitiveType.LineStrip, 0, 1);

besides a whole other bunch of stuff I found here.

I was curious how to standardize the length units for display. It seems that 2.7f in each direction from the center is roughly the size of the screen. I was also thinking about making it just a top-left coordinate system. Any ideas on how to do any of this?

Share this post


Link to post
Share on other sites
Rather then fooling around with vertex buffers, you might want to check out the Direct3D.Line class. If you've ever used the sprite class then it should look familiar, it works in pretty much the same way.

Share this post


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

  • Advertisement