From 3316a577473301f66c69a6a5bdb0275466ddb0cf Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Mon, 7 Oct 2019 14:31:00 +0300
Subject: [PATCH] Experiments with different splat weightings

---
 .../cpp/include/ftl/cuda/intersections.hpp    |  3 +--
 components/renderers/cpp/src/splat_render.cpp |  2 +-
 components/renderers/cpp/src/splatter.cu      | 21 ++++++++++++-------
 3 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/components/renderers/cpp/include/ftl/cuda/intersections.hpp b/components/renderers/cpp/include/ftl/cuda/intersections.hpp
index 9cfdbc254..9008d68c6 100644
--- a/components/renderers/cpp/include/ftl/cuda/intersections.hpp
+++ b/components/renderers/cpp/include/ftl/cuda/intersections.hpp
@@ -49,8 +49,7 @@ __device__ inline bool intersectDisk(const float3 &n, const float3 &p0, float ra
  * @param l Normalised ray direction in camera space
  * @return Radius from centre of disk where intersection occurred.
  */
-__device__ inline float intersectDistance(const float3 &n, const float3 &p0, const float3 &l0, const float3 &l) { 
-    float t = 0; 
+__device__ inline float intersectDistance(const float3 &n, const float3 &p0, const float3 &l0, const float3 &l, float &t) { 
     if (intersectPlane(n, p0, l0, l, t)) { 
         const float3 p = l0 + l * t; 
         const float3 v = p - p0; 
diff --git a/components/renderers/cpp/src/splat_render.cpp b/components/renderers/cpp/src/splat_render.cpp
index 39016f575..553bd3ab6 100644
--- a/components/renderers/cpp/src/splat_render.cpp
+++ b/components/renderers/cpp/src/splat_render.cpp
@@ -406,7 +406,7 @@ bool Splatter::render(ftl::rgbd::VirtualSource *src, ftl::rgbd::Frame &out, cuda
 		// Convert normal to single float value
 		temp_.create<GpuMat>(Channel::Colour, Format<uchar4>(camera.width, camera.height));
 		ftl::cuda::normal_visualise(out.getTexture<float4>(Channel::Normals), temp_.createTexture<uchar4>(Channel::Colour),
-				make_float3(-0.3f, 0.2f, 1.0f),
+				make_float3(0.3f, 0.2f, 1.0f),
 				light_diffuse_,
 				light_ambient_, stream);
 
diff --git a/components/renderers/cpp/src/splatter.cu b/components/renderers/cpp/src/splatter.cu
index 786bb7215..a26cd502d 100644
--- a/components/renderers/cpp/src/splatter.cu
+++ b/components/renderers/cpp/src/splatter.cu
@@ -176,6 +176,7 @@ __device__ inline float make(float v) {
     //float depth = 0.0f;
     //float contrib = 0.0f;
     float depth = 1000.0f;
+    float pdepth = 1000.0f;
 
     struct Result {
         float weight;
@@ -198,22 +199,23 @@ __device__ inline float make(float v) {
 
         if (d < params.camera.minDepth || d > params.camera.maxDepth) continue;
 
-        const float3 camPos = params.camera.screenToCam((int)(x+u),(int)(y+v),d);
+        const float3 camPos = params.camera.screenToCam((int)(x)+u,(int)(y)+v,d);
         const float3 camPos2 = params.camera.screenToCam((int)(x),(int)(y),d);
         const float3 worldPos = params.m_viewMatrixInverse * camPos;
 
 
         // Assumed to be normalised
-		float4 n = normals.tex2D((int)(x+u), (int)(y+v));
+        float4 n = normals.tex2D((int)(x+u), (int)(y+v));
+        n /= length(n);
 		//if (length(make_float3(n)) == 0.0f) printf("BAD NORMAL\n");
 
         // Does the ray intersect plane of splat?
         float t = 1000.0f;
-        if (ftl::cuda::intersectPlane(make_float3(n), worldPos, origin, ray, t)) { //} && fabs(t-camPos.z) < 0.01f) {
+        const float r = ftl::cuda::intersectDistance(make_float3(n), worldPos, origin, ray, t);
+        if (r != PINF) { //} && fabs(t-camPos.z) < 0.01f) {
             // Adjust from normalised ray back to original meters units
 			t *= scale;
-            const float3 camPos3 = params.camera.screenToCam((int)(x),(int)(y),t);
-            float weight = ftl::cuda::spatialWeighting(camPos, camPos3, 2.0f*(camPos3.z/params.camera.fx));
+            float weight = ftl::cuda::weighting(r, 2.0f/params.camera.fx); // (1.0f/params.camera.fx) / (t/params.camera.fx)
 
             /* Buehler C. et al. 2001. Unstructured Lumigraph Rendering. */
             /* Orts-Escolano S. et al. 2016. Holoportation: Virtual 3D teleportation in real-time. */
@@ -226,12 +228,17 @@ __device__ inline float make(float v) {
 
             if (weight <= 0.0f) continue;
 
-            depth = min(depth, t);
+            //depth = min(depth, t);
+            if (t < depth) {
+                pdepth = depth;
+                depth = t;
+            }
             results[i/WARP_SIZE] = {weight, t, in.tex2D((int)x+u, (int)y+v)};
         }
     }
 
     depth = warpMin(depth);
+    pdepth = warpMin(pdepth);
 
     float adepth = 0.0f;
     float contrib = 0.0f;
@@ -378,7 +385,7 @@ __global__ void dibr_attribute_contrib_kernel(
     const float d = (float)depth_in.tex2D((int)screenPos.x, (int)screenPos.y) / 1000.0f;
 
 	const A input = generateInput(in.tex2D(x, y), params, worldPos);
-	const float weight = ftl::cuda::weighting(fabs(camPos.z - d), 0.01f);
+	const float weight = ftl::cuda::weighting(fabs(camPos.z - d), 0.002f);
 	const B weighted = make<B>(input) * weight; //weightInput(input, weight);
 
 	if (weight > 0.0f) {
-- 
GitLab