I'm trying to create a mirror.
I got the render to texture thingy done.
I'm first using the technique of calculation the "virtual camera" position behind the mirror, then render the scene from that position. I need to somehow "clip" the scene to the boundaries of the mirror, so i guess theres two ways to do it. One is to set the clip planes using device->SetClipPlane(...), but ... help ... help :) :)
Matrix4x4 mT(g_pApp->GetDirect3DWrapper()->GetViewMatrix()); mT.InverseTransform(mT); D3DXMatrixTranspose( (D3DXMATRIX*)&mT, (D3DXMATRIX*)&mT); D3DXPLANE plane, planeT; plane.a = m_matSurfaceBasis.up().x; plane.b = m_matSurfaceBasis.up().y; plane.c = m_matSurfaceBasis.up().z; plane.d = -GetWateryWasteFromID(m_iNearestWWID)->GetRadius(); D3DXVec4Transform((D3DXVECTOR4*)&planeT,(D3DXVECTOR4*)&plane,(D3DXMATRIX*)&mT); DWORD queryPlanes; pD3DDevice->GetRenderState(D3DRS_CLIPPLANEENABLE, &queryPlanes); pD3DDevice->SetClipPlane(0,planeT); pD3DDevice->SetRenderState( D3DRS_CLIPPLANEENABLE, D3DCLIPPLANE0 ); GetCamera()->Render(); SAFE_RELEASE(pSurface); pD3DDevice->SetRenderState( D3DRS_CLIPPLANEENABLE, queryPlanes ); |

SetClipPlane ????
s3ns
//-----------------------------------------------------------------------------
// Name: RenderMirror()
// Desc: Renders the scene as reflected in a mirror. The corners of the mirror
// define a plane, which is used to build the reflection matrix. The
// scene is rendered with the cull-mode reversed, since all normals in
// the scene are likewise reflected.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RenderMirror()
{
D3DXMATRIX matWorldSaved;
D3DXMATRIX matReflectInMirror;
D3DXPLANE plane;
// Save the world matrix so it can be restoredm_pd3dDevice->GetTransform( D3DTS_WORLD, &matWorldSaved );
// Get the four corners of the mirror. (This should be dynamic rather than // hardcoded.)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 matrixD3DXPlaneFromPoints( &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 are // 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 reflected scene. For the "normal" scene, RenderScene() was prior called from another function with default render states.RenderScene();
// Restore the modified render statesm_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,
sizeof(MIRRORVERTEX) );m_pd3dDevice->SetVertexShader( D3DFVF_MIRRORVERTEX );
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
return S_OK;}
Che2Palle
DJCrowell
http://www.pieterg.com/Tutorials/tutorial10.php