Sign in to follow this  

Problem with reflection with directx 9 /c++

This topic is 2383 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi eveyone,

I am an amateur Directx programmer and have trouble to make a simple reflection mapping using planar reflection. The attached file show that my projected surface doesn't fit with the rest of my scene.
I' ve been struggling with this problem now for a few days...



Here is the code, I can't figure out what's the problem..

[font="Consolas"][size="2"][font="Consolas"][size="2"]D3DXMATRIX ReflectionViewMatrix;

D3DXPLANE WaterPlaneW;

D3DXMATRIX mWorld, mTWorld;

D3DXMatrixInverse(&mWorld,NULL,&matWorld);

D3DXMatrixTranspose(&mWorld,&mWorld);

WaterPlane = D3DXPLANE(0,-1,0,2);

D3DXPlaneTransform(&WaterPlaneW,&WaterPlane,&mWorld);

D3DXMatrixMultiply(&MatReflectW,&matWorld,&MatReflect);

D3DXMatrixReflect(&MatReflectW, &WaterPlane);







ReflectionViewMatrix = matView*matProj;



D3DXMatrixInverse(&ReflectionViewMatrix ,NULL,&ReflectionViewMatrix);

D3DXMatrixTranspose(&ReflectionViewMatrix ,&ReflectionViewMatrix );

D3DXPLANE ViewSpacePlane;

ReflectionPlane = D3DXPLANE(0,-1,0,2);

D3DXPlaneNormalize(&ReflectionPlane,&ReflectionPlane);

D3DXPlaneTransform(&ViewSpacePlane,&ReflectionPlane,&ReflectionViewMatrix);

ViewPort.X = 0;

ViewPort.Y = 0;

ViewPort.Height = 512;

ViewPort.Width = 512;



ViewPort.MaxZ = 1.0f;

ViewPort.MinZ = 0.0f;

d3ddev->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);



RenderToSurface->BeginScene(Surface,&ViewPort);



d3ddev->Clear(0,NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f, 0.0f);

d3ddev->SetClipPlane(0,ViewSpacePlane);

d3ddev->SetRenderState(D3DRS_CLIPPLANEENABLE,D3DCLIPPLANE0);

d3ddev->SetTransform(D3DTS_WORLD,&(MatReflectW));



[/size][/font]

[/size][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"]/* Rendering the scene again*/[/color][/size][/font][/color][/font][/color][/font]

[font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"][/color][/size][/font][/color][/font][/color][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] D3DXMATRIX matRot,

matScale,

matTrans,

Result;

ter->RenderTerrain();

d3ddev->SetRenderState(D3DRS_CULLMODE,D3DCULL_CW);



cube->RenderNorthFace();

cube->RenderTopFace();

cube->RenderEastFace();

cube->RenderSouthFace();

cube->RenderWestFace();

cube->RenderBackFace();

D3DXMatrixTranslation(&matTrans,60,4,40);

D3DXMatrixRotationX(&matRot,D3DXToRadian(90));

D3DXMatrixScaling(&matScale,3,3,3);

Result = matScale*matRot*matTrans;

d3ddev->SetTransform(D3DTS_WORLD, &Result);

mesh->RenderMesh();

D3DXMatrixTranslation(&matTrans,86,3,70);

D3DXMatrixRotationX(&matRot,D3DXToRadian(90));

D3DXMatrixScaling(&matScale,3,3,3);

Result = matScale*matRot*matTrans;

d3ddev->SetTransform(D3DTS_WORLD, &Result);

mesh->RenderMesh();

D3DXMatrixTranslation(&matTrans,30,1,15);

D3DXMatrixRotationX(&matRot,D3DXToRadian(90));

D3DXMatrixScaling(&matScale,3,3,3);

Result = matScale*matRot*matTrans;

d3ddev->SetTransform(D3DTS_WORLD, &Result);

mesh->RenderMesh();

D3DXMatrixTranslation(&matTrans,20,1,15);

D3DXMatrixRotationX(&matRot,D3DXToRadian(90));

D3DXMatrixScaling(&matScale,3,3,3);

Result = matScale*matRot*matTrans;

d3ddev->SetTransform(D3DTS_WORLD, &Result);

mesh->RenderMesh();

D3DXMatrixTranslation(&matTrans,60,1,45);

D3DXMatrixRotationX(&matRot,D3DXToRadian(90));

D3DXMatrixScaling(&matScale,3,3,3);

Result = matScale*matRot*matTrans;

d3ddev->SetTransform(D3DTS_WORLD, &Result);

mesh->RenderMesh();

D3DXMatrixTranslation(&matTrans,70,5,45);

D3DXMatrixRotationX(&matRot,D3DXToRadian(90));

D3DXMatrixScaling(&matScale,3,3,3);

Result = matScale*matRot*matTrans;

d3ddev->SetTransform(D3DTS_WORLD, &Result);

mesh->RenderMesh();

D3DXMatrixTranslation(&matTrans,65,5,40);

D3DXMatrixRotationX(&matRot,D3DXToRadian(90));

D3DXMatrixScaling(&matScale,3,3,3);

Result = matScale*matRot*matTrans;

d3ddev->SetTransform(D3DTS_WORLD, &Result);

mesh->RenderMesh();

D3DXMatrixTranslation(&matTrans,60,2,90);

D3DXMatrixRotationX(&matRot,D3DXToRadian(90));

D3DXMatrixScaling(&matScale,0.5,0.5,0.5);

Result = matScale*matRot*matTrans;

d3ddev->SetTransform(D3DTS_WORLD, &Result);

mesh1->RenderMesh();

D3DXMatrixTranslation(&matTrans,80,2,60);

D3DXMatrixRotationX(&matRot,D3DXToRadian(90));

D3DXMatrixScaling(&matScale,0.5,0.5,0.5);

Result = matScale*matRot*matTrans;

d3ddev->SetTransform(D3DTS_WORLD, &Result);

mesh2->RenderMesh();



[/size][/font]

[/size][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"]/* Rendering the scene again*/[/color][/size][/font][/color][/font][/color][/font]

[font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][size="2"][color="#008000"][/color][/size][/font][/color][/font][/color][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]

d3ddev->SetRenderState(D3DRS_CLIPPLANEENABLE,FALSE);

d3ddev->SetTransform(D3DTS_WORLD,&(matWorld));

RenderToSurface->EndScene(D3DX_FILTER_NONE);

d3ddev->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);

[/size][/font][/size][/font]
Thanks in advance for any help you could give me !

Share this post


Link to post
Share on other sites

This topic is 2383 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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