0

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.

sbuci
  • 11
  • 4

1 Answers1

0

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

Community
  • 1
  • 1
davidsbro
  • 2,761
  • 4
  • 23
  • 33
  • Thanks! It actually solves my problem halfway, but in the update shown image you can see the still existing part of my problem. – sbuci Aug 18 '13 at 05:59
  • Which do you draw first, the primitives or the models? And is the window actually clear (ex, alpha set to 0), or is the window a "hole" in the model? – davidsbro Aug 19 '13 at 15:17
  • First I draw the primitives, and the window isn't a hole. It's opacity is about 10%... – sbuci Aug 26 '13 at 17:09
  • Try switching the draw order of the primitives and the models – davidsbro Aug 26 '13 at 17:10
  • I think this article will help you: http://blogs.msdn.com/b/shawnhar/archive/2009/02/18/depth-sorting-alpha-blended-objects.aspx I believe to fix your window problem, you should sort your cars by distance from the camera, and then draw the farthest cars first and the losest car last. – davidsbro Aug 26 '13 at 17:24
  • Actually I tried it yet, but the result is confusing. The primitives (road network) are MOVING always relative to the last created car, and they turn into white. – sbuci Aug 26 '13 at 17:30
  • Thanks, I'll try that method written in the article, hope it will help! – sbuci Aug 26 '13 at 17:31
  • I edited my answer to try to answer the rest of your question – davidsbro Aug 26 '13 at 19:02
  • Your update is great idea! I tried it descending and ascending too, and works, but the distances I'm estimating don't seem to be correct. Through the windows the vehicles positioned in the same distance are visible only selective :D My best idea was project/unproject the position vector, but not perfect. – sbuci Aug 30 '13 at 06:29