Sign in to follow this  
Byron

Thick line drawing

Recommended Posts

This topic has probably come up a few times, so I apologise if I am re-hashing old ground here: I want to draw thick lines in my DX based renderer. Previously, the way I used to draw thick lines in a 2D system was to get the 'normal' of a line (-y,x) and then project the coordinates out along the normal in both directions to add some thickness to the line. Works fine in 2D but now I need it in 3D. My idea is to use the same method to derive the view coordinates as I do with billboards as seen here and simply work as if in 2D. This will at least allow me to use Z. Does anyone have a better idea or algorithm? I may need to texture the lines at some point... P.S. I have briefly looked at D3DXLine but even with the transform this seems to work in 2D only - i.e I couldn't see how to specify depth into the scene.

Share this post


Link to post
Share on other sites
I haven't seen any techdemo that utilizes sw vertex shader for this(or any other) effect, however a friend of mine used sw vertex shader at the times he was developing for really legacy HW(GF 2 and similar; without HW T&L) and it ran rather well. And it being a really simple shader I would give it a try

Share this post


Link to post
Share on other sites
Quote:
My idea is to use the same method to derive the view coordinates as I do with billboards as seen here and simply work as if in 2D. This will at least allow me to use Z.

If you just want straightforward thick lines, this will do the trick. But rather than drawing several parallel lines, which would cause confusion under different coordinate systems, you should use triangles. The technique is similar to billboarding, as you said:

Suppose the line is to be drawn from x0 to x1, and the normalised viewing direction is given by v, both in 3-space. I'll assume that the viewer is positioned at the origin to simplify the arithmetic. If this isn't the case, you'll need to translate everything before you start.
Calculate the direction vector representing the line:

x = x1 - x0

Then take the cross product to get a vector in 3-space that represents the orthogonal compement vector when projected to screen space:

u = Vec3Cross(x, v)

Using this vector, the lines will have uniform thickness regardless of the distance from the viewer. If you want them to thin out as they get further away, you need to normalise:

u = Vec3Normalize(u)

From here you are free to extrude the line into a rectangle

u_half = Vec3Scale(u, LINE_THICKNESS * 0.5f)
DrawTriangle(x0 - u_half, x0 + u_half, x1 + u_half)
DrawTriangle(x1 - u_half, x1 + u_half, x0 - u_half)

where LINE_THICKNESS is in the usual distance units.
Extending this to incorportae textures should be trivial.

Regards
Admiral

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this