0

Perlin ノイズを使用して地形を生成し、.raw ファイルに保存したいと考えています。

Nehe の HeightMap チュートリアルから、.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は 1 次元なので、x、y 対応を高さの値にどのように書き込めばよいかわかりません。Ken Perlin のページでlibnoiseまたはnoise2 関数を使用して、1024x1024 マトリックスの各値をポイントの高さに対応させることを計画していましたが、.raw ファイルは 1 つの次元に格納されています。どうすれば x を作成できますか? y 通信の仕事がありますか。

4

1 に答える 1

1

A を同じ次元の 2 次元配列とします。

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

この行列を 1 次元配列として設計することもできます。

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

最初のケース (2 次元) では、 のような表記法を使用して、2 番目の配列の最初の要素にアクセスしますA[1][0]。ただし、2 番目のケース (1 次元) では、 のような表記法を使用して同じ要素にアクセスしますA[1 * n + 0]。ここnで、 は論理的に含まれる各配列の長さで、この場合は 3 です。同じインデックス値 (1 と 0) を引き続き使用することに注意してください。ただし、1 次元の場合はn、オフセットにその乗数を含める必要があります。

于 2012-02-08T20:20:37.833 に答える