地形跟随算法中的负行和列

时间:2016-05-09 20:33:39

标签: c++ graphics 3d directx-11 terrain

我正在尝试进行地形跟踪,并且我在xz平面中得到负摄像头位置。现在我得到一个超出边界的异常,因为行或col是负数。如何正确地将网格的单元格转换为原点,给出负的相机坐标。

这是两个函数

int cGrid::getHeightmapEntry(int row, int col)
{
    return m_heightmap[row * 300 + col];
}

float cGrid::getHeight(float x, float z, float _width, float _depth, int _cellSpacing)
{
    // Translate on xz-plane by the transformation that takes
    // the terrain START point to the origin.
    x = ((float)_width / 2.0f) + x;
    z = ((float)_depth / 2.0f) - z;

    // Scale down by the transformation that makes the 
    // cellspacing equal to one.  This is given by 
    // 1 / cellspacing since; cellspacing * 1 / cellspacing = 1.
    x /= (float)_cellSpacing;
    z /= (float)_cellSpacing;

    // From now on, we will interpret our positive z-axis as
    // going in the 'down' direction, rather than the 'up' direction.
    // This allows to extract the row and column simply by 'flooring'
    // x and z:

    float col = ::floorf(x);
    float row = ::floorf(z);

    if (row < 0 || col<0)
    {
        row = 0;
    }
    // get the heights of the quad we're in:
    // 
    //  A   B
    //  *---*
    //  | / |
    //  *---*  
    //  C   D

    float A = getHeightmapEntry(row, col);
    float B = getHeightmapEntry(row, col + 1);
    float C = getHeightmapEntry(row + 1, col);
    float D = getHeightmapEntry(row + 1, col + 1);

    //
    // Find the triangle we are in:
    //

    // Translate by the transformation that takes the upper-left
    // corner of the cell we are in to the origin.  Recall that our 
    // cellspacing was nomalized to 1.  Thus we have a unit square
    // at the origin of our +x -> 'right' and +z -> 'down' system.
    float dx = x - col;
    float dz = z - row;

    // Note the below compuations of u and v are unneccessary, we really
    // only need the height, but we compute the entire vector to emphasis
    // the books discussion.
    float height = 0.0f;
    if (dz < 1.0f - dx)  // upper triangle ABC
    {
        float uy = B - A; // A->B
        float vy = C - A; // A->C

        // Linearly interpolate on each vector.  The height is the vertex
        // height the vectors u and v originate from {A}, plus the heights
        // found by interpolating on each vector u and v.
        height = A + Lerp(0.0f, uy, dx) +  Lerp(0.0f, vy, dz);
    }
    else // lower triangle DCB
    {
        float uy = C - D; // D->C
        float vy = B - D; // D->B

        // Linearly interpolate on each vector.  The height is the vertex
        // height the vectors u and v originate from {D}, plus the heights
        // found by interpolating on each vector u and v.
        height = D + Lerp(0.0f, uy, 1.0f - dx) +  Lerp(0.0f, vy, 1.0f - dz);
    }

    return height;
}

enter image description here

float height = m_Grid.getHeight(position.x, position.y, 49 * 300, 49 * 300, 6.1224489795918367f);
    if (height != 0)
    {
        position.y = height + 10.0f;
    }

    m_Camera.SetPosition(position.x, position.y, position.z);


bool cGrid::readRawFile(std::string fileName, int m, int n)
{

    // A height for each vertex
    std::vector<BYTE> in(m*n);
    std::ifstream inFile(fileName.c_str(), std::ios_base::binary);
    if (!inFile)
        return false;
    inFile.read(
        (char*)&in[0], // buffer
        in.size());// number of bytes to read into buffer
    inFile.close();
    // copy BYTE vector to int vector
    m_heightmap.resize(n*m);
    for (int i = 0; i < in.size(); i++)
        m_heightmap[i] = (float)((in[i])/255)*50.0f;
    return true;
}

m_Grid.readRawFile("castlehm257.raw", 50, 50);

1 个答案:

答案 0 :(得分:1)

我推断您在300乘300矩阵内存储50乘50的矩阵,以表示49乘49个单元的网格。我还推断m_GridcGrid类型的对象。您的代码似乎包含以下错误:

来电m_Grid.getHeight的参数(2)不是z值。

调用m_Grid.getHeight的参数(3)与参数(5)不一致。

调用m_Grid.getHeight的参数(4)与参数(5)不一致。

在调用float的参数(5)中对文字int隐式转换为m_Grid.getHeight - 该值将被截断。

尝试将函数调用更改为:

float height = m_Grid.getHeight(position.x, position.z, 49 * cellspacing, 49 * cellspacing, cellspacing);

- 其中cellspacing如图所示。

另外,请尝试将cGrid::getHeight的参数(5)从int _cellSpacing更改为float _cellSpacing

(因为我对你的代码的理解已经发展,我已经编辑了几次这个答案。)