• Create Account

Need scary sound effects or creepy audio loops for your next horror-themed game? Check out Highscore Vol.3 - The Horror Edition in our marketplace. 50 sounds and 10 loops for only \$9.99!

# thick line - fixed width ?

6 replies to this topic

### #1jkristia  Members   -  Reputation: 147

Like
0Likes
Like

Posted 07 October 2013 - 10:24 PM

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.

### #2Nik02  Crossbones+   -  Reputation: 2311

Like
1Likes
Like

Posted 07 October 2013 - 11:49 PM

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, 07 October 2013 - 11:52 PM.

Niko Suni

### #3jkristia  Members   -  Reputation: 147

Like
0Likes
Like

Posted 08 October 2013 - 07:12 AM

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.

### #4Nik02  Crossbones+   -  Reputation: 2311

Like
0Likes
Like

Posted 08 October 2013 - 08:22 AM

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, 08 October 2013 - 08:33 AM.

Niko Suni

### #5jkristia  Members   -  Reputation: 147

Like
0Likes
Like

Posted 08 October 2013 - 10:00 PM

>>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.

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

### #6Nik02  Crossbones+   -  Reputation: 2311

Like
1Likes
Like

Posted 08 October 2013 - 10:29 PM

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, 08 October 2013 - 10:36 PM.

Niko Suni

### #7jkristia  Members   -  Reputation: 147

Like
0Likes
Like

Posted 09 October 2013 - 07:13 AM

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

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

PARTNERS