2012-11-26 42 views
1

我的紋理顯示模型有一些問題。 一切正常,但是,我使用循環重複紋理來表示屏幕上的20×20的地板。我的紋理重複正確。但我不明白爲什麼我的所有紋理都會產生閃爍...... 我注意到圖像疊加在一起。我確定我檢查了我的循環編碼是否正確。爲什麼我的圖片在我的xna項目中閃爍?

見截圖: enter image description here

我的代碼(循環函數生成地):

//Function draw - ground land 
    private void draw_groundLand(Vector3 position_model_origin) 
    { 
     //example generation mode 4x4 cubes 
     int[,,] MatriceWorldCube = new int[1,2,2]; 
     MatriceWorldCube[0, 0, 0] = 1; 
     MatriceWorldCube[0, 0, 1] = 1; 
     MatriceWorldCube[0, 1, 0] = 2; 
     MatriceWorldCube[0, 1, 1] = 1; 

     int height = MatriceWorldCube.GetLength(0); 
     int width = MatriceWorldCube.GetLength(1); 
     int length = MatriceWorldCube.GetLength(2); 

     Vector3 pos_reference = position_model_origin; 

     for (int thickness = 0; thickness < height; thickness ++) 
     { 

      for (int column = 0; column < width; column ++) 
      { 

       for (int line = 0; line < length ; line ++) 
       { 


         // Copy any parent transforms. 
         Matrix[] transforms = new Matrix[model_ground_land1.Bones.Count]; 
         model_ground_land1.CopyAbsoluteBoneTransformsTo(transforms); 

         // Draw the model. A model can have multiple meshes, so loop. 
         foreach (ModelMesh mesh in model_ground_land1.Meshes) 
         { 
          // This is where the mesh orientation is set, as well 
          // as our camera and projection. 
          foreach (BasicEffect effect in mesh.Effects) 
          { 
           effect.EnableDefaultLighting(); 
           effect.World = transforms[mesh.ParentBone.Index] * 
          Matrix.CreateRotationY(cubeGroundLand1_modelRotation) * Matrix.CreateTranslation(position_model_origin); 
           effect.View = View; 
           effect.Projection = Projection; 


          } 

          // Draw the mesh, using the effects set above. 
          mesh.Draw(); 
         } 



        position_model_origin.X = (float)(line +1); 
       } 
       position_model_origin.X = pos_reference.X; 
       position_model_origin.Z = (float)(column +1); 

      } 
      position_model_origin.Z = pos_reference.Z; 
      position_model_origin.Y = (float)(thickness+1); 

     } 
     position_model_origin.Y = pos_reference.Y; 
     position_model_origin = pos_reference; 
    } 

預先感謝您的幫助。我失去了耐心(整個週末^ ^)

+3

看起來像[z-fighting](http://en.wikipedia.org/wiki/Z-fighting)。 – CodeCaster

+0

好的謝謝你我不知道這個問題。我剛開始使用「XNA」。你建議如何避免這種情況?如何設置Z戰鬥? 非常感謝。 –

+1

來自鏈接:_「通過使用更高分辨率的深度緩衝區,通過z緩衝在某些情況下,或通過簡單地移動多邊形進一步分開,可以減少Z戰鬥。看起來你在同一個地方有太多的多邊形。 – CodeCaster

回答

2

這是Z戰鬥。到目前爲止,Z軸緩衝精度隨着距離的下降而下降。物體有更多的「閃爍」GPU不能確定哪個多邊形處於頂部,因爲Z軸緩衝區的尾部不夠精確,無法區分2個幾乎相等的值。

你有6點的方式來解決這個問題:

  1. 移動幾何圖形或不顯示低於部分。

  2. 使用類似polygon offset in OpenGL

  3. 使用CPU Z分類,而不是Z緩衝。

  4. 只能使用2個紋理,而不是2個對象+一些着色器(我不知道你想達到什麼)

  5. 使用大Z緩衝區一個對象。

  6. 將夾平面彼此靠近會增加精度。

+0

哦,好的。這個解釋非常感謝你,但是如何移動幾何? ...對不起,我是xna的noob。我測試了這個:DepthStencilState depthState = new DepthStencilState(); depthState.DepthBufferEnable = true; depthState.DepthBufferWriteEnable = true; GraphicsDevice.DepthStencilState = depthState;但它不起作用 –

+0

@MehdiBugnard只是稍微移動一個重疊的對象,直到閃爍消失。 – JAre

+0

好吧,我要去試試這個。再次感謝你 。我回來談論我的結果 –