2012-02-08 79 views
0

我想通過Perlin噪聲生成地形並將其存儲到.raw文件中。如何將地形寫入.raw文件?

Nehe's HeightMap tutorial我知道.raw文件被讀這樣的:

#define MAP_SIZE  1024  

void LoadRawFile(LPSTR strName, int nSize, BYTE *pHeightMap) 
{ 
    FILE *pFile = NULL; 

    // Let's open the file in Read/Binary mode. 
    pFile = fopen(strName, "rb"); 

    // Check to see if we found the file and could open it 
    if (pFile == NULL)  
    { 
     // Display our error message and stop the function 
     MessageBox(NULL, "Can't find the height map!", "Error", MB_OK); 
     return; 
    } 

    // Here we load the .raw file into our pHeightMap data array. 
    // We are only reading in '1', and the size is the (width * height) 
    fread(pHeightMap, 1, nSize, pFile); 

    // After we read the data, it's a good idea to check if everything read fine. 
    int result = ferror(pFile); 

    // Check if we received an error. 
    if (result) 
    { 
     MessageBox(NULL, "Can't get data!", "Error", MB_OK); 
    } 

    // Close the file. 
    fclose(pFile); 

}

pHeightMap是一維的,所以我不明白,我怎麼會寫在x,y對應到一個身高值。我打算使用libnoisenoise2 function on Ken Perlin's page,使一個1024x1024矩陣中的每個值與該點的高度相對應,但.raw文件存儲在一個維度中,我如何使x,y函數在那裏工作?

+1

看看它是如何用在'Height'函數中的。 – user786653 2012-02-08 19:07:20

回答

1

設A是一個2維陣列具有相等的尺寸:

A[3][3] = { 
      {'a', 'b', 'c'}, 
      {'d', 'e', 'f'}, 
      {'g', 'h', 'i'} 
      } 

您也可以設計該矩陣爲一個一維數組:

A[9] = { 
     'a', 'b', 'c', 
     'd', 'e', 'f', 
     'g', 'h', 'i' 
     } 

在第一種情況(2-維度),可以使用類似於A[1][0]的符號訪問第二個數組中的第一個元素。但是,在第二種情況下(1維),可以使用類似於A[1 * n + 0]的符號來訪問相同的元素,其中n是每個邏輯包含數組的長度,在這種情況下爲3。請注意,您仍然使用相同的索引值(1和0),但對於單維情況,您必須包含該偏移量的乘數n