Sign in to follow this  
Nameles

Reflection troubles

Recommended Posts

Nameles    122
Hello forum! I’m trying to get reflections to work in DX9 and I’m having a bit of a problem. Right now my test code is in a modified D3D sample app. I’m using a right handed coordinate system with Z up and all I’m interested in is getting the little triangle to render both reflected and not reflected. When that’s done I’ll set up the render target and everything else but for now I just want to see the little polygon get reflected :). This is how I’m setting up the reflection.
void SetupMatrices( bool reflect )
{
	UINT  iTime  = timeGetTime() % 10000;
	FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 10000.0f;

	D3DXMATRIXA16 matWorld;
	D3DXMatrixRotationY( &matWorld, fAngle );

	D3DXVECTOR3 vEyePt( 0.0f, -10.0f, 3.0f );
	D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
	D3DXVECTOR3 vUpVec( 0.0f, 0.0f, 1.0f );

	D3DXMATRIXA16 matView;
	D3DXMatrixLookAtRH( &matView, &vEyePt, &vLookatPt, &vUpVec );

	if(reflect)
	{
		D3DXPLANE plane(0.0f, 0.0f, 1.0f, 0.0f);
		D3DXMATRIXA16 matReflect;
		D3DXMatrixReflect(&matReflect, &plane);
		matView = matView * matReflect;
	}

	D3DXMATRIXA16 matProj;
	D3DXMatrixPerspectiveFovRH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );

	g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
	g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
	g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
	// Turn off culling, so we see both original and reflected triangle
	g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
}


I call this once with reflect true and then draw the triangle, then again with reflect false and render it again. The non reflected poly is drawn spinning around the Y-Axis which is pointing away from the near plane. The reflected poly is not drawn at all. If I set the reflection plane to D3DXPLANE plane(0.0f, 1.0f, 0.0f, 0.0f); I see the reflected poly but its definitely not correct. What is wrong with this picture? Isn’t the plane I want to reflect around the XY plane with normal 0,0,1?

Share this post


Link to post
Share on other sites
matches81    474
have you tried building the reflection matrix for yourself?
For your plane (0.f, 0.f, 1.f, 0.f) it should be

/ 1 0 0 0
| 0 1 0 0 |
| 0 0 -1 0 |
\ 0 0 0 1 /

try that and see what happens.
I also tried to do some reflections using the D3DXMatrixReflect() function and didn´t get any working solution... You could also see and check if the matrix created by that function looks right.

Share this post


Link to post
Share on other sites
Armadon    1091
The D3DXMatrixReflect works great,
It builds a matrix that could be used to then render your object as a reflection.
I wrote a simple tutorial on reflection and that can be found here (Sample code included - C# and Managed Direct3D)
The steps I follow is simple assuming you have a left handed coordinate system which is most common.

1) Render your objects with their world transformation matrices.
2) Create the Reflection Matrix.
i.e.
C# and Managed Direct3D

//Calculate reflection matrix
matReflect.Reflect(new Plane(0.0f, 1.0f, 0.0f, 0.0f));//Normal of plane is +y (up)
matObject = matReflect * Matrix.Translation(0.0f, -20.0f, 0.0f);//concat the reflection matrix with the objects matrix and move it 20 units down.
//Render the object with the matrix
renderer.Device.Transform.World = matObject;
Object.DrawSubset(0);



I hope this helps.
Take care.

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