Search code examples
c++graphics3ddirectx-11terrain

Negative row and column in terrain following algorithm


I'm trying to do terrain following, and I get a negative camera position in the xz plane. Now I get an out of boundary exception, because the row or the col is negative. How would I transform the cell of the grid to the origin correctly, giving negative camera coordinates.

Here is the two functions

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);

Solution

  • I infer that you’re storing a 50 by 50 matrix inside a 300 by 300 matrix, to represent a grid of 49 by 49 cells. I also infer that m_Grid is an object of type cGrid. Your code appears to contain the following errors:

    Argument(2) of call m_Grid.getHeight is not a z value.

    Argument(3) of call m_Grid.getHeight is inconsistent with argument(5).

    Argument(4) of call m_Grid.getHeight is inconsistent with argument(5).

    Implicit cast of literal float to int in argument(5) of call m_Grid.getHeight - the value will be truncated.

    Try changing your function call to this:

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

    -- where cellspacing is as defined in your diagram.

    Also, try changing parameter(5) of cGrid::getHeight from int _cellSpacing to float _cellSpacing.

    (I have edited this answer a couple of times as my understanding of your code has evolved.)