Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Wave Shader  (Read 5641 times)

0 Members and 1 Guest are viewing this topic.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Wave Shader
« on: October 14, 2012, 02:08:17 pm »
Hi I would like to apply a wave shader (just like the one in the examples) to a texture. But since a texture only has 4 vertices I don't think I can use the one from the examples. I'm not very good at shader programming, so I don't really know how to implement the effect as a fragment shader... can anybody help me out?

Another small question: What's the name of the blur effect that is used in the example?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Wave Shader
« Reply #1 on: October 14, 2012, 03:32:36 pm »
Quote
But since a texture only has 4 vertices
Texture and geometry are two different things. You can map a texture on a 4-vertex sprite, but also on a subdivided quad with a lot more vertices.

Quote
What's the name of the blur effect that is used in the example?
What do you mean?
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Wave Shader
« Reply #2 on: October 14, 2012, 03:41:05 pm »
Yes but to do that I would have to either use a geometry shader to subdivide my rendertexture or I would have to implement a class that displays a texture, but has a lot more quads. To specify my question: Does anybody know how to achieve the effect with a fragment shader?

What do you mean?
Well what kind of a blur is it? (i.e.: gaussian blur,...?)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Wave Shader
« Reply #3 on: October 14, 2012, 03:47:18 pm »
Why do you think it is so complicated? Just create your own vertex array, with all the vertices you need in it, and map your texture on it. Very easy.

Quote
Does anybody know how to achieve the effect with a fragment shader?
When you compute pixel (x, y) in your shader, instead of looking up pixel (x, y) in the source texture, lookup pixel (x + a, y + b) where (a, b) are coefficients that simulate a wave.

Quote
Well what kind of a blur is it? (i.e.: gaussian blur,...?)
Yes, a Gaussian blur.
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Wave Shader
« Reply #4 on: October 14, 2012, 10:36:36 pm »
Ok thanks. I'll try and implement a fragment shader myself. But I got another question: in your vertex shader, why do you use cos(calculations) and add sin(calculations) * 0.3 to it?

Yes, a Gaussian blur.
Great, thanks. Is it a vertical and a horizontal gaussian blur? And if it is are the step after each other or is it a bilinear gaussian blur?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Wave Shader
« Reply #5 on: October 14, 2012, 11:09:53 pm »
Quote
But I got another question: in your vertex shader, why do you use cos(calculations) and add sin(calculations) * 0.3 to it?
That's the wave equation. Can you be more specific?

Quote
Great, thanks. Is it a vertical and a horizontal gaussian blur? And if it is are the step after each other or is it a bilinear gaussian blur?
Why are you asking so many questions? ;D
The code reads neighbour pixels, and makes a weighted average of them. I just used coefficients that made it look ok. There's really nothing else to know.
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Wave Shader
« Reply #6 on: October 15, 2012, 08:11:28 am »
I'm sorry I didn't mean to bother or annoy you. It's just that I haven't worked with blur before and your Gaussian blur shader looks different from the other implementations that I've seen. Like this one http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/ for example. So I was wondering what type it is and why it looks different.

About the wave equation. It's pretty much the same thing. I haven't worked with a wave shader before. The only explanation I found was this one http://www.gamedev.net/topic/607082-sine-wave-shader/ (scroll to the bottom). And that one only uses sin. So I was wondering why yours uses sine and cosine. Could you explain that to me?

Thanks for the help!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Wave Shader
« Reply #7 on: October 15, 2012, 08:41:33 am »
Quote
I'm sorry I didn't mean to bother or annoy you
You don't :)

I just mean that you should probably look at my shaders (they are really simple to understand) and start experimenting with your own formulas, rather than asking so many questions first. I can't answer all your questions, because when I wrote these shaders I just did a lot of tests and adjustements to make them look good. I didn't try to understand what kind of Gaussian blur or wave equation I used.

Don't take these formulas too seriously, they are just hacks. The important thing to remember is that:
- a blur effect works by taking a weighted average of neighbour pixels
- a wave formula is made of one or more cos/sin terms (using more terms makes the wave less "symetric")

Which coefficients you use is really up to you, and are generally found by testing and hacking.
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Wave Shader
« Reply #8 on: October 15, 2012, 10:44:13 pm »
Thanks that's exactly what I needed to know. When I start on a new topic, I usually look at existing code. That's why I looked at the examples. I didn't understand if you implemented a certain algorithm or if you where just hacking till it looked good. That's why I was asking.
Thanks for clearing it up and the useful information. I will read some more about blur techniques then and experiment with the sin equation then.
On thing I still don't understand and I couldn't find an explanation on the internet for. How does adding a cosine wave make the wave less "symetric"?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Wave Shader
« Reply #9 on: October 15, 2012, 11:00:39 pm »
cos and sin are the same, they are just offseted by Pi/2, so it doesn't make a big difference here. What's important is the number of coefficients: the more sin/cos terms you add to the equation, the less it looks like a regular sine curve.
Laurent Gomila - SFML developer

 

anything