Skip to content

Commit a27c7a1

Browse files
luca-della-vedovaOnur Berk Töremjcarrollchapulina
authored
Use assimp for loading meshes (#393)
Signed-off-by: Luca Della Vedova <[email protected]> Signed-off by: Onur Berk Tore [email protected] Signed-off-by: Louise Poubel <[email protected]> Signed-off-by: Michael Carroll <[email protected]> Co-authored-by: Onur Berk Töre <[email protected]> Co-authored-by: Michael Carroll <[email protected]> Co-authored-by: Louise Poubel <[email protected]>
1 parent 3224324 commit a27c7a1

18 files changed

+1749
-44
lines changed

.github/ci/packages.apt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
libassimp-dev
12
libavcodec-dev
23
libavdevice-dev
34
libavformat-dev

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ gz_find_package(AVCODEC REQUIRED_BY av PRETTY libavcodec)
122122
# Find avutil
123123
gz_find_package(AVUTIL REQUIRED_BY av PRETTY libavutil)
124124

125+
#------------------------------------
126+
# Find assimp
127+
gz_find_package(GzAssimp REQUIRED_BY graphics PRETTY assimp)
128+
125129

126130
message(STATUS "-------------------------------------------\n")
127131

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C) 2022 Open Source Robotics Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
#ifndef GZ_COMMON_ASSIMPLOADER_HH_
18+
#define GZ_COMMON_ASSIMPLOADER_HH_
19+
20+
#include <string>
21+
#include <gz/common/graphics/Export.hh>
22+
#include <gz/common/MeshLoader.hh>
23+
24+
#include <gz/utils/ImplPtr.hh>
25+
26+
namespace gz
27+
{
28+
namespace common
29+
{
30+
/// \class AssimpLoader AssimpLoader.hh gz/common/AssimpLoader.hh
31+
/// \brief Class used to load mesh files using the assimp lodaer
32+
class GZ_COMMON_GRAPHICS_VISIBLE AssimpLoader : public MeshLoader
33+
{
34+
/// \brief Constructor
35+
public: AssimpLoader();
36+
37+
/// \brief Destructor
38+
public: virtual ~AssimpLoader();
39+
40+
/// \brief Load a mesh
41+
/// \param[in] _filename Mesh file to load
42+
/// \return Pointer to a new Mesh
43+
public: virtual Mesh *Load(const std::string &_filename) override;
44+
45+
/// \internal
46+
/// \brief Pointer to private data.
47+
GZ_UTILS_UNIQUE_IMPL_PTR(dataPtr)
48+
};
49+
}
50+
}
51+
#endif

graphics/include/gz/common/Material.hh

+10-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <gz/math/Color.hh>
2525
#include <gz/common/graphics/Export.hh>
2626
#include <gz/common/EnumIface.hh>
27+
#include <gz/common/Image.hh>
2728
#include <gz/common/Pbr.hh>
2829

2930
#include <gz/utils/ImplPtr.hh>
@@ -102,15 +103,22 @@ namespace gz
102103

103104
/// \brief Set a texture image
104105
/// \param[in] _tex The name of the texture, which must be in the
105-
/// resource path
106-
public: void SetTextureImage(const std::string &_tex);
106+
/// resource path or its name if _img is provided
107+
/// \param[in] _img The image containing the texture if image has been
108+
/// loaded in memory
109+
public: void SetTextureImage(const std::string &_tex,
110+
const std::shared_ptr<const Image> &_img = nullptr);
107111

108112
/// \brief Set a texture image
109113
/// \param[in] _tex The name of the texture
110114
/// \param[in] _resourcePath Path which contains _tex
111115
public: void SetTextureImage(const std::string &_tex,
112116
const std::string &_resourcePath);
113117

118+
/// \brief Gets the texture image, if the texture was loaded from memory
119+
/// \return A pointer to the image that was loaded from memory
120+
public: std::shared_ptr<const Image> TextureData() const;
121+
114122
/// \brief Get a texture image
115123
/// \return The name of the texture image (if one exists) or an empty
116124
/// string

graphics/include/gz/common/MeshManager.hh

+10-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ namespace gz
4545
class SubMesh;
4646

4747
/// \class MeshManager MeshManager.hh gz/common/MeshManager.hh
48-
/// \brief Maintains and manages all meshes
48+
/// \brief Maintains and manages all meshes. Supported mesh formats are
49+
/// STL (STLA, STLB), COLLADA, OBJ, GLTF (GLB) and FBX. By default only GLTF
50+
/// and FBX are loaded using assimp loader, however if GZ_MESH_FORCE_ASSIMP
51+
/// environment variable is set, then MeshManager will use assimp loader for
52+
/// all supported mesh formats.
4953
class GZ_COMMON_GRAPHICS_VISIBLE MeshManager
5054
: public SingletonT<MeshManager>
5155
{
@@ -240,8 +244,12 @@ namespace gz
240244
const gz::math::Vector2d &_segments,
241245
const gz::math::Vector2d &_uvTile);
242246

247+
/// \brief Sets the forceAssimp flag by reading the GZ_MESH_FORCE_ASSIMP
248+
/// environment variable. If forceAssimp true, MeshManager uses Assimp
249+
/// for loading all mesh formats, otherwise only for GLTF and FBX.
250+
public: void SetAssimpEnvs();
251+
243252
/// \brief Tesselate a 2D mesh
244-
///
245253
/// Makes a zigzag pattern compatible with strips
246254
/// \param[in] _sm the mesh to tesselate
247255
/// \param[in] _meshWith mesh width

