Question

I'm going to simulate the moving of cars on a road-network. First I draw the roads with userindexedprimitives, and it works fine. After that in specific moments I add models to the scene. These models are moving on the road, and it seems to be okay. Looking on them from behind looks like good, because they follow each other roughly in the order of creation. But in front view the app draws always the last time added vehicle first, and so on, thus they are drawn on each other, there is no cover. Perhaps it can be recognized on the image (link deleted, see update). The effect file I'm using is THIS, and the CurrentTechnique is the "ColoredNoShading". First I thought the problem could be this setting, but the other possibilities are throwing exceptions about missing vertex information (COLOR0 or NORMAL etc.) and I didn't cope with them... perhaps the solution is very simple, just I didn't find out...

Could someone please help me with this?

Thanks in advance

The code is based on this scheme:

private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
{
foreach (ModelMesh mesh in model.Meshes)
{
    foreach (BasicEffect effect in mesh.Effects)
    {
        effect.World = world;
        effect.View = view;
        effect.Projection = projection;
    } 
    mesh.Draw();
}
}

Related to the view and projection matrix:

viewMatrix = Matrix.CreateLookAt(new Vector3(0, 170, 0), new Vector3(0, 0, 0), new Vector3(0, 0, -1));
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, graphics.GraphicsDevice.Viewport.AspectRatio, 1.0f, 30000.0f);

effect.CurrentTechnique = effect.Techniques["ColoredNoShading"];
effect.Parameters["xProjection"].SetValue(projectionMatrix);
effect.Parameters["xView"].SetValue(viewMatrix); 

UPDATE:

using the DepthStencilState property it's better, but on THIS new image the problem is visible... through the glasses of the vechicle we can see only the vertices drawn by userindexedprimitives, and no models.

Was it helpful?

Solution

I think one thing that could be causing your problem is whether or not your GraphicsDevice.RenderState.DepthBufferEnable is set to true. (If you are drawing spritebatches, this is very likely the problem.) I would check this, because I had a similar drawing problem and setting GraphicsDevice.RenderState.DepthBufferEnable to true every draw (before I drew the models) fixed the problem. If you are using XNA 4.0, instead of using the code above, you'll have to do something like this:

DepthStencilState depthBufferState = new DepthStencilState(); 
depthBufferState.DepthBufferEnable = true;
GraphicsDevice.DepthStencilState = depthBufferState;

Here is a link that might be helpful. XNA - Drawing Quads (and primitives) in right order

EDIT:

To answer your question about the windows on the cars not drawing properly, to fix this you need to draw the cars in the correct order: Farthest away cars first and then closer cars. You could try something like this:

Game Logic:

List<Model> modellist;
Public Override Void Update
{
    //Update Logic
    foreach (Model m in n)
    {
         m.Update(cameraPosition);
    }
} 
Public Override Void Draw(GameTime gametime)
{
    //Draw Primitives and then sort models by distance from camera
    List<Model> n = modellist.OrderByDescending(x => x.DistanceFromCamera).ToList<Model>();
    foreach (Model m in n)
    {
        //Draw Model m
    }
}

Model Class

class Model
{
    private int distanceFromCamera = 0;
    public int DistanceFromCamera
    {
        get { return distanceFromCamera; }
        set { distanceFromCamera = value; }
    }

    public Vector3 Position;

    public void Update(Vector3 CameraPos)
    {
        //...
        distanceFromCamera = Vector3.Distance(CameraPos, this.Position);
    }
}

You could also have call OrderByDescending() in the Update void; this would probably be more effective. But hopefully this will point you in the right direction. HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top