iMSTK
Interactive Medical Simulation Toolkit
imstkVTKPolyDataRenderDelegate.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkVTKPolyDataRenderDelegate.h"
8 #include "imstkRenderMaterial.h"
9 #include "imstkVisualModel.h"
10 #include "imstkColorFunction.h"
11 
12 #include <vtkActor.h>
13 #include <vtkColorTransferFunction.h>
14 #include <vtkPolyDataMapper.h>
15 #include <vtkProperty.h>
16 
17 namespace imstk
18 {
19 void
21 {
22  std::shared_ptr<RenderMaterial> material = m_visualModel->getRenderMaterial();
23  vtkSmartPointer<vtkProperty> actorProperty = vtkActor::SafeDownCast(m_actor)->GetProperty();
24  vtkSmartPointer<vtkPolyDataMapper> polyMapper = vtkPolyDataMapper::SafeDownCast(m_mapper);
25 
26  if (material->getScalarVisibility() && polyMapper != nullptr)
27  {
28  // Convert color table
29  std::shared_ptr<ColorFunction> imstkLookupTable = material->getColorLookupTable();
30  const double* range = imstkLookupTable->getRange().data();
31  double spacing = (range[1] - range[0]) / imstkLookupTable->getNumberOfColors();
32  vtkNew<vtkColorTransferFunction> lookupTable;
33  lookupTable->SetColorSpaceToRGB();
34  for (int i = 0; i < imstkLookupTable->getNumberOfColors(); i++)
35  {
36  const double t = static_cast<double>(i) / imstkLookupTable->getNumberOfColors();
37  const Color& color = imstkLookupTable->getColor(i);
38  lookupTable->AddRGBPoint(t * (range[1] - range[0]) + range[0] + spacing * 0.5, color.r, color.g, color.b);
39  }
40 
41  switch (imstkLookupTable->getColorSpace())
42  {
43  case ColorFunction::ColorSpace::RGB:
44  lookupTable->SetColorSpaceToRGB();
45  break;
46  case ColorFunction::ColorSpace::HSV:
47  lookupTable->SetColorSpaceToHSV();
48  break;
49  case ColorFunction::ColorSpace::LAB:
50  lookupTable->SetColorSpaceToLab();
51  break;
52  case ColorFunction::ColorSpace::DIVERING:
53  lookupTable->SetColorSpaceToDiverging();
54  break;
55  default:
56  lookupTable->SetColorSpaceToRGB();
57  break;
58  }
59 
60  polyMapper->SetLookupTable(lookupTable);
61  polyMapper->SetScalarVisibility(material->getScalarVisibility());
62  }
63 
64  // Colors & Light
65  const Color& diffuseColor = material->getDiffuseColor();
66  const Color& ambientColor = material->getAmbientColor();
67  const Color& specularColor = material->getSpecularColor();
68  const Color& edgeColor = material->getEdgeColor();
69  const Color& vertexColor = material->getVertexColor();
70  const Color& surfaceColor = material->getColor();
71  const Color& coatColor = material->getCoatColor();
72  const Color& edgeTintColor = material->getEdgeTint();
73 
74  // Phong
75  actorProperty->SetDiffuseColor(diffuseColor.r, diffuseColor.g, diffuseColor.b);
76  actorProperty->SetDiffuse(material->getDiffuse());
77  actorProperty->SetAmbientColor(ambientColor.r, ambientColor.g, ambientColor.b);
78  actorProperty->SetAmbient(material->getAmbient());
79  actorProperty->SetSpecularColor(specularColor.r, specularColor.g, specularColor.b);
80  actorProperty->SetSpecularPower(material->getSpecularPower());
81  actorProperty->SetSpecular(material->getSpecular());
82 
83  // PBR, ORM (occlusion, roughness, metallic)
84  actorProperty->SetOcclusionStrength(material->getOcclusionStrength());
85  actorProperty->SetRoughness(material->getRoughness());
86  actorProperty->SetMetallic(material->getMetalness());
87  actorProperty->SetNormalScale(material->getNormalStrength());
88  // PBR clearcoat
89  actorProperty->SetAnisotropy(material->getAnisotropy());
90  actorProperty->SetAnisotropyRotation(material->getAnisotropyRotation());
91  actorProperty->SetBaseIOR(material->getBaseIOR());
92  actorProperty->SetCoatColor(coatColor.r, coatColor.g, coatColor.b);
93  actorProperty->SetCoatIOR(material->getCoatIOR());
94  actorProperty->SetCoatNormalScale(material->getCoatNormalScale());
95  actorProperty->SetCoatRoughness(material->getCoatRoughness());
96  actorProperty->SetCoatStrength(material->getCoatStrength());
97  actorProperty->SetEdgeTint(edgeTintColor.r, edgeTintColor.g, edgeTintColor.b);
98 
99  // Base
100  actorProperty->SetColor(surfaceColor.r, surfaceColor.g, surfaceColor.b);
101  actorProperty->SetVertexColor(vertexColor.r, vertexColor.g, vertexColor.b);
102  actorProperty->SetEdgeColor(edgeColor.r, edgeColor.g, edgeColor.b);
103  actorProperty->SetLineWidth(material->getLineWidth());
104  actorProperty->SetPointSize(material->getPointSize());
105  actorProperty->SetBackfaceCulling(material->getBackFaceCulling());
106  actorProperty->SetOpacity(material->getOpacity());
107  actorProperty->SetRenderPointsAsSpheres(material->getRenderPointsAsSpheres());
108 
109  if (material->getShadingModel() == RenderMaterial::ShadingModel::PBR)
110  {
111  actorProperty->LightingOn();
112  actorProperty->SetInterpolationToPBR();
113  }
114  else if (material->getShadingModel() == RenderMaterial::ShadingModel::Phong)
115  {
116  actorProperty->LightingOn();
117  actorProperty->SetInterpolationToPhong();
118  }
119  else if (material->getShadingModel() == RenderMaterial::ShadingModel::Gouraud)
120  {
121  actorProperty->LightingOn();
122  actorProperty->SetInterpolationToGouraud();
123  }
124  else if (material->getShadingModel() == RenderMaterial::ShadingModel::Flat)
125  {
126  actorProperty->LightingOn();
127  actorProperty->SetInterpolationToFlat();
128  }
129  else if (material->getShadingModel() == RenderMaterial::ShadingModel::None)
130  {
131  actorProperty->LightingOff();
132  }
133 
134  // Display mode
135  switch (material->getDisplayMode())
136  {
137  case RenderMaterial::DisplayMode::Wireframe:
138  actorProperty->SetRepresentationToWireframe();
139  actorProperty->SetEdgeVisibility(false);
140  break;
141  case RenderMaterial::DisplayMode::Points:
142  actorProperty->SetRepresentationToPoints();
143  actorProperty->SetEdgeVisibility(false);
144  break;
145  case RenderMaterial::DisplayMode::WireframeSurface:
146  actorProperty->SetRepresentationToSurface();
147  actorProperty->SetEdgeVisibility(true);
148  break;
149  case RenderMaterial::DisplayMode::Surface:
150  default:
151  actorProperty->SetRepresentationToSurface();
152  actorProperty->SetEdgeVisibility(false);
153  actorProperty->SetVertexVisibility(false);
154  break;
155  }
156 
157  m_actor->SetVisibility(m_visualModel->isVisible() ? 1 : 0);
158 }
159 } // namespace imstk
Phong shading model (default)
std::shared_ptr< VisualModel > m_visualModel
imstk visual model (contains data (geometry) and render specification (render material)) ...
Flat shading model with no interpolation.
Renders without shading, no lighting.
Compound Geometry.
Gouraud shading model (default)
void updateRenderProperties() override
Updates the actor and mapper properties from the currently set VisualModel.
Color in RGB space.
Definition: imstkColor.h:24
Physically based rendering.