iMSTK
Interactive Medical Simulation Toolkit
imstkRenderMaterial.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 "imstkRenderMaterial.h"
8 #include "imstkLogger.h"
9 
10 namespace imstk
11 {
12 RenderMaterial::RenderMaterial()
13 {
14  // Instantiating one type of each texture per material
15  for (int i = 0; i < static_cast<int>(Texture::Type::None); i++)
16  {
17  m_textures.emplace_back(std::make_shared<Texture>("", static_cast<Texture::Type>(i)));
18  }
19 }
20 
21 void
22 RenderMaterial::setDisplayMode(const DisplayMode displayMode)
23 {
24  if (displayMode != m_displayMode)
25  {
26  m_displayMode = displayMode;
27  postModified();
28  }
29 }
30 
31 void
32 RenderMaterial::setLineWidth(const double width)
33 {
34  if (width != m_lineWidth)
35  {
36  m_lineWidth = width;
37  postModified();
38  }
39 }
40 
41 void
42 RenderMaterial::setPointSize(const double size)
43 {
44  if (size != m_pointSize)
45  {
46  m_pointSize = size;
47  postModified();
48  }
49 }
50 
51 void
52 RenderMaterial::setBackFaceCulling(const bool culling)
53 {
54  if (culling != m_backfaceCulling)
55  {
56  m_backfaceCulling = culling;
57  postModified();
58  }
59 }
60 
61 void
62 RenderMaterial::backfaceCullingOn()
63 {
64  this->setBackFaceCulling(true);
65 }
66 
67 void
68 RenderMaterial::backfaceCullingOff()
69 {
70  this->setBackFaceCulling(false);
71 }
72 
73 void
74 RenderMaterial::setDiffuseColor(const Color& color)
75 {
76  if (m_diffuseColor != color)
77  {
78  m_diffuseColor = color;
79  postModified();
80  }
81 }
82 
83 void
84 RenderMaterial::setColor(const Color& color)
85 {
86  this->setDiffuseColor(color);
87 }
88 
89 void
90 RenderMaterial::setSpecularColor(const Color& color)
91 {
92  if (m_specularColor != color)
93  {
94  m_specularColor = color;
95  postModified();
96  }
97 }
98 
99 void
100 RenderMaterial::setAmbientColor(const Color& color)
101 {
102  if (m_ambientColor != color)
103  {
104  m_ambientColor = color;
105  postModified();
106  }
107 }
108 
109 void
110 RenderMaterial::setMetalness(const double metalness)
111 {
112  if (m_metalness != metalness)
113  {
114  m_metalness = metalness;
115  postModified();
116  }
117 }
118 
119 void
120 RenderMaterial::setRoughness(const double roughness)
121 {
122  if (roughness != m_roughness)
123  {
124  m_roughness = roughness;
125  postModified();
126  }
127 }
128 
129 void
130 RenderMaterial::setEmissivity(const double emissivity)
131 {
132  if (m_emissivity != emissivity)
133  {
134  m_emissivity = emissivity;
135  postModified();
136  }
137 }
138 
139 void
140 RenderMaterial::setAnisotropy(const double anisotropy)
141 {
142  if (m_anisotropy != anisotropy)
143  {
144  m_anisotropy = anisotropy;
145  postModified();
146  }
147 }
148 
149 void
150 RenderMaterial::setAnisotropyRotation(const double anisotropyRotation)
151 {
152  if (m_anisotropyRotation != anisotropyRotation)
153  {
154  m_anisotropyRotation = anisotropyRotation;
155  postModified();
156  }
157 }
158 
159 void
160 RenderMaterial::setBaseIOR(const double baseIOR)
161 {
162  if (m_baseIOR != baseIOR)
163  {
164  m_baseIOR = baseIOR;
165  postModified();
166  }
167 }
168 
169 void
170 RenderMaterial::setCoatColor(const Color& coatColor)
171 {
172  if (m_coatColor != coatColor)
173  {
174  m_coatColor = coatColor;
175  postModified();
176  }
177 }
178 
179 void
180 RenderMaterial::setCoatIOR(const double coatIOR)
181 {
182  if (m_coatIOR != coatIOR)
183  {
184  m_coatIOR = coatIOR;
185  postModified();
186  }
187 }
188 
189 void
190 RenderMaterial::setCoatNormalScale(const double coatNormalScale)
191 {
192  if (m_coatNormalScale != coatNormalScale)
193  {
194  m_coatNormalScale = coatNormalScale;
195  postModified();
196  }
197 }
198 
199 void
200 RenderMaterial::setCoatRoughness(const double coatRoughness)
201 {
202  if (m_coatRoughness != coatRoughness)
203  {
204  m_coatRoughness = coatRoughness;
205  postModified();
206  }
207 }
208 
209 void
210 RenderMaterial::setCoatStrength(const double coatStrength)
211 {
212  if (m_coatStrength != coatStrength)
213  {
214  m_coatStrength = coatStrength;
215  postModified();
216  }
217 }
218 
219 void
220 RenderMaterial::setEdgeTint(const Color& edgeTint)
221 {
222  if (m_edgeTint != edgeTint)
223  {
224  m_edgeTint = edgeTint;
225  postModified();
226  }
227 }
228 
229 std::shared_ptr<Texture>
230 RenderMaterial::getTexture(Texture::Type type)
231 {
232  if (type >= Texture::Type::None)
233  {
234  LOG(WARNING) << "error: Invalid texture format";
235  return nullptr;
236  }
237  return m_textures[(unsigned int)type];
238 }
239 
240 void
241 RenderMaterial::addTexture(std::shared_ptr<Texture> texture)
242 {
243  if (texture->getType() >= Texture::Type::None)
244  {
245  LOG(WARNING) << "Invalid texture format";
246  return;
247  }
248  m_textures[static_cast<size_t>(texture->getType())] = texture;
249  postEvent(Event(texturesModified()));
250 }
251 
252 void
253 RenderMaterial::removeTexture(std::shared_ptr<Texture> texture)
254 {
255  // The texture (object) must exist
256  auto iter = std::find(m_textures.begin(), m_textures.end(), texture);
257  if (iter != m_textures.end())
258  {
259  const size_t type = static_cast<size_t>(texture->getType());
260  m_textures[type] = std::make_shared<Texture>("", static_cast<Texture::Type>(type));
261  postEvent(Event(texturesModified()));
262  }
263 }
264 
265 void
266 RenderMaterial::removeTexture(Texture::Type type)
267 {
268  // If the texture already has path "" then it is empty
269  const size_t typeInt = static_cast<size_t>(type);
270  std::shared_ptr<Texture> prevTex = m_textures[typeInt];
271  if (prevTex->getPath() != "")
272  {
273  m_textures[typeInt] = std::make_shared<Texture>("", type);
274  postEvent(Event(texturesModified()));
275  }
276 }
277 
278 void
280 {
281  if (m_blendMode != blendMode)
282  {
283  m_blendMode = blendMode;
284  postModified();
285  }
286 }
287 
288 void
289 RenderMaterial::setShadingModel(const ShadingModel& model)
290 {
291  if (model != m_shadingModel)
292  {
293  m_shadingModel = model;
294  postModified();
295  }
296 }
297 
298 void
299 RenderMaterial::setOcclusionStrength(const double occlusionStrength)
300 {
301  if (occlusionStrength != m_occlusionStrength)
302  {
303  m_occlusionStrength = occlusionStrength;
304  postModified();
305  }
306 }
307 
308 void
309 RenderMaterial::setNormalStrength(const double normalStrength)
310 {
311  if (normalStrength != m_normalStrength)
312  {
313  m_normalStrength = normalStrength;
314  postModified();
315  }
316 }
317 
318 void
319 RenderMaterial::setEdgeColor(const Color& color)
320 {
321  if (color != m_edgeColor)
322  {
323  m_edgeColor = color;
324  postModified();
325  }
326 }
327 
328 void
329 RenderMaterial::setVertexColor(const Color& color)
330 {
331  if (color != m_vertexColor)
332  {
333  m_vertexColor = color;
334  postModified();
335  }
336 }
337 
338 void
339 RenderMaterial::setOpacity(const double opacity)
340 {
341  if (m_opacity != opacity)
342  {
343  m_opacity = opacity;
344  postModified();
345  }
346 }
347 
348 void
349 RenderMaterial::setBackfaceCulling(const bool culling)
350 {
351  if (m_backfaceCulling != culling)
352  {
353  m_backfaceCulling = culling;
354  postModified();
355  }
356 }
357 
358 void
359 RenderMaterial::setColorLookupTable(std::shared_ptr<ColorFunction> lut)
360 {
361  if (m_lookupTable != lut)
362  {
363  m_lookupTable = lut;
364  postModified();
365  }
366 }
367 
368 void
369 RenderMaterial::setScalarVisibility(const bool scalarVisibility)
370 {
371  if (m_scalarVisibility != scalarVisibility)
372  {
373  m_scalarVisibility = scalarVisibility;
374  postModified();
375  }
376 }
377 } // namespace imstk
void addTexture(std::shared_ptr< Texture > texture)
Add/Remove/Get texture.
Base class for events which contain a type, priority, and data priority defaults to 0 and uses a grea...
Type
Texture type - determines filtering.
Definition: imstkTexture.h:30
ShadingModel
surface shading model. Defaults to Phong
double m_emissivity
-------------—PBR specific properties----------------—
DisplayMode
Display mode for the scene objects.
Compound Geometry.
BlendMode
Volume rendering blend mode.
double m_roughness
Value for roughness with range: [0.0, 1.0].
BlendMode m_blendMode
-----------—Volume rendering properties-------------—
virtual void setBlendMode(const BlendMode blendMode)
Get/Set blend mode This function only works for volumes, particles and decals currently and the MAXIM...
Color in RGB space.
Definition: imstkColor.h:24
double m_anisotropy
-------------—PBR Clearcoat properties----------------—
void postEvent(const T &e)
Emits the event Direct observers will be immediately called, in sync Queued observers will receive th...
double m_metalness
Value for metalness with range: [0.0, 1.0].
bool m_backfaceCulling
For performance, uncommon for this to be false.
std::vector< std::shared_ptr< Texture > > m_textures
Ordered by Texture::Type.
double m_lineWidth
----------—Wireframe specific properties-------------—