Commit 05f0ca0d authored by unknown's avatar unknown
Browse files

LS now works a bit better.

parent f4847c16
......@@ -18,10 +18,10 @@ uint faceRangeIndex(uint in_face, uint out_face)
uvec2 getPatch(const in Mesh mesh, vec3 at_position, int axis, int face){
bool flip_indices = face>2;
return uvec2(
return clamp(uvec2(
flip_indices ? (mesh.linespace.resolution[c_face_width_axises[axis]] - (at_position[c_face_width_axises[axis]] / mesh.linespace.patch_size)) : (at_position[c_face_width_axises[axis]] / mesh.linespace.patch_size),
flip_indices ? (mesh.linespace.resolution[c_face_height_axises[axis]] - (at_position[c_face_height_axises[axis]] / mesh.linespace.patch_size)) : (at_position[c_face_height_axises[axis]] / mesh.linespace.patch_size)
);
), uvec2(0), uvec2(mesh.linespace.resolution[c_face_width_axises[axis]], mesh.linespace.resolution[c_face_height_axises[axis]]));
}
struct PatchInfo
......@@ -33,7 +33,7 @@ struct PatchInfo
float t;
};
float getDistance(const in Ray ray, const in Hit hit)
float getDistance(const in Ray ray, const in Hit hit, int dir_sign)
{
Mesh mesh = b_meshes[hit.mesh];
Triangle triangle = mesh.triangles[hit.triangle];
......@@ -42,7 +42,11 @@ float getDistance(const in Ray ray, const in Hit hit)
Vertex v2 = mesh.vertices[triangle.indices[2]];
Vertex v3 = mesh.vertices[triangle.indices[0]];
return length(hit__getFromBarycentric(hit.barycentric, triangle, position).xyz - ray.origin);
vec3 vector = hit__getFromBarycentric(hit.barycentric, triangle, position).xyz - ray.origin;
dir_sign = int(sign(dot(vector, ray.direction)));
return length(vector);
}
bool getLineIndex(const in Ray ray, const in Mesh mesh, inout uint line_id, inout bool direction_swapped)
......@@ -53,21 +57,23 @@ bool getLineIndex(const in Ray ray, const in Mesh mesh, inout uint line_id, inou
direction_swapped = false;
//If the AABB is not intersected, cancel the traversal.
if (!intersectsRayBounds(ray, mesh.linespace.bounds, tmin, tmax, face_in, face_out))
Ray very_long_ray = ray;
if (!intersectsRayBounds(very_long_ray, mesh.linespace.bounds, tmin, tmax, face_in, face_out))
return false;
PatchInfo entry_info;
PatchInfo exit_info;
// face_in > face_out is an unsupported configuration for reasons of generation performance and memory conservation.
// If that happens, flip tmin, tmax and the two faces accordingly and mark it as flipped to we can take the farthest intersection in the shaft instead of the nearest.
direction_swapped = face_in > face_out;
PatchInfo entry_info;
PatchInfo exit_info;
// -------- t values -------------
entry_info.t = direction_swapped ? tmax : tmin;
exit_info.t = direction_swapped ? tmin : tmax;
// -------- faces ----------------
entry_info.face = direction_swapped ? face_out : face_in;
exit_info.face = direction_swapped ? face_in : face_out;
/////////////////////////////////////////////////////////////
//calculate entry and exit points on the minimum-normalized bounding box
//(so that bounds.min is at (0,0,0) and bounds.max is at the former bounding box's size)
......@@ -77,10 +83,11 @@ bool getLineIndex(const in Ray ray, const in Mesh mesh, inout uint line_id, inou
entry_info.axis = entry_info.face % 3;
exit_info.axis = exit_info.face % 3;
//TODO check if neg faces should be mirrored somehow
// getPatch mirrors the patches on the negative axises per definition.
entry_info.patch_index = getPatch(mesh, entry_info.position, entry_info.axis, entry_info.face);
exit_info.patch_index = getPatch(mesh, exit_info.position, exit_info.axis, exit_info.face);
//Now with all that information, we can retrieve the line index.
uint offset_index = faceRangeIndex(entry_info.face, exit_info.face);
uint offset = mesh.offsets[offset_index];
......@@ -103,7 +110,6 @@ bool linespace__traverse(const in uint mesh_id, const in Ray ray, inout Hit hit)
Ray ray_in = ray;
ray_in.direction = normalize(mat3(mesh.inverse_transformation) * ray_in.direction.xyz);
// TODO: Add a distance check.
ray_in.origin = (mesh.inverse_transformation * vec4(ray_in.origin.xyz, 1)).xyz;
uint line_id;
......@@ -114,8 +120,27 @@ bool linespace__traverse(const in uint mesh_id, const in Ray ray, inout Hit hit)
}
else
{
// TODO: Add a distance check.
Line line = mesh.lines[line_id];
hit = direction_swapped ? line.farthest : line.nearest;
Hit first = direction_swapped ? line.farthest : line.nearest;
Hit second = direction_swapped ? line.nearest : line.farthest;
bool use_second = false;
int dir_sign;
float nearest_distance = getDistance(ray, first, dir_sign);
if(dir_sign == -1){
float farthest_distance = getDistance(ray, second, dir_sign);
use_second = true;
if(dir_sign == -1)
{
return false;
}
}
hit = use_second ? second : first;
hit.mesh = mesh_id;
//Found an intersected shaft. But we don't yet know whether it contains geometry, so look it up as a last step.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment