Drawing 3D Models over primitives in XNA

Started by
13 comments, last by LemonBiscuit 10 years, 2 months ago

Hello,

I use DrawIndexedPrimtives to draw my terrain. If I draw a model over it, the intersection between the model and the terrain is pretty messy:

http://i.imgur.com/zbEXzQM.png (the water is only a blue plane (3D model))

http://i.imgur.com/fzXvFy6.png (here the grey thing is a model)

How can I fix it?

Thank you

Advertisement

Looks a lot like z fighting, but I can't imagine why. Model.Draw uses DrawIndexedPrimitives underneath the covers, so this shouldn't make a difference.

I think you'll need to post more of your drawing code (what values are you passing to your shader, what blend states and depthstencil states are you using).

One possibility is that your objects are being drawn at roughly the same depth. This could happen if your near/far plane are really far apart (show us how you create your projection matrix).

Here's the projection matrix:


 _projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(40), 0.02f, 10000, GraphicsDevice.Viewport.AspectRatio);

I don't use any special blenstates/depthpencils, I tried the projection with 100 instead of 10000, and it still the same result. sad.png

EDIT: The problem seems to be coming from the near plane. I set 1f as the near plane, and it worked. Now, I'll need to fix the problem that I can't see the very near models (FPS Guns)..

Thank you for your help!

Take a look at http://msdn.microsoft.com/en-us/library/windows/desktop/bb219616%28v=vs.85%29.aspx

From that article:

At a [far- to near-plane] ratio of 1,000, 98 percent of the range is spent on the first 2 percent of the depth range.

With a near plane of 0.02, and a far plane at 10000, your ratio was 500,000! Always keep the ratio of far- to near-plane as small as can possibly be used.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Its because you are using Gouraud shading and that is smoothing out your terrain.

You would have to increase your vertex count or find where the overlapping mesh is on the terrain and do some edge filtering on the terrain mesh where they meet.

Let me know how you do it because I'll need to do it when I get to the bells and whistles stage lol.


Its because you are using Gouraud shading and that is smoothing out your terrain.

You're incorrect. It has nothing to do with the way he is shading it. We've already established it was z-fighting due to limited depth buffer precision because of the large far/near plane ratio.

I can't find a way to fix my problem with my hands.

I set the nearplane to 0.02f so I can draw models very close to the camera, and as my game is a FPS, I need to draw guns close to the camera.

Is there any solutions to counter fix that?

Draw your gun last, clear depth before and use a different projection. It will be "wrong" (gun is now always drawn, like a HUD) but it should work.

EDIT: See last comment below.

See http://msdn.microsoft.com/en-us/library/windows/desktop/bb206341%28v=vs.85%29.aspx

In particular:

the D3DVIEWPORT9 structure... MinZ and MaxZ indicate the depth-ranges into which the scene will be rendered... For instance, to render a heads-up display in a game, you can set both values to 0.0 to force the system to render objects in a scene in the foreground...

You should draw the weapons (and any other head-up display items) first, with viewport.MaxZ = 0. [EDIT - use a projection matrix with 0.02 / 1.0 or whatever works to draw the HUD.] Then set MaxZ back to 1 for the rest of the render. [EDIT - and set the projection matrix with 1.0 / 10000, or whatever works for the terrain.] That ensures they'll be rendered in the foreground, and will provide a little efficiency in (not) drawing occluded objects later.

@Unbird: smile.png Your method will work also. But maybe better not to take the time to clear the depth buffer?

EDIT: Draw the weapons first with a small ratio projection. Then set a larger projection ratio for the rest of the scene. No changes needed for the viewport. Sorry about this mess of a post.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Here's what I did:

I set back the projection to (1, 10000) as nearplane/farplane.
I created another to (0.02, 1) to apply it to my weapon draw.

I moved my weapon draw to first, and but I have a problem, the weapon traverses all models and terrain, it's like if it was huge.

Also I can't set nearplane and farplane to 0, XNA asks me to have a positive value for nearPlaneDistance.

This topic is closed to new replies.

Advertisement