From 12ca0ca9751b874fe40f065fc808703dad200bc2 Mon Sep 17 00:00:00 2001
From: Fabian Paus <fabian.paus@kit.edu>
Date: Thu, 10 Oct 2019 15:29:45 +0200
Subject: [PATCH] Remove normals from mesh (only use generated ones) - This
 avoids back culling, normal and lighting issues

---
 .../components/ArViz/Coin/VisualizerMesh.h    | 52 +++++--------------
 .../components/ArVizExample/ArVizExample.cpp  | 15 ++----
 source/RobotAPI/interface/ArViz/Elements.ice  |  4 --
 3 files changed, 16 insertions(+), 55 deletions(-)

diff --git a/source/RobotAPI/components/ArViz/Coin/VisualizerMesh.h b/source/RobotAPI/components/ArViz/Coin/VisualizerMesh.h
index 656a656d0..d3f38e679 100644
--- a/source/RobotAPI/components/ArViz/Coin/VisualizerMesh.h
+++ b/source/RobotAPI/components/ArViz/Coin/VisualizerMesh.h
@@ -19,34 +19,27 @@ namespace armarx::viz::coin
             SoMaterialBinding* myBinding = new SoMaterialBinding;
             myBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED;
 
-            myMaterials = new SoMaterial;
-            myCoords = new SoCoordinate3;
-            normals = new SoNormal;
-
-            SoNormalBinding* normBinding = new SoNormalBinding;
-            normBinding->value = SoNormalBinding::PER_VERTEX_INDEXED;
+            materials = new SoMaterial;
+            coords = new SoCoordinate3;
 
             SoShapeHints* hints = new SoShapeHints;
             // Disable back culling and enable two-sided lighting
             hints->vertexOrdering = SoShapeHints::VertexOrdering::COUNTERCLOCKWISE;
             hints->shapeType = SoShapeHints::ShapeType::UNKNOWN_SHAPE_TYPE;
 
-            myFaceSet = new SoIndexedFaceSet;
+            faceSet = new SoIndexedFaceSet;
 
             auto* sep = node();
             sep->addChild(myBinding);
-            sep->addChild(myMaterials);
-            sep->addChild(myCoords);
-            sep->addChild(normals);
-            sep->addChild(normBinding);
+            sep->addChild(materials);
+            sep->addChild(coords);
             sep->addChild(hints);
-            sep->addChild(myFaceSet);
+            sep->addChild(faceSet);
         }
 
-        SoMaterial* myMaterials;
-        SoCoordinate3* myCoords;
-        SoNormal* normals;
-        SoIndexedFaceSet* myFaceSet;
+        SoMaterial* materials;
+        SoCoordinate3* coords;
+        SoIndexedFaceSet* faceSet;
     };
 
     struct VisualizerMesh: TypedElementVisualizer<VisualizerMesh, ElementMesh, VisuDataMesh>
@@ -92,7 +85,7 @@ namespace armarx::viz::coin
             }
 
             // Define colors for the faces
-            SoMaterial* myMaterials = data->myMaterials;
+            SoMaterial* myMaterials = data->materials;
             myMaterials->diffuseColor.setValues(0, colorSize, matDif.data());
             myMaterials->ambientColor.setValues(0, colorSize, matAmb.data());
             myMaterials->transparency.setValues(0, colorSize, transp.data());
@@ -107,25 +100,10 @@ namespace armarx::viz::coin
             }
 
             // Define coordinates for vertices
-            data->myCoords->point.setValues(0, vertexSize, vertexPositions.data());
-
-            // define nomals array
-            int normalSize = (int)element.normals.size();
-            // per vertex normals
-            std::vector<SbVec3f> normalsArray(normalSize);
-            for (int i = 0; i < normalSize; i++)
-            {
-                auto n = element.normals[i];
-                normalsArray[i].setValue(n.e0, n.e1, n.e2);
-            }
+            data->coords->point.setValues(0, vertexSize, vertexPositions.data());
 
-            // Define coordinates for vertices
-            data->normals->vector.setValues(0, normalSize, normalsArray.data());
-
-            // define faces and normals
             int facesSize = (int)element.faces.size();
             std::vector<int32_t> faces(facesSize * 4);
-            std::vector<int32_t> normalIndx(facesSize * 4);
             std::vector<int32_t> matInx(facesSize * 4);
 
             for (int i = 0; i < facesSize; i++)
@@ -137,20 +115,14 @@ namespace armarx::viz::coin
                 faces[i * 4 + 2] = face.v2;
                 faces[i * 4 + 3] = SO_END_FACE_INDEX;
 
-                normalIndx[i * 4 + 0] = face.n0;
-                normalIndx[i * 4 + 1] = face.n1;
-                normalIndx[i * 4 + 2] = face.n2;
-                normalIndx[i * 4 + 3] = SO_END_FACE_INDEX;
-
                 matInx[i * 4 + 0] = face.c0;
                 matInx[i * 4 + 1] = face.c1;
                 matInx[i * 4 + 2] = face.c2;
                 matInx[i * 4 + 3] = SO_END_FACE_INDEX;
             }
 
-            SoIndexedFaceSet* myFaceSet = data->myFaceSet;
+            SoIndexedFaceSet* myFaceSet = data->faceSet;
             myFaceSet->coordIndex.setValues(0, faces.size(), faces.data());
-            myFaceSet->normalIndex.setValues(0, normalIndx.size(), normalIndx.data());
             myFaceSet->materialIndex.setValues(0, matInx.size(), matInx.data());
         }
     };
diff --git a/source/RobotAPI/components/ArVizExample/ArVizExample.cpp b/source/RobotAPI/components/ArVizExample/ArVizExample.cpp
index 47fe296b9..ce146f416 100644
--- a/source/RobotAPI/components/ArVizExample/ArVizExample.cpp
+++ b/source/RobotAPI/components/ArVizExample/ArVizExample.cpp
@@ -24,6 +24,7 @@
 
 #include <Eigen/Eigen>
 
+#include <ArmarXCore/core/time/CycleUtil.h>
 #include <ArmarXCore/core/time/TimeUtil.h>
 
 using namespace armarx;
@@ -84,6 +85,7 @@ void ArVizExample::onDisconnectComponent()
 
 void ArVizExample::update()
 {
+    CycleUtil c(20);
     while (!task->isStopped())
     {
         double timeInSeconds = TimeUtil::GetTime().toSecondsDouble();
@@ -262,30 +264,21 @@ void ArVizExample::update()
                 {255, 0, 255, 0},
                 {255, 0, 0, 255},
             };
-            armarx::Vector3f normals[] =
-            {
-                {0.0f, 0.0f, 1.0f},
-                {0.0f, 1.0f, 0.0f},
-            };
             armarx::viz::Face faces[] =
             {
                 {
-                    2, 1, 0,
                     0, 1, 2,
-                    0, 0, 0
+                    0, 1, 2,
                 },
                 {
                     1, 2, 3,
                     0, 1, 2,
-                    1, 1, 1
                 },
             };
             std::size_t verticesSize = sizeof(vertices) / sizeof(vertices[0]);
             mesh->vertices.assign(vertices, vertices + verticesSize);
             std::size_t colorsSize = sizeof(colors) / sizeof(colors[0]);
             mesh->colors.assign(colors, colors + colorsSize);
-            std::size_t normalsSize = sizeof(normals) / sizeof(normals[0]);
-            mesh->normals.assign(normals, normals + normalsSize);
             std::size_t facesSize = sizeof(faces) / sizeof(faces[0]);
             mesh->faces.assign(faces, faces + facesSize);
 
@@ -299,7 +292,7 @@ void ArVizExample::update()
 
         topic->updateLayers({layer});
 
-        TimeUtil::SleepMS(10);
+        c.waitForCycleDuration();
     }
 }
 
diff --git a/source/RobotAPI/interface/ArViz/Elements.ice b/source/RobotAPI/interface/ArViz/Elements.ice
index 2aac43d49..49296fb3d 100644
--- a/source/RobotAPI/interface/ArViz/Elements.ice
+++ b/source/RobotAPI/interface/ArViz/Elements.ice
@@ -124,9 +124,6 @@ module viz
         int c0 = 0;
         int c1 = 0;
         int c2 = 0;
-        int n0 = 0;
-        int n1 = 0;
-        int n2 = 0;
     };
 
     sequence<Face> FaceSeq;
@@ -135,7 +132,6 @@ module viz
     {
         Vector3fSeq vertices;
         ColorSeq colors;
-        Vector3fSeq normals;
         FaceSeq faces;
     };
 
-- 
GitLab