Sign in to follow this  
Andregolas

Z-Buffer Configuration - Managed DX

Recommended Posts

Hi @ all, I'm having trouble with the Z-Buffer, ehatever I do it doesn't work porperly :( Here's a bit of my code:
public void InitDevices()
    {
        /*** Direct3D ***/
        //PresentParameters
        pp.BackBufferFormat = Microsoft.DirectX.Direct3D.Manager.Adapters[0].CurrentDisplayMode.Format;
        pp.BackBufferCount = 1;
        pp.BackBufferWidth = Settings.Width;
        pp.BackBufferHeight = Settings.Height;

        pp.SwapEffect = SwapEffect.Discard;

        //Z-Buffer
        pp.EnableAutoDepthStencil = true;
        pp.AutoDepthStencilFormat = DepthFormat.D24S8;

        //Vertical - Sync
        pp.PresentationInterval = PresentInterval.Immediate;
        
        //AntiAliasing
        (...)
        
        //Device
        (...)

        //Z-Buffer
        D3D_Device.RenderState.ZBufferEnable = true;
        D3D_Device.RenderState.ZBufferWriteEnable = true;

        //Anti Aliasing
        D3D_Device.RenderState.MultiSampleAntiAlias = true;
}
I hope you can tell me if I have to add or delete something to get the Buffer working?

Share this post


Link to post
Share on other sites
A) How do you know it doesn't work?

B) What are the near and far planes set to in the Projection matrix creation?

Share this post


Link to post
Share on other sites
Thx for your answer,

A) If I deactiate culling, objects aren't drawed properly, some Vertices in the back are in the front of those in the front etc. If I activate culling (CounterClockwise) most objects are drawn properly, but some objects in front of others are in behind them and some objects are always in the front.

B)Code:

D3D_Device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, 1.0f, 0.0f, 250.0f);


I hope that will help.

Share this post


Link to post
Share on other sites
What Sirob was probably already getting at, your near clipping plane should be at a depth > 0. Setting it a small value like 0.1 will most likely fix the issues you're currently seeing. For more information about the zbuffer check out this page, it's worth a read.

Share this post


Link to post
Share on other sites
More specifically (and for future readers), a near-Z value of 0.0f causes a divide by 0, which causes all z-values to become invalid values. After that, whatever depth a fragment had is lost. Sounds like changing the near-Z to anything other than 0 (I'd go for something over 0.1f) should fix it right up.

Share this post


Link to post
Share on other sites

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