From 205b2014d68a59e62e9bab8e92e62e0ef8b4867e Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Tue, 8 Oct 2019 11:39:31 +0300
Subject: [PATCH] Use point density

---
 components/renderers/cpp/src/splat_render.cpp  |  3 +++
 components/renderers/cpp/src/splatter.cu       | 17 ++++++++++++-----
 components/renderers/cpp/src/splatter_cuda.hpp |  1 +
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/components/renderers/cpp/src/splat_render.cpp b/components/renderers/cpp/src/splat_render.cpp
index f766fa1e3..813cbda83 100644
--- a/components/renderers/cpp/src/splat_render.cpp
+++ b/components/renderers/cpp/src/splat_render.cpp
@@ -259,6 +259,7 @@ void Splatter::_renderChannel(
 		if (is_4chan) {
 			ftl::cuda::splat(
 				accum_.getTexture<float4>(Channel::Normals),
+				accum_.getTexture<float>(Channel::Density),
 				accum_.getTexture<float4>(channel_out),
 				temp_.getTexture<int>(Channel::Depth2),
 				out.createTexture<float>(Channel::Depth),
@@ -268,6 +269,7 @@ void Splatter::_renderChannel(
 		} else if (is_float) {
 			ftl::cuda::splat(
 				accum_.getTexture<float4>(Channel::Normals),
+				accum_.getTexture<float>(Channel::Density),
 				accum_.getTexture<float>(channel_out),
 				temp_.getTexture<int>(Channel::Depth2),
 				out.createTexture<float>(Channel::Depth),
@@ -277,6 +279,7 @@ void Splatter::_renderChannel(
 		} else {
 			ftl::cuda::splat(
 				accum_.getTexture<float4>(Channel::Normals),
+				accum_.getTexture<float>(Channel::Density),
 				accum_.getTexture<uchar4>(channel_out),
 				temp_.getTexture<int>(Channel::Depth2),
 				out.createTexture<float>(Channel::Depth),
diff --git a/components/renderers/cpp/src/splatter.cu b/components/renderers/cpp/src/splatter.cu
index a26cd502d..70be6c7ba 100644
--- a/components/renderers/cpp/src/splatter.cu
+++ b/components/renderers/cpp/src/splatter.cu
@@ -150,6 +150,7 @@ __device__ inline float make(float v) {
  __global__ void splat_kernel(
         //TextureObject<float4> points,       // Original 3D points
         TextureObject<float4> normals,
+        TextureObject<float> density,
         TextureObject<T> in,
         TextureObject<int> depth_in,        // Virtual depth map
         TextureObject<float> depth_out,   // Accumulated output
@@ -205,7 +206,7 @@ __device__ inline float make(float v) {
 
 
         // 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");
 
@@ -214,8 +215,9 @@ __device__ inline float make(float v) {
         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;
-            float weight = ftl::cuda::weighting(r, 2.0f/params.camera.fx); // (1.0f/params.camera.fx) / (t/params.camera.fx)
+            t *= scale;
+            const float dens = density.tex2D((int)(x)+u, (int)(y)+v);
+            float weight = ftl::cuda::weighting(r, dens/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. */
@@ -269,6 +271,7 @@ __device__ inline float make(float v) {
 template <typename T>
 void ftl::cuda::splat(
         TextureObject<float4> &normals,
+        TextureObject<float> &density,
         TextureObject<T> &colour_in,
         TextureObject<int> &depth_in,        // Virtual depth map
         TextureObject<float> &depth_out,
@@ -279,6 +282,7 @@ void ftl::cuda::splat(
 
     splat_kernel<8,T><<<gridSize, blockSize, 0, stream>>>(
         normals,
+        density,
         colour_in,
         depth_in,
         depth_out,
@@ -290,6 +294,7 @@ void ftl::cuda::splat(
 
 template void ftl::cuda::splat<uchar4>(
         TextureObject<float4> &normals,
+        TextureObject<float> &density,
         TextureObject<uchar4> &colour_in,
         TextureObject<int> &depth_in,        // Virtual depth map
         TextureObject<float> &depth_out,
@@ -297,7 +302,8 @@ template void ftl::cuda::splat<uchar4>(
 		const SplatParams &params, cudaStream_t stream);
 		
 template void ftl::cuda::splat<float4>(
-	TextureObject<float4> &normals,
+    TextureObject<float4> &normals,
+    TextureObject<float> &density,
 	TextureObject<float4> &colour_in,
 	TextureObject<int> &depth_in,        // Virtual depth map
 	TextureObject<float> &depth_out,
@@ -305,7 +311,8 @@ template void ftl::cuda::splat<float4>(
 	const SplatParams &params, cudaStream_t stream);
 
 template void ftl::cuda::splat<float>(
-	TextureObject<float4> &normals,
+    TextureObject<float4> &normals,
+    TextureObject<float> &density,
 	TextureObject<float> &colour_in,
 	TextureObject<int> &depth_in,        // Virtual depth map
 	TextureObject<float> &depth_out,
diff --git a/components/renderers/cpp/src/splatter_cuda.hpp b/components/renderers/cpp/src/splatter_cuda.hpp
index 463beefb1..684b0de55 100644
--- a/components/renderers/cpp/src/splatter_cuda.hpp
+++ b/components/renderers/cpp/src/splatter_cuda.hpp
@@ -17,6 +17,7 @@ namespace cuda {
 	template <typename T>
 	void splat(
         ftl::cuda::TextureObject<float4> &normals,
+		ftl::cuda::TextureObject<float> &density,
 		ftl::cuda::TextureObject<T> &colour_in,
         ftl::cuda::TextureObject<int> &depth_in,        // Virtual depth map
         ftl::cuda::TextureObject<float> &depth_out,
-- 
GitLab