Commit e959b41b authored by Nicholas Milef's avatar Nicholas Milef

ENH: Added texture manager and render material class

Co-Authored-by: Alexis Girault's avatarAlexis Girault <alexis.girault@kitware.com>
parent a177c405
......@@ -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)