graphics/include/gz/common/Pbr.hh

+47-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
#ifndef GZ_COMMON_PBR_HH_
1818
#define GZ_COMMON_PBR_HH_
1919

20+
#include <memory>
2021
#include <string>
2122

2223
#include <gz/utils/ImplPtr.hh>
2324

2425
#include <gz/common/graphics/Export.hh>
26+
#include <gz/common/Image.hh>
2527

2628
namespace gz
2729
{
@@ -84,12 +86,20 @@ namespace common
8486
/// has not been specified.
8587
public: std::string NormalMap() const;
8688

89+
/// \brief Gets the normal map data,
90+
/// if the texture was loaded from memory, otherwise a nullptr
91+
/// \return A pointer to the image that was loaded from memory
92+
public: std::shared_ptr<const Image> NormalMapData() const;
93+
8794
/// \brief Set the normal map filename.
8895
/// \param[in] _map Filename of the normal map.
8996
/// \param[in] _space Space that the normal map is defined in.
9097
/// Defaults to tangent space.
98+
/// \param[in] _img The image containing the texture if image has been
99+
/// loaded in memory
91100
public: void SetNormalMap(const std::string &_map,
92-
NormalMapSpace _space = NormalMapSpace::TANGENT);
101+
NormalMapSpace _space = NormalMapSpace::TANGENT,
102+
const std::shared_ptr<const Image> &_img = nullptr);
93103

94104
/// \brief Get the normal map type, either tangent or object space
95105
/// \return Space that the normal map is defined in
@@ -123,7 +133,15 @@ namespace common
123133

124134
/// \brief Set the roughness map filename for metal workflow.
125135
/// \param[in] _map Filename of the roughness map.
126-
public: void SetRoughnessMap(const std::string &_map);
136+
/// \param[in] _img The image containing the texture if image has been
137+
/// loaded in memory
138+
public: void SetRoughnessMap(const std::string &_map,
139+
const std::shared_ptr<const Image> &_img = nullptr);
140+
141+
/// \brief Gets the roughness map data,
142+
/// if the texture was loaded from memory, otherwise a nullptr
143+
/// \return A pointer to the image that was loaded from memory
144+
public: std::shared_ptr<const Image> RoughnessMapData() const;
127145

128146
/// \brief Get the metalness map filename for metal workflow. This will be
129147
/// an empty string if a metalness map has not been set.
@@ -133,7 +151,15 @@ namespace common
133151

134152
/// \brief Set the metalness map filename for metal workflow.
135153
/// \param[in] _map Filename of the metalness map.
136-
public: void SetMetalnessMap(const std::string &_map);
154+
/// \param[in] _img The image containing the texture if image has been
155+
/// loaded in memory
156+
public: void SetMetalnessMap(const std::string &_map,
157+
const std::shared_ptr<const Image> &_img = nullptr);
158+
159+
/// \brief Gets the metalness map data,
160+
/// if the texture was loaded from memory, otherwise a nullptr
161+
/// \return A pointer to the image that was loaded from memory
162+
public: std::shared_ptr<const Image> MetalnessMapData() const;
137163

138164
/// \brief Get the emissive map filename. This will be an empty string
139165
/// if an emissive map has not been set.
@@ -143,18 +169,34 @@ namespace common
143169

144170
/// \brief Set the emissive map filename.
145171
/// \param[in] _map Filename of the emissive map.
146-
public: void SetEmissiveMap(const std::string &_map);
172+
/// \param[in] _img The image containing the texture if image has been
173+
/// loaded in memory
174+
public: void SetEmissiveMap(const std::string &_map,
175+
const std::shared_ptr<const Image> &_img = nullptr);
176+
177+
/// \brief Gets the emissive map data,
178+
/// if the texture was loaded from memory, otherwise a nullptr
179+
/// \return A pointer to the image that was loaded from memory
180+
public: std::shared_ptr<const Image> EmissiveMapData() const;
147181

148182
/// \brief Get the light map filename. This will be an empty string
149183
/// if an light map has not been set.
150184
/// \return Filename of the light map, or empty string if a light
151185
/// map has not been specified.
152186
public: std::string LightMap() const;
153187

188+
/// \brief Gets the light map data,
189+
/// if the texture was loaded from memory, otherwise a nullptr
190+
/// \return A pointer to the image that was loaded from memory
191+
public: std::shared_ptr<const Image> LightMapData() const;
192+
154193
/// \brief Set the light map filename.
155194
/// \param[in] _map Filename of the light map.
156195
/// \param[in] _uvSet Index of the texture coordinate set
157-
public: void SetLightMap(const std::string &_map, unsigned int _uvSet = 0u);
196+
/// \param[in] _img The image containing the texture if image has been
197+
/// loaded in memory
198+
public: void SetLightMap(const std::string &_map, unsigned int _uvSet = 0u,
199+
const std::shared_ptr<const Image> &_img = nullptr);
158200

159201
/// \brief Get the light map texture coordinate set.
160202
/// \return Index of the light map texture coordinate set

0 commit comments

Comments
 (0)