Jump to content
  • Advertisement
Sign in to follow this  
Asem

D3D9 Mirror Problem

This topic is 4416 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

I'm a beginner using directx9 . The objects in the scene render fine but there is something that doesn't make sense when the mirror renders the objects. The objects (sphere, triangle, and regular square) move position when I move the camera like they are dependant on it. I also made sure every object had its own matrix. The drawing code I have is all located in the display function the camera maxtrix is at the top with it's own matrix and movement variables. If what I'm saying is confusing another example is that when you move one of the objects themselves very close to the mirror the mirror won't reflect the object. The names for the Matrices are wierd because I haven't gone back to change the names :)
/*
Scene representation:
---------------------
The whole wall is a mirror
The Objects float  not very far from the mirror
  -------------
  |           |--
  | Mirror    |/ 
  ------------
 /  Objects  /
/-----------/
Ground
*/

///////////////////////////////////////////////
//Created Object 4 : Square_Wall Mirror Part //
//Located At End Of Code After Drawing All   //
//Objects Normally                           //
///////////////////////////////////////////////

Device->SetRenderState (D3DRS_STENCILENABLE, true);

Device->SetRenderState (D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
Device->SetRenderState (D3DRS_STENCILREF, 0x1);
Device->SetRenderState (D3DRS_STENCILMASK, 0xFFFFFFFF);
Device->SetRenderState (D3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
Device->SetRenderState (D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
Device->SetRenderState (D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
Device->SetRenderState (D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);


Device->SetRenderState (D3DRS_ZWRITEENABLE, false);
Device->SetRenderState (D3DRS_ALPHABLENDENABLE, true);
Device->SetRenderState (D3DRS_SRCBLEND, D3DBLEND_ZERO);
Device->SetRenderState (D3DRS_DESTBLEND, D3DBLEND_ONE);

//Mirrored Object//

Device->SetStreamSource (0, VertexBuffer2, 0, sizeof(VertexNT));	
Device->SetFVF (VertexNT::FVF);								
Device->SetTexture   (0, texture[4]);							
Device->SetMaterial  (&d3d::WHITE_MTRL2);					
Device->SetTransform (D3DTS_WORLD, &(Scale10xy*Trans4));			
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);					


Device->SetRenderState (D3DRS_ZWRITEENABLE, true);
Device->SetRenderState (D3DRS_STENCILFUNC, D3DCMP_EQUAL);
Device->SetRenderState (D3DRS_STENCILPASS, D3DSTENCILOP_KEEP);

///////////////////////////////////////////////////
///////////////////////////////////////////////////

D3DXMATRIX W, T, R;
D3DXPLANE plane(0.0f, 0.0f, -1.0f, 1.0f);
D3DXMatrixReflect(&R, &plane);


Device->Clear(0, 0, D3DCLEAR_ZBUFFER, D3DXCOLOR (D3DCOLOR_XRGB (255, 0, 255)), 1.0f, 0);
Device->SetRenderState(D3DRS_SRCBLEND,  D3DBLEND_DESTCOLOR);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);



/////////////////////////////////////////////////
/////////////////////////////////////////////////

//Created Object 1 : Triangle//

Device->SetStreamSource (0, VertexBuffer, 0, sizeof(VertexNT));				
Device->SetFVF (VertexNT::FVF);								
Device->SetTransform(D3DTS_WORLD, &(Ry * Trans3 * R));					
Device->SetMaterial (&d3d::WHITE_MTRL);							
Device->SetTexture (0, texture[3]);							
Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);					
Device->DrawPrimitive (D3DPT_TRIANGLELIST, 0, 2);					


//Created Object 2 : Square//

Device->SetStreamSource (0, VertexBuffer2, 0, sizeof(VertexNT));			
Device->SetFVF (VertexNT::FVF);					
Device->SetTransform(D3DTS_WORLD, &(T2_Ry * R));
Device->SetMaterial (&d3d::WHITE_MTRL);
Device->SetTexture (0, texture[2]);
Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);					
Device->DrawPrimitive (D3DPT_TRIANGLELIST, 0, 4);					


//Created Object 3 : Square_Ground//

Device->SetStreamSource (0, VertexBuffer3, 0, sizeof(VertexNT));			
Device->SetFVF (VertexNT::FVF);								
Device->SetTransform(D3DTS_WORLD, &(Rx * Origin * Scale10xz * R));			
Device->SetSamplerState (0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);			
Device->SetSamplerState (0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);			
Device->SetTexture (0, texture[1]);							
Device->SetMaterial (&d3d::WHITE_MTRL2);						
Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);					
Device->DrawPrimitive (D3DPT_TRIANGLELIST, 0, 4);					


//Created Object 5 : Sphere, Transperancy//

Device->SetRenderState (D3DRS_ALPHABLENDENABLE , true);					
Device->SetRenderState (D3DRS_SRCBLEND , D3DBLEND_SRCALPHA);
Device->SetRenderState (D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
Device->SetTransform(D3DTS_WORLD, &(Ry*Trans*R));					
nSphereMat = d3d::WHITE_MTRL;								
nSphereMat.Diffuse.a = alpha;								
Device->SetMaterial (&nSphereMat);							
Device->SetTexture (0, texture[0]);							
Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);					
Sphere->DrawSubset (0);									

Device->SetSamplerState (0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);			
Device->SetSamplerState (0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);			

Device->SetRenderState (D3DRS_ALPHABLENDENABLE , false);				


///////////////////////////////////////////////////////
///////////////////////////////////////////////////////


Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
Device->SetRenderState( D3DRS_STENCILENABLE, false);
Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);









Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!