2013-02-26 126 views
2

我正在開發一個小遊戲,我會畫一個重複紋理的場地(土地)。我的問題是渲染的結果。這給人以看到我的立方體周圍的一切看起來像一個光影。 是否可以標準化燈光或去除繪圖功能中的陰影效果?如何在XNA中刪除模型中的光線/陰影效果?

對不起我的英文不好..

下面是截圖,以便更好地理解我的問題。 enter image description here

這裏我的代碼繪製函數(vertexbuffer實例化模型)

// Draw Function (instancing model - vertexbuffer) 
    public void DrawModelHardwareInstancing(Model model,Texture2D texture, Matrix[] modelBones, 
            Matrix[] instances, Matrix view, Matrix projection) 
    { 
     if (instances.Length == 0) 
      return; 

     // If we have more instances than room in our vertex buffer, grow it to the neccessary size. 
     if ((instanceVertexBuffer == null) || 
      (instances.Length > instanceVertexBuffer.VertexCount)) 
     { 
      if (instanceVertexBuffer != null) 
       instanceVertexBuffer.Dispose(); 

      instanceVertexBuffer = new DynamicVertexBuffer(Game.GraphicsDevice, instanceVertexDeclaration, 
                  instances.Length, BufferUsage.WriteOnly); 
     } 

     // Transfer the latest instance transform matrices into the instanceVertexBuffer. 
     instanceVertexBuffer.SetData(instances, 0, instances.Length, SetDataOptions.Discard); 

     foreach (ModelMesh mesh in model.Meshes) 
     { 
      foreach (ModelMeshPart meshPart in mesh.MeshParts) 
      { 
       // Tell the GPU to read from both the model vertex buffer plus our instanceVertexBuffer. 
       Game.GraphicsDevice.SetVertexBuffers(
        new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset, 0), 
        new VertexBufferBinding(instanceVertexBuffer, 0, 1) 
       ); 

       Game.GraphicsDevice.Indices = meshPart.IndexBuffer; 


       // Set up the instance rendering effect. 
       Effect effect = meshPart.Effect; 
       //effect.CurrentTechnique = effect.Techniques["HardwareInstancing"]; 
       effect.Parameters["World"].SetValue(modelBones[mesh.ParentBone.Index]); 
       effect.Parameters["View"].SetValue(view); 
       effect.Parameters["Projection"].SetValue(projection); 
       effect.Parameters["Texture"].SetValue(texture); 


       // Draw all the instance copies in a single call. 
       foreach (EffectPass pass in effect.CurrentTechnique.Passes) 
       { 
        pass.Apply(); 

        Game.GraphicsDevice.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0, 
                  meshPart.NumVertices, meshPart.StartIndex, 
                  meshPart.PrimitiveCount, instances.Length); 
       } 
      } 



     } 
    } 
    // ### END FUNCTION DrawModelHardwareInstancing 
+0

你知道這是照明問題還是紋理問題? – 2013-02-26 21:14:25

+0

您使用什麼程序設計模型? – 2013-02-26 21:15:51

+0

我使用Blender,是的,我是sur,不是紋理問題 – 2013-02-26 21:23:44

回答

3

問題是您正在使用的立方體網格。法線是平均的,但我想你想讓它們與立方體的表面正交。

您將不得不使用總共24個頂點(每邊4個)而不是8個頂點。每個角落都會有3個頂點具有相同的位置,但不同的法線,每個相鄰面:

two cubes, one with shared normals, one with correct normals

如果FBX導出不能配置正確導出法線簡單地創建自己的立方網:

var vertices = new VertexPositionNormalTexture[24]; 

// Initialize the vertices, set position and texture coordinates 
// ... 

// Set normals 

// front face 
vertices[0].Normal = new Vector3(1, 0, 0); 
vertices[1].Normal = new Vector3(1, 0, 0); 
vertices[2].Normal = new Vector3(1, 0, 0); 
vertices[3].Normal = new Vector3(1, 0, 0); 

// back face 
vertices[4].Normal = new Vector3(-1, 0, 0); 
vertices[5].Normal = new Vector3(-1, 0, 0); 
vertices[6].Normal = new Vector3(-1, 0, 0); 
vertices[7].Normal = new Vector3(-1, 0, 0); 

// ... 
1

看起來你已經有了不正確計算/無法線。 Look at this example, specifically part 3.

正常是描述光將反射離開該頂點/聚如果照射垂直於它的方向的向量。

I like this picture to demonstrate藍線是曲線上每個特定點的法線方向。

在XNA中,你可以計算出正常與頂點的多邊形的像這樣vert1,vert2和vert3:

Vector3 dir = Vector3.Cross(vert2 - vert1, vert3 - vert1); 
Vector3 norm = Vector3.Normalize(dir); 

在有很多,這是通過建模軟件自動完成的情況下這樣的計算是不必要的。如果您要在代碼中創建多維數據集,您可能確實需要執行該計算。

+0

我道歉你是什麼意思?謝謝 – 2013-02-26 21:13:14

+0

我將編輯答案以提供更多詳細信息。 – MrLeap 2013-02-26 21:14:40

+0

好的!我使用Blender生成模型('fbx'),並在我的XNA項目中加載此模型後。這意味着我可以在我的代碼或我的Blender項目中更改燈光效果? – 2013-02-26 21:27:35