When I draw several instances of my 3d models, like a grid-formation of square terrain pieces, the models are drawn overlapping each other in the order I call draw on them, rather than overlapping as a result of distance to camera…
Do I need to re-sequence my terrainpiece draws to match camera position, by manually drawing the most distant pieces first,
-OR-
is there a clever way of adding each model to a combined scene, which I can then draw?
-I figure there must be a way to draw a combined scene, so scene-wide lights and shadows can work…
-I would be gratefull for any help or relevant links, as I have found it difficult to sniff out good tutorials on this stuff…
Sounds like your depth buffer states are not being set correctly. Try doing as Tom suggests in this thread:
That is the “clever way”, ie make use of the GPU’s built-in depth read/write functionality. The most efficient way to render opaque objects is to sort them from closest to furthest. This is because the pixel shader does not need to run for any pixels already written to the screen with a smaller depth value.
For semi-opaque objects you need to use alpha blending and render them from furthest to closest to avoid sorting artefacts. It’s common to store scene geometry in multiple lists and sort them relative to the camera depending on their opaque/transparent state.
Thank you, but I think I’m un-able to derive anything usefull from the linked posts, other than that the problem in the picture looks familiar compared to mine…
Looking at the discussed tutorial, I see they are working with programmed terrain ie generated vertices… -I am working with a collection of pre-built blender models…
I have tried adding the line by Tom to my code, but it doesnt seem to have any effect…
I can still only render my scene from a certain range of angles… Other-wise things are drawn in reverse, back to front and inverted looking…
I think my problem is I am drawing each model individually in a for-loop…
-I need something like a buffer or complete scene to send them to, which I can then draw after my for-loop…
How do I make a model in the foreground block the view of a model in the back ground?
As soon as I move on to my second model, its like it forgets the first models exsists
…
It shouldn’t matter whether the terrain geometry is generated at runtime or comes in as multiple models. The normal way to draw your scene is to loop through your objects and draw them one at a time - the graphics device handles the depth sorting for you (although as I said before you can sort things to help it out).
I think we’ll need to see some of your code, particularly the initialisation and draw routines. So long as you are creating your back buffer with a correct DepthFormat and setting the DepthStencilState before drawing your 3D models, it should be working.
Thank you for your response… I have tried lots of stuff, that I have removed again… Back to square one, here are the relevant code pieces I use for the 3d stuff…
-This code reads my 2d grid map, and places a model for each grid in a square formation in 3d…
You will notice my code has no mention of z-depth buffers or stencils, as I can find no examples with code that even effects my draw method…
So, I dont have any code in my initialize methods besides trivial things like variable assignments and such…
All I do relating to graphics is change my back-buffer dimensions…
and in my game1 constructor method, i have:
graphics = new GraphicsDeviceManager(this);
Now, I know how to create presentation parameters, but I cant figure out how to apply them…
-When I try, it tells me the property ‘GraphicsDevice’ is read only…
How exactly do I create (or reset) a device using these presentationparameters?
Sure, this is some of the code from an Android game. I checked and it appears that actual XNA games didn’t need to do this by default, so in theory MonoGame games should not either.
Ok, thanks so much, got the constructor and event-handler… Now I just need to implement what you wrote in your last sentance…
Below is the code I use for drawing things. Could you help point out where and how to “set depthstencilstate” ?
-I have been through some trial and error, getting me nowhere…
Since you already had this line before your model drawing, removing spritebatch calls should not have been necessary. So it would seem something strange is going on.
Ah that’s the first I’ve heard of you using a Render Target! You probably need to explicitly give the render target a depth buffer. The constructor with less arguments does not create one by default. Something like:
Yeah, sorry I didnt think that was relevant… Lesson learned… -I tried to spare you as much code as possible, as I am building on top of a larger 2d game… ITs a bit of a mess
For future people with scimilar problems:
Follow link below for a clean example of drawing various things to various rendertargets…
Shows how to setup the rendertargets with all the correct depth settings… EASY.