Jump to content
Sign in to follow this  
Simplicity

Reflection using Stencil Buffer

This topic is 4852 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 follow the technique from the book "Introdction to 3D Game Programming with DirectX 9.0" by Frank D. Luna but gets an unusual result: The issue is with the culling mode I think. The inside of the cube is actually being draw instead of the outside reflected. The process follows(ignoring transformation for now):
cd_pCamera->SetPosition( 10.0f, 5.0f, -10.0f );
cd_pCamera->Update();

Matrix4x4f MatWorld;
MatWorld.MakeIdentity();
cd_pRenderer->SetWorld( MatWorld );
cd_pMesh->Render();

//render mirror
cd_pRenderer->SetTexture( 0, 0 );
cd_pRenderer->RenderPrimitive( cd_VSMirror, PT_TRIANGLE_STRIP, 0, 2 );

//--Apply reflection process--//
cd_pRenderer->EnableStencil( true );
cd_pRenderer->SetStencilFunction( CMPFUNC_ALWAYS );
cd_pRenderer->SetStencilRef( 0x1 );
cd_pRenderer->SetStencilMask( 0xffffffff );
cd_pRenderer->SetStencilWriteMask( 0xffffffff );
cd_pRenderer->SetStencilZFail( STENCILOP_KEEP );
cd_pRenderer->SetStencilFail( STENCILOP_KEEP );
cd_pRenderer->SetStencilPass( STENCILOP_REPLACE );

cd_pRenderer->EnableZWrite( false );
cd_pRenderer->EnableAlphaBlending( true );
cd_pRenderer->SetSourceBlend( BLENDM_ZERO );
cd_pRenderer->SetDestBlend( BLENDM_ONE );

cd_pRenderer->RenderPrimitive( cd_VSMirror, PT_TRIANGLE_STRIP, 0, 2 );

cd_pRenderer->EnableZWrite( true );

cd_pRenderer->SetStencilFunction( CMPFUNC_EQUAL );
cd_pRenderer->SetStencilPass( STENCILOP_KEEP );

//clear depth buffer
cd_pViewport->Clear( false, true, false );

cd_pRenderer->SetSourceBlend( BLENDM_DESTCOLOR );
cd_pRenderer->SetDestBlend( BLENDM_ZERO );

cd_pRenderer->SetCullMode( CM_CW );
MatWorld.MakeTranslation( Vector3f(0.0f, 0.0f, 8.0f) );
cd_pRenderer->SetWorld( MatWorld );
cd_pMesh->Render();

cd_pRenderer->EnableStencil( false );
cd_pRenderer->EnableAlphaBlending( false );
cd_pRenderer->SetCullMode( CM_CCW );
//--//

I specify the cube geometry like so: DirectForce Model //ID txt //Txt for text, bin for binary Version{1} //Version MaterialCount{1} //Number of materials MeshCount{1} //Number of meshes Material{ 1.000000, 1.000000, 1.000000, 1.000000; //Diffuse r, g, b, a 1.000000, 1.000000, 1.000000, 1.000000; //Ambient r, g, b, a 0.000000, 0.000000, 0.000000, 0.000000; //Specular r, g, b, a 0.000000, 0.000000, 0.000000, 0.000000; //Emissive r, g, b, a 0.000000; //Specular power "cloud.tga"; //Texture filename } Mesh{ //Mesh 0; //Material index 3; //Vertex format: 3 - PN1TC 24; //Vertex count 12; //Triangle count Vertices{ -1.0, -1.0, -1.0, 0.0, 0.0, -1.0, 0.0, 1.0; -1.0, 1.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0; 1.0, -1.0, -1.0, 0.0, 0.0, -1.0, 1.0, 1.0; 1.0, 1.0, -1.0, 0.0, 0.0, -1.0, 1.0, 0.0; 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0; 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0; -1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0; -1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0; -1.0, 1.0, -1.0, 0.0, 1.0, 0.0, 0.0, 1.0; -1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0; 1.0, 1.0, -1.0, 0.0, 1.0, 0.0, 1.0, 1.0; 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0; -1.0, -1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0; -1.0, -1.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.0; 1.0, -1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0; 1.0, -1.0, -1.0, 0.0, 1.0, 0.0, 1.0, 0.0; -1.0, -1.0, 1.0, -1.0, 0.0, 0.0, 0.0, 1.0; -1.0, 1.0, 1.0, -1.0, 0.0, 0.0, 0.0, 0.0; -1.0, -1.0, -1.0, -1.0, 0.0, 0.0, 1.0, 1.0; -1.0, 1.0, -1.0, -1.0, 0.0, 0.0, 1.0, 0.0; 1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 0.0, 1.0; 1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 0.0, 0.0; 1.0, -1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0; 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0; } Triangles{ 0, 1, 2; //Vertex indices 2, 1, 3; 4, 5, 6; 6, 5, 7; 8, 9, 10; 10, 9, 11; 12, 13, 14; 14, 13, 15; 16, 17, 18; 18, 17, 19; 20, 21, 22; 22, 21, 23; } } This is the custom file format I defined for my engine, but you get the idea. Any idea on what I'm doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
yes, its the culling, just set the cull to CW before rendering the reflection.

Luck!
Guimo

Share this post


Link to post
Share on other sites
I did and the result is the inside of the cube being drawn. If I leave the culling bo CCW, the whole cube would be drawn without reflection.

Share this post


Link to post
Share on other sites
The code that does the reflection is missing. You need to reflect either the view matrix or the mesh vertices about the plane of the mirror. Translating is not the same as reflecting. Look up D3DXMatrixReflect.

Share this post


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

×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!