OpenGL - 当我从一个单元格移动到另一个单元格时,相机移动有一个闩锁
OpenGL - Camera movement have a latch when I move from a cell to another
大家好,我正在使用 C++ 和 GLFW + GLEW 库开发 OpenGL 游戏。
我已经从高度图生成了一个地形,现在我正试图在它上面移动,计算我采取的每一步的相机高度 (y co-ordinate)。
我的地图被分成三角形,这是它的示例图像 GL_LINE 然后是 GL_FILL 多边形模式
我通过计算从一个三角形顶点到另一个顶点的两个向量来计算高度,然后我做了叉积来计算它的法向量;我从它那里获得了我的 y 坐标并且它正在工作但是我注意到当我的高度从一个三角形传递到另一个三角形时并没有得到平滑的变化而且我不知道为什么,因为在同一个三角形上移动时 y 尺度嗯。
我现在将尽可能多地向您展示我的代码,然后我将向您展示一个 运行.
的示例
float Terrain::getCollisionHeight(float x, float z) {
//lato is the side of my triangle and it seems to work when is 1.0f but not with other values, now I put it at 5.0f
//x_pos and z_pos gives the triangle coordinates in the heightmap ( I THINK THE ERROR MIGHT BE IN THE FOLLOWING 2 LINES)
float x_pos = (int)(x / lato);
float z_pos = (int)(z / lato);
cout << "x: " << (int)x << " - z: " << (int)z << endl;
cout << "x pos: " << x_pos << " - z pos: " << z_pos << endl;
cout << "I'm in triangle: " << x_pos << ", " << z_pos << endl;
//i calculate _x and _z to understand if I am in an upper triangle or lower triangle (my draw function draws two triangles at the time as a square)
float _x = x - x_pos;
float _z = z - z_pos;
//then I calculate the coordinates of the two vectors: p(x,y,z) v1 is p1-p0 and v2 is p2-p1
//I always get the height value from a height function that returns the corresponding value of the heightmap given x,z
float x0, x1, x2, y0, y1, y2, z0, z1, z2;
x0 = x_pos*lato;
z0 = z_pos*lato;
y0 = hm->getHeight(x_pos, z_pos);
x2 = x0 + lato;
z2 = z0 + lato;
y2 = hm->getHeight(x_pos + 1, z_pos + 1);
if (_x >= _z) {
cout << "upper triangle" << endl;
x1 = x0 + lato;
z1 = z0;
y1 = hm->getHeight(x_pos + 1, z_pos);
} else {
cout << "lower triangle" << endl;
x1 = x0;
z1 = z0 + lato;
y1 = hm->getHeight(x_pos, z_pos + 1);
}
//i make the cross product and then I return the calculated y (height) value
vec3 v = cross(vec3(x1 - x0, y1 - y0, z1 - z0), vec3(x2 - x1, y2 - y1, z2 - z1));
float y =(-v[0] * x - v[2] * z + v[0] * x0 + v[1] * y0 + v[2] * z0) / v[1];
cout << "p0: " << x0 << " " << y0 << " " << z0 << endl;
cout << "p1: " << x1 << " " << y1 << " " << z1 << endl;
cout << "p2: " << x2 << " " << y2 << " " << z2 << endl;
cout << "PP:" << x << " " << y<< " " << z << endl;
return y;
}
现在我将展示改变三角形时我的位置变化有多大:
这是一个相同的三角形运动,y 得到正确的值
这是我从一个三角形传递到另一个三角形时得到的闩锁
这个闩锁在演奏时被高度感知。
我能做些什么来修复它?非常感谢
这是一个问题的视频,horizon 由于 fraps 存在伪影
https://www.youtube.com/watch?v=CiRKVK89Fkk&feature=youtu.be
我至少发现一处错误。
这个:
float _x = x - x_pos;
float _z = z - z_pos;
应该是这样的:
float _x = (x / lato) - x_pos;
float _z = (z / lato) - z_pos;
这就是它在 lato = 1.
工作的原因
大家好,我正在使用 C++ 和 GLFW + GLEW 库开发 OpenGL 游戏。
我已经从高度图生成了一个地形,现在我正试图在它上面移动,计算我采取的每一步的相机高度 (y co-ordinate)。
我的地图被分成三角形,这是它的示例图像 GL_LINE 然后是 GL_FILL 多边形模式
我通过计算从一个三角形顶点到另一个顶点的两个向量来计算高度,然后我做了叉积来计算它的法向量;我从它那里获得了我的 y 坐标并且它正在工作但是我注意到当我的高度从一个三角形传递到另一个三角形时并没有得到平滑的变化而且我不知道为什么,因为在同一个三角形上移动时 y 尺度嗯。
我现在将尽可能多地向您展示我的代码,然后我将向您展示一个 运行.
的示例float Terrain::getCollisionHeight(float x, float z) {
//lato is the side of my triangle and it seems to work when is 1.0f but not with other values, now I put it at 5.0f
//x_pos and z_pos gives the triangle coordinates in the heightmap ( I THINK THE ERROR MIGHT BE IN THE FOLLOWING 2 LINES)
float x_pos = (int)(x / lato);
float z_pos = (int)(z / lato);
cout << "x: " << (int)x << " - z: " << (int)z << endl;
cout << "x pos: " << x_pos << " - z pos: " << z_pos << endl;
cout << "I'm in triangle: " << x_pos << ", " << z_pos << endl;
//i calculate _x and _z to understand if I am in an upper triangle or lower triangle (my draw function draws two triangles at the time as a square)
float _x = x - x_pos;
float _z = z - z_pos;
//then I calculate the coordinates of the two vectors: p(x,y,z) v1 is p1-p0 and v2 is p2-p1
//I always get the height value from a height function that returns the corresponding value of the heightmap given x,z
float x0, x1, x2, y0, y1, y2, z0, z1, z2;
x0 = x_pos*lato;
z0 = z_pos*lato;
y0 = hm->getHeight(x_pos, z_pos);
x2 = x0 + lato;
z2 = z0 + lato;
y2 = hm->getHeight(x_pos + 1, z_pos + 1);
if (_x >= _z) {
cout << "upper triangle" << endl;
x1 = x0 + lato;
z1 = z0;
y1 = hm->getHeight(x_pos + 1, z_pos);
} else {
cout << "lower triangle" << endl;
x1 = x0;
z1 = z0 + lato;
y1 = hm->getHeight(x_pos, z_pos + 1);
}
//i make the cross product and then I return the calculated y (height) value
vec3 v = cross(vec3(x1 - x0, y1 - y0, z1 - z0), vec3(x2 - x1, y2 - y1, z2 - z1));
float y =(-v[0] * x - v[2] * z + v[0] * x0 + v[1] * y0 + v[2] * z0) / v[1];
cout << "p0: " << x0 << " " << y0 << " " << z0 << endl;
cout << "p1: " << x1 << " " << y1 << " " << z1 << endl;
cout << "p2: " << x2 << " " << y2 << " " << z2 << endl;
cout << "PP:" << x << " " << y<< " " << z << endl;
return y;
}
现在我将展示改变三角形时我的位置变化有多大:
这是一个相同的三角形运动,y 得到正确的值
这是我从一个三角形传递到另一个三角形时得到的闩锁
这个闩锁在演奏时被高度感知。 我能做些什么来修复它?非常感谢
这是一个问题的视频,horizon 由于 fraps 存在伪影 https://www.youtube.com/watch?v=CiRKVK89Fkk&feature=youtu.be
我至少发现一处错误。
这个:
float _x = x - x_pos;
float _z = z - z_pos;
应该是这样的:
float _x = (x / lato) - x_pos;
float _z = (z / lato) - z_pos;
这就是它在 lato = 1.
工作的原因