2013-02-13 174 views
3

我一直在試圖結合幾個riemers教程來製作紋理和點亮的地形。我幾乎在那裏,但我無法正確使用紋理。我相信SetUpVertices()中的問題是紋理座標的設置。我現在知道代碼讀取它們都設置爲(0,0),我需要它,以便它們被設置爲紋理的角落,但我似乎無法獲得正確的代碼。任何人都可以協助?xna紋理座標

private void SetUpVertices() 
{ 
    vertices = new VertexPositionNormalTexture[terrainWidth * terrainHeight]; 
    for (int x = 0; x < terrainWidth; x++) 
    { 
     for (int y = 0; y < terrainHeight; y++) 
     { 
      vertices[x + y * terrainWidth].Position = new Vector3(x, -y, heightData[x, y]); 
      vertices[x + y * terrainWidth].TextureCoordinate.X = 0; 
      vertices[x + y * terrainWidth].TextureCoordinate.Y = 0; 
     } 
    } 
} 

我已經添加Game1.cs的全部代碼,這pastie http://pastebin.com/REd8QDZA

+0

我已經編輯你的題目。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2013-02-13 17:15:14

+0

考慮到標籤與帖子內容的相關性,如果標題本身也是相關的,標題將不可避免地會包含它們? – luigivampa 2013-02-13 17:19:42

+0

不,不是。請閱讀該鏈接。如果標籤與問題相關,則將它們放置在標籤區域中。如果它們與問題無關,那麼請不要使用它們。 – 2013-02-13 17:34:37

回答

3

您可以通過從0到1的插值伸展在表面紋理:

vertices[x + y * terrainWidth].TextureCoordinate.X = x/(terrainWidth - 1.0); 
vertices[x + y * terrainWidth].TextureCoordinate.Y = y/(terrainHeight - 1.0); 
+0

我知道這會是一件簡單的事情,但我無法做到。我已經堅持了好幾天!我愛你:) – luigivampa 2013-02-13 17:10:15

+0

我注意到,當我縮放地形時,紋理在整個地形上都被拉伸了。我希望實現的是添加到每個三角形的紋理。感謝您已經提供的幫助,但我可以爲您提出另一個建議嗎? – luigivampa 2013-02-13 18:22:30

+0

@luigivampa你需要從0 - 1開始指定紋理,如果你想重複,只需去<0 and > 1. – 2013-02-13 18:23:40