Sign in to follow this  

Reflection using Stencil Buffer

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

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