# Drawing 3D Models over primitives in XNA

This topic is 1796 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

Edited by LemonBiscuit

##### Share on other sites

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).

Edited by phil_t

##### Share on other sites

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.

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)..

Edited by LemonBiscuit

##### Share on other sites

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.

##### Share on other sites

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.

##### Share on other sites

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.

##### Share on other sites

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?

##### Share on other sites

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.

##### Share on other sites

EDIT: See last comment below.

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: 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.

Edited by Buckeye

##### Share on other sites

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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 9
• ### Forum Statistics

• Total Topics
634084
• Total Posts
3015403
×