Home » Community » Forums » DirectX and XNA » Problem with water reflections
Intel sponsors gamedev.net search:
Control Panel Register Bookmarks Who's Online Active Topics Stats FAQ Search

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 Problem with water reflections
Post New Topic  Post Reply 
I'm having a little problem with water reflections. I'm rendering the reflected scene into the texture. Then I'll render the scene normal way to the default back buffer. After this I'm drawing a screen size plane with the reflected scene texture applied. Everything works fine, except all the reflected scene now appears in front of everything rendered to the back buffer. The following screen will tell everything:

http://img.photobucket.com/albums/v734/Maskotti/example.jpg

I guess I have to use a stencil buffer to make this work, but I can't find the working stencil operations to eliminate the correct pixels from the screen size plane.

I'd be very grateful if someone could help me with this problem.

 User Rating: 1021   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Yes, you need to use the stencil buffer, or texture the water geometry with the reflection texture. If you use the stencil buffer, then you don't need to render to a texture, you just render the reflection straight to the scene (using stencil test).


John Bolton
Locomotive Games (THQ)
Current Project: Destroy All Humans (Wii). IN STORES NOW!


 User Rating: 1839   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by JohnBolton
Yes, you need to use the stencil buffer, or texture the water geometry with the reflection texture. If you use the stencil buffer, then you don't need to render to a texture, you just render the reflection straight to the scene (using stencil test).


The reason for rendering to the texture is indeed that I want later project this reflection texture onto water geometry. However, I need some instructions for projecting a screen-size texture correctly onto water planes...

Do you know more about texturing the water geometry with the reflection texture (making projection in fixed function pipeline)? :)

 User Rating: 1021   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Actually, I can forget the whole stencil buffer stuff if someone could just tell me how to project the texture onto water geometry using fixed function pipeline...

 User Rating: 1021   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by Maskotti
Actually, I can forget the whole stencil buffer stuff if someone could just tell me how to project the texture onto water geometry using fixed function pipeline...


I spent about five minutes writing a semi-verbose explanation for you, but then I remembered that Yann has already answered every questoin ever. Here is a link to a thread that should definitely help you out. The math will of course be slightly different due to the OpenGL / Direct3D differences, but this will be a good place to start looking. Feel free to ask for clarification if you have specific questions after you've read the thread.

Water Reflection and Refraction with normal map with out pixel shaders


Anthony Whitaker
[Boanerges Studios] :: [RZ2 Games] :: [Exigent Game Art] :: [Personal Site] :: [GraphicsDev.net]

 User Rating: 1353   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by Sages
I spent about five minutes writing a semi-verbose explanation for you, but then I remembered that Yann has already answered every questoin ever. Here is a link to a thread that should definitely help you out. The math will of course be slightly different due to the OpenGL / Direct3D differences, but this will be a good place to start looking. Feel free to ask for clarification if you have specific questions after you've read the thread.

Water Reflection and Refraction with normal map with out pixel shaders


Thanks for the tip, but I have already read that thread. I'll try to shortly describe what I'm doing at the moment:

First I'm setting the texture where the reflected scene is rendered to stage 0.

pDevice->SetTexture(0, m_pReflections);

At the water geometry rendering, I'm taking the view and projection matrices that I'm using for rendering the reflections into the texture.

D3DXMATRIX mResult, mView, mProject;
mView = CS3DWaterReflectionsRenderer::ReflectionView();
mProject = CS3DWaterReflectionsRenderer::ReflectionProjection();

Next I'm multiplying these matrices (I can go wrong here, but I guess this is the way to combine these two).

D3DXMatrixMultiply(&mResult, &mView, &mProject);

Then I'm setting the texture sampler and stage states that I guess should be ok.

pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
pDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
pDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3 | D3DTTFF_PROJECTED );

pDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);


Finally I'm setting the reflection matrix to the texture stage 0 and render the geometry.

pDevice->SetTransform(D3DTS_TEXTURE0, &mResult);
.
.
... rendering the water geometry...


I'm definitely going wrong somewhere because I'm getting badly warping surface on the water planes.

 User Rating: 1021   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Comments? Anyone?

 User Rating: 1021   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

I'm getting a little bit tired with this issue now. I've read several threads about projective textures and I get just horrible results.

I read about matrix you need for remapping the texture and I really think I should have everything to make it work. This is how the source code was changed:

First I create a new matrix for remapping.

