2014-06-20 27 views
2

首先,原諒我的英語,我是法國人...... 我寫了一個不起作用的代碼。 我想繪製一個像Minecraft這樣的多元塊,但是在Voxel中。 在我的地圖的一側,結果是好的:XNA倍數立方體

This render is opaque, good !

,但在另一邊,我得到這個:

But in this side of my map, it's no good :(

一些面孔出現通過一些其他的臉.. 誰有想法?我的法線?

我翻拍功能()不使用,它是刪除面...

namespace LandOfCube.Models 
{ 
    class Block 
    { 
     VertexBuffer instanceBuffer; 
     public enum Form 
     { 
      all, 
      noUp, 
      noDown, 
      noRight, 
      noLeft, 
      noFront, 
      noBack 
     } 
     public Form cubeForm = Form.all; 

     GraphicsDevice Graphics; 
     Effect shader; 
     private VertexBuffer vb; 
     Color CubeColor; 
     public Vector3 pos; 

     VertexPositionColorNormal[] vertices; 
     VertexPositionColorNormal[] verticesNoUp; 
     VertexPositionColorNormal[] verticesNoDown; 
     VertexPositionColorNormal[] verticesNoFront; 
     VertexPositionColorNormal[] verticesNoBack; 
     VertexPositionColorNormal[] verticesNoRight; 
     VertexPositionColorNormal[] verticesNoLeft; 


     private VertexPositionColorNormal[] Front = new VertexPositionColorNormal[5]; 
     private VertexPositionColorNormal[] Back = new VertexPositionColorNormal[5]; 
     private VertexPositionColorNormal[] Right = new VertexPositionColorNormal[5]; 
     private VertexPositionColorNormal[] Up = new VertexPositionColorNormal[5]; 
     private VertexPositionColorNormal[] Left = new VertexPositionColorNormal[5]; 
     private VertexPositionColorNormal[] Down = new VertexPositionColorNormal[5]; 

     public Vector3 normalFront = Vector3.Forward; 
     public Vector3 normalBack = Vector3.Backward; 
     public Vector3 normalTop = Vector3.Up; 
     public Vector3 normalBottom = Vector3.Down; 
     public Vector3 normalLeft = Vector3.Left; 
     public Vector3 normalRight = Vector3.Right; 

     public Block(GraphicsDevice graph, Color color, Vector3 position, ContentManager content) 
     { 


      Graphics = graph; 

      shader = content.Load<Effect>("effects"); 
      CubeColor = color; 
      pos = position; 

      Init(); 
      SetVertices(); 

     } 





     bool test = false; 
     public void Draw(Camera camera) 
     { 



      Graphics.SetVertexBuffer(vb); 

      shader.CurrentTechnique = shader.Techniques["Colored"]; 


      shader.Parameters["xView"].SetValue(camera.View); 
      shader.Parameters["xProjection"].SetValue(camera.Projection); 
      shader.Parameters["xWorld"].SetValue(Matrix.Identity); 
      Vector3 lightDirection = new Vector3(1.0f, -1.0f, -1.0f); 
      lightDirection.Normalize(); 
      shader.Parameters["xLightDirection"].SetValue(lightDirection); 
      shader.Parameters["xAmbient"].SetValue(0.1f); 
      shader.Parameters["xEnableLighting"].SetValue(true); 


      foreach (EffectPass pass in shader.CurrentTechnique.Passes) 
      { 

       pass.Apply(); 
       this.Graphics.DrawPrimitives(PrimitiveType.TriangleList, 0, vertices.Length/3); 
      } 




     } 








     #region Methode abstraites 

     private void Init() 
     { 

      InitFront(); 
      InitBack(); 
      InitDown(); 
      InitUp(); 
      InitLeft(); 
      InitRight(); 
     } 



     public void Remake() 
     { 
      switch (cubeForm) 
      { 
       case Form.noBack: 
        vertices = verticesNoBack; 
        break; 
       case Form.noFront: 
        vertices = verticesNoFront; 
        break; 
       case Form.noUp: 
        vertices = verticesNoUp; 
        break; 
       case Form.noDown: 
        vertices = verticesNoDown; 
        break; 
       case Form.noRight: 
        vertices = verticesNoRight; 
        break; 
       case Form.noLeft: 
        vertices = verticesNoLeft; 
        break; 
      } 
      vb = new VertexBuffer(Graphics, typeof(VertexPositionColorNormal), vertices.Length, BufferUsage.None); 
      vb.SetData(vertices); 

      verticesNoBack = null; 
      verticesNoDown = null; 
      verticesNoFront = null; 
      verticesNoLeft = null; 
      verticesNoRight = null; 
      verticesNoUp = null; 

      test = true; 
     } 




