fw4spl
SMaterial.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "visuVTKAdaptor/SMaterial.hpp"
8 
9 #include <fwData/Material.hpp>
10 #include <fwData/mt/ObjectReadLock.hpp>
11 
12 #include <fwServices/macros.hpp>
13 
14 #include <fwTools/fwID.hpp>
15 
16 #include <fwVtkIO/vtk.hpp>
17 
18 #include <vtkImageData.h>
19 #include <vtkOpenGLRenderWindow.h>
20 #include <vtkOpenGLTexture.h>
21 #include <vtkProperty.h>
22 #include <vtkRenderer.h>
23 #include <vtkRenderWindowInteractor.h>
24 #include <vtkSmartPointer.h>
25 #include <vtkTexture.h>
26 #include <vtkTextureObject.h>
27 
28 fwServicesRegisterMacro( ::fwRenderVTK::IAdaptor, ::visuVTKAdaptor::SMaterial );
29 
30 namespace visuVTKAdaptor
31 {
32 
33 const ::fwServices::IService::KeyType SMaterial::s_MATERIAL_INPUT = "material";
34 
35 //------------------------------------------------------------------------------
36 
37 SMaterial::SMaterial() noexcept :
38  m_property(vtkProperty::New()),
39  m_manageProperty(true),
40  m_lighting(true)
41 {
42 }
43 
44 //------------------------------------------------------------------------------
45 
46 SMaterial::~SMaterial() noexcept
47 {
48  if (m_manageProperty)
49  {
50  m_property->Delete();
51  }
52  m_property = NULL;
53 }
54 
55 //------------------------------------------------------------------------------
56 
57 void SMaterial::setVtkProperty(vtkProperty* property)
58 {
59  if (m_manageProperty)
60  {
61  m_property->Delete();
62  m_property = NULL;
63  }
64 
65  if (property)
66  {
67  m_manageProperty = false;
68  m_property = property;
69  }
70  else
71  {
72  m_manageProperty = true;
73  m_property = vtkProperty::New();
74  }
75  this->setVtkPipelineModified();
76 }
77 
78 //------------------------------------------------------------------------------
79 
80 vtkProperty* SMaterial::getVtkProperty() const
81 {
82  return m_property;
83 }
84 
85 //------------------------------------------------------------------------------
86 
88 {
89  this->configureParams();
90 }
91 
92 //------------------------------------------------------------------------------
93 
95 {
96  this->initialize();
97 }
98 
99 //------------------------------------------------------------------------------
100 
102 {
103  ::fwData::Material::csptr material = this->getInput < ::fwData::Material >(s_MATERIAL_INPUT);
104  SLM_ASSERT("Missing material", material);
105 
106  this->updateMaterial( material );
107  this->requestRender();
108 }
109 
110 //------------------------------------------------------------------------------
111 
113 {
114 
115 }
116 
117 //------------------------------------------------------------------------------
118 
120 {
121  //3DVSP-like rendering
122  m_property->SetLighting(material->getShadingMode() > 0);
123 
124  ::fwData::Color::sptr diffuse = material->diffuse();
125  m_property->SetDiffuseColor(diffuse->red(), diffuse->green(), diffuse->blue());
126 
127  // Use alpha from the diffuse color
128  m_property->SetOpacity( diffuse->alpha() );
129 
130  ::fwData::Color::sptr ambient = material->ambient();
131  m_property->SetAmbientColor(ambient->red(), ambient->green(), ambient->blue());
132 
133  m_property->SetSpecularColor(1., 1., 1.);
134  m_property->SetSpecularPower(100.); //Shininess
135 
136  // set texture
137  ::fwData::Image::sptr diffTex = material->getDiffuseTexture();
138 
139  if(diffTex != NULL)
140  {
141  ::fwData::mt::ObjectReadLock lock(diffTex);
142 
143  if (diffTex->getSizeInBytes() != 0)
144  {
145  vtkSmartPointer< vtkImageData > vtkImage = vtkSmartPointer< vtkImageData >::New();
146  ::fwVtkIO::toVTKImage( diffTex, vtkImage );
147 
148  vtkRenderWindow* win = this->getRenderer()->GetRenderWindow();
149 
150  vtkSmartPointer<vtkTexture> vtkTex;
151  if(win->IsA("vtkOpenGLRenderWindow"))
152  {
153  int dims[3];
154  vtkImage->GetDimensions(dims);
155  const int type = vtkImage->GetScalarType();
156  const int noc = vtkImage->GetNumberOfScalarComponents();
157 
158  // Use a Texture object to finely create the texture
159  vtkSmartPointer<vtkTextureObject> to = vtkSmartPointer< vtkTextureObject >::New();
160  to->SetContext(dynamic_cast<vtkOpenGLRenderWindow*>(this->getRenderer()->GetRenderWindow()));
161  to->Create2DFromRaw(dims[0], dims[1], noc, type,
162  const_cast<void*>(static_cast<const void* const>(vtkImage->GetScalarPointer())));
163 
164  vtkSmartPointer<vtkOpenGLTexture> vtkGLTex = vtkSmartPointer< vtkOpenGLTexture >::New();
165  vtkGLTex->SetTextureObject(to);
166  vtkTex = vtkGLTex;
167  }
168  else
169  {
170  vtkTex = vtkSmartPointer< vtkTexture >::New();
171  vtkTex->SetInputData(vtkImage);
172  }
173 
176  vtkTex->SetRepeat( wrapping == ::fwData::Material::REPEAT );
177  vtkTex->SetEdgeClamp( wrapping == ::fwData::Material::CLAMP );
178  vtkTex->SetInterpolate( filtering == ::fwData::Material::LINEAR );
179  m_property->RemoveTexture("diffuse");
180  m_property->SetTexture("diffuse", vtkTex);
181  }
182  }
183 
184  switch(material->getRepresentationMode())
185  {
186  case ::fwData::Material::SURFACE:
187  m_property->SetRepresentationToSurface();
188  m_property->EdgeVisibilityOff();
189  break;
190 
191  case ::fwData::Material::EDGE:
192  m_property->SetRepresentationToSurface();
193  m_property->EdgeVisibilityOn();
194  break;
195 
196  case ::fwData::Material::WIREFRAME:
197  m_property->SetRepresentationToWireframe();
198  break;
199 
200  case ::fwData::Material::POINT:
201  m_property->SetRepresentationToPoints();
202  break;
203 
204  default:
205  OSLM_ASSERT("Unknown material representation mode : " << material->getRepresentationMode(), false );
206  }
207 
208  switch(material->getShadingMode())
209  {
211  case ::fwData::Material::AMBIENT:
212  break;
213  case ::fwData::Material::PHONG:
214  m_property->SetInterpolationToPhong();
215  break;
216 
217  case ::fwData::Material::GOURAUD:
218  m_property->SetInterpolationToGouraud();
219  break;
220 
221  case ::fwData::Material::FLAT:
222  m_property->SetInterpolationToFlat();
223  break;
224 
225  default:
226  OSLM_ASSERT("Unknown shading mode : " << material->getShadingMode(), false );
227  }
228 
229  m_property->Modified();
230  this->setVtkPipelineModified();
231 }
232 
233 //------------------------------------------------------------------------------
234 
236 {
237  KeyConnectionsMap connections;
238  connections.push( s_MATERIAL_INPUT, ::fwData::Material::s_MODIFIED_SIG, s_UPDATE_SLOT);
239 
240  return connections;
241 }
242 
243 //------------------------------------------------------------------------------
244 
245 } //namespace visuVTKAdaptor
void updateMaterial(std::shared_ptr< const ::fwData::Material > material)
Definition: SMaterial.cpp:119
#define CSPTR(_cls_)
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
WrappingType & getDiffuseTextureWrapping()
get/set the texture wrapping
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
Definition: SMaterial.cpp:94
#define OSLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:310
FWRENDERVTK_API vtkRenderer * getRenderer()
Returns the renderer.
WrappingType
Texture wrapping types.
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
This class defines a material. A material is represented by an ambient color and a diffuse color...
Manage material representation of meshes.
Definition: SMaterial.hpp:37
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
Definition: SMaterial.cpp:101
virtual VISUVTKADAPTOR_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
Definition: SMaterial.cpp:235
FWRENDERVTK_API void configureParams()
Parse the xml configuration for renderer, picker and transform.
A helper to lock object on read mode.
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
Definition: SMaterial.cpp:112
FilteringType
Texture filtering types.
FWRENDERVTK_API void requestRender()
notify a render request iff vtkPipeline is modified
FWRENDERVTK_API void setVtkPipelineModified()
End-user have to call this method when a vtk structure has been modified, thus a render request will ...
ShadingType & getShadingMode()
get/set the shading models(flat, gouraud, phong)
FWDATA_API Color::sptr diffuse() const
returns editable diffuse color
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
Definition: SMaterial.cpp:87
FWDATA_API Image::sptr getDiffuseTexture() const
returns editable diffuse texture
RepresentationType & getRepresentationMode()
get/set the representation models (edge, point, wireframe, surface)
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
FWRENDERVTK_API void initialize()
Initialize the adaptor with the associated render service. (must be call in starting).
FilteringType & getDiffuseTextureFiltering()
get/set the texture filtering
FWDATA_API Color::sptr ambient() const
returns editable ambient color
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177