3D ordering.

Hi there.

I’m messing about with some 3D stuff, just some simple movement mechanics with objectives and characters.

I thought the point of the 3D was all the models would be worked out in the graphics card and displayed in the right order. But I have objects furthest away displayed in front of the closest. Or the other way around depending on the draw routines.

So how on earth do you draw all your models in different object lists? I loop through the objectives and draw them, then I loop through the characters and draw those.

Do I have to copy every single object to a different list in depth order to get this right, or am I missing something?

Thanks.

your z buffer is off is all.

public static DepthStencilState DS_DepthBufferEnabled = new DepthStencilState() { DepthBufferEnable = true, DepthBufferFunction = CompareFunction.LessEqual };

That gets defined at class scope states make a lot of garbage you don’t want to declare them in method scope.

GraphicsDevice.DepthStencilState =DS_DepthBufferEnabled;

spriteBatch.Begin(SpriteSortMode.Immediate,Helper.BS_NonPremultiplied,Helper.SS_PointBorder,Helper.DS_DepthBufferEnabled,Helper.RS_CullNoneSolid,effect,null);

If you want proper transparency though you will need to sort things based on distance and draw them farthest to nearest. Or draw everything transparent last with a shader to handle it in a cheap way which is what i usually do for simple stuff.

The depth buffer is enabled by default, but if you’re using SpriteBatch if you call SpriteBatch.Begin with default arguments it will disable the depth buffer. You will want to set the DepthStencilState to enable depth buffer reading and writing before doing your 3D rendering:

GraphicsDevice.DepthStencilState = DepthStencilState.Default;