Jump to content
  • Advertisement
Sign in to follow this  
ZappBranigan

Problem with stencil buffer

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

Howdy, I just have a quick question that's really confusing me. I'm trying to do some very simple shadows using the stencil buffer but instead of the shadow appearing black and alpha blended with the ground it appears white (with the correct shape) and not alpha blended. I have closely followed several tutorials but I am at a loss as to why this is happening. First of all when I'm initializing everything I call SetupStencilBuffer()
void SetupStencilBuffer()
{
    Device->SetRenderState(D3DRS_STENCILENABLE,    true);
    Device->SetRenderState(D3DRS_STENCILFUNC,      D3DCMP_EQUAL);
    Device->SetRenderState(D3DRS_STENCILREF,       0x0);
    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_INCR);
}

In my render loop I then do my drawing
    //Render the sky box
    Device->SetTransform(D3DTS_WORLD, &skyPosition);
    skybox->RenderMesh();

    //Draw the pistons
    Device->SetTransform(D3DTS_WORLD, &engPosition);
    pistons->RenderMesh();

    //Draw the engine transparently
    Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
    Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
    Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_SRCCOLOR);
    Device->SetTransform(D3DTS_WORLD, &engPosition);
    engine->RenderMesh();
    Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);

    //Draw the shadow of the engine
    castShadow();

castShadow() consists of
void castShadow()
{
    //Create the light ray plane used casting the shadow
    D3DXVECTOR4 lightRay(0.0f, 50.0f, 0.0f, 1.0f);
    D3DXPLANE basePlane(0.0f, 1.0f, 0.0f, 9.0f);

    //Position us at shadow position
    D3DXMATRIX S;
    D3DXMatrixShadow(&S, &lightRay, &basePlane);
    D3DXMATRIX worldShadow;
    worldShadow = engPosition * S;
    Device->SetTransform(D3DTS_WORLD, &worldShadow);

    //Set our render states
    Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
    Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
    Device->SetRenderState(D3DRS_STENCILENABLE, true);   
    Device->SetRenderState(D3DRS_ZENABLE, false);
 
    //Draw the actual shadow (model drawn with a black D3DMATERIAL9 with diffuse    
    //transparency of 0.5f and SetTexture(0, 0))
    engine->RenderShadow();

    //Set our render states back
    Device->SetRenderState(D3DRS_ZENABLE, true);
    Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
    Device->SetRenderState(D3DRS_STENCILENABLE,    false);
}

just for the sake of clarity I am clearing my buffer every frame with
Device->Clear( 0, NULL, 
      D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL,
      D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f), 1.0f, 0 );

Any idea of where I might be going wrong would be greatly appreciated. Paul

Share this post


Link to post
Share on other sites
Advertisement
Have you made sure lighting is enabled, the appropriate ambient lighting is set, and the material of your shadow mesh has an ambient component?

Share this post


Link to post
Share on other sites
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!