Commit 0f5f2bfc authored by unknown's avatar unknown
Browse files

LS works now a bit more

parent b9801e52
......@@ -118,11 +118,14 @@ struct Line
Hit farthest;
};
struct LineSpace
struct Linespace
{
Bounds bounds;
uvec3 resolution;
float patch_size;
uint offsets[15];
uint line_count;
};
struct Mesh
......@@ -136,10 +139,7 @@ struct Mesh
uint id;
uint p[2];
LineSpace linespace;
uint offsets[15];
uint line_count;
Linespace linespace;
mat4 transformation;
mat4 inverse_transformation;
......
......@@ -24,7 +24,12 @@ bool intersectsRayBounds(const in Ray ray, const in Bounds bounds)
}
int faceID(float t, float t1, float t2, float t3, float t4, float t5, float t6) {
return t == t1 ? 3 : (t == t2 ? 0 : (t == t3 ? 4 : (t == t4 ? 1 : (t == t5 ? 5 : 2))));
return t == t1 ? 3 //neg_x
: (t == t2 ? 0 //pos_x
: (t == t3 ? 4 //neg_y
: (t == t4 ? 1 //pos_y
: (t == t5 ? 5 //neg_z
: 2)))); //pos_z
}
bool intersectsRayBounds(const in Ray ray, const in Bounds bounds, inout float tmin, inout float tmax, inout int face_tmin, inout int face_tmax)
......@@ -122,6 +127,60 @@ bool intersectsRayTriangleMesh(const in Ray ray, int triangle_index, const in Me
return false;
}
bool intersectsRayTriangleMeshLocal(const in Ray ray, int triangle_index, const in Mesh mesh, inout float t, inout float u, inout float v)
{
float EPSILON = FLT_EPSILON;
vec3 e1, e2; //Edge1, Edge2
vec3 P, Q, T;
float det, inv_det;
vec3 v1 = (mesh.vertices[mesh.triangles[triangle_index].indices[0]].position).xyz;
vec3 v2 = (mesh.vertices[mesh.triangles[triangle_index].indices[1]].position).xyz;
vec3 v3 = (mesh.vertices[mesh.triangles[triangle_index].indices[2]].position).xyz;
//Find vectors for two edges sharing V1
e1 = v2 - v1;
e2 = v3 - v1;
//Begin calculating determinant - also used to calculate u parameter
P = cross(ray.direction.xyz, e2);
//if determinant is near zero, ray lies in plane of triangle
det = dot(e1, P);
//NOT CULLING
if (det > -EPSILON && det < EPSILON)
return false;
inv_det = 1.f / det;
//calculate distance from V1 to ray origin
T = ray.origin.xyz - v1;
//Calculate u parameter and test bound
u = dot(T, P) * inv_det;
//The intersection lies outside of the triangle
if (u < 0.f || u > 1.f) return false;
//Prepare to test v parameter
Q = cross(T, e1);
//Calculate V parameter and test bound
v = dot(ray.direction.xyz, Q) * inv_det;
//The intersection lies outside of the triangle
if (v < 0.f || u + v > 1.f) return false;
t = dot(e2, Q) * inv_det;
if (t > EPSILON) { //ray intersection
return true;
}
// No hit, no win
return false;
}
bool deprecated_______intersectsTriangleBounds(const in Triangle tri, const in Bounds bounds)
{
// SAT: Separating Axis Theorem
......
......@@ -6,28 +6,19 @@
#include <raytracer/intersections.glh>
#include <raytracer/std.glh>
#include <raytracer/utilities.glh>
#include <raytracer/mesh_ls.glh>
#include <raytracer/mesh_linespace.glh>
Bounds getNodeBounds(int index, const in Mesh mesh)
{
return mesh.nodes[index].bounds;
}
//"Stackless" BVH-Traversal.
// See https://pdfs.semanticscholar.org/7e81/6b82fb92df08d7bc0d9805f8988754e0d8c1.pdf
bool traverse(const in uint mesh_id, const in Ray ray, inout Hit hit, const in bool nearest_only, const in float max_distance, inout float t_min)
bool traverse(const in Mesh mesh, const in Ray local_ray, const in bool use_first, const in float max_distance, inout Hit hit, inout float t_min)
{
float t = 0;
Mesh mesh = b_meshes[mesh_id];
//For ease of use, substitute Ray<->OBB for Ray<->AABB by transforming the ray into object space.
Ray ray_in = ray;
ray_in.direction = normalize(mat3(mesh.inverse_transformation) * ray_in.direction.xyz);
ray_in.origin = (mesh.inverse_transformation * vec4(ray_in.origin.xyz, 1)).xyz;
// Check once the AABB for the whole scene
bool hit_scene = intersectsRayBounds(ray_in, getNodeBounds(0, mesh), max_distance);
bool hit_scene = intersectsRayBounds(local_ray, getNodeBounds(0, mesh), max_distance);
bool hit_triangle = false;
int current_node = 0;
......@@ -39,10 +30,10 @@ bool traverse(const in uint mesh_id, const in Ray ray, inout Hit hit, const in b
if (mesh.nodes[current_node].type == 0) //Inner node.
{
int id_left = int(mesh.nodes[current_node].left_idx);
bool hit_left = intersectsRayBounds(ray_in, getNodeBounds(id_left, mesh), max_distance);
bool hit_left = intersectsRayBounds(local_ray, getNodeBounds(id_left, mesh), max_distance);
int id_right = int(mesh.nodes[current_node].right_idx);
bool hit_right = intersectsRayBounds(ray_in, getNodeBounds(id_right, mesh), max_distance);
bool hit_right = intersectsRayBounds(local_ray, getNodeBounds(id_right, mesh), max_distance);
//both hit
if (hit_left && hit_right)
......@@ -83,21 +74,21 @@ bool traverse(const in uint mesh_id, const in Ray ray, inout Hit hit, const in b
float u, v;
for (int i = start; i <= end; i++)
{
if (intersectsRayTriangleMesh(ray, i, mesh, t, u, v))
if (intersectsRayTriangleMeshLocal(local_ray, i, mesh, t, u, v))
{
if (t < t_min && t < max_distance) //test for ray_length
{
t_min = t;
hit_triangle = true;
if (nearest_only)
if (use_first)
{
return true;
}
hit.barycentric = vec2(u, v);
hit.triangle = i;
hit.mesh = mesh_id;
hit.mesh = mesh.id;
}
}
......@@ -127,30 +118,30 @@ bool traverse(const in uint mesh_id, const in Ray ray, inout Hit hit, const in b
return hit_triangle;
}
#define USE_LS
#define ALT_LS
bool bvh__nearestIntersection(const in Ray ray, inout Hit hit)
{
#ifdef USE_LS
bool ls_hits = false;
float distance = FLT_MAX;
//mesh id != 0 crashes.... ._.
ls_hits = bool(int(ls_hits) | int(linespace__traverse(0, ray, hit, distance)));
// ls_hits = bool(int(ls_hits) | int(linespace__traverse(1, ray, hit, distance)));
// ls_hits = bool(int(ls_hits) | int(linespace__traverse(2, ray, hit, distance)));
// ls_hits = bool(int(ls_hits) | int(linespace__traverse(3, ray, hit, distance)));
// ls_hits = bool(int(ls_hits) | int(linespace__traverse(4, ray, hit, distance)));
return ls_hits;
#ifdef ALT_LS
float current_t = FLT_MAX;
float min_t = FLT_MAX;
bool hits = false;
for (int i = 0; i < b_meshes.length(); ++i)
{
Mesh mesh = b_meshes[i];
hits = bool(int(hits) | int(ls__traverse(mesh, ray__makeMeshLocal(ray, mesh), false, FLT_MAX, hit, min_t)));
}
return hits;
#else
float current_t = FLT_MAX;
float min_t = FLT_MAX;
bool hits = false;
for (int i = 0; i < b_meshes.length(); ++i)
{
hits = bool(int(hits) | int(traverse(i, ray, hit, false, current_t, min_t)));
//current_t = min_t;
Mesh mesh = b_meshes[i];
hits = bool(int(hits) | int(traverse(mesh, ray__makeMeshLocal(ray, mesh), false, current_t, hit, min_t)));
}
return hits;
......@@ -159,33 +150,19 @@ bool bvh__nearestIntersection(const in Ray ray, inout Hit hit)
bool bvh__intersectsAny(const in Ray ray, const in float max_distance)
{
#ifdef ALT_LS
return false;
#else
Hit unused_hit;
float dist = FLT_MAX;
#ifdef USE_LS
//return false;
for (int i = 1; i < 2; ++i)
{
return linespace__traverse(i, ray, unused_hit, dist);
}
#endif
//for (int i = 0; i < b_meshes.length(); ++i)
//{
// if (linespace__traverse(i, ray, unused_hit))
// return true;
// //current_t = min_t;
//}
//return false;
float current_t = max_distance;
for (int i = 0; i < b_meshes.length(); ++i)
{
if (traverse(i, ray, unused_hit, true, max_distance, current_t))
Mesh mesh = b_meshes[i];
if (traverse(mesh, ray__makeMeshLocal(ray, mesh), true, max_distance, unused_hit, current_t))
return true;
}
return false;
#endif
}
#endif //__GL_BVH
\ No newline at end of file
......@@ -59,6 +59,15 @@ Vertex hit__getVertex(const in Hit hit, out Material material)
return vertex;
}
Ray ray__makeMeshLocal(const in Ray ray, const in Mesh mesh)
{
Ray ray_in = ray;
ray_in.direction = normalize(mat3(mesh.inverse_transformation) * ray_in.direction.xyz);
ray_in.origin = (mesh.inverse_transformation * vec4(ray_in.origin.xyz, 1)).xyz;
return ray_in;
}
vec4 bounds__size(const in Bounds bounds)
{
return bounds.max - bounds.min;
......
......@@ -48,7 +48,7 @@ namespace glare
//Create a skybox from cube map.
m_skybox = std::make_shared<core::Skybox>(core::Skybox::collectFilesFrom(core::asset("/textures/ryfjallet/")));
initializeScene(m_current_scene_root / "boxes.dae", 1.f);
initializeScene(m_current_scene_root / "anotherone.dae", 1.f);
initializeRenderRequirements();
initializeAdvanced();
initializeGUI();
......
......@@ -82,7 +82,11 @@ namespace glare
glm::uint id;
glm::uint p[2];
LineSpaceData linespace;
//LineSpaceData linespace;
glm::vec4 bounds_min;
glm::vec4 bounds_max;
glm::uvec3 resolution;
float patch_size;
glm::uint offsets[15];
glm::uint line_count;
......
......@@ -91,7 +91,7 @@ namespace glare
Mesh LocalCollector::makeMesh(glm::uint material_index) const
{
Mesh mesh;
mesh.id = id();
mesh.id = unsigned(id());
mesh.datastructure_buffer_ptr = m_datastructure->buffer().getResidentAddress();
mesh.transformation = m_transform;
mesh.inverse_transformation = glm::inverse(m_transform);
......@@ -99,9 +99,10 @@ namespace glare
mesh.vertex_buffer_ptr = m_vertex_buffer->getResidentAddress();
mesh.triangle_buffer_ptr = m_triangle_buffer->getResidentAddress();
mesh.linespace.bounds = m_linespace->bounds();
mesh.linespace.patch_size = m_linespace->subdivisions().size;
mesh.linespace.resolution = glm::uvec3(m_linespace->subdivisions().subdivisions);
mesh.bounds_min = m_linespace->bounds().min;
mesh.bounds_max = m_linespace->bounds().max;
mesh.patch_size = m_linespace->subdivisions().size;
mesh.resolution = glm::uvec3(m_linespace->subdivisions().subdivisions);
mesh.lines_buffer = m_linespace->lineBuffer().getResidentAddress();
for (int i = 0; i < 15; ++i) {
......
......@@ -46,6 +46,26 @@ namespace glare
gl::clearTextureImage(m_color_store->getID(), 0, gl::TextureFormat::eRGBA, gl::Type::eFloat, glm::value_ptr(glm::vec4(0)));
}
/* std::unique_ptr<core::Buffer<gl::BufferType::eShaderStorage>> m_debug_buffer;
struct debug_data_t
{
glm::uvec3 data_02;
glm::uint data_01;
glm::uvec4 data_03;
glm::uvec4 data_05;
glm::vec4 data_04;
friend std::ostream& operator<< (std::ostream& stream, const debug_data_t& dd) {
return stream << "data_01: " << dd.data_01 << std::endl
<< "data_02: " << dd.data_02.x << ", " << dd.data_02.y << ", " << dd.data_02.z << std::endl
<< "data_03: " << dd.data_03.x << ", " << dd.data_03.y << ", " << dd.data_03.z << std::endl
<< "data_04: " << dd.data_04.x << ", " << dd.data_04.y << ", " << dd.data_04.z << std::endl
<< "data_05: " << dd.data_05.x << ", " << dd.data_05.y << ", " << dd.data_05.z << std::endl;
}
};
std::vector<debug_data_t> debug_data(100000, { {0, 0, 0 }, 0 });*/
const core::TextureRGBA_32F &Raytracer::render(uint32_t width, uint32_t height)
{
if (width != m_last_width || height != m_last_height) {
......@@ -55,6 +75,10 @@ namespace glare
m_render_target = std::make_unique<core::TextureRGBA_32F>(width, height);
m_color_store = std::make_unique<core::TextureRGBA_32F>(width, height);
m_current_sample = 0;
/* if (!m_debug_buffer) {
m_debug_buffer = std::make_unique<core::Buffer<gl::BufferType::eShaderStorage>>();
}*/
}
for (unsigned sample = 0; sample < m_samples_per_frame; sample++) {
......@@ -74,6 +98,8 @@ namespace glare
m_render_shader->updateStorageBuffer("mesh_buffer", m_collector->meshBuffer());
m_render_shader->updateStorageBuffer("material_buffer", m_collector->materialBuffer());
// m_debug_buffer->setData(debug_data, gl::BufferUsage::eDynamicCopy);
//m_render_shader->updateStorageBuffer("debug_data_buffer", m_debug_buffer);
m_render_shader->updateUniformStruct("u_camera", m_collector->getActiveCamera());
......@@ -99,6 +125,17 @@ namespace glare
core::ClockGL::instance().start();
m_render_shader->dispatchCompute2D(width, 4, height, 4);
// glFinish();
// auto vec = m_debug_buffer->read<debug_data_t>(debug_data.size());
//Log_Error << "OJUTPUTATOP --------------------------------------------- ";
//for (auto &&elem : vec) {
// if (elem.data_01 > 3839) {
// Log_Error << elem;
// }
//}
++m_current_sample;
}
......
......@@ -20,7 +20,7 @@ namespace glare
unsigned goodSubdivision(size_t polygon_count, glm::vec3 bounds_size)
{
return 4;// unsigned(5 * glm::ceil(glm::log2(glm::max(size_t(2), polygon_count)) * glm::log(log2((glm::compMax(bounds_size) / glm::compMin(bounds_size)) + 2 * glm::e<float>()))));
return 12;// unsigned(5 * glm::ceil(glm::log2(glm::max(size_t(2), polygon_count)) * glm::log(log2((glm::compMax(bounds_size) / glm::compMin(bounds_size)) + 2 * glm::e<float>()))));
}
}
......
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