2

Big headhache on XNA 4.0 concerning a depth problem:

enter image description here


I've already found many answers to similar problems, but no one work for me...

The device is set like this:

xnaPanel1.Device.BlendState = BlendState.Opaque;                
xnaPanel1.Device.DepthStencilState = DepthStencilState.Default;
xnaPanel1.Device.PresentationParameters.DepthStencilFormat = DepthFormat.Depth24Stencil8;
[...]
Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 4.0f / 3.0f, 0.1f, 1000f);

As a brutal problem resolver, I have tried most DepthStencilFormat and DepthStencilState possibilities... No one works like i want.

Concerning the projection matrix, I've tried many nearclip and farclip value too. (cube width: 10f) but can't get the correct result.

I've tested this with many different texture, all opaque.

I don't use a BasicEffect but an effect using texture + normal map, can it be the source of the problem?

CubeEffect.fx

[...]
sampler2D colorMap = sampler_state
{
Texture = <colorMapTexture>;
    MagFilter = Linear;
    MinFilter = Anisotropic;
    MipFilter = Linear;
    MaxAnisotropy = 16;
};
sampler2D normalMap = sampler_state
{
   Texture = <normalMapTexture>;
   MagFilter = Linear;
   MinFilter = Anisotropic;
   MipFilter = Linear;
   MaxAnisotropy = 16;
};
[...]

Edit: I tried with a BasicEffect and problem is the same...

So... Thanks for any help ;)

NSV
  • 41
  • 5

3 Answers3

2

Ok that's it.

pp.DepthStencilFormat = DepthFormat.Depth24Stencil8;

have to be before device creation call.

So I don't know at this time why this:

Device.PresentationParameters.DepthStencilFormat = DepthFormat.Depth24Stencil8;

previously called in my main Draw function, doesn't work...

Conclusions?

PresentationParameters pp = new PresentationParameters();
pp.IsFullScreen = false;
pp.BackBufferHeight = this.renderControl.Height;
pp.BackBufferWidth = this.renderControl.Width;
pp.DeviceWindowHandle = renderControl.Handle;
pp.DepthStencilFormat = DepthFormat.Depth24Stencil8;
this.graphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.HiDef, pp);

Now working fine!

NSV
  • 41
  • 5
1

PresentationParameters is a structure that defines how the device is created. You've already seen that when you create the graphics device you need to pass the structure in, which is only used for initial configuration.

The device stores the presentation parameters on it, but changing it does nothing unless you call Reset on the device, which will reinitialize the device to use whatever parameters you've changed. This is an expensive operation (so you won't want to do it very often).

Trevor Sundberg
  • 1,584
  • 1
  • 14
  • 25
0

Basically GraphicsDevice.PresentationParameters is an output - writing to it doesn't actually change the device state. It gets updated whenever the device is set-up or reset.

Generally you will be setting up the GraphicsDevice using GraphicsDeviceManager - it handles setting-up, resetting, and tearing-down the device for you. It is part of the default XNA Game template project.

The correct way to modify states is to set the desired values on GraphicsDeviceManager. In your case, you can simply set PreferredDepthStencilFormat.

Once you do this, you need to either set-up the device (ie: specify your settings in your game constructor and XNA will do the rest), or reset the device by calling GraphicsDeviceManager.ApplyChanges - which you should usually only do in response to user input (and obviously not every frame). See this answer for details.


You'll note that there are some presentation parameters that are not directly settable on the GraphicsDeviceManager. To change these you would have to attach an event handler to GraphicsDeviceManager.PreparingDeviceSettings. The event argument will give you access to a version of the presentation parameters you can usefully modify (e.GraphicsDeviceInformation.PresentationParameters) - the settings in there are what gets used when the graphics device is created.

Community
  • 1
  • 1
Andrew Russell
  • 26,924
  • 7
  • 58
  • 104