Water ripples

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;
}

Well, when I use this code I get perfect reflections…only when the camera is pointed straight down at the water plane:

	T = gl_NormalMatrix * (nmat *  gl_MultiTexCoord2.xyz);
	B = gl_NormalMatrix * (nmat *  gl_MultiTexCoord3.xyz);
	N = gl_NormalMatrix * (nmat *  gl_Normal);

It works perfect now. I am so smart! I am so smart! S-M-R-T!

good for you :smiley:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.