Straight line between two points (shader)

Hey,

just started getting to know them shaders a little bit and I’m already pretty lost I think. I haven’t found a really good “beginners” tutorial using google-foo either. The basic problem I try to solve now is drawing a line between two points in the fragment shader. It’s not really what I want to do in the end, but once I solve that I guess I can figure the rest out, or rather I hope to.

So, using a “standard” vertex passthrough shader I have point A vectorStart, and point B vectorEnd which I pass as uniform to the fragment shader.

Now I understand that the fragment shader runs for every pixel on the screen, I hope that’s right.

How do I do the


if gl_FragCoord == isOnTheWayBetweenPointAandPointB {
 make it blue }

How do I find out the “isOnTheWayBetween” thing? :smiley:
It’s probably more of a math question, but I also suck at that :smiley:

But maybe there is an easy way to solve this? Thanks guys :slight_smile:

You’ll need to pass some sort of data type using the varying keyword from the vertex shader into the fragment shader. Booleans cannot be passed through shaders (I believe), so use some sort of other value. Like if a certain integer has the value 1, then draw this fragment into the line.

Just draw a line(glLines or what ever). You can than use something like a 1D texture-coordinate to do something according where on the line a pixel is. If you want to draw a more fancy line(smooth border or a gradient along the normal) use a screen-aligned quad

GLSL is not made to draw stuff. It is made to modified stuff you sent via OpenGL.

Thanks for all the answers so far.
I really thought this would be easier :frowning:

That might be true, although I was under the assumption that this would somehow be possible. When I see what all the “shader-gurus” can do with the GLSL-web-sandbox thing :slight_smile:

yes, it is totally possible to draw a line and any other shape just with a fragment shader. You can do a myriad of nice tricks and effects like all the people do in these short code snippets on the web, but this is not the way you want to do basic things in a normal app.

Doing it the standard way is easier to do, integrates better into your renderer and over all a lot faster.

Checking if a point lies exactly on a line isn’t going to work. A mathematical line, meaning a line with no width, doesn’t cover anything. What you want is to calculate the distance from the current pixel to the mathematical line formed by your two vertices and check if that distance is within / 2. If your line is 1 pixel wide, you’d accept a distance of up to 0.5 pixels away from the line in both directions.

All you need to do this can be found in this Wiki article: http://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line

In case anyone’s wondering, this is a useful thing to do in shaders if you want to anti alias a line for example.

What are you trying to solve?

BTW, regarding anti-aliased lines, I wrote about it in this thread:

[quote]GLSL is not made to draw stuff
[/quote]
Tell that to iq, the author of Shader Toy. :stuck_out_tongue:

I actually wrote my first ray marching shader the other day, not nearly as daunting as I thought it would be. Still; though, I can’t wrap my head around much of the crazy stuff the demo sceners come up with.
http://glsl.heroku.com/e#14157.0

First off thanks for all the answers.

Thanks, although that link is intimidating :smiley:

Well I wanted to draw a line from Point A to Point B in the shader so I can “light up” the rest of the scene around it. You know, kinda like 2d lightning, but looks like I’m going to do it in code as it’s too hard for me to do in a shader and I don’t want to waste 2 weeks on such a “simple” feature :frowning:

@davedes: but it’s not really the way you want to implement shaders in a game. A simple static scene means every fragment has to consider every object.