mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 18:48:36 +02:00
+ | Some opti
This commit is contained in:
@ -64,6 +64,7 @@ struct GPUVolume
|
||||
struct GPUBvhData
|
||||
{
|
||||
mat4 transform;
|
||||
mat4 inv_transform;
|
||||
vec3 offset;
|
||||
float scale;
|
||||
|
||||
|
@ -262,7 +262,7 @@ hitInfo traverseBVHs(Ray ray, inout Stats stats)
|
||||
GPUBvhData bvh_data = BvhData[i];
|
||||
|
||||
mat3 transformMatrix = mat3(bvh_data.transform);
|
||||
mat3 inverseTransformMatrix = inverse(transformMatrix);
|
||||
mat3 inverseTransformMatrix = mat3(bvh_data.inv_transform);
|
||||
|
||||
Ray transformedRay;
|
||||
transformedRay.direction = normalize(transformMatrix * ray.direction);
|
||||
|
@ -93,31 +93,26 @@ bool intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit)
|
||||
return (valid);
|
||||
}
|
||||
|
||||
bool intersectTriangle(Ray ray, GPUTriangle obj, out hitInfo hit)
|
||||
bool intersectTriangle(Ray ray, GPUTriangle tri, out hitInfo hit)
|
||||
{
|
||||
vec3 vertex1 = obj.vertex1 - obj.position;
|
||||
vec3 vertex2 = obj.vertex2 - obj.position;
|
||||
vec3 edgeAB = tri.vertex1 - tri.position;
|
||||
vec3 edgeAC = tri.vertex2 - tri.position;
|
||||
vec3 normalVector = tri.normal;
|
||||
vec3 ao = ray.origin - tri.position;
|
||||
vec3 dao = cross(ao, ray.direction);
|
||||
|
||||
vec3 pvec = cross(ray.direction, vertex2);
|
||||
float det = dot(vertex1, pvec);
|
||||
vec3 tvec = ray.origin - obj.position;
|
||||
|
||||
float invDet = 1.0 / det;
|
||||
float u = dot(tvec, pvec) * invDet;
|
||||
vec3 qvec = cross(tvec, vertex1);
|
||||
float v = dot(ray.direction, qvec) * invDet;
|
||||
float t = dot(vertex2, qvec) * invDet;
|
||||
|
||||
bool valid = abs(det) > 1e-8 &&
|
||||
u >= 0.0 && u <= 1.0 &&
|
||||
v >= 0.0 && (u + v) <= 1.0 &&
|
||||
t > 0.0;
|
||||
|
||||
hit.t = t;
|
||||
hit.position = ray.origin + ray.direction * t;
|
||||
hit.normal = obj.normal * sign(-dot(ray.direction, obj.normal));
|
||||
|
||||
return (valid);
|
||||
float determinant = -dot(ray.direction, normalVector);
|
||||
float invDet = 1 / determinant;
|
||||
|
||||
float dst = dot(ao, normalVector) * invDet;
|
||||
float u = dot(edgeAC, dao) * invDet;
|
||||
float v = -dot(edgeAB, dao) * invDet;
|
||||
float w = 1 - u - v;
|
||||
|
||||
hit.position = ray.origin + ray.direction * dst;
|
||||
hit.normal = normalVector;
|
||||
hit.t = dst;
|
||||
return (determinant >= 1E-8 && dst >= 0 && u >= 0 && v >= 0 && w >= 0);
|
||||
}
|
||||
|
||||
bool intersectCube(Ray ray, GPUObject obj, out hitInfo hit)
|
||||
|
@ -79,26 +79,6 @@ Ray transparencyRay(hitInfo hit, Ray ray, GPUMaterial mat, inout uint rng_state)
|
||||
return newRay;
|
||||
}
|
||||
|
||||
|
||||
// Ray transparencyRay(hitInfo hit, Ray ray, GPUMaterial mat, inout uint rng_state)
|
||||
// {
|
||||
// vec3 specular_origin = hit.position + hit.normal * 0.001;
|
||||
// vec3 specular_dir = mix(normalize(reflect(ray.direction, hit.normal)), lambertRay(hit, ray, mat, rng_state).direction, mat.roughness);
|
||||
|
||||
// vec3 transparency_origin = ray.origin + ray.direction * hit.last_t + ray.direction * 0.001;
|
||||
// vec3 transparency_dir = ray.direction;
|
||||
|
||||
// Ray specular_ray = Ray(specular_origin, specular_dir);
|
||||
// Ray transparency_ray = Ray(transparency_origin, transparency_dir);
|
||||
|
||||
// bool is_transparent = (mat.metallic >= randomValue(rng_state));
|
||||
|
||||
// if (is_transparent)
|
||||
// return (transparency_ray);
|
||||
// return (specular_ray);
|
||||
// }
|
||||
|
||||
|
||||
Ray newRay(hitInfo hit, Ray ray, inout uint rng_state)
|
||||
{
|
||||
GPUObject obj;
|
||||
|
@ -22,7 +22,7 @@ Ray portalRay(Ray ray, hitInfo hit)
|
||||
ray.origin = portal_2.position + rotation * relative;
|
||||
ray.direction = normalize(rotation * ray.direction);
|
||||
|
||||
ray.origin += ray.direction * 0.01f;
|
||||
ray.origin += ray.direction * 0.01f;
|
||||
|
||||
return (ray);
|
||||
}
|
||||
@ -39,6 +39,7 @@ hitInfo traceScene(Ray ray)
|
||||
GPUObject obj = objects[i];
|
||||
|
||||
hitInfo temp_hit;
|
||||
|
||||
if (intersect(ray, obj, temp_hit) && temp_hit.t < hit.t)
|
||||
{
|
||||
hit.t = temp_hit.t;
|
||||
@ -118,36 +119,6 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data)
|
||||
return (hit);
|
||||
}
|
||||
|
||||
// hitInfo traverseBVHs(Ray ray)
|
||||
// {
|
||||
// hitInfo hit;
|
||||
|
||||
// hit.t = 1e30;
|
||||
// hit.obj_index = -1;
|
||||
|
||||
// for (int i = 0; i < u_bvhNum; i++)
|
||||
// {
|
||||
// GPUBvhData bvh_data = BvhData[i];
|
||||
|
||||
// ray.origin -= bvh_data.offset;
|
||||
// hitInfo temp_hit = traceBVH(ray, bvh_data);
|
||||
|
||||
// if (temp_hit.t < hit.t)
|
||||
// {
|
||||
// hit.t = temp_hit.t;
|
||||
// hit.last_t = temp_hit.last_t;
|
||||
// hit.obj_index = temp_hit.obj_index;
|
||||
// hit.mat_index = temp_hit.mat_index;
|
||||
// hit.position = temp_hit.position;
|
||||
// hit.normal = temp_hit.normal;
|
||||
// }
|
||||
|
||||
// ray.origin += bvh_data.offset;
|
||||
// }
|
||||
|
||||
// return (hit);
|
||||
// }
|
||||
|
||||
hitInfo traverseBVHs(Ray ray)
|
||||
{
|
||||
hitInfo hit;
|
||||
@ -160,7 +131,7 @@ hitInfo traverseBVHs(Ray ray)
|
||||
GPUBvhData bvh_data = BvhData[i];
|
||||
|
||||
mat3 transformMatrix = mat3(bvh_data.transform);
|
||||
mat3 inverseTransformMatrix = inverse(transformMatrix);
|
||||
mat3 inverseTransformMatrix = mat3(bvh_data.inv_transform);
|
||||
|
||||
Ray transformedRay;
|
||||
transformedRay.direction = normalize(transformMatrix * ray.direction);
|
||||
|
Reference in New Issue
Block a user