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  bool shouldWaitForWearables() const { return _waitForWearables; }
62  const QVariantHash& getMapping() const { return _mapping; }
63 
64 protected:
65  // Shared across all geometries, constant throughout lifetime
66  HFMModel::ConstPointer _hfmModel;
67  MaterialMapping _materialMapping;
68  std::shared_ptr<const GeometryMeshes> _meshes;
69  std::shared_ptr<const GeometryMeshParts> _meshParts;
70 
71  // Copied to each geometry, mutable throughout lifetime via setTextures
72  NetworkMaterials _materials;
73 
74  QUrl _animGraphOverrideUrl;
75  QVariantHash _mapping; // parsed contents of FST file.
76  bool _waitForWearables { false };
77 
78 private:
79  mutable bool _areTexturesLoaded { false };
80 };
81 
83 class GeometryResource : public Resource, public Geometry {
84  Q_OBJECT
85 public:
86  using Pointer = QSharedPointer<GeometryResource>;
87 
88  GeometryResource(const QUrl& url, const ModelLoader& modelLoader) : Resource(url), _modelLoader(modelLoader) {}
89  GeometryResource(const GeometryResource& other);
90 
91  QString getType() const override { return "Geometry"; }
92 
93  virtual void deleter() override;
94 
95  virtual void downloadFinished(const QByteArray& data) override;
96  void setExtra(void* extra) override;
97 
98  virtual bool areTexturesLoaded() const override { return isLoaded() && Geometry::areTexturesLoaded(); }
99 
100 private slots:
101  void onGeometryMappingLoaded(bool success);
102 
103 protected:
104  friend class ModelCache;
105 
106  Q_INVOKABLE void setGeometryDefinition(HFMModel::Pointer hfmModel, const MaterialMapping& materialMapping);
107 
108  // Geometries may not hold onto textures while cached - that is for the texture cache
109  // Instead, these methods clear and reset textures from the geometry when caching/loading
110  bool shouldSetTextures() const { return _hfmModel && _materials.empty(); }
111  void setTextures();
112  void resetTextures();
113 
114  virtual bool isCacheable() const override { return _loaded && _isCacheable; }
115 
116 private:
117  ModelLoader _modelLoader;
118  GeometryMappingPair _mappingPair;
119  QUrl _textureBaseURL;
120  bool _combineParts;
121 
122  GeometryResource::Pointer _geometryResource;
123  QMetaObject::Connection _connection;
124 
125  bool _isCacheable{ true };
126 };
127 
128 class GeometryResourceWatcher : public QObject {
129  Q_OBJECT
130 public:
131  using Pointer = std::shared_ptr<GeometryResourceWatcher>;
132 
133  GeometryResourceWatcher() = delete;
134  GeometryResourceWatcher(Geometry::Pointer& geometryPtr) : _geometryRef(geometryPtr) {}
135 
136  void setResource(GeometryResource::Pointer resource);
137 
138  QUrl getURL() const { return (bool)_resource ? _resource->getURL() : QUrl(); }
139  int getResourceDownloadAttempts() { return _resource ? _resource->getDownloadAttempts() : 0; }
140  int getResourceDownloadAttemptsRemaining() { return _resource ? _resource->getDownloadAttemptsRemaining() : 0; }
141 
142 private:
143  void startWatching();
144  void stopWatching();
145 
146 signals:
147  void finished(bool success);
148 
149 private slots:
150  void resourceFinished(bool success);
151  void resourceRefreshed();
152 
153 private:
154  GeometryResource::Pointer _resource;
155  Geometry::Pointer& _geometryRef;
156 };
157 
159 class ModelCache : public ResourceCache, public Dependency {
160  Q_OBJECT
161  SINGLETON_DEPENDENCY
162 
163 public:
164 
165  GeometryResource::Pointer getGeometryResource(const QUrl& url,
166  const GeometryMappingPair& mapping =
167  GeometryMappingPair(QUrl(), QVariantHash()),
168  const QUrl& textureBaseUrl = QUrl());
169 
170  GeometryResource::Pointer getCollisionGeometryResource(const QUrl& url,
171  const GeometryMappingPair& mapping =
172  GeometryMappingPair(QUrl(), QVariantHash()),
173  const QUrl& textureBaseUrl = QUrl());
174 
175 protected:
176  friend class GeometryResource;
177 
178  virtual QSharedPointer<Resource> createResource(const QUrl& url) override;
179  QSharedPointer<Resource> createResourceCopy(const QSharedPointer<Resource>& resource) override;
180 
181 private:
182  ModelCache();
183  virtual ~ModelCache() = default;
184  ModelLoader _modelLoader;
185 };
186 
187 class MeshPart {
188 public:
189  MeshPart(int mesh, int part, int material) : meshID { mesh }, partID { part }, materialID { material } {}
190  int meshID { -1 };
191  int partID { -1 };
192  int materialID { -1 };
193 };
194 
195 #endif // hifi_ModelCache_h
A geometry loaded from the network.
Definition: ModelCache.h:83
virtual bool isCacheable() const override
Checks whether the resource is cacheable.
Definition: ModelCache.h:114
virtual void downloadFinished(const QByteArray &data) override
Definition: ModelCache.cpp:220
Stores cached model geometries.
Definition: ModelCache.h:159
virtual QSharedPointer< Resource > createResource(const QUrl &url) override
Creates a new resource.
Definition: ModelCache.cpp:392
Base class for resource caches.
Definition: ResourceCache.h:197
Base class for resources.
Definition: ResourceCache.h:415
virtual bool isLoaded() const
Checks whether the resource has loaded.
Definition: ResourceCache.h:439
The runtime model format.
Definition: HFM.h:305