Problem with water refraction

Started by
3 comments, last by Master of Riddles 17 years, 9 months ago
I'd like to add refractions to my water. Underneath I submitted code I use for rendering reflections. I thought that to do refractions it would be enough to invert m_plClip normal but then my refractions are moving with the camera. Proper ones are however visible below the water surface. I'd be grateful for any suggestions what's wrong.

m_matNewView=matView;
D3DXMatrixIdentity(&m_matReflect);

float fLevel=woda->GetWaterLevel();

D3DXPlaneFromPointNormal(&m_plReflect,						             &D3DXVECTOR3(0.0f, fLevel, 0.0f),
			       &D3DXVECTOR3(0.0f, 1.0f, 0.0f));

D3DXPlaneFromPointNormal(&m_plClip,
                         &D3DXVECTOR3(0.0f, fLevel, 0.0f),
                         &D3DXVECTOR3(0.0f, -1.0f, 0.0f));

D3DXMatrixReflect(&m_matReflect, &m_plReflect);
matView=m_matReflect*matView;

D3DXPlaneNormalize(&m_plClip, &m_plClip);

D3DXMatrixInverse(&m_mView, NULL, &m_matNewView);
D3DXMatrixTranspose(&m_mView, &m_mView);
D3DXMatrixInverse(&m_mProj, NULL, &matProj);
D3DXMatrixTranspose(&m_mProj, &m_mProj);
D3DXPlaneTransform(&m_plClip, &m_plClip, &(m_mView*m_mProj));

D3DDev->SetClipPlane(0, (float*)m_plClip);
D3DDev->SetRenderState(D3DRS_CLIPPLANEENABLE, D3DCLIPPLANE0);
Advertisement
Anyone any ideas?
Here is how I render my ocean:

void DX9_Water::Render(DX9_Texture *tex){	// MATERIAL	GFX.lpDevice -> SetMaterial(&material);	// REFLECTION	GFX.lpDevice -> SetTexture(1, lpReflectionTexture);	GFX.lpDevice -> SetTextureStageState(1,D3DTSS_TEXCOORDINDEX,D3DTSS_TCI_CAMERASPACEPOSITION);	GFX.lpDevice -> SetTextureStageState(1,D3DTSS_TEXTURETRANSFORMFLAGS,D3DTTFF_COUNT3 | D3DTTFF_PROJECTED);    GFX.lpDevice -> SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE );    GFX.lpDevice -> SetTextureStageState(1, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );	// REMAP	if(GFX.KeyDown(DIK_K))RE-=0.01f;	if(GFX.KeyDown(DIK_L))RE+=0.01f;	GFX.lpDevice -> SetTransform(D3DTS_TEXTURE1, &matRemap);	// BUMPWAVES	tex->Use(0);	GFX.lpDevice -> SetTextureStageState(0,D3DTSS_TEXCOORDINDEX,0); 	// Left right    GFX.lpDevice -> SetTextureStageState(0, D3DTSS_BUMPENVMAT00,   F2DW(0)); //xscale    GFX.lpDevice -> SetTextureStageState(0, D3DTSS_BUMPENVMAT10,   F2DW(0)); //xpos	// Up down    GFX.lpDevice -> SetTextureStageState(0, D3DTSS_BUMPENVMAT01,   F2DW(-fBlurryFactor)); // ypos    GFX.lpDevice -> SetTextureStageState(0, D3DTSS_BUMPENVMAT11,   F2DW(-fBlurryFactor)); // yscale	GFX.lpDevice -> SetTextureStageState(0, D3DTSS_BUMPENVLSCALE,  F2DW(1.0f) );    GFX.lpDevice -> SetTextureStageState(0, D3DTSS_BUMPENVLOFFSET, F2DW(0) );    GFX.lpDevice -> SetTextureStageState(0, D3DTSS_COLOROP,   D3DTOP_BUMPENVMAPLUMINANCE );	GFX.lpDevice -> SetTextureStageState(0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1);	GFX.lpDevice -> SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);	GFX.lpDevice -> SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE );    GFX.lpDevice -> SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT );	// NO CULL	GFX.lpDevice -> SetRenderState( D3DRS_CULLMODE,         D3DCULL_NONE );	// TRANSPARENCY    GFX.lpDevice -> SetRenderState(D3DRS_ALPHABLENDENABLE, 1);	GFX.lpDevice -> SetRenderState(D3DRS_LIGHTING, 1);	GFX.lpDevice -> SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);	GFX.lpDevice -> SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);	GFX.lpDevice -> SetFVF(VertexFVF);	GFX.lpDevice -> DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, dwNumVertices, dwNumIndices / 3,								   indices,D3DFMT_INDEX16, vertices, sizeof(Vertex));		GFX.dwPolygonsRendered += dwNumIndices / 3;	GFX.lpDevice -> SetRenderState( D3DRS_CULLMODE,         D3DCULL_CCW );	GFX.lpDevice -> SetTextureStageState(0,D3DTSS_TEXTURETRANSFORMFLAGS,D3DTTFF_DISABLE);	GFX.lpDevice -> SetTextureStageState(0,D3DTSS_TEXCOORDINDEX,0);	GFX.lpDevice -> SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_DISABLE);	GFX.lpDevice -> SetTextureStageState( 2, D3DTSS_COLOROP,   D3DTOP_DISABLE );}


Try it, and say what you like. Though I'm having problems with that. You'll notice them when you try... :)
Wow, that's a lot of fixed function stuff. I bet that would be much simpler to do in a shader. But then it wouldn't support those older cards right.

As to the original question, are you using that same clip plane to render everything? You are transforming it into clip space, which is only appropriate for objects drawn with shaders. With FFP, you have to specifiy the clip plane in world space.
Yes I know and that's why I transform it that way - in my app I draw everything using shaders. Forgot to mention that.

This topic is closed to new replies.

Advertisement