3

I've read nearly all ocean animation topics(programming and math too), and finally I decided to render it with Gerstner waves with reflection, refraction and caustics. Well, now my reflection is working with flat plane and with only vertical displacements,but with Gerstner waves I displace the x,z coords too, and my reflection texture coordinates goes out of range when my camera is under a specific height or with changing the angle. (the closer the ocean surface is, the more the texture wraps)

So, my shader codes: Gerstner Wave:

vec3 calcWave(vec2 X,float t,float A,vec2 K,float L)
{
vec3 wave;
float k = 2*pi/L;
float W = sqrt(g*k);
wave.xz = -((K/k)*A*sin(dot(K,X)-W*t));
wave.y = A*cos(dot(K,X)-W*t)/2-A/2;
//I do it this way so the max wave amplitude is always
//lower than the plane of the reflection, so I can't see bellow it
return wave;
};

Texture Coordinates

VERTEX SHADER:

undisplaced_world_Vertex.xzw=world_Vertex.xzw;
//this is before the wave calculation, so it contains simple grid coordinates without any displacement
undisplaced_world_Vertex.y = waterLevel;

FRAGMENT SHADER:

vec4 screenCoord = mvp_matrix*undisplaced_world_Vertex;
vec2 projCoord=vec2((screen_coord.x/screen_coord.w+1)/2,(screen_coord.y/screen_coord.w+1)/2);

I've just read the reflection part in Deep-Water Animation and Rendering article, but i have no idea how to implement it.

My question is how to project the texture coordinates of the reflection texture, so it fits my 2 expectations:

  • the texture coordinates are always in range, or there is a minimal wrap at the edge of the screen
  • from every angle I can't see "under" the texture (or just a very little:P)

Also, it will be displaced with the normals.

EDIT:

Problems with understanding:

vec3 R = ReflectLeave(viewerDir,norma­l);
float4 projR = float4(Rh,0)*ReflViewProjT­M;
float2 reflUV = (projR.xy / projR.z) * float2(0.5,-0.5)+float2(0.5,0.5);
half4 refl = tex2D(reflTex,reflUV);

:/

David Szalai
  • 2,363
  • 28
  • 47
  • One extraordinarily cheap hack you could try is using a Mirrored Repeat texture wrap mode. This comes in handy for a lot of post-processing effects that suffer a similar problem, like SSAO. It does not actually solve the problem, but to the human eye the mirrored wrapping behavior often looks convincingly natural; humans are stupid. – Andon M. Coleman Oct 12 '13 at 05:41
  • thanks, that works, and yesterday I got this code: /*vec3 R = ReflectLeave(viewerDir,norma­l); float4 projR = float4(Rh,0)*ReflViewProjT­M; float2 reflUV = (projR.xy / projR.z) * float2(0.5,-0.5) + float2(0.5,0.5); half4 refl = tex2D(reflTex,reflUV);*/ but its not clear, reflectleave, Rh, reflviewprojTM-are they reflect(...), R as in the first row and mvp matrix reflected over the plane? – David Szalai Oct 12 '13 at 07:47
  • Sorry, I am having difficulty reading that message in comment form - could you maybe edit your question with this new update? – Andon M. Coleman Oct 12 '13 at 08:45
  • sure, it is from a youtuber who has pretty nice looking water, like in crysis 1 at the ocean. He/she said its from Sobo/2K Czech. – David Szalai Oct 12 '13 at 19:23
  • You know, I really do not know what this is. `reflect (...)` is part of Shader Model 1.0 in HLSL, so there is no need to ever re-invent the wheel. `ReflectLeave (...)` must do something else, but without comments I could not say what. Since `R` is not referenced anywhere and mixes OpenGL-style vector notation with the rest of the code which is HLSL, it is even more puzzling :) – Andon M. Coleman Oct 12 '13 at 22:09

0 Answers0