I got basic reflection working, and now I am trying to add ripples using a normal map. However, the additional of the ripple normal vector does not seem to be correct. The reflection moves around according to the camera rotation, which it should not.
I think the problem is that my TBN values are calculated relative to the water plane’s matrix, and are not in view space. How can I fix this? Thanks.
Reflection.vert:
#ifdef LW_INSTANCED
#extension GL_EXT_gpu_shader4 : enable
#extension GL_EXT_bindable_uniform : enable
bindable uniform mat4 InstanceMatrix[ LW_MAX_PASS_SIZE ];
#endif
uniform float aspect;
varying vec4 projectSpace;
varying vec2 texcoord;
varying vec3 T,B,N;
varying vec4 ModelVertex;
uniform mat4 MeshMatrix;
void main(){
mat4 mat;
#ifdef LW_INSTANCED
#ifndef LW_SKIN
mat = InstanceMatrix[gl_InstanceID];
#endif
#endif
#ifndef LW_INSTANCED
#ifndef LW_SKIN
mat = MeshMatrix;
#endif
#endif
mat3 nmat = mat3(mat[0].xyz,mat[1].xyz,mat[2].xyz);
ModelVertex = gl_Vertex;
ModelVertex = mat * ModelVertex;
gl_FrontColor=gl_Color;
projectSpace = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_Position = projectSpace;
projectSpace.xy = (projectSpace.xy + vec2(projectSpace.w)) * 0.5;
texcoord=gl_MultiTexCoord0.xy;
T = normalize( nmat * gl_MultiTexCoord2.xyz );
B = normalize( nmat * gl_MultiTexCoord3.xyz );
N = normalize( nmat * gl_Normal );
}
Reflection.frag:
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform float AppTime;
varying vec3 T,B,N;
varying vec4 projectSpace;
varying vec2 texcoord;
varying vec4 ModelVertex;
void main(){
vec3 normal;
vec4 bumpcolor;
bumpcolor = texture2D(texture1,vec2(texcoord.x*4.0,texcoord.y*4.0 + AppTime / 10000.0));
normal = normalize(bumpcolor.xyz - 0.5);
normal = T * normal.x + B * normal.y + N * normal.z;
gl_FragColor = texture2DProj(texture0, projectSpace + vec4(normal.x,normal.y,normal.z,0.0) ) * gl_Color;
}