VisualizationMesh.cpp
Go to the documentation of this file.
1#include "VisualizationMesh.h"
2
3#include <Inventor/nodes/SoCoordinate3.h>
4#include <Inventor/nodes/SoIndexedFaceSet.h>
5#include <Inventor/nodes/SoMaterial.h>
6#include <Inventor/nodes/SoMaterialBinding.h>
7#include <Inventor/nodes/SoNormal.h>
8#include <Inventor/nodes/SoShapeHints.h>
9
10namespace armarx::viz::coin
11{
12
14 {
15 SoMaterialBinding* myBinding = new SoMaterialBinding;
16 myBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED;
17
18 materials = new SoMaterial;
19 coords = new SoCoordinate3;
20
21 SoShapeHints* hints = new SoShapeHints;
22 // Disable back culling and enable two-sided lighting
23 hints->vertexOrdering = SoShapeHints::VertexOrdering::COUNTERCLOCKWISE;
24 hints->shapeType = SoShapeHints::ShapeType::UNKNOWN_SHAPE_TYPE;
25
26 faceSet = new SoIndexedFaceSet;
27
28 node->addChild(myBinding);
29 node->addChild(materials);
30 node->addChild(coords);
31 node->addChild(hints);
32 node->addChild(faceSet);
33 }
34
35 bool
37 {
38 int colorSize = (int)element.colors.size();
39 bool noColorsArray = colorSize == 0;
40 if (colorSize == 0)
41 {
42 colorSize = 1;
43 }
44 matColor.resize(colorSize);
45 transp.resize(colorSize);
46
47 const float conv = 1.0f / 255.0f;
48 if (noColorsArray)
49 {
50 auto color = element.color;
51 float r = color.r * conv;
52 float g = color.g * conv;
53 float b = color.b * conv;
54 float a = color.a * conv;
55 matColor[0].setValue(r, g, b);
56 transp[0] = 1.0f - a;
57 }
58 else
59 {
60 for (int i = 0; i < colorSize; i++)
61 {
62 auto color = element.colors[i];
63 float r = color.r * conv;
64 float g = color.g * conv;
65 float b = color.b * conv;
66 float a = color.a * conv;
67 matColor[i].setValue(r, g, b);
68 transp[i] = 1.0f - a;
69 }
70 }
71
72 // Define colors for the faces
73 materials->diffuseColor.setValuesPointer(colorSize, matColor.data());
74 materials->ambientColor.setValuesPointer(colorSize, matColor.data());
75 materials->transparency.setValuesPointer(colorSize, transp.data());
76
77 // define vertex array
78 int vertexSize = (int)element.vertices.size();
79 vertexPositions.resize(vertexSize);
80 for (int i = 0; i < vertexSize; i++)
81 {
82 auto v = element.vertices[i];
83 vertexPositions[i].setValue(v.e0, v.e1, v.e2);
84 }
85
86 // Define coordinates for vertices
87 coords->point.setValuesPointer(vertexSize, vertexPositions.data());
88
89 int facesSize = (int)element.faces.size();
90 faces.resize(facesSize * 4);
91 matInx.resize(facesSize * 4);
92
93 for (int i = 0; i < facesSize; i++)
94 {
95 auto& face = element.faces[i];
96
97 faces[i * 4 + 0] = face.v0;
98 faces[i * 4 + 1] = face.v1;
99 faces[i * 4 + 2] = face.v2;
100 faces[i * 4 + 3] = SO_END_FACE_INDEX;
101
102 matInx[i * 4 + 0] = face.c0;
103 matInx[i * 4 + 1] = face.c1;
104 matInx[i * 4 + 2] = face.c2;
105 matInx[i * 4 + 3] = SO_END_FACE_INDEX;
106 }
107
108 faceSet->coordIndex.setValuesPointer(faces.size(), faces.data());
109 faceSet->materialIndex.setValuesPointer(matInx.size(), matInx.data());
110
111 return true;
112 }
113} // namespace armarx::viz::coin
bool update(ElementType const &element)