Skip to content
Snippets Groups Projects
Commit e959b41b authored by Nicholas Milef's avatar Nicholas Milef
Browse files

ENH: Added texture manager and render material class

parent a177c405
No related branches found
No related tags found
No related merge requests found
Showing
with 843 additions and 104 deletions
......@@ -6,6 +6,7 @@ imstk_add_library( Geometry
DEPENDS
Core
Assimp
Materials
VegaFEM::volumetricMesh
${VTK_LIBRARIES}
)
......
......@@ -342,44 +342,4 @@ SurfaceMesh::getDefaultTCoords()
return m_defaultTCoords;
}
void
SurfaceMesh::addTexture(std::string tFileName, std::string tCoordsName)
{
if (tCoordsName == "")
{
tCoordsName = m_defaultTCoords;
if (tCoordsName == "")
{
LOG(WARNING) << "Can not add texture without default texture coordinates. ";
return;
}
}
if (!m_pointDataMap.count(tCoordsName))
{
LOG(WARNING) << "Mesh does not hold any array named " << tCoordsName << ". "
<< "Can not add texture.";
return;
}
m_textureMap[tCoordsName] = tFileName;
}
const std::map<std::string, std::string>&
SurfaceMesh::getTextureMap() const
{
return m_textureMap;
}
std::string
SurfaceMesh::getTexture(std::string tCoordsName) const
{
if (!m_textureMap.count(tCoordsName))
{
LOG(WARNING) << "No texture filename associated with coordinates " << tCoordsName << ".";
return "";
}
return m_textureMap.at(tCoordsName);
}
} // imstk
......@@ -147,18 +147,11 @@ public:
int getNumTriangles() const;
///
/// \brief Set/Get the array defining the default texture coordinates
/// \brief Set/Get the array defining the default material coordinates
///
void setDefaultTCoords(std::string arrayName);
std::string getDefaultTCoords();
///
/// \brief Add texture by giving the texture file name and the texture coordinates array name
///
void addTexture(std::string tFileName, std::string tCoordsName = "");
const std::map<std::string, std::string>& getTextureMap() const;
std::string getTexture(std::string tCoordsName) const;
protected:
std::vector<TriangleArray> m_trianglesVertices; ///> Triangle connectivity
......@@ -171,8 +164,7 @@ protected:
StdVectorOfVec3d m_vertexTangents; ///> Tangents of the vertices
StdVectorOfVec3d m_vertexBitangents; ///> Bitangents of the vertices
std::string m_defaultTCoords = ""; ///> Name of the array used as default texture coordinates
std::map<std::string, std::string> m_textureMap; ///> Mapping texture coordinates to texture
std::string m_defaultTCoords = ""; ///> Name of the array used as default material coordinates
};
} // imstk
......
......@@ -182,4 +182,16 @@ Geometry::getTypeName() const
}
}
void
Geometry::setRenderMaterial(std::shared_ptr<RenderMaterial> renderMaterial)
{
m_renderMaterial = renderMaterial;
}
std::shared_ptr<RenderMaterial>
Geometry::getRenderMaterial() const
{
return m_renderMaterial;
}
} // imstk
......@@ -23,7 +23,9 @@
#define imstkGeometry_h
#include "g3log/g3log.hpp"
#include "imstkMath.h"
#include "imstkRenderMaterial.h"
namespace imstk
{
......@@ -142,12 +144,19 @@ public:
///
const std::string getTypeName() const;
///
/// \brief Set/Get render material
///
void setRenderMaterial(std::shared_ptr<RenderMaterial> renderMaterial);
std::shared_ptr<RenderMaterial> getRenderMaterial() const;
protected:
Type m_type; ///> Geometry type
Vec3d m_position; ///> position
Quatd m_orientation; ///> orientation
double m_scaling = 1; ///> Scaling
std::shared_ptr<RenderMaterial> m_renderMaterial = nullptr; // Render material
};
} //imstk
......
#-----------------------------------------------------------------------------
# Create target
#-----------------------------------------------------------------------------
include(imstkAddLibrary)
imstk_add_library( Materials
DEPENDS
Core
)
#-----------------------------------------------------------------------------
# Testing
#-----------------------------------------------------------------------------
if( iMSTK_BUILD_TESTING )
add_subdirectory( Testing )
endif()
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#include "imstkRenderMaterial.h"
namespace imstk
{
RenderMaterial::RenderMaterial()
{
// Instantiating one type of each texture per material
for (int i = 0; i < (int)Texture::Type::NONE; i++)
{
m_textures.emplace_back(std::make_shared<Texture>("", (Texture::Type)i));
}
}
const RenderMaterial::DisplayMode
RenderMaterial::getDisplayMode() const
{
return m_displayMode;
}
void
RenderMaterial::setDisplayMode(const DisplayMode displayMode)
{
if (displayMode == m_displayMode)
{
return;
}
m_displayMode = displayMode;
m_stateModified = true;
m_modified = true;
}
const float
RenderMaterial::getLineWidth() const
{
return m_lineWidth;
}
void
RenderMaterial::setLineWidth(const float width)
{
if (width == m_lineWidth)
{
return;
}
m_lineWidth = width;
m_stateModified = true;
m_modified = true;
}
const float
RenderMaterial::getPointSize() const
{
return m_pointSize;
}
void
RenderMaterial::setPointSize(const float size)
{
if (size == m_pointSize)
{
return;
}
m_pointSize = size;
m_stateModified = true;
m_modified = true;
}
const bool
RenderMaterial::getBackFaceCulling() const
{
return m_backfaceCulling;
}
void
RenderMaterial::setBackFaceCulling(const bool culling)
{
if (culling == m_backfaceCulling)
{
return;
}
m_backfaceCulling = culling;
m_stateModified = true;
m_modified = true;
}
void
RenderMaterial::backfaceCullingOn()
{
this->setBackFaceCulling(true);
}
void
RenderMaterial::backfaceCullingOff()
{
this->setBackFaceCulling(false);
}
const Color&
RenderMaterial::getDiffuseColor() const
{
return m_diffuseColor;
}
void
RenderMaterial::setDiffuseColor(const Color color)
{
m_diffuseColor = color;
m_modified = true;
}
const Color&
RenderMaterial::getSpecularColor() const
{
return m_specularColor;
}
void
RenderMaterial::setSpecularColor(const Color color)
{
m_specularColor = color;
m_modified = true;
}
const float&
RenderMaterial::getSpecularity() const
{
return m_specularity;
}
void
RenderMaterial::setSpecularity(const float specularity)
{
m_specularity = specularity;
}
std::shared_ptr<Texture>
RenderMaterial::getTexture(Texture::Type type)
{
if (type >= Texture::Type::NONE)
{
LOG(WARNING) << "RenderMaterial::getTexture error: Invalid texture format";
return nullptr;
}
return m_textures[type];
}
void
RenderMaterial::addTexture(std::shared_ptr<Texture> texture)
{
if (texture->getType() >= Texture::Type::NONE)
{
LOG(WARNING) << "RenderMaterial::addTexture: Invalid texture format";
return;
}
m_textures[texture->getType()] = texture;
}
}
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#ifndef imstkRenderMaterial_h
#define imstkRenderMaterial_h
#include "imstkTexture.h"
#include "imstkColor.h"
#include "imstkTextureManager.h"
#include <memory>
#include <vector>
#include "g3log/g3log.hpp"
namespace imstk
{
class RenderMaterial
{
public:
enum DisplayMode
{
SURFACE,
WIREFRAME,
POINTS,
WIREFRAME_SURFACE
};
///
/// \brief Constructor
///
RenderMaterial();
///
/// \brief Get/Set display mode
///
const DisplayMode getDisplayMode() const;
void setDisplayMode(const DisplayMode displayMode);
///
/// \brief Get/Set line width or the wireframe
///
const float getLineWidth() const;
void setLineWidth(const float width);
///
/// \brief Get/Set point size
///
const float getPointSize() const;
void setPointSize(const float size);
///
/// \brief Backface culling on/off
///
const bool getBackFaceCulling() const;
void setBackFaceCulling(const bool culling);
void backfaceCullingOn();
void backfaceCullingOff();
///
/// \brief Get/Set the diffuse color
///
const Color& getDiffuseColor() const;
void setDiffuseColor(const Color color);
///
/// \brief Get/Set the specular color (only set for metals)
///
const Color& getSpecularColor() const;
void setSpecularColor(const Color color);
///
/// \brief Get/Set the specularity
///
const float& getSpecularity() const;
void setSpecularity(const float specularity);
///
/// \brief Add/Get texture
///
void addTexture(std::shared_ptr<Texture> texture);
std::shared_ptr<Texture> getTexture(Texture::Type type);
protected:
friend class VTKRenderDelegate;
// State
DisplayMode m_displayMode = DisplayMode::SURFACE;
float m_lineWidth = 1.0;
float m_pointSize = 1.0;
bool m_backfaceCulling = true; ///< For performance, uncommon for this to be false
// Colors
Color m_diffuseColor = Color::White;
Color m_specularColor = Color::Black;
// Classical values
float m_specularity = 0.0; ///< Not shiny by default
// Textures
std::vector<std::shared_ptr<Texture>> m_textures; ///< Ordered by Texture::Type
bool m_stateModified = true; ///< Flag for expensive state changes
bool m_modified = true; ///< Flag for any material property changes
};
}
#endif
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#include "imstkTexture.h"
namespace imstk
{
Texture::Texture(std::string path, Type type)
{
m_path = path;
m_type = type;
}
const Texture::Type
Texture::getType() const
{
return m_type;
}
const std::string
Texture::getPath() const
{
return m_path;
}
}
\ No newline at end of file
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#ifndef imstkTexture_h
#define imstkTexture_h
#include <string>
#include <memory>
namespace imstk
{
///
/// \class Texture
///
/// \brief iMSTK texture class. There are a few texture types that
/// dictate how texture are to be treated.
///
class Texture
{
public:
///
/// \brief Texture type - determines filtering
///
enum Type
{
DIFFUSE = 0, // Also used for albedo
NORMAL,
SPECULAR,
ROUGHNESS,
METALNESS,
AMBIENT_OCCLUSION,
CAVITY,
CUBEMAP,
NONE
};
///
/// \brief Constructor
/// \param path Path to the texture source file
/// \param type Type of texture
///
Texture(std::string path = "", Type type = DIFFUSE);
///
/// \brief Destructor
///
virtual ~Texture() {}
///
/// \brief Get type
///
const Type getType() const;
///
/// \brief Get path
///
const std::string getPath() const;
protected:
Type m_type; ///< Texture type
std::string m_path; ///< Texture file path
};
}
// This method is defined to allow for the map to be properly indexed by Texture objects
namespace std
{
template<> struct less<std::shared_ptr<imstk::Texture>>
{
bool operator() (const std::shared_ptr<imstk::Texture>& texture1,
const std::shared_ptr<imstk::Texture>& texture2) const
{
if (texture2->getType() != texture2->getType())
{
return (texture2->getType() < texture2->getType());
}
if (texture1->getPath() != texture2->getPath())
{
return (texture1->getPath() < texture2->getPath());
}
return false;
}
};
}
#endif
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#ifndef imstkTextureDelegate_h
#define imstkTextureDelegate_h
#include <string>
#include <memory>
#include "imstkTexture.h"
namespace imstk
{
///
/// \class TextureDelegate
///
/// \brief iMSTK texture delegate abstract class
///
class TextureDelegate
{
protected:
///
/// \brief Constructor
/// \param texture The texture
///
TextureDelegate() {}
///
/// \brief Abstract function to load textures
/// \param texture Texture to load
///
virtual void loadTexture(std::shared_ptr<Texture> texture) = 0;
};
}
#endif
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#ifndef imstkTextureManager_h
#define imstkTextureManager_h
#include "imstkTexture.h"
#include "imstkTextureDelegate.h"
#include <map>
#include <string>
#include <memory>
namespace imstk
{
template<class T>
class TextureManager
{
static_assert(std::is_base_of<TextureDelegate, T>::value, "T isn't a subclass of TextureDelegate");
public:
///
/// \brief Add texture
///
std::shared_ptr<T> getTextureDelegate(std::shared_ptr<Texture> texture);
protected:
friend class VTKRenderer;
///
/// \brief Constructor
///
TextureManager() {}
std::map<std::shared_ptr<Texture>, std::shared_ptr<T>> m_textureMap;
};
template<class T> std::shared_ptr<T>
TextureManager<T>::getTextureDelegate(std::shared_ptr<Texture> texture)
{
if (m_textureMap.count(texture) == 0)
{
auto textureDelegate = std::make_shared<T>();
textureDelegate->loadTexture(texture);
m_textureMap[texture] = textureDelegate;
}
return m_textureMap[texture];
}
}
#endif
......@@ -5,6 +5,7 @@ include(imstkAddLibrary)
imstk_add_library( Rendering
H_FILES
imstkVTKRenderer.h
imstkVTKTextureDelegate.h
RenderDelegate/imstkVTKCubeRenderDelegate.h
RenderDelegate/imstkVTKLineMeshRenderDelegate.h
RenderDelegate/imstkVTKPlaneRenderDelegate.h
......@@ -18,6 +19,7 @@ imstk_add_library( Rendering
CPP_FILES
imstkVTKRenderer.cpp
imstkVTKTextureDelegate.cpp
RenderDelegate/imstkVTKCubeRenderDelegate.cpp
RenderDelegate/imstkVTKLineMeshRenderDelegate.cpp
RenderDelegate/imstkVTKPlaneRenderDelegate.cpp
......
......@@ -139,6 +139,7 @@ VTKRenderDelegate::update()
{
// TODO : only when rigid transform applied
this->updateActorTransform();
this->updateActorProperties();
}
void
......@@ -159,4 +160,64 @@ VTKRenderDelegate::updateActorTransform()
m_transform->Translate(pos[0], pos[1], pos[2]);
}
void
VTKRenderDelegate::updateActorProperties()
{
auto material = this->getGeometry()->getRenderMaterial();
if (!material || !material->m_modified)
{
return;
}
auto actorProperty = m_actor->GetProperty();
// Colors & Light
auto diffuseColor = material->m_diffuseColor;
auto specularColor = material->m_specularColor;
auto specularity = material->m_specularity;
actorProperty->SetDiffuseColor(diffuseColor.r, diffuseColor.g, diffuseColor.b);
actorProperty->SetSpecularColor(specularColor.r, specularColor.g, specularColor.b);
actorProperty->SetSpecularPower(specularity);
actorProperty->SetSpecular(1.0);
// Material state is now up to date
material->m_modified = false;
if (!material->m_stateModified)
{
return;
}
// Display mode
switch (material->m_displayMode)
{
case RenderMaterial::DisplayMode::WIREFRAME:
actorProperty->SetRepresentationToWireframe();
actorProperty->SetEdgeVisibility(false);
break;
case RenderMaterial::DisplayMode::POINTS:
actorProperty->SetRepresentationToPoints();
actorProperty->SetEdgeVisibility(false);
break;
case RenderMaterial::DisplayMode::WIREFRAME_SURFACE:
actorProperty->SetRepresentationToSurface();
actorProperty->SetEdgeVisibility(true);
break;
case RenderMaterial::DisplayMode::SURFACE:
default:
actorProperty->SetRepresentationToSurface();
actorProperty->SetEdgeVisibility(false);
break;
}
// Display properties
actorProperty->SetLineWidth(material->m_lineWidth);
actorProperty->SetPointSize(material->m_pointSize);
actorProperty->SetBackfaceCulling(material->m_backfaceCulling);
// Material state is now up to date
material->m_stateModified = false;
}
} // imstk
......@@ -25,12 +25,16 @@
#include <memory>
#include "imstkGeometry.h"
#include "imstkRenderMaterial.h"
#include "imstkVTKTextureDelegate.h"
#include "imstkTextureManager.h"
#include "vtkSmartPointer.h"
#include "vtkAlgorithmOutput.h"
#include "vtkActor.h"
#include "vtkPolyDataMapper.h"
#include "vtkTransform.h"
#include "vtkProperty.h"
namespace imstk
{
......@@ -50,35 +54,40 @@ public:
~VTKRenderDelegate() = default;
///
/// \brief
/// \brief Instantiate proper render delegate
///
static std::shared_ptr<VTKRenderDelegate> make_delegate(std::shared_ptr<Geometry>geom);
static std::shared_ptr<VTKRenderDelegate> make_delegate(std::shared_ptr<Geometry> geom);
///
/// \brief
/// \brief Set up normals and mapper
///
void setUpMapper(vtkAlgorithmOutput *source, const bool rigid);
///
/// \brief
/// \brief Return geometry to render
///
virtual std::shared_ptr<Geometry> getGeometry() const = 0;
///
/// \brief
/// \brief Get VTK renderered object
///
vtkSmartPointer<vtkActor> getVtkActor() const;
///
/// \brief
/// \brief Update render delegate
///
virtual void update();
///
/// \brief
/// \brief Update rendere delegate transform based on the geometry shallow transform
///
void updateActorTransform();
///
/// \brief Update render delegate properties based on the geometry render material
///
void updateActorProperties();
protected:
///
/// \brief Default constructor (protected)
......
......@@ -36,6 +36,7 @@
#include <vtkImageReader2.h>
#include <vtkTexture.h>
#include <vtkProperty.h>
#include <vtkOpenGLPolyDataMapper.h>
#include "g3log/g3log.hpp"
......@@ -85,62 +86,27 @@ VTKSurfaceMeshRenderDelegate::VTKSurfaceMeshRenderDelegate(std::shared_ptr<Surfa
auto source = vtkSmartPointer<vtkTrivialProducer>::New();
source->SetOutput(polydata);
// Setup Mapper & Actor
this->setUpMapper(source->GetOutputPort(), false);
this->updateActorTransform();
// Copy textures
int unit = 0;
auto readerFactory = vtkSmartPointer<vtkImageReader2Factory>::New();
for (auto const &texturePair : surfaceMesh->getTextureMap())
if (m_geometry->getDefaultTCoords() != "")
{
std::string tCoordsName = texturePair.first;
std::string tFileName = texturePair.second;
// Convert texture coordinates
auto tcoords = surfaceMesh->getPointDataArray(tCoordsName);
auto tcoords = m_geometry->getPointDataArray(m_geometry->getDefaultTCoords());
auto vtkTCoords = vtkSmartPointer<vtkFloatArray>::New();
vtkTCoords->SetNumberOfComponents(2);
vtkTCoords->SetName(tCoordsName.c_str());
vtkTCoords->SetName(m_geometry->getDefaultTCoords().c_str());
for (auto const tcoord : tcoords)
{
double tuple[2] = {tcoord[0], tcoord[1]};
double tuple[2] = { tcoord[0], tcoord[1] };
vtkTCoords->InsertNextTuple(tuple);
}
polydata->GetPointData()->SetTCoords(vtkTCoords);
// Read texture image
auto imgReader = readerFactory->CreateImageReader2(tFileName.c_str());
if (!imgReader)
{
LOG(WARNING) << "Could not find reader for " << tFileName;
continue;
}
imgReader->SetFileName(tFileName.c_str());
imgReader->Update();
auto texture = vtkSmartPointer<vtkTexture>::New();
texture->SetInputConnection(imgReader->GetOutputPort());
texture->SetBlendingMode(vtkTexture::VTK_TEXTURE_BLENDING_MODE_ADD);
/* /!\ VTKTextureWrapMode not yet supported in VTK 7
* See here for some work that needs to be imported back to upstream:
* https://gitlab.kitware.com/iMSTK/vtk/commit/62a7ecd8a5f54e243c26960de22d5d1d23ef932b
*
texture->SetWrapMode(vtkTexture::VTKTextureWrapMode::ClampToBorder);
* /!\ MultiTextureAttribute not yet supported in VTK 7
* See here for some work that needs to be imported back to upstream:
* https://gitlab.kitware.com/iMSTK/vtk/commit/ae373026755db42b6fdce5093109ef1a39a76340
*
// Link texture unit to texture attribute
m_mapper->MapDataArrayToMultiTextureAttribute(unit, tCoordsName.c_str(),
vtkDataObject::FIELD_ASSOCIATION_POINTS);
*/
// Set texture
m_actor->GetProperty()->SetTexture(unit, texture);
unit++;
polydata->GetPointData()->SetTCoords(vtkTCoords);
}
// Setup Mapper & Actor
this->setUpMapper(source->GetOutputPort(), false);
this->updateActorTransform();
this->updateActorProperties();
}
void
......@@ -158,4 +124,46 @@ VTKSurfaceMeshRenderDelegate::getGeometry() const
return m_geometry;
}
void
VTKSurfaceMeshRenderDelegate::initializeTextures(TextureManager<VTKTextureDelegate>& textureManager)
{
auto material = m_geometry->getRenderMaterial();
if (material == nullptr)
{
return;
}
// Go through all of the textures
for (int unit = 0; unit < Texture::Type::NONE; unit++)
{
// Get imstk texture
auto texture = material->getTexture((Texture::Type)unit);
if (texture->getPath() == "")
{
continue;
}
// Get vtk texture
auto textureDelegate = textureManager.getTextureDelegate(texture);
/* /!\ VTKTextureWrapMode not yet supported in VTK 7
* See here for some work that needs to be imported back to upstream:
* https://gitlab.kitware.com/iMSTK/vtk/commit/62a7ecd8a5f54e243c26960de22d5d1d23ef932b
*
texture->SetWrapMode(vtkTexture::VTKTextureWrapMode::ClampToBorder);
* /!\ MultiTextureAttribute not yet supported in VTK 7
* See here for some work that needs to be imported back to upstream:
* https://gitlab.kitware.com/iMSTK/vtk/commit/ae373026755db42b6fdce5093109ef1a39a76340
*
// Link texture unit to texture attribute
m_mapper->MapDataArrayToMultiTextureAttribute(unit, tCoordsName.c_str(),
vtkDataObject::FIELD_ASSOCIATION_POINTS);
*/
// Set texture
m_actor->GetProperty()->SetTexture(unit, textureDelegate->getTexture());
}
}
} // imstk
......@@ -25,6 +25,8 @@
#include <memory>
#include "imstkVTKRenderDelegate.h"
#include "imstkVTKTextureDelegate.h"
#include "imstkTextureManager.h"
class vtkDoubleArray;
......@@ -61,11 +63,15 @@ public:
///
std::shared_ptr<Geometry> getGeometry() const override;
///
/// \brief Initialize textures
///
void initializeTextures(TextureManager<VTKTextureDelegate>& textureManager);
protected:
std::shared_ptr<SurfaceMesh> m_geometry; ///> Geometry to render
vtkSmartPointer<vtkDoubleArray> m_mappedVertexArray; ///> Mapped array of vertices
};
}
......
......@@ -24,6 +24,7 @@
#include "imstkScene.h"
#include "imstkCamera.h"
#include "imstkVTKRenderDelegate.h"
#include "imstkVTKSurfaceMeshRenderDelegate.h"
#include "vtkLightActor.h"
#include "vtkCameraActor.h"
......@@ -60,6 +61,16 @@ VTKRenderer::VTKRenderer(std::shared_ptr<Scene> scene)
m_objectVtkActors.push_back( delegate->getVtkActor() );
}
// Initialize textures for surface mesh render delegates
for ( const auto& renderDelegate : m_renderDelegates )
{
auto smRenderDelegate = std::dynamic_pointer_cast<VTKSurfaceMeshRenderDelegate>(renderDelegate);
if (smRenderDelegate)
{
smRenderDelegate->initializeTextures(m_textureManager);
}
}
// Lights and light actors
for ( const auto& light : scene->getLights() )
{
......
......@@ -26,6 +26,8 @@
#include <vector>
#include "imstkMath.h"
#include "imstkTextureManager.h"
#include "imstkVTKTextureDelegate.h"
#include "vtkSmartPointer.h"
#include "vtkRenderer.h"
......@@ -115,6 +117,8 @@ protected:
std::vector<std::shared_ptr<VTKRenderDelegate>> m_renderDelegates;
Mode m_currentMode = Mode::EMPTY;
TextureManager<VTKTextureDelegate> m_textureManager;
};
}
......
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#include "imstkVTKTextureDelegate.h"
#include "g3log/g3log.hpp"
namespace imstk
{
vtkSmartPointer<vtkTexture>
VTKTextureDelegate::getTexture() const
{
return m_sourceTexture;
}
void
VTKTextureDelegate::loadTexture(std::shared_ptr<Texture> texture)
{
std::string tFileName = texture->getPath();
auto readerFactory = vtkSmartPointer<vtkImageReader2Factory>::New();
// Read texture image
auto imgReader = readerFactory->CreateImageReader2(texture->getPath().c_str());
if (!imgReader)
{
LOG(WARNING) << "VTKTextureDelegate::loadTexture error: could not find reader for "
<< tFileName;
return;
}
imgReader->SetFileName(tFileName.c_str());
imgReader->Update();
m_sourceTexture = vtkSmartPointer<vtkTexture>::New();
m_sourceTexture->SetInputConnection(imgReader->GetOutputPort());
m_sourceTexture->SetBlendingMode(vtkTexture::VTK_TEXTURE_BLENDING_MODE_ADD);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment