Reflectionmap question

I am trying to translate it from the python example on the forum.
Why would it change anything if I see a running python example? I have posted my code and the link to the python example. I can post you a link to a simple test project for this issue if that helps. Looking at my code on the paper it should really not be a problem but somewhere is a knot i cant figure out. I am not very used to graphics programming so thats why some questions might seem a bit obvious.

ok I have done another test… this to me looks like its rendering a reflection of a reflection of a reflection etc …

any ideas what I can try to verify this theory or even fix it ? maybe I am overlooking something really obvious ?

Putting it in Python allows you to run an already-working example. And then you can step through and change things, one at a time, and see what happens. You can also look at variables and scene graph settings live and manipulate them on-the-fly. It’s just a suggestion on my part, of course; it is perfectly possible to continue solely with one half of the translation available, as you’re doing, but I think you’re doing it the hard way.

As you note, you are seeing the reflection of the reflection, because your mirror camera is also seeing the reflection itself. The clip plane might have stopped this, but only if the reflection polygon is entirely behind the clip plane (and technically it should actually be exactly coplanar, which means it won’t be entirely behind). Most people avoid this by using camera masks to hide the reflection polygon from the mirror camera.

David

okay I added the bitmask as you suggested and that worked. I am still convinced that something is wrong with my clip-plane (as in it doesn’t work at all) because I tried to add another ball below the water plane (-25units) and now it reflects that ball as well. I tried also changed the clip plane to be a couple of units above the water plane but that didn’t work either.

here a temporary picture of how the reflection looks (without the clip-plane problem).

I’ll try to do some tests and update here about the clip-plane issue tomorrow.

hello I didn’t have time the last couple of days to look at this but now I am back on it and there is still 2 issues that appear to be the problem.

first of all my reflection image seems to behave correctly when it comes to position of the balls on the water but the image it produces is wrong because it reflects what the “main camera” sees

here a picture to make it more clear - note the orange arrows - that should really show the balls from below rather than from the camera view.
The snapshot was taken at frameX so just while running the code normally.

also note that the reflection camera appears to be in the right position (-z and oppoite pitch and rotation)

I associate the rendered texture with a display region which I setup as follow :

	gMyBuffer = gTheWindow->get_graphics_output()->make_texture_buffer("My Buffer", 512, 512);

	//setup the reflection camera
	PT(PerspectiveLens) pLens = new PerspectiveLens();
	pLens->set_fov(39.0f);
	DisplayRegion* pRender3D = gTheWindow->get_display_region_3d();
	float w = pRender3D->get_pixel_width();
	float h = pRender3D->get_pixel_height();
	float aspect_ratio = w / h;
	pLens->set_aspect_ratio(aspect_ratio);
	pLens->set_near_far(1.0f, 5000.0f);
	PT(Camera) pReflectionCam = new Camera("ReflectionCamera", pLens);
	pReflectionCam->set_camera_mask(BitMask32::bit(1));
	gReflCamNP = root.attach_new_node(pReflectionCam);

	gWaterGridNode.hide(BitMask32::bit(1));

	PT(DisplayRegion) displayRegion = gMyBuffer->make_display_region();
	displayRegion->set_camera(gReflCamNP);

	mReflectionMap = gMyBuffer->get_texture();
	mReflectionMap->set_wrap_u( Texture::WM_clamp);
	mReflectionMap->set_wrap_v( Texture::WM_clamp);
	mReflectionMap->set_minfilter(Texture::FT_linear);
	mReflectionMap->set_magfilter(Texture::FT_linear);

from what I can tell from the above code/numbers this is what the scene looks like

anyone has an idea why the reflection doesnt seem to be rendered from the reflection camera point of view but rather from the defaultCam :question:

ok I have now created a simpler water project just so I can get a decent reflection going without all the bling…

anyone can download it from here https://docs.google.com/open?id=0B3IV9TQwnFeuQWg2MEV3VXZaUTg

its meant to be a simple port from this python code posted a while back by the user “Flavio” in this thread [url]Water Reflections]

I am not sure everything is setup as it should be but there is a reflection texture. The texture presents the same problem that I have in my other project. The reflection itself just seems to reflect the wrong side of the geometry it is trying to reflect (to test this just rotate the camera in various directions and observe the reflection on the plane).

if anyone can help me solve this mistery you wouldnt just do me a huge favour but also contribute to share this code the the panda community once it works.

I found the problem!!

the reflection matrix returned from a plane sets the scale to 1,1,-1 this causes all the camera problems for the reflection camera!

if I reset the scale to 1,1,1 after setting the matrix of the reflection camera it all works as intended!

the update function looks like this now

void RenderWater()
{
	UpdateWater();
	//UpdateWaterMaps();

	NodePath cameras = gTheWindow->get_camera_group();
	NodePath defaultCam = cameras.get_child(0);

	LMatrix4f reflectionMat = gWaterPlane.get_reflection_mat();
	LMatrix4f camMat = defaultCam.get_mat(gTheWindow->get_render());

	LMatrix4f reflCamMat = camMat * reflectionMat;

	LVector3f camPos = defaultCam.get_pos(gTheWindow->get_render());

	gReflCamNP.set_mat(reflCamMat);
	gReflCamNP.set_scale(1,1,1);

	//gRefrCamNP.set_mat(gTheWindow->get_render().get_mat());

	UpdateWaterParameters();
}
 

here is the proof! Note the upside down smileys in the reflection.

and a close up

I think this needs some investigation of the panda devs as well as this might well be a bug in the panda code.

oh and as an update to flavios sample shader ,the shader needs be adjusted to get the correct texture loopup - I think he compensated in some way maybe not realising that the reflection just looked wrong (or maybe there really is a bug that got introduced with a later version of panda).

anyway … adjust his shader like this (together with the scaling issue) and it will fix his sample as well.

       // projective matrix (MR) 
       float4x4 scaleMatrix = { 0.5f, 0.0f, 0.0f, 0.5f, 
								0.0f, -0.5f, 0.0f, 0.5f, 
                                0.0f, 0.0f, 0.5f, 0.5f, 
                                0.0f, 0.0f, 0.0f, 1.0f }; 
      float4x4 matMR = mul(scaleMatrix, mat_modelproj); 

(note the -0.5) in the scaling matrix .

I’m not convinced that you’ve found the right solution. A reflection map should have a scale to -1 in it; that’s what makes it a reflection. However, this scale to -1 has other confounding side-effects; in particular, it reverses the vertex winding order of all of your polygons, which means that you will turn everything inside-out if you don’t correct for it with M_cull_counterclockwise or the equivalent; and this will maybe make your spheres seem un-mirrored again.

Try it with some geometry other than smileys.

David

hi drwr, I have take some screenshots for you with the standard panda models available.

and some close-ups

and

then last I immerged the panda dude half into the water

to me the reflections look correct like this.