Overte C++ Documentation
ModelCache.h
1 //
2 // ModelCache.h
3 // libraries/model-networking
4 //
5 // Created by Zach Pomerantz on 3/15/16.
6 // Copyright 2016 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_ModelCache_h
13 #define hifi_ModelCache_h
14 
15 #include <QtCore/QSharedPointer>
16 
17 #include <DependencyManager.h>
18 #include <ResourceCache.h>
19 
20 #include <graphics/Asset.h>
21 
22 #include "FBXSerializer.h"
23 #include <procedural/ProceduralMaterialCache.h>
24 #include <material-networking/TextureCache.h>
25 #include "ModelLoader.h"
26 
27 class MeshPart;
28 
29 using GeometryMappingPair = std::pair<QUrl, QVariantHash>;
30 Q_DECLARE_METATYPE(GeometryMappingPair)
31 
32 class Geometry {
33 public:
34  using Pointer = std::shared_ptr<Geometry>;
35  using WeakPointer = std::weak_ptr<Geometry>;
36 
37  Geometry() = default;
38  Geometry(const Geometry& geometry);
39  virtual ~Geometry() = default;
40 
41  // Immutable over lifetime
42  using GeometryMeshes = std::vector<std::shared_ptr<const graphics::Mesh>>;
43  using GeometryMeshParts = std::vector<std::shared_ptr<const MeshPart>>;
44 
45  // Mutable, but must retain structure of vector
46  using NetworkMaterials = std::vector<std::shared_ptr<NetworkMaterial>>;
47 
48  bool isHFMModelLoaded() const { return (bool)_hfmModel; }
49 
50  const HFMModel& getHFMModel() const { return *_hfmModel; }
51  const HFMModel::ConstPointer& getConstHFMModelPointer() const { return _hfmModel; }
52  const MaterialMapping& getMaterialMapping() const { return _materialMapping; }
53  const GeometryMeshes& getMeshes() const { return *_meshes; }
54  const std::shared_ptr<NetworkMaterial> getShapeMaterial(int shapeID) const;
55 
56  const QVariantMap getTextures() const;
57  void setTextures(const QVariantMap& textureMap);
58 
59  virtual bool areTexturesLoaded() const;
60  const QUrl& getAnimGraphOverrideUrl() const { return _animGraphOverrideUrl; }
61  const QVariantHash& getMapping() const { return _mapping; }
62 
63 protected:
64  // Shared across all geometries, constant throughout lifetime
65  HFMModel::ConstPointer _hfmModel;
66  MaterialMapping _materialMapping;
67  std::shared_ptr<const GeometryMeshes> _meshes;
68  std::shared_ptr<const GeometryMeshParts> _meshParts;
69 
70  // Copied to each geometry, mutable throughout lifetime via setTextures
71  NetworkMaterials _materials;
72 
73  QUrl _animGraphOverrideUrl;
74  QVariantHash _mapping; // parsed contents of FST file.
75 
76 private:
77  mutable bool _areTexturesLoaded { false };
78 };
79 
81 class GeometryResource : public Resource, public Geometry {
82  Q_OBJECT
83 public:
84  using Pointer = QSharedPointer<GeometryResource>;
85 
86  GeometryResource(const QUrl& url, const ModelLoader& modelLoader) : Resource(url), _modelLoader(modelLoader) {}
87  GeometryResource(const GeometryResource& other);
88 
89  QString getType() const override { return "Geometry"; }
90 
91  virtual void deleter() override;
92 
93  virtual void downloadFinished(const QByteArray& data) override;
94  void setExtra(void* extra) override;
95 
96  virtual bool areTexturesLoaded() const override { return isLoaded() && Geometry::areTexturesLoaded(); }
97 
98 private slots:
99  void onGeometryMappingLoaded(bool success);
100 
101 protected:
102  friend class ModelCache;
103 
104  Q_INVOKABLE void setGeometryDefinition(HFMModel::Pointer hfmModel, const MaterialMapping& materialMapping);
105 
106  // Geometries may not hold onto textures while cached - that is for the texture cache
107  // Instead, these methods clear and reset textures from the geometry when caching/loading
108  bool shouldSetTextures() const { return _hfmModel && _materials.empty(); }
109  void setTextures();
110  void resetTextures();
111 
112  virtual bool isCacheable() const override { return _loaded && _isCacheable; }
113 
114 private:
115  ModelLoader _modelLoader;
116  GeometryMappingPair _mappingPair;
117  QUrl _textureBaseURL;
118  bool _combineParts;
119 
120  GeometryResource::Pointer _geometryResource;
121  QMetaObject::Connection _connection;
122 
123  bool _isCacheable{ true };
124 };
125 
126 class GeometryResourceWatcher : public QObject {
127  Q_OBJECT
128 public:
129  using Pointer = std::shared_ptr<GeometryResourceWatcher>;
130 
131  GeometryResourceWatcher() = delete;
132  GeometryResourceWatcher(Geometry::Pointer& geometryPtr) : _geometryRef(geometryPtr) {}
133 
134  void setResource(GeometryResource::Pointer resource);
135 
136  QUrl getURL() const { return (bool)_resource ? _resource->getURL() : QUrl(); }
137  int getResourceDownloadAttempts() { return _resource ? _resource->getDownloadAttempts() : 0; }
138  int getResourceDownloadAttemptsRemaining() { return _resource ? _resource->getDownloadAttemptsRemaining() : 0; }
139 
140 private:
141  void startWatching();
142  void stopWatching();
143 
144 signals:
145  void finished(bool success);
146 
147 private slots:
148  void resourceFinished(bool success);
149  void resourceRefreshed();
150 
151 private:
152  GeometryResource::Pointer _resource;
153  Geometry::Pointer& _geometryRef;
154 };
155 
157 class ModelCache : public ResourceCache, public Dependency {
158  Q_OBJECT
159  SINGLETON_DEPENDENCY
160 
161 public:
162 
163  GeometryResource::Pointer getGeometryResource(const QUrl& url,
164  const GeometryMappingPair& mapping =
165  GeometryMappingPair(QUrl(), QVariantHash()),
166  const QUrl& textureBaseUrl = QUrl());
167 
168  GeometryResource::Pointer getCollisionGeometryResource(const QUrl& url,
169  const GeometryMappingPair& mapping =
170  GeometryMappingPair(QUrl(), QVariantHash()),
171  const QUrl& textureBaseUrl = QUrl());
172 
173 protected:
174  friend class GeometryResource;
175 
176  virtual QSharedPointer<Resource> createResource(const QUrl& url) override;
177  QSharedPointer<Resource> createResourceCopy(const QSharedPointer<Resource>& resource) override;
178 
179 private:
180  ModelCache();
181  virtual ~ModelCache() = default;
182  ModelLoader _modelLoader;
183 };
184 
185 class MeshPart {
186 public:
187  MeshPart(int mesh, int part, int material) : meshID { mesh }, partID { part }, materialID { material } {}
188  int meshID { -1 };
189  int partID { -1 };
190  int materialID { -1 };
191 };
192 
193 #endif // hifi_ModelCache_h
A geometry loaded from the network.
Definition: ModelCache.h:81
virtual bool isCacheable() const override
Checks whether the resource is cacheable.
Definition: ModelCache.h:112
virtual void downloadFinished(const QByteArray &data) override
Definition: ModelCache.cpp:220
Stores cached model geometries.
Definition: ModelCache.h:157
virtual QSharedPointer< Resource > createResource(const QUrl &url) override
Creates a new resource.
Definition: ModelCache.cpp:391
Base class for resource caches.
Definition: ResourceCache.h:196
Base class for resources.
Definition: ResourceCache.h:409
virtual bool isLoaded() const
Checks whether the resource has loaded.
Definition: ResourceCache.h:439
The runtime model format.
Definition: HFM.h:302