Changing the rotation of SimpleWaterProcessor

Hi,



I have been looking at SimpleWaterProcessor. While the example code works fine (where the water sits on the x-z plane), as soon as I try to rotate the water plane, the reflection goes nuts, as can be seen in the following image:





After looking into the code for a while, I found that inside the postQueue() method of the SimpleWaterProcessor class, the normal for the reflection camera is set as follows:



[java]

reflectionCam.lookAt(targetLocation, Vector3f.UNIT_Y);

[/java]



This explains why the reflection only works on the x-z plane, the normal is always set to the UNIT_Y vector.



If we recalculate the normal of the relfectionCam, then rotation would work properly, as follows:

[java]

// tempVec and calcVect are just temporary vector3f objects

tempVect.set( sceneCam.getLocation() ).addLocal( sceneCam.getUp() );

float planeDistance = plane.pseudoDistance( tempVect );

calcVect.set(plane.getNormal()).multLocal( planeDistance * 2.0f );

camReflectUp.set( tempVect.subtractLocal( calcVect ) ).subtractLocal( loc ).normalizeLocal().negateLocal();



// now set the up vector

reflectionCam.lookAt(targetLocation, camReflectUp);

[/java]

1 Like

Nothing is going nuts, you have to update the reflection plane when you move the spatial.

I did update the reflection plane. This is the code I used to rotate the waterPlane:



[java]

waterPlane.setLocalRotation(new Quaternion().fromAngleAxis(FastMath.HALF_PI, Vector3f.UNIT_X));

waterProcessor.setPlane(new Plane(new Vector3f(0,0,1), 0));

[/java]



The bug is with the SimpleWaterProcessor where the up vecotor of the reflectionCam is not set properly.

1 Like

Oops, yeah, you’re right. Sorry… somehow I got distracted by the layout and ignored the code… Thanks, I will fix that. :slight_smile:

1 Like

Thanks! This will work great as a mirror :slight_smile:

Ok, I added your fix pretty much as it was in svn revision 7147, thanks again.

Edit: Narf, the temp vars are already used in lookAt… have to create new ones… Wait for rev 7148 :wink:



Yes, I always wanted to add a stripped down version of the shader just for mirrors, but you should be able to configure this already so that it will yield a good mirror, and the shader is really not very resource hungry as it is.

nice thanks