地形图hittest越界问题

This commit is contained in:
胤龙 2022-12-19 20:31:42 +08:00
parent 8bdf5cc390
commit 35b940a0fe
2 changed files with 11 additions and 3 deletions

View File

@ -158,8 +158,8 @@ void SceneViewer::initializeGL() {
terrainFragmentShader.dispose(); terrainFragmentShader.dispose();
// Test Code Start // Test Code Start
_sky = new SkyBox("E:\\Repositories\\CollegeProjects\\CGAssignments\\FinalProject\\SkyBoxes"); _sky = new SkyBox("D:\\ProgrammingFile\\SceneEditor\\SkyBoxes");
_terrain = new Terrain("E:\\Repositories\\CollegeProjects\\CGAssignments\\FinalProject\\Terrains"); _terrain = new Terrain("D:\\ProgrammingFile\\SceneEditor\\Terrains");
_dirLight = new DirLight(); _dirLight = new DirLight();

View File

@ -147,6 +147,8 @@ float Terrain::GetHeight(float px, float pz) {
float ans = (x - fx) * (Point[gx][gz] - Point[x][z]) + Point[x][z]; float ans = (x - fx) * (Point[gx][gz] - Point[x][z]) + Point[x][z];
//float ans = Point[x][z];
return ans; return ans;
} }
@ -166,9 +168,12 @@ glm::vec3 Terrain::GetNormal(glm::vec3 pos) {
HitRecord Terrain::hit(const Ray& ray) { HitRecord Terrain::hit(const Ray& ray) {
glm::vec3 orig = ray.origin(); glm::vec3 orig = ray.origin();
glm::vec3 dir = ray.direction(); glm::vec3 dir = ray.direction();
if (orig.x >= width / 2 || orig.x <= -width / 2 || orig.z >= height / 2 || orig.z <= -height / 2) {
return HitRecord();
}
// A good ray step is half of the blockScale // A good ray step is half of the blockScale
glm::vec3 rayStep = dir * (float)width * 0.25f; glm::vec3 rayStep = dir;
glm::vec3 rayStartPosition = orig; glm::vec3 rayStartPosition = orig;
// Linear search - Loop until find a point inside and outside the terrain Vector3 // Linear search - Loop until find a point inside and outside the terrain Vector3
@ -179,6 +184,9 @@ HitRecord Terrain::hit(const Ray& ray) {
{ {
lastRayPosition = orig; lastRayPosition = orig;
orig += rayStep; orig += rayStep;
if (orig.x >= width / 2 || orig.x <= -width / 2 || orig.z >= height / 2 || orig.z <= -height / 2) {
return HitRecord();
}
map_height = GetHeight(orig.x, orig.z); map_height = GetHeight(orig.x, orig.z);
} }