- // highest point in the cell
- float max_height = std::max(std::max(height[0], height[1]), std::max(height[2], height[3]));
-
- // check where the ray exits the current cell
- // test how far away the ray is from each plane and choose the closest
- x_near = (float(cell.x + step.x) - ray.origin.x) * inverse_direction.x;
- z_near = (float(cell.y + step.y) - ray.origin.z) * inverse_direction.z;
- // if dir is 0, inverse dir is infinity. multiplying 0 by infinity is NaN. min(x, inf) is x, min(x, nan) is x
- t_min = std::min(x_near, z_near);
- // heightof the ray at exit
- float cur_height = ray.origin.y + (t_min * ray.direction.y);
- // lowest point of the ray in the cell
- float ray_low = std::min(prev_height, cur_height);
- // store exit height for next cell's entry height
- prev_height = cur_height;
-
- // check if we might end up below the surface
- // if this is true, there still could be no intersection if the ray is close to parallel to the surface
- // or due to precision issues, which are currently biting me
- if (ray_low < max_height) {
- // possibly, so check individual surfaces
- // the triangles used for rendering are (x,z), (x+1,z), (x,z+1) and
- // (x+1,z),(x+1,z+1), (x,z+1), so height indices 012 and 132
- if (TriangleIntersection(
- ray,
- glm::vec3(float(cell.x + 0), height[0], float(cell.y + 0)),
- glm::vec3(float(cell.x + 1), height[1], float(cell.y + 0)),
- glm::vec3(float(cell.x + 0), height[2], float(cell.y + 1)),
- point
- )) {
- return true;
- }
- if (TriangleIntersection(
- ray,
- glm::vec3(float(cell.x + 1), height[1], float(cell.y + 0)),
- glm::vec3(float(cell.x + 1), height[3], float(cell.y + 1)),
- glm::vec3(float(cell.x + 0), height[2], float(cell.y + 1)),
- point
- )) {
- return true;
- }
- // hmm, maybe I should check against planes and if true test if the XZ of the intersection points
- // lie within their corresponding half-square with some flexibility and somehow pick the right one
- //std::cout << " ray got below max floor height at cell " << cell << " but did not intersect a triangle" << std::endl;
+ // the triangles used for rendering are (x,z), (x+1,z), (x,z+1) and
+ // (x+1,z),(x+1,z+1), (x,z+1), so height indices 012 and 132
+ if (TriangleIntersection(
+ ray,
+ glm::vec3(float(cell.x + 0), height[0], float(cell.y + 0)),
+ glm::vec3(float(cell.x + 1), height[1], float(cell.y + 0)),
+ glm::vec3(float(cell.x + 0), height[2], float(cell.y + 1)),
+ point
+ )) {
+ return true;
+ }
+ if (TriangleIntersection(
+ ray,
+ glm::vec3(float(cell.x + 1), height[1], float(cell.y + 0)),
+ glm::vec3(float(cell.x + 1), height[3], float(cell.y + 1)),
+ glm::vec3(float(cell.x + 0), height[2], float(cell.y + 1)),
+ point
+ )) {
+ return true;