D3DXMATRIX mRemap;
ZeroMemory(&mRemap, sizeof(D3DXMATRIX));
mRemap._11 = 0.5f;
mRemap._22 = 0.5f;
mRemap._33 = 0.5f;
mRemap._41 = 0.5f;
mRemap._42 = 0.5f;
mRemap._43 = 0.5f;
mRemap._44 = 1.0f;


And new multipliers look like this:


D3DXMatrixMultiply(&mResult, &mProject, &mView);
D3DXMatrixMultiply(&mResult, &mRemap, &mRemap);


And this is the result I get:

http://img.photobucket.com/albums/v734/Maskotti/example2.jpg

Doesn't look too fancy...

 User Rating: 1021   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Ok, I managed to get it work now.

I had to get inverse view matrix (from the non-reflective rendering) in addition to the other matrices I described earlier, and multiply them in the following order:

mTexture = mInverseView * mReflectView * mReflectProjection * mRemap

Where
mInverseView = the inverse view matrix of non-reflective scene rendering,
mReflectView = the view matrix from rendering the reflections to the texture,
mReflectProjection = the projection matrix from rendering the reflections to the texture,
mRemap = manually constructed matrix for remapping the texture coordinates from -1..1 to 0..1.

Thanks. :)

 User Rating: 1021   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

One more problem to solve. This one came with the bump mapping.

The following picture shows what's wrong this time:

http://img.photobucket.com/albums/v734/Maskotti/example3.jpg

First, with bump mapping the reflections take a small offset from the actual model they are reflecting. If you look the 'tower' in the middle of the picture, you'll see that the reflections take a small step to the left. The step is getting bigger if you roll the camera.

Another problem coming with bump mapping is that the reflections are stretched from the edges. This can be seen on the right side of picture where the tree reflections are getting sucked to the right.

Both of these problems occur only with bump mapping. Without bump mapping the reflections get and stay as they're supposed to.

So, please share your information if you have any good ideas how to solve these problems. :)

Thanks.

 User Rating: 1021   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

That might not be the easiest problem in the world to fix. You'll probably need to paste your shaders. The problem is simply that you're perturbing the texture coordinates of the projected scene texture incorrectly or too greatly.

The texture wrap mode appears to be set to clamp, that's causing the smear along the edge. But again if the UVs weren't changing so drastically, you wouldn't notice that error.

I really don't think I'll be able to help much without seeing your pixel shader.

Regards,


Anthony Whitaker
[Boanerges Studios] :: [RZ2 Games] :: [Exigent Game Art] :: [Personal Site] :: [GraphicsDev.net]

 User Rating: 1353   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Actually I'm not using any shaders on reflections rendering. I guess I should scale a projective texture a little bit larger so that it will fill the entire screen (the current projection leaves "empty space" to the right side and bottom of the screen that causes the stretching with texture clamping). I was wondering if this scale can be easily merged into the remapping matrix. Of course I could take another scale matrix and add it into multiply chain, but I'm looking for something nicer way to do it (as nice as possible :-) ).

This is how the final remapping matrix looks like:

D3DXMATRIX mRemap;
mRemap._11=0.5f;
mRemap._12=0.0f;
mRemap._13=0.0f;
mRemap._14=0.0f;
mRemap._21=0.0f;
mRemap._22=-0.5f;
mRemap._23=0.0f;
mRemap._24=0.0f;
mRemap._31=0.0f;
mRemap._32=0.0f;
mRemap._33=0.5f;
mRemap._34=0.0f;
mRemap._41=0.5f;
mRemap._42=0.5f;
mRemap._43=0.5f;
mRemap._44=1.0f;

 User Rating: 1021   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

I got rid of the stretching by setting the remapping scale values _11 and _22 from 0.5 to 0.48 and from -0.5 to -0.48. I'm not sure if this is a stable solution, but this far it looks the same but without stretching. So for now it's just fine to me. :)

Anyway, there's at least one more problem left. The reflections take noticeable offset when the camera rolls. I took two screen shots that show what I mean.

When the camera is horizontally aligned, everything is just fine:

http://img.photobucket.com/albums/v734/Maskotti/roll1.jpg


But when we roll the camera, the reflections seem to get closer taking them away from the geometry they're reflected from (leaving blank water between geometry and reflections):

http://img.photobucket.com/albums/v734/Maskotti/roll2.jpg


If the camera gets rolled on the other direction, the reflections seem to get further (opposite direction than in roll2.jpg picture) from the camera (to under the scene geometry). This doesn't look that bad because there won't appear any gap between actual geometry and reflections, but this problem still need to get fixed.

Edit: The same problem seems to appear with camera pitch.

[Edited by - Maskotti on September 22, 2005 5:15:23 PM]

 User Rating: 1021   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: