From c305aeef134102917c98c32b67fa1595aed9f060 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Mon, 7 Oct 2019 12:58:01 +0300
Subject: [PATCH] Revert to old normals

---
 components/renderers/cpp/src/normals.cu       | 23 ++++++++++---------
 components/renderers/cpp/src/splat_render.cpp | 10 ++++----
 components/renderers/cpp/src/splatter.cu      |  5 ++--
 3 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/components/renderers/cpp/src/normals.cu b/components/renderers/cpp/src/normals.cu
index 7f9b1bb18..13b82e3a0 100644
--- a/components/renderers/cpp/src/normals.cu
+++ b/components/renderers/cpp/src/normals.cu
@@ -57,7 +57,7 @@ __global__ void computeNormals_kernel(ftl::cuda::TextureObject<float4> output,
 			const float  l = length(n);
 
 			if(l > 0.0f) {
-				output(x,y) = make_float4(pose * (n/-l), 1.0f);
+				output(x,y) = make_float4((n/-l), 1.0f);
 			}
 		}
 	}
@@ -131,22 +131,22 @@ __global__ void smooth_normals_kernel(ftl::cuda::TextureObject<float4> norms,
 
             //if (s > 0.0f) {
                 const float4 n = norms.tex2D((int)x+u,(int)y+v);
-                //if (n.w > 0.0f) {
+                if (n.w > 0.0f) {
                     nsum += make_float3(n) * s;
                     contrib += s;
-                //}
+                }
             //}
         }
     }
 
     // Compute dot product of normal with camera to obtain measure of how
     // well this point faces the source camera, a measure of confidence
-    float3 ray = pose * camera.screenToCam(x, y, 1.0f);
+    float3 ray = camera.screenToCam(x, y, 1.0f);
     ray = ray / length(ray);
     nsum /= contrib;
     nsum /= length(nsum);
 
-    output(x,y) = (contrib > 0.0f) ? make_float4(nsum, 1.0f) : make_float4(0.0f); //dot(nsum, ray)
+    output(x,y) = (contrib > 0.0f) ? make_float4(pose*nsum, 1.0f) : make_float4(0.0f);
 }
 
 void ftl::cuda::normals(ftl::cuda::TextureObject<float4> &output,
@@ -163,9 +163,10 @@ void ftl::cuda::normals(ftl::cuda::TextureObject<float4> &output,
     cudaSafeCall( cudaGetLastError() );
 
 	switch (radius) {
-	case 7: smooth_normals_kernel<7><<<gridSize, blockSize, 0, stream>>>(temp, output, input, camera, pose, smoothing);
-	case 5: smooth_normals_kernel<5><<<gridSize, blockSize, 0, stream>>>(temp, output, input, camera, pose, smoothing);
-	case 3: smooth_normals_kernel<3><<<gridSize, blockSize, 0, stream>>>(temp, output, input, camera, pose, smoothing);
+	case 9: smooth_normals_kernel<9><<<gridSize, blockSize, 0, stream>>>(temp, output, input, camera, pose, smoothing); break;
+	case 7: smooth_normals_kernel<7><<<gridSize, blockSize, 0, stream>>>(temp, output, input, camera, pose, smoothing); break;
+	case 5: smooth_normals_kernel<5><<<gridSize, blockSize, 0, stream>>>(temp, output, input, camera, pose, smoothing); break;
+	case 3: smooth_normals_kernel<3><<<gridSize, blockSize, 0, stream>>>(temp, output, input, camera, pose, smoothing); break;
 	}
     cudaSafeCall( cudaGetLastError() );
 
@@ -189,9 +190,9 @@ void ftl::cuda::normals(ftl::cuda::TextureObject<float4> &output,
 	cudaSafeCall( cudaGetLastError() );
 
 	switch (radius) {
-	case 7: smooth_normals_kernel<7><<<gridSize, blockSize, 0, stream>>>(temp, output, input, camera, pose_inv, smoothing);
-	case 5: smooth_normals_kernel<5><<<gridSize, blockSize, 0, stream>>>(temp, output, input, camera, pose_inv, smoothing);
-	case 3: smooth_normals_kernel<3><<<gridSize, blockSize, 0, stream>>>(temp, output, input, camera, pose_inv, smoothing);
+	case 7: smooth_normals_kernel<7><<<gridSize, blockSize, 0, stream>>>(temp, output, input, camera, pose, smoothing);
+	case 5: smooth_normals_kernel<5><<<gridSize, blockSize, 0, stream>>>(temp, output, input, camera, pose, smoothing);
+	case 3: smooth_normals_kernel<3><<<gridSize, blockSize, 0, stream>>>(temp, output, input, camera, pose, smoothing);
 	}
 	cudaSafeCall( cudaGetLastError() );
 
diff --git a/components/renderers/cpp/src/splat_render.cpp b/components/renderers/cpp/src/splat_render.cpp
index f96054587..39016f575 100644
--- a/components/renderers/cpp/src/splat_render.cpp
+++ b/components/renderers/cpp/src/splat_render.cpp
@@ -168,7 +168,7 @@ void Splatter::renderChannel(
 	out.get<GpuMat>(Channel::Normals).setTo(cv::Scalar(0.0f,0.0f,0.0f,0.0f), cvstream);
 
 	// Create normals first
-	/*for (auto &f : scene_->frames) {
+	for (auto &f : scene_->frames) {
 
 		ftl::cuda::dibr_attribute(
 			f.createTexture<float4>(Channel::Normals),
@@ -185,15 +185,15 @@ void Splatter::renderChannel(
 		out.getTexture<float4>(Channel::Normals),
 		temp_.getTexture<float>(Channel::Contribution),
 		stream
-	);*/
+	);
 
 	//auto &t = out.createTexture<float4>(Channel::Points, Format<float4>(params.camera.width, params.camera.height));
 	//ftl::cuda::point_cloud(t, temp_.getTexture<int>(Channel::Depth2), params.camera, params.m_viewMatrixInverse, 0, stream);
-	ftl::cuda::normals(out.createTexture<float4>(Channel::Normals),
+	/*ftl::cuda::normals(out.createTexture<float4>(Channel::Normals),
 		temp_.getTexture<float4>(Channel::Normals),
 		temp_.getTexture<int>(Channel::Depth2), 
-		5, 0.04f,
-		params.camera, params.m_viewMatrixInverse.getFloat3x3(), params.m_viewMatrix.getFloat3x3(), stream);
+		9, 0.04f,
+		params.camera, params.m_viewMatrixInverse.getFloat3x3(), params.m_viewMatrix.getFloat3x3(), stream);*/
 
 	//if (norm_filter_ > -0.1f) {
 	//	ftl::cuda::normal_filter(f.getTexture<float4>(Channel::Normals), f.getTexture<float4>(Channel::Points), s->parameters(), pose, norm_filter_, stream);
diff --git a/components/renderers/cpp/src/splatter.cu b/components/renderers/cpp/src/splatter.cu
index 36c4fa6d5..786bb7215 100644
--- a/components/renderers/cpp/src/splatter.cu
+++ b/components/renderers/cpp/src/splatter.cu
@@ -204,13 +204,14 @@ __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));
+		//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) {
             // Adjust from normalised ray back to original meters units
-            t *= scale;
+			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));
 
-- 
GitLab