我正在尝试进行地形跟踪,但在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;
}


c&#43;&#43; - 地形跟踪算法中的负行和负列-LMLPHP

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

最佳答案

我推测您要在300 x 300矩阵中存储50 x 50矩阵,以表示49 x 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

(随着我对您的代码的理解不断发展,我已经多次编辑了此答案。)

关于c++ - 地形跟踪算法中的负行和负列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37124960/

10-09 06:10