mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 18:48:36 +02:00
+ | Dragon and bvh
This commit is contained in:
@ -3,8 +3,8 @@ Pos=60,60
|
|||||||
Size=400,400
|
Size=400,400
|
||||||
|
|
||||||
[Window][Camera]
|
[Window][Camera]
|
||||||
Pos=1682,11
|
Pos=1652,11
|
||||||
Size=229,184
|
Size=259,192
|
||||||
|
|
||||||
[Window][Material]
|
[Window][Material]
|
||||||
Pos=1650,202
|
Pos=1650,202
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
# include "RT.hpp"
|
# include "RT.hpp"
|
||||||
|
|
||||||
|
struct GPUTriangle;
|
||||||
struct GPUObject;
|
struct GPUObject;
|
||||||
struct GPUBvh;
|
struct GPUBvh;
|
||||||
|
|
||||||
@ -30,13 +31,13 @@ struct AABB
|
|||||||
class BVH
|
class BVH
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
BVH(std::vector<GPUObject> &primitives, int first_primitive, int primitive_count);
|
BVH(std::vector<GPUTriangle> &primitives, int first_primitive, int primitive_count);
|
||||||
|
|
||||||
|
|
||||||
void showAABB(Scene *scene);
|
void showAABB(Scene *scene);
|
||||||
|
|
||||||
void updateBounds(std::vector <GPUObject> &primitives);
|
void updateBounds(std::vector <GPUTriangle> &primitives);
|
||||||
void subdivide(std::vector<GPUObject> &primitives);
|
void subdivide(std::vector<GPUTriangle> &primitives);
|
||||||
|
|
||||||
int size();
|
int size();
|
||||||
|
|
||||||
|
@ -33,6 +33,16 @@ struct GPUObject
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct GPUTriangle
|
||||||
|
{
|
||||||
|
alignas(16) glm::vec3 position;
|
||||||
|
alignas(16) glm::vec3 vertex1;
|
||||||
|
alignas(16) glm::vec3 vertex2;
|
||||||
|
alignas(16) glm::vec3 normal;
|
||||||
|
|
||||||
|
int mat_index;
|
||||||
|
};
|
||||||
|
|
||||||
struct GPUMaterial
|
struct GPUMaterial
|
||||||
{
|
{
|
||||||
alignas(16) glm::vec3 color;
|
alignas(16) glm::vec3 color;
|
||||||
@ -86,6 +96,8 @@ class Scene
|
|||||||
std::set<int> getGPULights();
|
std::set<int> getGPULights();
|
||||||
|
|
||||||
const std::vector<GPUObject> &getObjectData() const;
|
const std::vector<GPUObject> &getObjectData() const;
|
||||||
|
const std::vector<GPUTriangle> &getTriangleData() const;
|
||||||
|
|
||||||
std::vector<GPUMaterial> &getMaterialData();
|
std::vector<GPUMaterial> &getMaterialData();
|
||||||
GPUVolume &getVolume();
|
GPUVolume &getVolume();
|
||||||
std::vector<GPUBvh> &getBVH();
|
std::vector<GPUBvh> &getBVH();
|
||||||
@ -97,6 +109,8 @@ class Scene
|
|||||||
std::vector<GPUBvh> _gpu_bvh;
|
std::vector<GPUBvh> _gpu_bvh;
|
||||||
|
|
||||||
std::vector<GPUObject> _gpu_objects;
|
std::vector<GPUObject> _gpu_objects;
|
||||||
|
std::vector<GPUTriangle> _gpu_triangles;
|
||||||
|
|
||||||
std::vector<GPUMaterial> _gpu_materials;
|
std::vector<GPUMaterial> _gpu_materials;
|
||||||
|
|
||||||
std::set<int> _gpu_lights;
|
std::set<int> _gpu_lights;
|
||||||
|
@ -45,7 +45,7 @@ class Triangle : public Object
|
|||||||
// _vertex2 -= _position; //optimization
|
// _vertex2 -= _position; //optimization
|
||||||
// _vertex3 -= _position; //optimization
|
// _vertex3 -= _position; //optimization
|
||||||
|
|
||||||
_normal = glm::normalize(glm::cross(_vertex2, _vertex3)); //optimization
|
_normal = glm::normalize(glm::cross(_vertex2 - _position, _vertex3 - _position)); //optimization
|
||||||
|
|
||||||
_mat_index = mat_index;
|
_mat_index = mat_index;
|
||||||
}
|
}
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
newmtl 10517_Motorcycle_Helmet_v01
|
|
||||||
Ns 10.0000
|
|
||||||
Ni 1.5000
|
|
||||||
d 1.0000
|
|
||||||
Tr 0.0000
|
|
||||||
Tf 1.0000 1.0000 1.0000
|
|
||||||
illum 2
|
|
||||||
Ka 0.5880 0.5880 0.5880
|
|
||||||
Kd 0.5880 0.5880 0.5880
|
|
||||||
Ks 0.0000 0.0000 0.0000
|
|
||||||
Ke 0.0000 0.0000 0.0000
|
|
||||||
map_Ka 10517_Motorcycle_Helmet_v01_difuse.jpg
|
|
||||||
map_Kd 10517_Motorcycle_Helmet_v01_difuse.jpg
|
|
||||||
|
|
||||||
newmtl 10517_Motorcycle_Helmet_v01_glass
|
|
||||||
Ns 10.0000
|
|
||||||
Ni 1.5000
|
|
||||||
d 0.3000
|
|
||||||
Tr 0.7000
|
|
||||||
Tf 0.3000 0.3000 0.3000
|
|
||||||
illum 2
|
|
||||||
Ka 0.2549 0.2549 0.2549
|
|
||||||
Kd 0.2549 0.2549 0.2549
|
|
||||||
Ks 0.0000 0.0000 0.0000
|
|
||||||
Ke 0.0000 0.0000 0.0000
|
|
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Before Width: | Height: | Size: 108 KiB |
391704
obj/Dragon_80K.obj
Normal file
391704
obj/Dragon_80K.obj
Normal file
File diff suppressed because it is too large
Load Diff
38934
obj/Dragon_8K.obj
Normal file
38934
obj/Dragon_8K.obj
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,28 +1,27 @@
|
|||||||
CAM 0.582504 9.47921 50.0705 -20.6001 -87.3991 0 4 90 5
|
CAM 1.29625 0.667446 0.00169364 -24.6 178.8 0.03 1.3 45 5
|
||||||
|
|
||||||
MAT 200 200 200 0.0 0.0 0.0 //white
|
|
||||||
MAT 255 50 50 0.0 1.0 0.05 //red
|
|
||||||
MAT 50 255 50 0.0 0.0 0.0 //green
|
|
||||||
MAT 100 100 255 0.0 0.0 0.0 //blue
|
|
||||||
MAT 255 100 255 0.0 0.0 0.0 //purple
|
|
||||||
MAT 30 30 30 0.0 0.0 0.0 //black
|
|
||||||
|
|
||||||
MAT 255 255 255 5.0 0.0 0.0 //light
|
|
||||||
|
|
||||||
MAT 255 255 255 0.0 0.0 0.0 TRN // glass
|
|
||||||
|
|
||||||
# sp 0 -25 0 50 1
|
|
||||||
|
|
||||||
# tr -4 5 0 -3 5 0 -4 5 1 0
|
|
||||||
# tr 0 7 2 1 4 2 0 5 3 0
|
|
||||||
# tr 3 7 2 4 5 1 2 6 3 0
|
|
||||||
|
|
||||||
# tr 7 5 0 8 5 0 7 5 1 0
|
|
||||||
# tr 7 7 2 8 4 2 7 5 3 0
|
|
||||||
# tr 10 7 2 11 5 1 8 6 3 0
|
|
||||||
# tr 15 -2 4 11 5 1 8 6 3 0
|
|
||||||
# tr 15 -2 4 11 5 1 7 5 9 0
|
|
||||||
# tr 12 4 4 4 3 1 7 5 9 0
|
|
||||||
|
|
||||||
|
|
||||||
OBJ obj/10517_Motorcycle_Helmet_v01_L3.obj
|
MAT 255 255 255 0.0 0.75 1.0 // white 0
|
||||||
|
|
||||||
|
MAT 255 10 10 0.0 0.0 0.0 // red 1
|
||||||
|
MAT 30 30 30 0.0 0.0 0.0 // gray 2
|
||||||
|
MAT 10 255 10 0.0 0.0 0.0 // green 3
|
||||||
|
MAT 255 255 255 0.0 0.0 0.0 // white 4
|
||||||
|
MAT 50 50 255 0.0 0.0 0.0 // white 5
|
||||||
|
|
||||||
|
MAT 255 255 255 5.0 0.0 0.0 // white light 6
|
||||||
|
|
||||||
|
|
||||||
|
MAT 255 255 255 0.0 0.0 0.0 TRN // glass 7
|
||||||
|
|
||||||
|
pl 0 0 -2 0 0 1 1 // back wall
|
||||||
|
pl 0 0 2 0 0 -1 5 // front wall
|
||||||
|
pl 2 0 0 -1 0 0 3 // right wall
|
||||||
|
pl -2 0 0 1 0 0 2 // left wall
|
||||||
|
pl 0 2 0 0 -1 0 0 // ceiling
|
||||||
|
pl 0 -2 0 0 1 0 2 // floor
|
||||||
|
|
||||||
|
qu -1 1.999 -1 2 0 0 0 0 2 6
|
||||||
|
|
||||||
|
OBJ obj/Dragon_80K.obj
|
||||||
|
|
||||||
|
@ -20,6 +20,16 @@ struct GPUObject {
|
|||||||
int type; // 4
|
int type; // 4
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct GPUTriangle
|
||||||
|
{
|
||||||
|
vec3 position;
|
||||||
|
vec3 vertex1;
|
||||||
|
vec3 vertex2;
|
||||||
|
vec3 normal;
|
||||||
|
|
||||||
|
int mat_index;
|
||||||
|
};
|
||||||
|
|
||||||
struct GPUMaterial
|
struct GPUMaterial
|
||||||
{
|
{
|
||||||
vec3 color; // 12 + 4
|
vec3 color; // 12 + 4
|
||||||
@ -51,21 +61,6 @@ struct GPUVolume
|
|||||||
int enabled;
|
int enabled;
|
||||||
};
|
};
|
||||||
|
|
||||||
layout(std430, binding = 1) buffer ObjectBuffer
|
|
||||||
{
|
|
||||||
GPUObject objects[];
|
|
||||||
};
|
|
||||||
|
|
||||||
layout(std430, binding = 2) buffer MaterialBuffer
|
|
||||||
{
|
|
||||||
GPUMaterial materials[];
|
|
||||||
};
|
|
||||||
|
|
||||||
layout(std430, binding = 3) buffer LightsBuffer
|
|
||||||
{
|
|
||||||
int lightsIndex[];
|
|
||||||
};
|
|
||||||
|
|
||||||
struct GPUBvh
|
struct GPUBvh
|
||||||
{
|
{
|
||||||
vec3 min;
|
vec3 min;
|
||||||
@ -79,7 +74,28 @@ struct GPUBvh
|
|||||||
int first_primitive;
|
int first_primitive;
|
||||||
int primitive_count;
|
int primitive_count;
|
||||||
};
|
};
|
||||||
layout(std430, binding = 4) buffer BvhBuffer
|
|
||||||
|
layout(std430, binding = 1) buffer ObjectBuffer
|
||||||
|
{
|
||||||
|
GPUObject objects[];
|
||||||
|
};
|
||||||
|
|
||||||
|
layout(std430, binding = 2) buffer TriangleBuffer
|
||||||
|
{
|
||||||
|
GPUTriangle triangles[];
|
||||||
|
};
|
||||||
|
|
||||||
|
layout(std430, binding = 3) buffer MaterialBuffer
|
||||||
|
{
|
||||||
|
GPUMaterial materials[];
|
||||||
|
};
|
||||||
|
|
||||||
|
layout(std430, binding = 4) buffer LightsBuffer
|
||||||
|
{
|
||||||
|
int lightsIndex[];
|
||||||
|
};
|
||||||
|
|
||||||
|
layout(std430, binding = 5) buffer BvhBuffer
|
||||||
{
|
{
|
||||||
GPUBvh bvh[];
|
GPUBvh bvh[];
|
||||||
};
|
};
|
||||||
@ -116,6 +132,7 @@ struct hitInfo
|
|||||||
vec3 normal;
|
vec3 normal;
|
||||||
vec3 position;
|
vec3 position;
|
||||||
int obj_index;
|
int obj_index;
|
||||||
|
int mat_index;
|
||||||
};
|
};
|
||||||
|
|
||||||
#include "shaders/random.glsl"
|
#include "shaders/random.glsl"
|
||||||
@ -153,8 +170,7 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
|
|||||||
if (volume.enabled != 0)
|
if (volume.enabled != 0)
|
||||||
transmittance *= exp(-volume.sigma_t * hit.t);
|
transmittance *= exp(-volume.sigma_t * hit.t);
|
||||||
|
|
||||||
GPUObject obj = objects[hit.obj_index];
|
GPUMaterial mat = materials[hit.mat_index];
|
||||||
GPUMaterial mat = materials[obj.mat_index];
|
|
||||||
|
|
||||||
// RR
|
// RR
|
||||||
float p = max(color.r, max(color.g, color.b));
|
float p = max(color.r, max(color.g, color.b));
|
||||||
|
@ -41,6 +41,20 @@ layout(std430, binding = 1) buffer ObjectBuffer
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct GPUTriangle
|
||||||
|
{
|
||||||
|
vec3 position;
|
||||||
|
vec3 vertex1;
|
||||||
|
vec3 vertex2;
|
||||||
|
vec3 normal;
|
||||||
|
|
||||||
|
int mat_index;
|
||||||
|
};
|
||||||
|
layout(std430, binding = 2) buffer TriangleBuffer
|
||||||
|
{
|
||||||
|
GPUTriangle triangles[];
|
||||||
|
};
|
||||||
|
|
||||||
struct GPUBvh
|
struct GPUBvh
|
||||||
{
|
{
|
||||||
vec3 min;
|
vec3 min;
|
||||||
@ -54,7 +68,7 @@ struct GPUBvh
|
|||||||
int first_primitive;
|
int first_primitive;
|
||||||
int primitive_count;
|
int primitive_count;
|
||||||
};
|
};
|
||||||
layout(std430, binding = 4) buffer BvhBuffer
|
layout(std430, binding = 5) buffer BvhBuffer
|
||||||
{
|
{
|
||||||
GPUBvh bvh[];
|
GPUBvh bvh[];
|
||||||
};
|
};
|
||||||
@ -78,10 +92,10 @@ struct hitInfo
|
|||||||
vec3 normal;
|
vec3 normal;
|
||||||
vec3 position;
|
vec3 position;
|
||||||
int obj_index;
|
int obj_index;
|
||||||
|
int mat_index;
|
||||||
};
|
};
|
||||||
|
|
||||||
#include "shaders/intersect.glsl"
|
#include "shaders/intersect.glsl"
|
||||||
#include "shaders/bvh.glsl"
|
|
||||||
|
|
||||||
int traceRay(Ray ray)
|
int traceRay(Ray ray)
|
||||||
{
|
{
|
||||||
@ -100,9 +114,12 @@ int traceRay(Ray ray)
|
|||||||
return (num_hit);
|
return (num_hit);
|
||||||
}
|
}
|
||||||
|
|
||||||
int traceBVH(Ray ray)
|
hitInfo traceBVH(Ray ray, inout int num_hit)
|
||||||
{
|
{
|
||||||
int num_hit = 0;
|
hitInfo hit;
|
||||||
|
|
||||||
|
hit.t = 1e30;
|
||||||
|
hit.obj_index = -1;
|
||||||
|
|
||||||
const int MAX_STACK_SIZE = 64;
|
const int MAX_STACK_SIZE = 64;
|
||||||
int stack[MAX_STACK_SIZE];
|
int stack[MAX_STACK_SIZE];
|
||||||
@ -126,11 +143,15 @@ int traceBVH(Ray ray)
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < node.primitive_count; i++)
|
for (int i = 0; i < node.primitive_count; i++)
|
||||||
{
|
{
|
||||||
GPUObject obj = objects[node.first_primitive + i];
|
GPUTriangle obj = triangles[node.first_primitive + i];
|
||||||
|
|
||||||
hitInfo tmp;
|
hitInfo temp_hit;
|
||||||
if (intersect(ray, obj, tmp))
|
if (intersectTriangle(ray, obj, temp_hit) && temp_hit.t > 0.0f && temp_hit.t < hit.t + 0.0001)
|
||||||
num_hit;
|
{
|
||||||
|
hit.t = temp_hit.t;
|
||||||
|
hit.normal = temp_hit.normal;
|
||||||
|
hit.obj_index = node.first_primitive + i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,7 +165,7 @@ int traceBVH(Ray ray)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (num_hit);
|
return (hit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -169,9 +190,19 @@ void main()
|
|||||||
vec2 uv = ((vec2(pixel_coords)) / u_resolution) * 2.0 - 1.0;;
|
vec2 uv = ((vec2(pixel_coords)) / u_resolution) * 2.0 - 1.0;;
|
||||||
uv.x *= u_resolution.x / u_resolution.y;
|
uv.x *= u_resolution.x / u_resolution.y;
|
||||||
|
|
||||||
Ray ray = initRay(uv);
|
|
||||||
int hits = traceBVH(ray);
|
|
||||||
|
|
||||||
vec3 color = vec3(float(hits) / float(u_bvhNum / 4));
|
Ray ray = initRay(uv);
|
||||||
imageStore(output_image, pixel_coords, vec4(color, 1.));
|
|
||||||
|
int hits = 0;
|
||||||
|
hitInfo hit = traceBVH(ray, hits);
|
||||||
|
|
||||||
|
vec3 color = vec3(0.);
|
||||||
|
// if (hit.obj_index != -1)
|
||||||
|
// color = vec3(hit.normal);
|
||||||
|
if (hits > 150)
|
||||||
|
color = vec3(1., 0., 0.);
|
||||||
|
else
|
||||||
|
color = vec3(float(hits) / float(100));
|
||||||
|
|
||||||
|
imageStore(output_image, pixel_coords, vec4(color, 1.));
|
||||||
}
|
}
|
@ -93,35 +93,32 @@ bool intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit)
|
|||||||
return (valid);
|
return (valid);
|
||||||
}
|
}
|
||||||
|
|
||||||
// bool intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit)
|
bool intersectTriangle(Ray ray, GPUTriangle obj, out hitInfo hit)
|
||||||
// {
|
{
|
||||||
// vec3 pvec = cross(ray.direction, obj.vertex2);
|
vec3 vertex1 = obj.vertex1 - obj.position;
|
||||||
// float det = dot(obj.vertex1, pvec);
|
vec3 vertex2 = obj.vertex2 - obj.position;
|
||||||
|
|
||||||
// if (abs(det) < 1e-8) return (false); // det < 0.0
|
vec3 pvec = cross(ray.direction, vertex2);
|
||||||
|
float det = dot(vertex1, pvec);
|
||||||
|
vec3 tvec = ray.origin - obj.position;
|
||||||
|
|
||||||
// float invDet = 1.0 / det;
|
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;
|
||||||
|
|
||||||
// vec3 tvec = ray.origin - obj.position;
|
bool valid = abs(det) > 1e-8 &&
|
||||||
|
u >= 0.0 && u <= 1.0 &&
|
||||||
|
v >= 0.0 && (u + v) <= 1.0 &&
|
||||||
|
t > 0.0;
|
||||||
|
|
||||||
// float u = dot(tvec, pvec) * invDet;
|
hit.t = t;
|
||||||
// if (u < 0.0 || u > 1.0) return (false);
|
hit.position = ray.origin + ray.direction * t;
|
||||||
|
hit.normal = obj.normal * sign(-dot(ray.direction, obj.normal));
|
||||||
|
|
||||||
// vec3 qvec = cross(tvec, obj.vertex1);
|
return (valid);
|
||||||
// float v = dot(ray.direction, qvec) * invDet;
|
}
|
||||||
// if (v < 0.0 || u + v > 1.0) return (false);
|
|
||||||
|
|
||||||
// float t = dot(obj.vertex2, qvec) * invDet;
|
|
||||||
// if (t <= 0.0) return (false);
|
|
||||||
|
|
||||||
// hit.t = t;
|
|
||||||
// hit.position = ray.origin + ray.direction * t;
|
|
||||||
|
|
||||||
// vec3 normal = obj.normal;
|
|
||||||
// hit.normal = dot(ray.direction, normal) < 0.0 ? normal : -normal;
|
|
||||||
|
|
||||||
// return (true);
|
|
||||||
// }
|
|
||||||
|
|
||||||
bool intersectCube(Ray ray, GPUObject obj, out hitInfo hit)
|
bool intersectCube(Ray ray, GPUObject obj, out hitInfo hit)
|
||||||
{
|
{
|
||||||
|
@ -46,6 +46,7 @@ hitInfo traceScene(Ray ray)
|
|||||||
hit.t = temp_hit.t;
|
hit.t = temp_hit.t;
|
||||||
hit.last_t = temp_hit.last_t;
|
hit.last_t = temp_hit.last_t;
|
||||||
hit.obj_index = i;
|
hit.obj_index = i;
|
||||||
|
hit.mat_index = obj.mat_index;
|
||||||
hit.position = temp_hit.position;
|
hit.position = temp_hit.position;
|
||||||
hit.normal = temp_hit.normal;
|
hit.normal = temp_hit.normal;
|
||||||
}
|
}
|
||||||
@ -83,14 +84,15 @@ hitInfo traceBVH(Ray ray)
|
|||||||
{
|
{
|
||||||
for (int i = 0; i < node.primitive_count; i++)
|
for (int i = 0; i < node.primitive_count; i++)
|
||||||
{
|
{
|
||||||
GPUObject obj = objects[node.first_primitive + i];
|
GPUTriangle obj = triangles[node.first_primitive + i];
|
||||||
|
|
||||||
hitInfo temp_hit;
|
hitInfo temp_hit;
|
||||||
if (intersect(ray, obj, temp_hit) && temp_hit.t > 0.0f && temp_hit.t < hit.t + 0.0001)
|
if (intersectTriangle(ray, obj, temp_hit) && temp_hit.t > 0.0f && temp_hit.t < hit.t + 0.0001)
|
||||||
{
|
{
|
||||||
hit.t = temp_hit.t;
|
hit.t = temp_hit.t;
|
||||||
hit.last_t = temp_hit.last_t;
|
hit.last_t = temp_hit.last_t;
|
||||||
hit.obj_index = node.first_primitive + i;
|
hit.obj_index = node.first_primitive + i;
|
||||||
|
hit.mat_index = obj.mat_index;
|
||||||
hit.position = temp_hit.position;
|
hit.position = temp_hit.position;
|
||||||
hit.normal = temp_hit.normal;
|
hit.normal = temp_hit.normal;
|
||||||
}
|
}
|
||||||
@ -112,7 +114,10 @@ hitInfo traceBVH(Ray ray)
|
|||||||
|
|
||||||
hitInfo traceRay(Ray ray)
|
hitInfo traceRay(Ray ray)
|
||||||
{
|
{
|
||||||
if (true)
|
hitInfo hitBVH = traceBVH(ray);
|
||||||
return (traceBVH(ray));
|
hitInfo hitScene = traceScene(ray);
|
||||||
return (traceScene(ray));
|
|
||||||
|
if (hitBVH.t < hitScene.t)
|
||||||
|
return (hitBVH);
|
||||||
|
return (hitScene);
|
||||||
}
|
}
|
22
srcs/RT.cpp
22
srcs/RT.cpp
@ -27,10 +27,14 @@ int main(int argc, char **argv)
|
|||||||
glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &max_gpu_size);
|
glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &max_gpu_size);
|
||||||
|
|
||||||
const std::vector<GPUObject> &object_data = scene.getObjectData();
|
const std::vector<GPUObject> &object_data = scene.getObjectData();
|
||||||
|
const std::vector<GPUTriangle> &triangle_data = scene.getTriangleData();
|
||||||
|
|
||||||
const std::vector<GPUMaterial> &material_data = scene.getMaterialData();
|
const std::vector<GPUMaterial> &material_data = scene.getMaterialData();
|
||||||
|
|
||||||
std::cout << "Sending " << object_data.size() << " objects for " << \
|
std::cout << "Sending " << object_data.size() << " objects for " << \
|
||||||
object_data.size() * sizeof(GPUObject) + material_data.size() * sizeof(GPUMaterial) \
|
object_data.size() * sizeof(GPUObject) + \
|
||||||
|
triangle_data.size() * sizeof(GPUTriangle) + \
|
||||||
|
material_data.size() * sizeof(GPUMaterial) \
|
||||||
<< " / " << max_gpu_size << " bytes" << std::endl;
|
<< " / " << max_gpu_size << " bytes" << std::endl;
|
||||||
|
|
||||||
GLuint objectSSBO;
|
GLuint objectSSBO;
|
||||||
@ -39,24 +43,29 @@ int main(int argc, char **argv)
|
|||||||
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GPUObject) * object_data.size(), nullptr, GL_STATIC_DRAW);
|
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GPUObject) * object_data.size(), nullptr, GL_STATIC_DRAW);
|
||||||
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, objectSSBO);
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, objectSSBO);
|
||||||
|
|
||||||
|
GLuint trianglesSSBO;
|
||||||
|
glGenBuffers(1, &trianglesSSBO);
|
||||||
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, trianglesSSBO);
|
||||||
|
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GPUTriangle) * triangle_data.size(), nullptr, GL_STATIC_DRAW);
|
||||||
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, trianglesSSBO);
|
||||||
|
|
||||||
GLuint materialSSBO;
|
GLuint materialSSBO;
|
||||||
glGenBuffers(1, &materialSSBO);
|
glGenBuffers(1, &materialSSBO);
|
||||||
glBindBuffer(GL_SHADER_STORAGE_BUFFER, materialSSBO);
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, materialSSBO);
|
||||||
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GPUMaterial) * material_data.size(), nullptr, GL_STATIC_DRAW);
|
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GPUMaterial) * material_data.size(), nullptr, GL_STATIC_DRAW);
|
||||||
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, materialSSBO);
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, materialSSBO);
|
||||||
|
|
||||||
|
|
||||||
GLuint lightSSBO;
|
GLuint lightSSBO;
|
||||||
glGenBuffers(1, &lightSSBO);
|
glGenBuffers(1, &lightSSBO);
|
||||||
glBindBuffer(GL_SHADER_STORAGE_BUFFER, lightSSBO);
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, lightSSBO);
|
||||||
glBufferData(GL_SHADER_STORAGE_BUFFER, scene.getGPULights().size() * sizeof(int), nullptr, GL_STATIC_DRAW);
|
glBufferData(GL_SHADER_STORAGE_BUFFER, scene.getGPULights().size() * sizeof(int), nullptr, GL_STATIC_DRAW);
|
||||||
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, lightSSBO);
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, lightSSBO);
|
||||||
|
|
||||||
GLuint bvhSSBO;
|
GLuint bvhSSBO;
|
||||||
glGenBuffers(1, &bvhSSBO);
|
glGenBuffers(1, &bvhSSBO);
|
||||||
glBindBuffer(GL_SHADER_STORAGE_BUFFER, bvhSSBO);
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, bvhSSBO);
|
||||||
glBufferData(GL_SHADER_STORAGE_BUFFER, scene.getBVH().size() * sizeof(GPUBvh), nullptr, GL_STATIC_DRAW);
|
glBufferData(GL_SHADER_STORAGE_BUFFER, scene.getBVH().size() * sizeof(GPUBvh), nullptr, GL_STATIC_DRAW);
|
||||||
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, bvhSSBO);
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, bvhSSBO);
|
||||||
|
|
||||||
|
|
||||||
GLuint cameraUBO;
|
GLuint cameraUBO;
|
||||||
@ -85,6 +94,9 @@ int main(int argc, char **argv)
|
|||||||
glBindBuffer(GL_SHADER_STORAGE_BUFFER, objectSSBO);
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, objectSSBO);
|
||||||
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, object_data.size() * sizeof(GPUObject), object_data.data());
|
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, object_data.size() * sizeof(GPUObject), object_data.data());
|
||||||
|
|
||||||
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, trianglesSSBO);
|
||||||
|
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, triangle_data.size() * sizeof(GPUTriangle), triangle_data.data());
|
||||||
|
|
||||||
glBindBuffer(GL_SHADER_STORAGE_BUFFER, materialSSBO);
|
glBindBuffer(GL_SHADER_STORAGE_BUFFER, materialSSBO);
|
||||||
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, material_data.size() * sizeof(GPUMaterial), material_data.data());
|
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, material_data.size() * sizeof(GPUMaterial), material_data.data());
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
#include "BVH.hpp"
|
#include "BVH.hpp"
|
||||||
|
|
||||||
BVH::BVH(std::vector<GPUObject> &primitives, int first_primitive, int primitive_count) : _aabb(AABB(glm::vec3(1e30f), glm::vec3(-1e30f)))
|
BVH::BVH(std::vector<GPUTriangle> &primitives, int first_primitive, int primitive_count) : _aabb(AABB(glm::vec3(1e30f), glm::vec3(-1e30f)))
|
||||||
{
|
{
|
||||||
_left = nullptr;
|
_left = nullptr;
|
||||||
_right = nullptr;
|
_right = nullptr;
|
||||||
@ -26,14 +26,14 @@ BVH::BVH(std::vector<GPUObject> &primitives, int first_primitive, int primitive_
|
|||||||
subdivide(primitives);
|
subdivide(primitives);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BVH::updateBounds(std::vector<GPUObject> &primitives)
|
void BVH::updateBounds(std::vector<GPUTriangle> &primitives)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < _primitive_count; i++)
|
for (int i = 0; i < _primitive_count; i++)
|
||||||
{
|
{
|
||||||
GPUObject leaf_triangle = primitives[_first_primitive + i];
|
GPUTriangle leaf_triangle = primitives[_first_primitive + i];
|
||||||
|
|
||||||
if (leaf_triangle.type != (int)Object::Type::TRIANGLE)
|
// if (leaf_triangle.type != (int)Object::Type::TRIANGLE)
|
||||||
continue ;
|
// continue ;
|
||||||
|
|
||||||
_aabb.min = glm::min(_aabb.min, leaf_triangle.position);
|
_aabb.min = glm::min(_aabb.min, leaf_triangle.position);
|
||||||
_aabb.min = glm::min(_aabb.min, leaf_triangle.vertex1);
|
_aabb.min = glm::min(_aabb.min, leaf_triangle.vertex1);
|
||||||
@ -44,9 +44,9 @@ void BVH::updateBounds(std::vector<GPUObject> &primitives)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BVH::subdivide(std::vector<GPUObject> &primitives)
|
void BVH::subdivide(std::vector<GPUTriangle> &primitives)
|
||||||
{
|
{
|
||||||
if (_primitive_count <= 100)
|
if (_primitive_count <= 4)
|
||||||
return ;
|
return ;
|
||||||
|
|
||||||
glm::vec3 extent = _aabb.max - _aabb.min;
|
glm::vec3 extent = _aabb.max - _aabb.min;
|
||||||
@ -79,7 +79,6 @@ void BVH::subdivide(std::vector<GPUObject> &primitives)
|
|||||||
if (left_count == 0 || left_count == _primitive_count)
|
if (left_count == 0 || left_count == _primitive_count)
|
||||||
return ;
|
return ;
|
||||||
|
|
||||||
|
|
||||||
_is_leaf = false;
|
_is_leaf = false;
|
||||||
|
|
||||||
_left = new BVH(primitives, _first_primitive, left_count);
|
_left = new BVH(primitives, _first_primitive, left_count);
|
||||||
|
@ -105,7 +105,8 @@ bool ObjParser::addTriangleFromPolygon(std::vector<glm::vec3> &vertices, Scene &
|
|||||||
dot = glm::cross(v2 - v3, v2 - v1).z;
|
dot = glm::cross(v2 - v3, v2 - v1).z;
|
||||||
if(dot <= 0)
|
if(dot <= 0)
|
||||||
continue;
|
continue;
|
||||||
if(pointInTriangle((glm::vec3 [3]){v1, v2, v3}, vertices, i))
|
glm::vec3 triangleVertices[3] = {v1, v2, v3};
|
||||||
|
if(pointInTriangle(triangleVertices, vertices, i))
|
||||||
continue;
|
continue;
|
||||||
vertices.erase(vertices.begin() + i);
|
vertices.erase(vertices.begin() + i);
|
||||||
addTriangle(v1, v2 ,v3 , scene);
|
addTriangle(v1, v2 ,v3 , scene);
|
||||||
|
@ -53,7 +53,7 @@ bool Scene::parseScene(char *name)
|
|||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
BVH *bvh = new BVH(_gpu_objects, 0, _gpu_objects.size());
|
BVH *bvh = new BVH(_gpu_triangles, 0, _gpu_triangles.size());
|
||||||
_gpu_bvh = bvh->getGPUBvhs();
|
_gpu_bvh = bvh->getGPUBvhs();
|
||||||
|
|
||||||
return (true);
|
return (true);
|
||||||
@ -84,13 +84,6 @@ void Scene::addObject(Object *obj)
|
|||||||
gpu_obj.vertex1 = quad->getUp();
|
gpu_obj.vertex1 = quad->getUp();
|
||||||
gpu_obj.vertex2 = quad->getRight();
|
gpu_obj.vertex2 = quad->getRight();
|
||||||
}
|
}
|
||||||
else if (obj->getType() == Object::Type::TRIANGLE)
|
|
||||||
{
|
|
||||||
auto triangle = static_cast<Triangle *>(obj);
|
|
||||||
gpu_obj.vertex1 = triangle->getVertex2();
|
|
||||||
gpu_obj.vertex2 = triangle->getVertex3();
|
|
||||||
gpu_obj.normal = triangle->getNormal();
|
|
||||||
}
|
|
||||||
else if (obj->getType() == Object::Type::CUBE)
|
else if (obj->getType() == Object::Type::CUBE)
|
||||||
{
|
{
|
||||||
auto cube = static_cast<Cube *>(obj);
|
auto cube = static_cast<Cube *>(obj);
|
||||||
@ -103,6 +96,22 @@ void Scene::addObject(Object *obj)
|
|||||||
gpu_obj.normal = glm::vec3(cylinder->getRadius(), cylinder->getHeight(), 0.0f);
|
gpu_obj.normal = glm::vec3(cylinder->getRadius(), cylinder->getHeight(), 0.0f);
|
||||||
gpu_obj.transform = glm::mat4(cylinder->getRotation());
|
gpu_obj.transform = glm::mat4(cylinder->getRotation());
|
||||||
}
|
}
|
||||||
|
else if (obj->getType() == Object::Type::TRIANGLE)
|
||||||
|
{
|
||||||
|
GPUTriangle gpu_triangle;
|
||||||
|
|
||||||
|
auto triangle = static_cast<Triangle *>(obj);
|
||||||
|
gpu_triangle.position = obj->getPosition();
|
||||||
|
gpu_triangle.mat_index = obj->getMaterialIndex();
|
||||||
|
|
||||||
|
gpu_triangle.vertex1 = triangle->getVertex2();
|
||||||
|
gpu_triangle.vertex2 = triangle->getVertex3();
|
||||||
|
gpu_triangle.normal = triangle->getNormal();
|
||||||
|
|
||||||
|
_gpu_triangles.push_back(gpu_triangle);
|
||||||
|
|
||||||
|
return ;
|
||||||
|
}
|
||||||
else if (obj->getType() == Object::Type::PORTAL)
|
else if (obj->getType() == Object::Type::PORTAL)
|
||||||
{
|
{
|
||||||
auto portal = static_cast<Portal *>(obj);
|
auto portal = static_cast<Portal *>(obj);
|
||||||
@ -169,6 +178,11 @@ const std::vector<GPUObject> &Scene::getObjectData() const
|
|||||||
return (_gpu_objects);
|
return (_gpu_objects);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::vector<GPUTriangle> &Scene::getTriangleData() const
|
||||||
|
{
|
||||||
|
return (_gpu_triangles);
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<GPUMaterial> &Scene::getMaterialData()
|
std::vector<GPUMaterial> &Scene::getMaterialData()
|
||||||
{
|
{
|
||||||
return (_gpu_materials);
|
return (_gpu_materials);
|
||||||
|
@ -175,6 +175,7 @@ void Window::imGuiRender()
|
|||||||
|
|
||||||
ImGui::Text("Fps: %d", int(_fps));
|
ImGui::Text("Fps: %d", int(_fps));
|
||||||
ImGui::Text("Frame: %d", _frameCount);
|
ImGui::Text("Frame: %d", _frameCount);
|
||||||
|
ImGui::Text("Objects: %d", _scene->getObjectData().size() + _scene->getTriangleData().size());
|
||||||
|
|
||||||
ImGui::Separator();
|
ImGui::Separator();
|
||||||
if (ImGui::Checkbox("Accumulate", &accumulate))
|
if (ImGui::Checkbox("Accumulate", &accumulate))
|
||||||
|
Reference in New Issue
Block a user