+ | Some opti

This commit is contained in:
RedShip
2025-01-23 19:44:55 +01:00
parent 896cd02e45
commit c050caa789
12 changed files with 39 additions and 197 deletions

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 18:30:18 by ycontre #+# #+# */ /* Created: 2024/12/23 18:30:18 by ycontre #+# #+# */
/* Updated: 2025/01/19 18:46:38 by ycontre ### ########.fr */ /* Updated: 2025/01/23 18:30:18 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -89,6 +89,7 @@ struct GPUBvh
struct GPUBvhData struct GPUBvhData
{ {
glm::mat4 transform; glm::mat4 transform;
glm::mat4 inv_transform;
alignas(16) glm::vec3 offset; alignas(16) glm::vec3 offset;
float scale; float scale;

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 19:12:51 by ycontre #+# #+# */ /* Created: 2024/12/23 19:12:51 by ycontre #+# #+# */
/* Updated: 2025/01/17 18:17:30 by ycontre ### ########.fr */ /* Updated: 2025/01/23 19:39:33 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -42,21 +42,23 @@ class Triangle : public Object
_vertex2 = glm::vec3(x2, y2, z2); _vertex2 = glm::vec3(x2, y2, z2);
_vertex3 = glm::vec3(x3, y3, z3); _vertex3 = glm::vec3(x3, y3, z3);
// _vertex2 -= _position; //optimization
// _vertex3 -= _position; //optimization
_normal = glm::normalize(glm::cross(_vertex2 - _position, _vertex3 - _position)); //optimization _normal = glm::normalize(glm::cross(_vertex2 - _position, _vertex3 - _position)); //optimization
_centroid = (_position + _vertex2 + _vertex3) / 3.0f; _centroid = (_position + _vertex2 + _vertex3) / 3.0f;
// _vertex2 -= _position; //optimization
// _vertex3 -= _position; //optimization
_mat_index = mat_index; _mat_index = mat_index;
} }
Triangle(const glm::vec3& position, const glm::vec3& vertex2, const glm::vec3& vertex3, const int mat_index) Triangle(const glm::vec3& position, const glm::vec3& vertex2, const glm::vec3& vertex3, const int mat_index)
: Object(position, mat_index), _vertex2(vertex2), _vertex3(vertex3) { : Object(position, mat_index), _vertex2(vertex2), _vertex3(vertex3) {
// _vertex2 -= _position; //optimization
// _vertex3 -= _position; //optimization
_normal = glm::normalize(glm::cross(_vertex2 - _position, _vertex3 - _position)); //optimization _normal = glm::normalize(glm::cross(_vertex2 - _position, _vertex3 - _position)); //optimization
_centroid = (_position + _vertex2 + _vertex3) / 3.0f; _centroid = (_position + _vertex2 + _vertex3) / 3.0f;
// _vertex2 -= _position; //optimization
// _vertex3 -= _position; //optimization
} }
const glm::vec3 &getVertex2() const { return (_vertex2); } const glm::vec3 &getVertex2() const { return (_vertex2); }

View File

@ -987,7 +987,7 @@ vt 0.65883 0.412179
vt 0.544646 0.412179 vt 0.544646 0.412179
vt 0.715921 0.408093 vt 0.715921 0.408093
vt 0.487555 0.408093 vt 0.487555 0.408093
mtllib obj/Lowpoly_tree_sample.mtl mtllib Lowpoly_tree_sample.mtl
usemtl Bark usemtl Bark
g Default g Default
f 7 8 14 13 f 7 8 14 13

View File

@ -25,113 +25,4 @@ qu 11.5 0 -2.5 0 5 0 0 0 15 1
qu -2.5 0 11.5 0 5 0 15 0 0 4 qu -2.5 0 11.5 0 5 0 15 0 0 4
OBJ obj/Lowpoly_tree_sample.obj
OBJ obj/Dragon_8K.obj 0 0.38 0 1
OBJ obj/Dragon_8K.obj 0 0.38 1 1
OBJ obj/Dragon_8K.obj 0 0.38 2 1
OBJ obj/Dragon_8K.obj 0 0.38 3 1
OBJ obj/Dragon_8K.obj 0 0.38 4 1
OBJ obj/Dragon_8K.obj 0 0.38 5 1
OBJ obj/Dragon_8K.obj 0 0.38 6 1
OBJ obj/Dragon_8K.obj 0 0.38 7 1
OBJ obj/Dragon_8K.obj 0 0.38 8 1
OBJ obj/Dragon_8K.obj 0 0.38 9 1
OBJ obj/Dragon_8K.obj 1 0.38 0 1
OBJ obj/Dragon_8K.obj 1 0.38 1 1
OBJ obj/Dragon_8K.obj 1 0.38 2 1
OBJ obj/Dragon_8K.obj 1 0.38 3 1
OBJ obj/Dragon_8K.obj 1 0.38 4 1
OBJ obj/Dragon_8K.obj 1 0.38 5 1
OBJ obj/Dragon_8K.obj 1 0.38 6 1
OBJ obj/Dragon_8K.obj 1 0.38 7 1
OBJ obj/Dragon_8K.obj 1 0.38 8 1
OBJ obj/Dragon_8K.obj 1 0.38 9 1
OBJ obj/Dragon_8K.obj 2 0.38 0 1
OBJ obj/Dragon_8K.obj 2 0.38 1 1
OBJ obj/Dragon_8K.obj 2 0.38 2 1
OBJ obj/Dragon_8K.obj 2 0.38 3 1
OBJ obj/Dragon_8K.obj 2 0.38 4 1
OBJ obj/Dragon_8K.obj 2 0.38 5 1
OBJ obj/Dragon_8K.obj 2 0.38 6 1
OBJ obj/Dragon_8K.obj 2 0.38 7 1
OBJ obj/Dragon_8K.obj 2 0.38 8 1
OBJ obj/Dragon_8K.obj 2 0.38 9 1
OBJ obj/Dragon_8K.obj 3 0.38 0 1
OBJ obj/Dragon_8K.obj 3 0.38 1 1
OBJ obj/Dragon_8K.obj 3 0.38 2 1
OBJ obj/Dragon_8K.obj 3 0.38 3 1
OBJ obj/Dragon_8K.obj 3 0.38 4 1
OBJ obj/Dragon_8K.obj 3 0.38 5 1
OBJ obj/Dragon_8K.obj 3 0.38 6 1
OBJ obj/Dragon_8K.obj 3 0.38 7 1
OBJ obj/Dragon_8K.obj 3 0.38 8 1
OBJ obj/Dragon_8K.obj 3 0.38 9 1
OBJ obj/Dragon_8K.obj 4 0.38 0 1
OBJ obj/Dragon_8K.obj 4 0.38 1 1
OBJ obj/Dragon_8K.obj 4 0.38 2 1
OBJ obj/Dragon_8K.obj 4 0.38 3 1
OBJ obj/Dragon_8K.obj 4 0.38 4 1
OBJ obj/Dragon_8K.obj 4 0.38 5 1
OBJ obj/Dragon_8K.obj 4 0.38 6 1
OBJ obj/Dragon_8K.obj 4 0.38 7 1
OBJ obj/Dragon_8K.obj 4 0.38 8 1
OBJ obj/Dragon_8K.obj 4 0.38 9 1
OBJ obj/Dragon_8K.obj 5 0.38 0 1
OBJ obj/Dragon_8K.obj 5 0.38 1 1
OBJ obj/Dragon_8K.obj 5 0.38 2 1
OBJ obj/Dragon_8K.obj 5 0.38 3 1
OBJ obj/Dragon_8K.obj 5 0.38 4 1
OBJ obj/Dragon_8K.obj 5 0.38 5 1
OBJ obj/Dragon_8K.obj 5 0.38 6 1
OBJ obj/Dragon_8K.obj 5 0.38 7 1
OBJ obj/Dragon_8K.obj 5 0.38 8 1
OBJ obj/Dragon_8K.obj 5 0.38 9 1
OBJ obj/Dragon_8K.obj 6 0.38 0 1
OBJ obj/Dragon_8K.obj 6 0.38 1 1
OBJ obj/Dragon_8K.obj 6 0.38 2 1
OBJ obj/Dragon_8K.obj 6 0.38 3 1
OBJ obj/Dragon_8K.obj 6 0.38 4 1
OBJ obj/Dragon_8K.obj 6 0.38 5 1
OBJ obj/Dragon_8K.obj 6 0.38 6 1
OBJ obj/Dragon_8K.obj 6 0.38 7 1
OBJ obj/Dragon_8K.obj 6 0.38 8 1
OBJ obj/Dragon_8K.obj 6 0.38 9 1
OBJ obj/Dragon_8K.obj 7 0.38 0 1
OBJ obj/Dragon_8K.obj 7 0.38 1 1
OBJ obj/Dragon_8K.obj 7 0.38 2 1
OBJ obj/Dragon_8K.obj 7 0.38 3 1
OBJ obj/Dragon_8K.obj 7 0.38 4 1
OBJ obj/Dragon_8K.obj 7 0.38 5 1
OBJ obj/Dragon_8K.obj 7 0.38 6 1
OBJ obj/Dragon_8K.obj 7 0.38 7 1
OBJ obj/Dragon_8K.obj 7 0.38 8 1
OBJ obj/Dragon_8K.obj 7 0.38 9 1
OBJ obj/Dragon_8K.obj 8 0.38 0 1
OBJ obj/Dragon_8K.obj 8 0.38 1 1
OBJ obj/Dragon_8K.obj 8 0.38 2 1
OBJ obj/Dragon_8K.obj 8 0.38 3 1
OBJ obj/Dragon_8K.obj 8 0.38 4 1
OBJ obj/Dragon_8K.obj 8 0.38 5 1
OBJ obj/Dragon_8K.obj 8 0.38 6 1
OBJ obj/Dragon_8K.obj 8 0.38 7 1
OBJ obj/Dragon_8K.obj 8 0.38 8 1
OBJ obj/Dragon_8K.obj 8 0.38 9 1
OBJ obj/Dragon_8K.obj 9 0.38 0 1
OBJ obj/Dragon_8K.obj 9 0.38 1 1
OBJ obj/Dragon_8K.obj 9 0.38 2 1
OBJ obj/Dragon_8K.obj 9 0.38 3 1
OBJ obj/Dragon_8K.obj 9 0.38 4 1
OBJ obj/Dragon_8K.obj 9 0.38 5 1
OBJ obj/Dragon_8K.obj 9 0.38 6 1
OBJ obj/Dragon_8K.obj 9 0.38 7 1
OBJ obj/Dragon_8K.obj 9 0.38 8 1
OBJ obj/Dragon_8K.obj 9 0.38 9 1

View File

@ -64,6 +64,7 @@ struct GPUVolume
struct GPUBvhData struct GPUBvhData
{ {
mat4 transform; mat4 transform;
mat4 inv_transform;
vec3 offset; vec3 offset;
float scale; float scale;

View File

@ -262,7 +262,7 @@ hitInfo traverseBVHs(Ray ray, inout Stats stats)
GPUBvhData bvh_data = BvhData[i]; GPUBvhData bvh_data = BvhData[i];
mat3 transformMatrix = mat3(bvh_data.transform); mat3 transformMatrix = mat3(bvh_data.transform);
mat3 inverseTransformMatrix = inverse(transformMatrix); mat3 inverseTransformMatrix = mat3(bvh_data.inv_transform);
Ray transformedRay; Ray transformedRay;
transformedRay.direction = normalize(transformMatrix * ray.direction); transformedRay.direction = normalize(transformMatrix * ray.direction);

View File

@ -93,31 +93,26 @@ bool intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit)
return (valid); 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 edgeAB = tri.vertex1 - tri.position;
vec3 vertex2 = obj.vertex2 - obj.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 determinant = -dot(ray.direction, normalVector);
float det = dot(vertex1, pvec); float invDet = 1 / determinant;
vec3 tvec = ray.origin - obj.position;
float dst = dot(ao, normalVector) * invDet;
float invDet = 1.0 / det; float u = dot(edgeAC, dao) * invDet;
float u = dot(tvec, pvec) * invDet; float v = -dot(edgeAB, dao) * invDet;
vec3 qvec = cross(tvec, vertex1); float w = 1 - u - v;
float v = dot(ray.direction, qvec) * invDet;
float t = dot(vertex2, qvec) * invDet; hit.position = ray.origin + ray.direction * dst;
hit.normal = normalVector;
bool valid = abs(det) > 1e-8 && hit.t = dst;
u >= 0.0 && u <= 1.0 && return (determinant >= 1E-8 && dst >= 0 && u >= 0 && v >= 0 && w >= 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);
} }
bool intersectCube(Ray ray, GPUObject obj, out hitInfo hit) bool intersectCube(Ray ray, GPUObject obj, out hitInfo hit)

View File

@ -79,26 +79,6 @@ Ray transparencyRay(hitInfo hit, Ray ray, GPUMaterial mat, inout uint rng_state)
return newRay; 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) Ray newRay(hitInfo hit, Ray ray, inout uint rng_state)
{ {
GPUObject obj; GPUObject obj;

View File

@ -22,7 +22,7 @@ Ray portalRay(Ray ray, hitInfo hit)
ray.origin = portal_2.position + rotation * relative; ray.origin = portal_2.position + rotation * relative;
ray.direction = normalize(rotation * ray.direction); ray.direction = normalize(rotation * ray.direction);
ray.origin += ray.direction * 0.01f; ray.origin += ray.direction * 0.01f;
return (ray); return (ray);
} }
@ -39,6 +39,7 @@ hitInfo traceScene(Ray ray)
GPUObject obj = objects[i]; GPUObject obj = objects[i];
hitInfo temp_hit; hitInfo temp_hit;
if (intersect(ray, obj, temp_hit) && temp_hit.t < hit.t) if (intersect(ray, obj, temp_hit) && temp_hit.t < hit.t)
{ {
hit.t = temp_hit.t; hit.t = temp_hit.t;
@ -118,36 +119,6 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data)
return (hit); 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 traverseBVHs(Ray ray)
{ {
hitInfo hit; hitInfo hit;
@ -160,7 +131,7 @@ hitInfo traverseBVHs(Ray ray)
GPUBvhData bvh_data = BvhData[i]; GPUBvhData bvh_data = BvhData[i];
mat3 transformMatrix = mat3(bvh_data.transform); mat3 transformMatrix = mat3(bvh_data.transform);
mat3 inverseTransformMatrix = inverse(transformMatrix); mat3 inverseTransformMatrix = mat3(bvh_data.inv_transform);
Ray transformedRay; Ray transformedRay;
transformedRay.direction = normalize(transformMatrix * ray.direction); transformedRay.direction = normalize(transformMatrix * ray.direction);

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/16 21:48:48 by TheRed #+# #+# */ /* Created: 2025/01/16 21:48:48 by TheRed #+# #+# */
/* Updated: 2025/01/19 18:30:27 by ycontre ### ########.fr */ /* Updated: 2025/01/23 18:30:20 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 18:29:41 by ycontre #+# #+# */ /* Created: 2024/12/23 18:29:41 by ycontre #+# #+# */
/* Updated: 2025/01/21 14:45:04 by tomoron ### ########.fr */ /* Updated: 2025/01/23 18:31:52 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -155,6 +155,7 @@ void Scene::addBvh(std::vector<Triangle> &triangles, glm::vec3 offset, float sc
new_bvhs_list = bvh->getGPUBvhs(); new_bvhs_list = bvh->getGPUBvhs();
new_bvh_data.transform = transform * scale; new_bvh_data.transform = transform * scale;
new_bvh_data.inv_transform = glm::inverse(new_bvh_data.transform);
new_bvh_data.offset = offset; new_bvh_data.offset = offset;
new_bvh_data.scale = scale; new_bvh_data.scale = scale;
new_bvh_data.bvh_start_index = _gpu_bvh.size(); new_bvh_data.bvh_start_index = _gpu_bvh.size();

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 20:21:13 by ycontre #+# #+# */ /* Created: 2024/10/13 20:21:13 by ycontre #+# #+# */
/* Updated: 2025/01/19 18:58:42 by ycontre ### ########.fr */ /* Updated: 2025/01/23 18:46:34 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -67,7 +67,7 @@ Shader::Shader(std::string vertexPath, std::string fragmentPath, std::string com
const char *fragmentCode = loadFileWithIncludes(fragmentPath); const char *fragmentCode = loadFileWithIncludes(fragmentPath);
const char *computeCode = loadFileWithIncludes(computePath); const char *computeCode = loadFileWithIncludes(computePath);
printWithLineNumbers(computeCode); // printWithLineNumbers(computeCode);
_vertex = glCreateShader(GL_VERTEX_SHADER); _vertex = glCreateShader(GL_VERTEX_SHADER);