     public void SetVertices() 
     { 
      this.vertices = new VertexPositionColorNormal[36]; 
      int y = 0; 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Up[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Down[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Front[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Back[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Right[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Left[x]; y++; 
      } 

      setNoBack(); 
      setNoFront(); 
      setNoUp(); 
      setNoDown(); 
      setNoRight(); 
      setNoLeft(); 

      vb = new VertexBuffer(Graphics, typeof(VertexPositionColorNormal), vertices.Length, BufferUsage.None); 
      vb.SetData(vertices); 
      Clean(); 
     } 

     #region InitFaces 

     public void setNoBack() 
     { 
      this.verticesNoBack = new VertexPositionColorNormal[30]; 
      int y = 0; 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Up[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Down[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Front[x]; y++; 
      } 

      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Right[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Left[x]; y++; 
      } 
     } 
     public void setNoFront() 
     { 
      this.verticesNoFront = new VertexPositionColorNormal[30]; 
      int y = 0; 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Up[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Down[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Back[x]; y++; 
      } 

      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Right[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Left[x]; y++; 
      } 
     } 
     public void setNoUp() 
     { 
      this.verticesNoUp = new VertexPositionColorNormal[30]; 
      int y = 0; 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Front[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Down[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Back[x]; y++; 
      } 

      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Right[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Left[x]; y++; 
      } 
     } 
     public void setNoDown() 
     { 
      this.verticesNoFront = new VertexPositionColorNormal[30]; 
      int y = 0; 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Up[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Front[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Back[x]; y++; 
      } 

      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Right[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Left[x]; y++; 
      } 
     } 
     public void setNoLeft() 
     { 
      this.verticesNoLeft = new VertexPositionColorNormal[30]; 
      int y = 0; 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Up[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Down[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Back[x]; y++; 
      } 

      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Right[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Front[x]; y++; 
      } 
     } 
     public void setNoRight() 
     { 
      this.verticesNoRight = new VertexPositionColorNormal[30]; 
      int y = 0; 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Up[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Down[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Back[x]; y++; 
      } 

      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Front[x]; y++; 
      } 
      for (int x = 0; x < 6; x++) 
      { 
       this.vertices[y] = Left[x]; y++; 
      } 
     } 


     public void InitFront() 
     { 
      this.Front = new VertexPositionColorNormal[6]; 

      Front[0].Position = new Vector3(pos.X, pos.Y + 1, pos.Z); 
      Front[0].Color = Color.Blue; 
      Front[0].Normal = normalFront; 
      Front[1].Position = new Vector3(pos.X, pos.Y, pos.Z + 1); 
      Front[1].Color = Color.Blue; 
      Front[1].Normal = normalFront; 
      Front[2].Position = new Vector3(pos.X, pos.Y, pos.Z); 
      Front[2].Color = Color.Blue; 
      Front[2].Normal = normalFront; 


      Front[3].Position = new Vector3(pos.X, pos.Y + 1, pos.Z); 
      Front[3].Color = Color.Blue; 
      Front[3].Normal = normalFront; 
      Front[4].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1); 
      Front[4].Color = Color.Blue; 
      Front[4].Normal = normalFront; 
      Front[5].Position = new Vector3(pos.X, pos.Y, pos.Z + 1); 
      Front[5].Color = Color.Blue; 
      Front[5].Normal = normalFront; 
     } 
     public void InitBack() 
     { 
      this.Back = new VertexPositionColorNormal[6]; 

      Back[0].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1); 
      Back[0].Color = Color.Red; 
      Back[0].Normal = normalBack; 
      Back[1].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z); 
      Back[1].Color = Color.Red; 
      Back[1].Normal = normalBack; 
      Back[2].Position = new Vector3(pos.X + 1, pos.Y, pos.Z); 
      Back[2].Color = Color.Red; 
      Back[2].Normal = normalBack; 


      Back[3].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z + 1); 
      Back[3].Color = Color.Red; 
      Back[3].Normal = normalBack; 
      Back[4].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z); 
      Back[4].Color = Color.Red; 
      Back[4].Normal = normalBack; 
      Back[5].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1); 
      Back[5].Color = Color.Red; 
      Back[5].Normal = normalBack; 
     } 
     public void InitUp() 
     { 
      this.Up = new VertexPositionColorNormal[6]; 

      Up[0].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z); 
      Up[0].Color = Color.Black; 
      Up[0].Normal = normalTop; 
      Up[1].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z + 1); 
      Up[1].Color = Color.Black; 
      Up[1].Normal = normalTop; 
      Up[2].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1); 
      Up[2].Color = Color.Black; 
      Up[2].Normal = normalTop; 


      Up[3].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z); 
      Up[3].Color = Color.Black; 
      Up[3].Normal = normalTop; 
      Up[4].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1); 
      Up[4].Color = Color.Black; 
      Up[4].Normal = normalTop; 
      Up[5].Position = new Vector3(pos.X, pos.Y + 1, pos.Z); 
      Up[5].Color = Color.Black; 
      Up[5].Normal = normalTop; 
     } 
     public void InitDown() 
     { 
      this.Down = new VertexPositionColorNormal[6]; 

      Down[0].Position = new Vector3(pos.X + 1, pos.Y, pos.Z); 
      Down[0].Color = Color.Orange; 
      Down[0].Normal = normalBottom; 
      Down[1].Position = new Vector3(pos.X, pos.Y, pos.Z); 
      Down[1].Color = Color.Orange; 
      Down[1].Normal = normalBottom; 
      Down[2].Position = new Vector3(pos.X, pos.Y, pos.Z + 1); 
      Down[2].Color = Color.Orange; 
      Down[2].Normal = normalBottom; 


      Down[3].Position = new Vector3(pos.X + 1, pos.Y, pos.Z); 
      Down[3].Color = Color.Orange; 
      Down[3].Normal = normalBottom; 
      Down[4].Position = new Vector3(pos.X, pos.Y, pos.Z + 1); 
      Down[4].Color = Color.Orange; 
      Down[4].Normal = normalBottom; 
      Down[5].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1); 
      Down[5].Color = Color.Orange; 
      Down[5].Normal = normalBottom; 
     } 
     public void InitRight() 
     { 
      this.Right = new VertexPositionColorNormal[6]; 

      Right[0].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1); 
      Right[0].Color = Color.Green; 
      Right[0].Normal = normalRight; 
      Right[1].Position = new Vector3(pos.X, pos.Y, pos.Z + 1); 
      Right[1].Color = Color.Green; 
      Right[1].Normal = normalRight; 
      Right[2].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1); 
      Right[2].Color = Color.Green; 
      Right[3].Normal = normalRight; 


      Right[3].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1); 
      Right[3].Color = Color.Green; 
      Right[3].Normal = normalRight; 
      Right[4].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1); 
      Right[4].Color = Color.Green; 
      Right[4].Normal = normalRight; 
      Right[5].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z + 1); 
      Right[5].Color = Color.Green; 
      Right[5].Normal = normalRight; 
     } 
     public void InitLeft() 
     { 
      this.Left = new VertexPositionColorNormal[6]; 

      Left[0].Position = new Vector3(pos.X, pos.Y, pos.Z); 
      Left[0].Color = Color.Aqua; 
      Left[0].Normal = normalLeft; 
      Left[1].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z); 
      Left[1].Color = Color.Aqua; 
      Left[1].Normal = normalLeft; 
      Left[2].Position = new Vector3(pos.X, pos.Y + 1, pos.Z); 
      Left[2].Color = Color.Aqua; 
      Left[2].Normal = normalLeft; 


      Left[3].Position = new Vector3(pos.X, pos.Y, pos.Z); 
      Left[3].Color = Color.Aqua; 
      Left[3].Normal = normalLeft; 
      Left[4].Position = new Vector3(pos.X + 1, pos.Y, pos.Z); 
      Left[4].Color = Color.Aqua; 
      Left[4].Normal = normalLeft; 
      Left[5].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z); 
      Left[5].Color = Color.Aqua; 
      Left[5].Normal = normalLeft; 
     } 

     #endregion 
     private void setTriangleNormal(VertexPositionColorNormal v1, VertexPositionColorNormal v2, VertexPositionColorNormal v3) 
     { 

      Vector3 side1 = v1.Position - v3.Position; 
      Vector3 side2 = v1.Position - v2.Position; 
      Vector3 normal = Vector3.Cross(side1, side2); 

      v1.Normal += normal; 
      v2.Normal += normal; 
      v3.Normal += normal; 

      v1.Position.Normalize(); 
      v1.Position.Normalize(); 
      v1.Position.Normalize(); 
     } 



     public void Clean() 
     { 
      Front = null; 
      Back = null; 
      Right = null; 
      Left = null; 
      Up = null; 
      Down = null; 

     } 
     #endregion 

    } 
} 
+0

是的,當然我們需要代碼。 – tnw

+0

我懷疑無效的深度緩衝區或狀態。你是否篡改了這些設置? –

+0

不,我沒有碰過這個設置:/ – user3761048

回答

1

我的猜測是,你是纏繞在立方體的背面的頂點在錯誤的方向,使他們正常指向立方體的內部而不是外部。在3D代碼中一個非常常見的優化是隻畫出面向相機的三角形。

+0

這不是一個普通的優化。事實上,我不知道有這樣的引擎。你可能是指背面剔除。但是,正常情況與背面剔除無關。所有頂點的纏繞順序決定了臉部是否被剔除。無論如何,你不能在代碼中看到這種情況。 –

+0

背面剔除正是我所指的。它與表面法線直接相關。拋棄具有正常指向相機的任何表面。弄錯訂單將翻轉正常並導致表面被剔除。 –

+0

請小心你的措詞。法線是頂點的屬性。通常,這些屬性是通過面內插值的。該屬性不涉及背面剔除。我們可以引入另一個面的法線,這隻取決於頂點的位置。我們可以根據這個值定義撲殺。但是,這是不明確的,因此不常用。因此,我們只提到纏繞順序。不是另一張臉正常。 –