Using flat shear feature, we can shield some things we don't want to see, it is like a block, blocking our sight. This way we can create a mirror.
First, first we must construct a plane first. In 3D, we have two ways to construct a plane
1. We can determine a plane with a point and this plane on the plane.
D3DXVECTOR3 VPSITION, VNORMAL;
D3DXPLANE PLANE;
D3DXPLANEFROMPOINTNORMAL (& Plane, VPosition, vnormal);
2, through any three points on the plane, you can also determine a plane
D3DXVECTOR3 V1, V2, V3;
D3DXPLANE PLANE;
D3DXPLANEFROMPOINTS (& Plane, & V1, & V2, & V3);
Second, after determining the plane, we have to get the world transformation relative to this plane and change the current world transformation. Of course, we have to save the existing world transformation, so restored the world transformation after using the plane.
D3DXMAMATRIX MATSAVEWORLD, MATREFLECTMIRROR;
PD3DDEVICE-> GetTransFrom (D3DTS_WORLD, & MATSAVEWORLD);
D3DXMatrixreflect (& matReflectmirror, & Plane);
PD3DDEVICE-> SetTransFrom (D3DTS_WORLD, & MATREFLECTMIRROR);
Third, set D3DRS_CULLMODE as a reverse mode D3DCULL_CW,
PD3DDEVICE-> Setrenderstate (D3DRS_CULLMODE, D3DCULL_CW);
Fourth, set a plane shear method
PD3DDEVICE-> SetClipplane (0, & Plane); // Set multiple times when you want to cut from multiple faces.
/ / Then set it in the back of the rendered state
PD3DDEVICE-> Setrenderstate (D3DRS_CLIPPLANEENABLE, D3DCLIPPLANE0 / * | D3DCLIPPLANE1 * /);
/ / Inside the above function, when a plurality of shear planes are set, multiple parameters can be transmitted.
// D3DClipplane0 | D3DClipplane1 | D3DClipplane2 | D3DClipplane3
5. Draw something to see (ie, rendering it into the shear)
Renderscene ();
6. Put the output that has just rendered
PD3DDEVICE-> SetTransform (D3DTS_WORLD, & MATSAVEWORLD);
PD3DDEVICE-> Setrenderstate (D3DRS_CLIPPLANEENABLE, 0X00); // Clear Plane Cut
PD3DDEVICE-> Setrenderstate (D3DRS_CULLMODE, D3DCULL_CCW);
PD3DDEVICE-> SetRenderstate (D3DRS_ALPHABLENDENABLE, TRUE);
PD3DDEVICE-> Setrenderstate (D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
PD3DDEVICE-> SetRenderstate (D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
/ / Rendering the content just after plane cut here
Renderclip ();
PD3DDEVICE-> SetRenderstate (D3DRS_ALPHABLENDENABLE, FALSE);
Code example rendermirror () {D3DXMATRIXA16 MATWORLDSAVED; D3DXPLANE PLANE D3DXPLANE PLANE
// save the world matrix so it can be rest 3ddevice-> getTransform (D3DTS_WORLD, & MATWORLDSAVED);
// Get The Four Corners of the mirror (this sales ") D3DXVector3 a (-1.5f, 1.5f, 3.0f); D3DXVector3 B (1.5F, 1.5F, 3.0F); D3DXVector3 C (-1.5F, -1.5F, 3.0F); D3DXVector3 D (1.5F, -1.5F, 3.0F);
// Construct the reflection matrix D3DXPlaneFromPoints (& plane, & a, & b, & c); D3DXMatrixReflect (& matReflectInMirror, & plane); m_pd3dDevice-> SetTransform (D3DTS_WORLD, & matReflectInMirror);
// Reverse The Cull Mode (Since Normals Will Be Reflected) m_pd3ddevice-> setrenderstate (D3DRS_CULLMODE, D3DCULL_CW);
// SET the Custom Clip Planes (SO Geometry Is Clipped By Mirror Edges). // This Is The Heart of this Sample. The Mirror Has 4 Edges, So There Area // 4 Clip Planes, Each Defined by Two Mirror Vertices and The . eye point m_pd3dDevice-> SetClipPlane (0, * D3DXPlaneFromPoints (& plane, & b, & a, & m_vEyePt)); m_pd3dDevice-> SetClipPlane (1, * D3DXPlaneFromPoints (& plane, & d, & b, & m_vEyePt)); m_pd3dDevice-> SetClipPlane (2, * D3DXPlaneFromPoints (& plane, & c, & d, & m_vEyePt)); m_pd3dDevice-> SetClipPlane (3, * D3DXPlaneFromPoints (& plane, & a, & c, & m_vEyePt)); m_pd3dDevice-> SetRenderState (D3DRS_CLIPPLANEENABLE, D3DCLIPPLANE0 | D3DCLIPPLANE1 | D3DCLIPPLANE2 | D3DCLIPPLANE3);
// render the scene renderscene ();
// Restore the modified render states m_pd3dDevice-> SetTransform (D3DTS_WORLD, & matWorldSaved); m_pd3dDevice-> SetRenderState (D3DRS_CLIPPLANEENABLE, 0x00); m_pd3dDevice-> SetRenderState (D3DRS_CULLMODE, D3DCULL_CCW); // Finally, render the mirror itself (as an alpha- blended quad) m_pd3dDevice-> SetRenderState (D3DRS_ALPHABLENDENABLE, TRUE); m_pd3dDevice-> SetRenderState (D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); m_pd3dDevice-> SetRenderState (D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
M_PD3DDEVICE-> SetStreamSource (0, m_pmirrorvb, 0, sizeof (mirrorvertex)); m_pd3ddex :: fvf (m_pd3ddex :: fvf); m_pd3ddevice-> DrawPrimitive (D3DPT_TRIANGLESTRIP, 0, 2);
m_pd3ddevice-> setrenderstate (D3DRS_ALPHABLENDENABLE, FALSE);