# thick line - fixed width ?

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

## Recommended Posts

I'm trying to create a thick line class, and I have it working to the point where the line is always facing the camera and the line width is a fixed hard coded unit width, e.g. 0.1f

What I'm struggling with at the moment is to

1. have the line width fixed regardless of zoom level, e.g. I would like to have it 5 pixels wide, whether I have zoomed in or out.
2. To have the fixed width at both ends of the thick line in perspective view.

For #2 I thought it was as simple as using the inverse projection matrix, but ...

Any suggestions of how to attack this is much appreciated.

##### Share on other sites

You could transform (scale) the points of the line first, before constructing the wide geometry.

If you want to do this in GPU, you can use geometry shaders (D3D10 and up).

Edited by Nik02

##### Share on other sites

Yes, am I transforming the points into a rectangle with the width. That part is working. The part that I cannot get working is to

- keep the width constant, so the far point appears same thickness as the near point. This is where I thought I just needed to transform the rectangles vertices using the inverse projection matrix.

- calculate how wide the far end and near end needs to be if I want the line to appear e.g. 10 pixels wide, near and far, regardless of the zoom.

Hmmm. thinking of it, I think I need to use the inverse proj when calculating the width. I will try that tonight when I have more time to play.

##### Share on other sites

By the time the lines are in your screen, there is no notion of "near" and "far" (except for the pixel depth values, but that's beside the point); the relative distance of your geometry is represented by its scale (if you use perspective projection).

The width is automatically constant if you do the widening after transforming the line geometry (points of the yet non-widened line) to clip space (that is, multiplying by world * view * projection). You don't have to calculate the inverse of anything in this manner; you simply just output the lines with the desired width.

The shader setup would be as follows (metacode):

-transform incoming line points by wvp

-that's it

-take the incoming line segment

-calculate the rectangle representing said line segment in widened form

-generate two triangles for that rectangle, and output them for rasterization

-optionally, output some kind of line caps as well to prevent "breaking" of the line at its cusps

If you want to generate line caps, adjacency information for the line geometry is very useful. With it, you can read the adjacent points of the line segment in the GS as well, and you are better equipped to handle the joint geometry between the lines as you see fit. For simple round joints and caps, adjacency is not needed, though.

These steps can easily be implemented in pure software, too, in case you don't target a platform with GS capabilities.

Edited by Nik02

##### Share on other sites

>>These steps can easily be implemented in pure software, too, in case you don't target a platform with GS capabilities.

This is kind of what I'm trying to do. For now I'm just trying to figure out how to do it, and I'm recreating and recalculating the rectangles on each frame update. Once it is working, I might want to try and do the same in the shader.

I now have the widen line with 'almost' constant width regardless of the zoom (camera distance). Notice the line thickness is almost the same when zoomed out and in.

[attachment=18311:thickline_1.png]

[attachment=18312:thickline_2.png]

but I have not yet figured out how to 'cancel' the scaling caused by the perspective projection.

[attachment=18313:thickline_3.png]

##### Share on other sites

You seem to think that applying perspective projection is mandatory on the gpu side.

If you set the transformation to identity on vertex shader (effectively just pass through the vertices unmodified) or set the world, view and projection matrices to identity (on fixed pipeline), you can do your own transformations on software before sending the stuff to the device.

In D3D9 and earlier, it is also possible to use the XYZRHW position type in the vertices to mark them pre-transformed, in which case the device doesn't even try to transform them again. In D3D10 and later, the vertices are processed by the vertex shader anyway, no matter what - but the shader can just pass through the vertices.

So, your ideal "pipeline" (implemented in software) would be as follows:

Software side:

Transform line points by wvp

Widen the transformed lines; now, the line width is constant with respect to clip space

Hardware side:

Pass through the widened line geometry without multiplying with wvp

The point in this approach is that in the end there is no perspective projection to cancel, because you already did it before the widening

Edited by Nik02

##### Share on other sites

>>If you set the transformation to identity on vertex shader (effectively ...

Ahh, didn't think of that. I will give this a try tonigh

• 18
• 18
• 11
• 21
• 16
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!