Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Straight line between two points (shader)  (Read 1708 times)
0 Members and 1 Guest are viewing this topic.
Offline seismic

Senior Member


Medals: 4
Projects: 1



« Posted 2014-02-13 20:33:42 »

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
1  
2  
if gl_FragCoord == isOnTheWayBetweenPointAandPointB {
 make it blue }


How do I find out the "isOnTheWayBetween" thing? Cheesy
It's probably more of a math question, but I also suck at that Cheesy

But maybe there is an easy way to solve this? Thanks guys Smiley
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2014-02-13 20:39:48 »

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.

Offline Danny02
« Reply #2 - Posted 2014-02-13 21:32:43 »

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trollwarrior1
« Reply #3 - Posted 2014-02-13 21:41:52 »

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

Senior Member


Medals: 4
Projects: 1



« Reply #4 - Posted 2014-02-13 21:52:31 »

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

Quote
GLSL is not made to draw stuff. It is made to modified stuff you sent via OpenGL.
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 Smiley



Offline Danny02
« Reply #5 - Posted 2014-02-13 22:16:53 »

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.
Offline theagentd
« Reply #6 - Posted 2014-02-13 23:42:37 »

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

Myomyomyo.
Offline davedes
« Reply #7 - Posted 2014-02-14 01:22:46 »

What are you trying to solve?

BTW, regarding anti-aliased lines, I wrote about it in this thread:
http://www.java-gaming.org/topics/lwjgl-antialiased-lines/30894/msg/285889/view.html#msg285889

Quote
GLSL is not made to draw stuff
Tell that to iq, the author of Shader Toy. 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

Offline seismic

Senior Member


Medals: 4
Projects: 1



« Reply #8 - Posted 2014-02-14 07:44:42 »

First off thanks for all the answers.

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

Thanks, although that link is intimidating Cheesy

Quote
What are you trying to solve?

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 Sad

Online Roquen
« Reply #9 - Posted 2014-02-14 08:41:27 »

@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.
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (26 views)
2014-07-24 01:59:36

Riven (25 views)
2014-07-23 21:16:32

Riven (19 views)
2014-07-23 21:07:15

Riven (22 views)
2014-07-23 20:56:16

ctomni231 (51 views)
2014-07-18 06:55:21

Zero Volt (46 views)
2014-07-17 23:47:54

danieldean (37 views)
2014-07-17 23:41:23

MustardPeter (40 views)
2014-07-16 23:30:00

Cero (56 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!