Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Reflection using Stencil Buffer

Started by Simplicity Aug 1, 2005 at 8:08 PM 3 replies 650+ views
Original Post
Simplicity
Simplicity
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?
Guimo
Guimo
yes, its the culling, just set the cull to CW before rendering the reflection.

Luck!
Guimo
Simplicity
Simplicity
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.
JohnBolton
JohnBolton
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Simplicity
Simplicity
That is actually the problem, thanks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.