OpenGL - Water Shaders 'following' camera.

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

OpenGL - Water Shaders 'following' camera.

Postby adamski433 » Thu Mar 22, 2018 7:56 pm

Hello all. Just wanted to say I'm a long timer lerker, first time poster. I've been beating my head against the desk for the better part of the day on this...

First thing's first, here's my problem: I have some water reflection shaders in my 2D game that I'm happy with, however when I transition rooms and my camera moves, the water effects look like they are 'stuck' to the camera rather than being fixed-points in the world.

Image
https://i.imgur.com/KGYE2ab.gifv

Now I'll mention that I'm a real beginner when it comes to shaders. I feel like I got lucky getting to where I did. I'm using the libgdx-contribs/postprocessing library found here: https://github.com/manuelbua/libgdx-con ... processing for some effects like bloom, vignette, etc. For consistency, I have my water effect/filter extend the PostProccesorEffect and Filter from there.

Basically I'm just drawing my 'Water' layer to an FBO, passing it through the PostProccessorEffect's shader, then drawing it to the screen.

Here is my vertex shader:
Code: Select all
attribute vec4 a_position;
attribute vec4 a_texCoord0;
varying vec2 vertex_uv;

 void main()
 {
    vertex_uv = vec2(a_texCoord0.x, a_texCoord0.y) ;
    gl_Position = a_position;
 }


and my fragment shader:
Code: Select all
varying vec2 vertex_uv;

uniform sampler2D u_texture0;
uniform sampler2D u_texture_displacement;
uniform sampler2D u_texture_displacement2;
uniform float time;

void
main (void)
{
vec4 color = vec4(1.0,1.0,1.0,1.0);
 /*
     Code grabbing displacement map values over time, then slightly brightening 'color' accordingly
  */

 gl_FragColor      = texture2D(u_texture0, vertex_uv)*color;

}


a_position and a_texCoord0 come from the postprocessing library's FullscreenQuad.java : https://github.com/manuelbua/libgdx-con ... nQuad.java which is used for the PostProcessorEffect's.

My guess is that I need to make some changes in the vertex shader. I've sort of blindly tinkered with a few things, like multiplying gl_Position by the camera's projection matrix, but nothing seems to work. It's either off the screen entirely, or stuck to the camera.

Could somebody point me in the right direction? There is probably something obvious I'm missing.

EDIT: grammar and missing sentence.
adamski433
 
Posts: 2
Joined: Thu Mar 22, 2018 7:32 pm

Re: OpenGL - Water Shaders 'following' camera.

Postby tenfour04 » Fri Mar 23, 2018 12:52 am

I haven't used that library and you omitted some of your code, so all I can say is, you need to take the camera's position, divide x and y by the camera's viewportWidth and viewportHeight, negate the values, and pass that as a uniform vec2 to your shader. In your frag shader, you can add that to the UVs before using them in the last line.
tenfour04
 
Posts: 1191
Joined: Sat Jun 18, 2011 3:24 pm

Re: OpenGL - Water Shaders 'following' camera.

Postby adamski433 » Fri Mar 23, 2018 3:17 pm

It's working now :D

I passed along the offset (negated values of camera position / camera viewport) and added them to vertex_uv at the end of the fragment shader. That didn't work on it's own however, but I felt that was the right way to go. So before that, in the vertex shader, I subtract that offset from vertex_uv. I'd be lying if I said I understood exactly how that worked, but at this point I'm happy with it. It seemed to do the trick and it follows along nicely.


Thanks for the tip!
adamski433
 
Posts: 2
Joined: Thu Mar 22, 2018 7:32 pm


Return to General Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron