Jump to content
  • Advertisement
Sign in to follow this  
jkristia

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.

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

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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):

 

vertex shader:

-transform incoming line points by wvp

-that's it

 

geometry shader:

-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 this post


Link to post
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 this post


Link to post
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 smile.png

Edited by Nik02

Share this post


Link to post
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

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!