Cragganmore

Apologies in advance if this question has been covered elsewhere. I've searched through literally hundreds of posts here, but couldn't find anything relevant to my issue.

I'm working on some code to simultaneously display a 3d model from several different camera angles in several different viewports. Unfortunately, I've run into a rendering problem whenever I draw the model in a viewport.

For reference, here is a perspective rendering in 3DS Max of what the model looks like:

http://www.hph-tech.com/modelPerspective.jpg

This is a screenshot of the model (as seen from above) when I render it normally (i.e. not in a viewport):

http://www.hph-tech.com/modelNormal.jpg

And this is a screenshot of the model from the same angle, but when rendered in a viewport:

http://www.hph-tech.com/modelViewport.jpg

As you can see in the latter screenshot, the silver part of the model is being drawn on top of the red parts that should be in the foreground (among other draw-order issues - the propeller, the geometry at the nose of the model, etc).

My code to render this creates a viewport as follows:

public Viewport AddViewport(int x, int y, int width, int height)
{
Viewport newViewport = new Viewport();
newViewport.X = x; // 0
newViewport.Y = y; // 0
newViewport.Width = width; // 640
newViewport.Height = height; // 480
newViewport.MinDepth = 1f;
newViewport.MaxDepth = 10000f;

return newViewport;
}

When I go to render, I specify my created viewport using the Viewport object I created in AddViewport() above, as such:

graphicsService.GraphicsDevice.Viewport = theViewport;

I specify some options for my device:

graphicsService.GraphicsDevice.RenderState.AlphaTestEnable = false;
graphicsService.GraphicsDevice.RenderState.AlphaBlendEnable = false;
graphicsService.GraphicsDevice.RenderState.DepthBufferEnable = true;
graphicsService.GraphicsDevice.RenderState.DepthBufferFunction =
CompareFunction.LessEqual;
graphicsService.GraphicsDevice.RenderState.DepthBufferWriteEnable = true;

And then I draw my meshes:

Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);

foreach (ModelMesh modelMesh in model.Meshes)
{

foreach (BasicEffect effect in modelMesh.Effects)
{
// Transform
effect.EnableDefaultLighting();
effect.View = Guava.Camera.View;
effect.Projection = Guava.Camera.Projection;
effect.World = transforms[modelMesh.ParentBone.Index] *
Matrix.CreateTranslation(-center) *
Matrix.CreateScale(scale) *
Matrix.CreateFromQuaternion(rotation) *
Matrix.CreateTranslation(center + Position);
}

modelMesh.Draw();
}

Where Guava.Camera.View returns:

Matrix.CreateLookAt(viewPosition, Vector3.Zero, Vector3.Up);
[viewPosition is Vector3(0.0f, 50.0f, -4000.0f)]

And Guava.Camera.Projection returns:

Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
640f / 480f,
1f,
10000f);

Does anyone have any ideas why this renders OK when not in a viewport, but renders funky when in a viewport To reiterate for clarity's sake, if I don't create the viewport and make the call "graphicsService.GraphicsDevice.Viewport = theViewport;" then it renders perfectly as seen here:

http://www.hph-tech.com/modelNormal.jpg

If I do create the viewport and render to it, it renders like this:

http://www.hph-tech.com/modelViewport.jpg

Thanks in advance for any input. Hopefully there's a really obvious / really stupid solution :)

Jon


Re: XNA Framework Viewport depth buffer / draw order issue?

Tom Miller - MS

First thing I noticed was the unusually large numbers for MinDepth and MaxDepth on your viewport. Why so big It's normal to use 0.0f for min and 1.0f for max, those numbers are absolutely huge!




Re: XNA Framework Viewport depth buffer / draw order issue?

Cragganmore

Tom,

Absolutely huge, indeed. And you were absolutely right: that was my problem. As soon as I set MinDepth to 0f and MaxDepth to 1f, all my rendering issues went away. As to why I had them so large, that's most likely because I'm absolutely inexperienced :)

Many thanks. Now I can get back to coding!

Jon




Re: XNA Framework Viewport depth buffer / draw order issue?

Tom Miller - MS

Yay!




Re: XNA Framework Viewport depth buffer / draw order issue?

Kyle_W

Tom is the man alright. His book Managed DirectX 9 Graphics and Game Programming was awesome!



Re: XNA Framework Viewport depth buffer / draw order issue?

NeTBaPb

hmm i've heard somewhere that if u set min to 0 it possibly may lead to rendering artifacts




Re: XNA Framework Viewport depth buffer / draw order issue?

Cragganmore

Haha that is so ironic! The book sitting on my nightstand that I just started reading is none other than Managed DirectX 9 Kick Start: Graphics and Game Programming by....you guessed it, Tom Miller!

Thanks twice, Tom :)

Oh, and I have my min set to 0f with no rendering issues, so perhaps that's apocryphal Or perhaps it applies to situations apart from mine

Jon




Re: XNA Framework Viewport depth buffer / draw order issue?

Kyle_W

Well since XNA is replacing Managed DirectX, maybe you should wait until Tom writes a book on XNA for us.  Although actually MDX is still good to know since it can still do many things that XNA can't currently.