iMSTK
Interactive Medical Simulation Toolkit
imstkTexture.h
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 #pragma once
8 
9 #include "imstkEventObject.h"
10 #include "imstkColor.h"
11 
12 #include <string>
13 #include <memory>
14 
15 namespace imstk
16 {
17 class ImageData;
18 
24 class Texture : public EventObject
25 {
26 public:
30  enum class Type
31  {
32  Diffuse = 0, // Also used for albedo
33  Normal,
34  Roughness,
35  Metalness,
36  SubsurfaceScattering,
37  AmbientOcclusion,
38  Cavity,
39  Cubemap,
40  IrradianceCubeMap,
41  RadianceCubeMap,
42  ORM,
43  BRDF_LUT,
44  Emissive,
45  Anistropy,
46  CoatNormal,
47  None
48  };
49 
53  enum class FileType
54  {
55  Unknown,
56  Bmp,
57  Png,
58  Jpg,
59  Dds
60  };
61 
62  enum class WrapType
63  {
64  CLAMP_TO_EDGE, // Clamps without border color
65  CLAMP_TO_BORDER, // Pixels outside [0,1] use border color
66  REPEAT // Pixels outside [0,1] repeat back to [0,1] in a modulus fashion. Such that 1.3, becomes 0.3
67  };
68 
74  Texture(std::string path = "", Type type = Type::Diffuse);
75 
81  Texture(std::shared_ptr<ImageData> imageTex, Type type = Type::Diffuse);
82 
86  virtual ~Texture() = default;
87 
88  // *INDENT-OFF*
89  SIGNAL(Texture, modified);
90  // *INDENT-ON*
91 
92  void postModified() { this->postEvent(Event(modified())); }
93 
97  Type getType() const;
98 
102  const std::string& getPath() const { return m_path; }
103 
108 
112  bool getMipmapsEnabled() const { return m_mipmapsEnabled; }
113 
117  const WrapType& getWrapType() const { return m_wrapType; }
118  void setWrapType(const WrapType& repeat)
119  {
120  m_wrapType = repeat;
121  postModified();
122  }
123 
124  const Color& getBorderColor() const { return m_borderColor; }
125  void setBorderColor(const Color& color)
126  {
127  m_borderColor = color;
128  postModified();
129  }
130 
134  bool isAnisotropyEnabled() const { return m_anisotropyEnabled; }
135 
139  double getAnisotropyFactor() const { return m_anisotropyFactor; }
140 
144  void setImageData(std::shared_ptr<ImageData> imgData) { imageTexture = imgData; }
145 
149  void setInterpolation(const bool interpolation)
150  {
151  m_interpolation = interpolation;
152  postModified();
153  }
154 
158  bool getInterpolation() { return m_interpolation; }
159 
163  std::shared_ptr<ImageData> getImageData() const { return imageTexture; }
164 
165 protected:
166  std::shared_ptr<ImageData> imageTexture = nullptr;
168  std::string m_path = "";
169 
170  // Helps with texture aliasing (and a little with performance)
171  bool m_mipmapsEnabled = true;
172 
173  WrapType m_wrapType = WrapType::REPEAT;
174  Color m_borderColor = Color::Black;
175 
176  // Helps sharpen mipmapped textures at more extreme angles
177  bool m_anisotropyEnabled = true;
178  double m_anisotropyFactor = 1.0;
179 
180  // Use interpolation when texturing?
181  bool m_interpolation = true;
182 };
183 } // namespace imstk
184 
185 namespace std
186 {
191 template<> struct less<std::shared_ptr<imstk::Texture>>
192 {
193  bool operator()(const std::shared_ptr<imstk::Texture>& texture1,
194  const std::shared_ptr<imstk::Texture>& texture2) const
195  {
196  if (texture1->getType() != texture2->getType())
197  {
198  return (texture1->getType() < texture2->getType());
199  }
200 
201  if (texture1->getPath() != texture2->getPath())
202  {
203  return (texture1->getPath() < texture2->getPath());
204  }
205 
206  if (texture1->getMipmapsEnabled() != texture2->getMipmapsEnabled())
207  {
208  return (texture1->getMipmapsEnabled() < texture2->getMipmapsEnabled());
209  }
210 
211  if (texture1->isAnisotropyEnabled() != texture2->isAnisotropyEnabled())
212  {
213  return (texture1->isAnisotropyEnabled() < texture2->isAnisotropyEnabled());
214  }
215 
216  if (texture1->getAnisotropyFactor() != texture2->getAnisotropyFactor())
217  {
218  return (texture1->getAnisotropyFactor() < texture2->getAnisotropyFactor());
219  }
220 
221  return false;
222  }
223 };
224 } // namespace std
bool getInterpolation()
Get whether interpolation is used when sampling the texture.
Definition: imstkTexture.h:158
void setInterpolation(const bool interpolation)
Set whether interpolation is used when sampling the texture.
Definition: imstkTexture.h:149
Base class for events which contain a type, priority, and data priority defaults to 0 and uses a grea...
Texture(std::string path="", Type type=Type::Diffuse)
Constructor.
Type
Texture type - determines filtering.
Definition: imstkTexture.h:30
Compound Geometry.
std::string m_path
Texture file path.
Definition: imstkTexture.h:168
bool isAnisotropyEnabled() const
Get if anisotropic filtering is enabled.
Definition: imstkTexture.h:134
Type getType() const
Get type.
double getAnisotropyFactor() const
Get anisotropy factor.
Definition: imstkTexture.h:139
void setImageData(std::shared_ptr< ImageData > imgData)
Set the input image data, not required (paths to files can be used instead)
Definition: imstkTexture.h:144
bool getMipmapsEnabled() const
Get if mipmaps are enabled.
Definition: imstkTexture.h:112
FileType getFileType()
Get file extension.
Color in RGB space.
Definition: imstkColor.h:24
EventObject is the base class for all objects in iMSTK that can receive and emit events. It supports direct and queued observer functions. Direct observers receive events immediately on the same thread This can either be posted on an object or be a function pointer Queued observers receive events within their queue which they can process whenever they like. These can be connected with the connect/queuedConnect/disconnect functions Lambda recievers cannot be disconnected unless all receivers to a signal are removed.
A texture can be defined by file reference or ImageData input.
Definition: imstkTexture.h:24
const std::string & getPath() const
Get path.
Definition: imstkTexture.h:102
const WrapType & getWrapType() const
Get the wrapping type.
Definition: imstkTexture.h:117
std::shared_ptr< ImageData > getImageData() const
Get the input image data for the texture, not required (paths to files can be used instead) ...
Definition: imstkTexture.h:163
Type m_type
Texture type.
Definition: imstkTexture.h:167
void postEvent(const T &e)
Emits the event Direct observers will be immediately called, in sync Queued observers will receive th...
virtual ~Texture()=default
Destructor.
This method is defined to allow for the map to be properly indexed by Texture objects.