gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:40:50 27 4
gpt4 key购买 nike

我正在尝试进行地形跟随,但我在 xz 平面上得到了一个负相机位置。现在我得到了一个越​​界异常,因为行或列是负数。我如何将网格的单元格正确地转换到原点,给出负相机坐标。

这是两个函数

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

最佳答案

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

调用 m_Grid.getHeight 的参数 (2) 不是 z 值。

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

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

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

尝试将您的函数调用更改为此:

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com