Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment shader - drawing a line?

I was interested in how to draw a line with a specific width (or multiple lines) using a fragment shader. I stumbled on the this post which seems to explain it.

The challenge I have is understanding the logic behind it.

A couple of questions:

  1. Our coordinate space in this example is (0.0-1.0,0.0-1.0), correct?
  2. If so, what is the purpose of the "uv" variable. Since thickness is 500, the "uv" variable will be very small. Therefore the distances from it to pont 1 and 2 (stored in the a and b variables)?
  3. Finally, what is the logic behind the h variable?
like image 355
Stefan Avatar asked Nov 01 '22 12:11

Stefan


1 Answers

i will try to answer all of your questions one by one:

1) Yes, this is in fact correct.

2) It is common in 3d computer graphics to express coordinates(within certain boundaries) with floating-point values between 0 and 1(or between -1 and 1). First of all, this makes it quite easy to decide whether a given value crosses said boundary or not, and abstracts away from a concept of "pixel" being a discrete image unit; furthermore this common practise can be found pretty much everywhere else(think of device coordinates or texture coordinates)

Don't be afraid that values that you are working with are less than one; in fact, in computer graphics you usually deal with floating-point arithmetics, and FLOAT types are quite good at expressing Real values line around the "1" point.

3) The formula give for h consists of 2 parts: the square-root part, and the 2/c coefficient. The square root part should be well known from scholl math classes - this is Heron formula for the area of a triangle(between a,b,c). 2/c extracts the height of the said triangle, which is stored in h and is also the distance between point uv and the "ground line" of the triangle. This distance is then used to decide, where is uv in relation to the line p1-p2.

like image 112
Pavel Beliy Avatar answered Nov 16 '22 13:11

Pavel Beliy