Sign in to follow this  
Sam Segura

[Resolved] Directx9 Reflection Water

Recommended Posts

Hello everybody, 

I want to add reflection on my water but i have a problem with it. 

I try to add an inverted view on my water, but i see black water when i try that.

 

	D3DMATERIAL9 mtrl;
	ZeroMemory(&mtrl, sizeof(mtrl));

	D3DLIGHT9 light;
	ZeroMemory(&light, sizeof(light));
	light.Type = D3DLIGHT_DIRECTIONAL;

	light.Specular.r = 1.0f;
	light.Specular.g = 1.0f;
	light.Specular.b = 1.0f;
	light.Specular.a = 1.0f;

	light.Range = 1000;
	light.Falloff = 0;
	light.Attenuation0 = 1;
	light.Attenuation1 = 0;
	light.Attenuation2 = 0;

	mtrl.Specular.r = 1.5f;
	mtrl.Specular.g = 1.5f;
	mtrl.Specular.b = 1.5f;
	mtrl.Specular.a = 1.5f;
	mtrl.Power = 20;

	D3DXMATRIX oldView; // Ancienne vue.
	D3DXMATRIX oldWorld; // Ancien monde.
	D3DXMATRIX oldProj; // Ancienne projection.
	D3DXPLANE plane(0.0f, 1.0f, 0.0f, 0.0f); // Plane d'inversion.
	D3DXMATRIX inversion;  // Matrix d'inversion.
	D3DXMatrixReflect(&inversion, &plane); // inversion calculé.

	D3DXMATRIX matEauViewReflection; // matrix de la vue de la réflection de l'eau.
	D3DXMATRIX matEauWorldReflection; // matrix du monde de la réflection de l'eau.
	D3DXMATRIX matEauProjReflection; // matrix de la projection de la réflection de l'eau.

	STATEMANAGER.GetTransform(D3DTS_VIEW, &oldView); // récupération de la vue.
	STATEMANAGER.GetTransform(D3DTS_WORLD, &oldWorld); // récupération du monde.
	STATEMANAGER.GetTransform(D3DTS_PROJECTION, &oldProj); // récupération de la projection.

	matEauViewReflection = oldView; // enregistrement de l'ancienne vue dans la nouvelle.
	matEauWorldReflection = oldWorld; // enregistrement de l'ancien monde dans le nouveau monde.
	matEauProjReflection = oldProj; // enregistrement de l'ancienne projection dans la nouvelle.

	D3DXMatrixInverse(&matEauViewReflection, NULL, &matEauViewReflection);
	D3DXMatrixInverse(&matEauWorldReflection, NULL, &matEauWorldReflection);
	D3DXMatrixInverse(&matEauProjReflection, NULL, &matEauProjReflection);

	D3DXMatrixScaling(&matEauViewReflection, m_fWaterTexCoordBase, -m_fWaterTexCoordBase, 0.0f); // Calcule de la taille de la vue.
	D3DXMatrixScaling(&matEauWorldReflection, m_fWaterTexCoordBase, -m_fWaterTexCoordBase, 0.0f); // Calcule de la taille du monde.
	D3DXMatrixScaling(&matEauProjReflection, m_fWaterTexCoordBase, -m_fWaterTexCoordBase, 0.0f); // Calcule de la taille de la projection.


	/*D3DXMatrixMultiply(&matEauViewReflection, &inversion, &matEauViewReflection); // Calcule de la vue d'inversion de la vue de l'eau.
	D3DXMatrixMultiply(&matEauWorldReflection, &inversion, &matEauWorldReflection); // Calcule de la vue d'inversion du monde de l'eau.
	D3DXMatrixMultiply(&matEauProjReflection, &inversion, &matEauProjReflection); // Calcule de la vue d'inversion de la projection de l'eau.
	*/
	STATEMANAGER.SaveTransform(D3DTS_VIEW, &matEauViewReflection); // Application de la vue lors du render.
	STATEMANAGER.SaveTransform(D3DTS_WORLD, &matEauWorldReflection); // Application du monde lors du render.
	STATEMANAGER.SaveTransform(D3DTS_PROJECTION, &matEauProjReflection); // Application de la projection lors du render.

	STATEMANAGER.SaveRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	STATEMANAGER.SaveRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	STATEMANAGER.SaveRenderState(D3DRS_LIGHTING, TRUE);
	STATEMANAGER.SaveRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
	STATEMANAGER.SaveRenderState(D3DRS_SPECULARENABLE, TRUE);
	STATEMANAGER.SaveMaterial(&mtrl);
	STATEMANAGER.SetLight(0, &light);
	STATEMANAGER.m_lpD3DDev->LightEnable(0, TRUE);

	STATEMANAGER.SaveRenderState(D3DRS_STENCILENABLE, TRUE);
	STATEMANAGER.SaveRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
	STATEMANAGER.SaveRenderState(D3DRS_STENCILREF, 0x1);
	STATEMANAGER.SaveRenderState(D3DRS_STENCILMASK, 0xffffffff);
	STATEMANAGER.SaveRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff);
	STATEMANAGER.SaveRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
	STATEMANAGER.SaveRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
	STATEMANAGER.SaveRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
	STATEMANAGER.SaveRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
	STATEMANAGER.SaveRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

	std::vector<std::pair<float, long> >::iterator i;

	for (i = m_PatchVector.begin(); i != m_PatchVector.end(); ++i)
	{
		DrawReflection(i->second);
	}


	for (i = m_PatchVector.begin(); i != m_PatchVector.end(); ++i)
	{
		DrawReflection(i->second);
	}

	//////////////////////////////////////////////////////////////////////////
	// RenderState

	STATEMANAGER.RestoreRenderState(D3DRS_DIFFUSEMATERIALSOURCE);
	STATEMANAGER.RestoreRenderState(D3DRS_COLORVERTEX);
	STATEMANAGER.RestoreRenderState(D3DRS_ZWRITEENABLE);
	STATEMANAGER.RestoreRenderState(D3DRS_ALPHABLENDENABLE);
	STATEMANAGER.RestoreRenderState(D3DRS_CULLMODE);
	STATEMANAGER.RestoreRenderState(D3DRS_LIGHTING);
	STATEMANAGER.RestoreRenderState(D3DRS_SPECULARENABLE);
	STATEMANAGER.RestoreRenderState(D3DRS_STENCILENABLE);
	STATEMANAGER.RestoreRenderState(D3DRS_STENCILFUNC);
	STATEMANAGER.RestoreRenderState(D3DRS_STENCILREF);
	STATEMANAGER.RestoreRenderState(D3DRS_STENCILMASK);
	STATEMANAGER.RestoreRenderState(D3DRS_STENCILWRITEMASK);
	STATEMANAGER.RestoreRenderState(D3DRS_STENCILZFAIL);
	STATEMANAGER.RestoreRenderState(D3DRS_STENCILFAIL);
	STATEMANAGER.RestoreRenderState(D3DRS_STENCILPASS);
	STATEMANAGER.RestoreRenderState(D3DRS_SRCBLEND);
	STATEMANAGER.RestoreRenderState(D3DRS_DESTBLEND);
	STATEMANAGER.RestoreMaterial();
	STATEMANAGER.m_lpD3DDev->LightEnable(0, FALSE);
	STATEMANAGER.SetTransform(D3DTS_VIEW, &oldView);
	STATEMANAGER.SetTransform(D3DTS_WORLD, &oldWorld);
	STATEMANAGER.SetTransform(D3DTS_PROJECTION, &oldProj);

And I draw it with primitive like that:

	STATEMANAGER.SetStreamSource(0, pkVB->GetD3DVertexBuffer(), sizeof(SWaterVertex));
	STATEMANAGER.DrawPrimitive(D3DPT_TRIANGLELIST, 0, uPriCount);

I try some other path , but i get some strange view.

Thank in advance for the help.
 

Edited by Herra07

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this