2013-08-17 25 views
0

我要模擬汽車在道路網絡上的移動。首先,我用userindexedprimitives繪製道路,並且它工作正常。在那之後的特定時刻,我將模型添加到場景中。這些模型正在前進,而且似乎沒有問題。從背後看它們看起來很不錯,因爲它們按照創作的順序大致相互關聯。但從前面看,該應用程序總是最後一次添加車輛,等等,因此它們相互吸引,沒有遮擋。也許它可以在圖像上識別(鏈接已刪除,請參閱更新)。我使用的效果文件是THIS,CurrentTechnique是「ColoredNoShading」。首先,我認爲問題可能是這個設置,但其他可能性是拋出關於缺少頂點信息(COLOR0或NORMAL等)的例外,我沒有處理它們......也許解決方案很簡單,只是我沒有'噸找出...爲什麼要按創建順序繪製XNA模型?

有人可以幫我這個嗎?預先

由於

該代碼是基於這樣的方法:

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(); 
} 
} 

相關視圖和投影矩陣:使用DepthStencilState

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財產更好,但在THIS新形象親瑕疵是可見的......通過車輛的眼鏡,我們只能看到由用戶指定的原始圖繪製的頂點,而沒有模型。

+0

你可以添加'View'和'Projection'聲明嗎? – pinckerman

回答

0

我認爲可能導致您的問題的一件事是您的GraphicsDevice.RenderState.DepthBufferEnable設置爲true。 (如果你正在繪製spritebatches,這很可能是這個問題。)我會檢查這個,因爲我有一個類似的繪圖問題,並且設置GraphicsDevice.RenderState.DepthBufferEnabletrue每次繪製(繪製模型之前)解決了這個問題。如果您正在使用XNA 4.0,而不是使用上面的代碼,你必須做這樣的事情:

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

下面是可能會有所幫助的鏈接。 XNA - Drawing Quads (and primitives) in right order

編輯:

要回答你的問題有關的汽車窗戶不拉正確,要解決這個問題,你需要繪製汽車以正確的順序:第一最遠汽車,然後接近汽車。你可以嘗試這樣的事:

遊戲邏輯:在更新無效

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 
    } 
} 

模型類

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); 
    } 
} 

你也可以有通話OrderByDescending();這可能會更有效。但希望這會讓你指向正確的方向。 HTH

+0

謝謝!它實際上解決了我的問題,但在更新顯示的圖像中,您可以看到我的問題仍存在的部分。 – sbuci

+0

你最先繪製哪些基元或模型?而且這個窗口實際上是否清晰(例如,alpha設置爲0)還是窗口是模型中的「洞」? – davidsbro

+0

首先我繪製原始圖,窗口不是洞。它的不透明度約爲10%... – sbuci