Overte C++ Documentation
RenderableModelEntityItem.h
1 //
2 // RenderableModelEntityItem.h
3 // interface/src/entities
4 //
5 // Created by Brad Hefta-Gaub on 8/6/14.
6 // Copyright 2014 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_RenderableModelEntityItem_h
13 #define hifi_RenderableModelEntityItem_h
14 
15 #include <ModelEntityItem.h>
16 
17 #include <QString>
18 #include <QStringList>
19 
20 #include <ModelEntityItem.h>
21 #include <AnimationCache.h>
22 #include <Model.h>
23 #include <model-networking/ModelCache.h>
24 #include <MetaModelPayload.h>
25 
26 #include "RenderableEntityItem.h"
27 
28 class Model;
29 class EntityTreeRenderer;
30 
31 namespace render { namespace entities {
32 class ModelEntityRenderer;
33 } }
34 
35 class ModelEntityWrapper : public ModelEntityItem {
36  using Parent = ModelEntityItem;
37  friend class render::entities::ModelEntityRenderer;
38 
39 protected:
40  ModelEntityWrapper(const EntityItemID& entityItemID) : Parent(entityItemID) {}
41  void setModel(const ModelPointer& model);
42  ModelPointer getModel() const;
43 
44  bool _needsInitialSimulation { true };
45 private:
46  ModelPointer _model;
47 };
48 
49 class RenderableModelEntityItem : public ModelEntityWrapper {
50  Q_OBJECT
51 
52  friend class render::entities::ModelEntityRenderer;
53  using Parent = ModelEntityWrapper;
54 public:
55  static EntityItemPointer factory(const EntityItemID& entityID, const EntityItemProperties& properties);
56 
57  RenderableModelEntityItem(const EntityItemID& entityItemID, bool dimensionsInitialized);
58  virtual ~RenderableModelEntityItem();
59 
60  virtual void setUnscaledDimensions(const glm::vec3& value) override;
61 
62  virtual EntityItemProperties getProperties(const EntityPropertyFlags& desiredProperties, bool allowEmptyDesiredProperties) const override;
63  void updateModelBounds();
64 
65  glm::vec3 getPivot() const override;
66  virtual bool supportsDetailedIntersection() const override;
67  virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
68  const glm::vec3& viewFrustumPos, OctreeElementPointer& element, float& distance,
69  BoxFace& face, glm::vec3& surfaceNormal,
70  QVariantMap& extraInfo, bool precisionPicking) const override;
71  virtual bool findDetailedParabolaIntersection(const glm::vec3& origin, const glm::vec3& velocity,
72  const glm::vec3& acceleration, const glm::vec3& viewFrustumPos, OctreeElementPointer& element,
73  float& parabolicDistance, BoxFace& face, glm::vec3& surfaceNormal,
74  QVariantMap& extraInfo, bool precisionPicking) const override;
75 
76  virtual void setShapeType(ShapeType type) override;
77  virtual void setCompoundShapeURL(const QString& url) override;
78  virtual void setModelURL(const QString& url) override;
79 
80  virtual bool isReadyToComputeShape() const override;
81  virtual void computeShapeInfo(ShapeInfo& shapeInfo) override;
82  bool unableToLoadCollisionShape();
83 
84  virtual bool contains(const glm::vec3& point) const override;
85  void stopModelOverrideIfNoParent();
86 
87  virtual bool shouldBePhysical() const override;
88  void simulateRelayedJoints();
89  bool getJointMapCompleted();
90  void setJointMap(std::vector<int> jointMap);
91  int avatarJointIndex(int modelJointIndex);
92  void setOverrideTransform(const Transform& transform, const glm::vec3& offset);
93 
94  // these are in the frame of this object (model space)
95  virtual glm::quat getAbsoluteJointRotationInObjectFrame(int index) const override;
96  virtual glm::vec3 getAbsoluteJointTranslationInObjectFrame(int index) const override;
97  virtual int getJointParent(int index) const override;
98  virtual bool setAbsoluteJointRotationInObjectFrame(int index, const glm::quat& rotation) override;
99  virtual bool setAbsoluteJointTranslationInObjectFrame(int index, const glm::vec3& translation) override;
100 
101  virtual glm::quat getLocalJointRotation(int index) const override;
102  virtual glm::vec3 getLocalJointTranslation(int index) const override;
103  virtual bool setLocalJointRotation(int index, const glm::quat& rotation) override;
104  virtual bool setLocalJointTranslation(int index, const glm::vec3& translation) override;
105 
106  virtual void setJointRotations(const QVector<glm::quat>& rotations) override;
107  virtual void setJointRotationsSet(const QVector<bool>& rotationsSet) override;
108  virtual void setJointTranslations(const QVector<glm::vec3>& translations) override;
109  virtual void setJointTranslationsSet(const QVector<bool>& translationsSet) override;
110 
111  virtual void locationChanged(bool tellPhysics = true, bool tellChildren = true) override;
112 
113  virtual int getJointIndex(const QString& name) const override;
114  virtual QStringList getJointNames() const override;
115 
116 private:
117  bool needsUpdateModelBounds() const;
118  void autoResizeJointArrays();
119  void copyAnimationJointDataToModel();
120  bool readyToAnimate() const;
121  void fetchCollisionGeometryResource();
122 
123  QString getCollisionShapeURL() const;
124 
125  GeometryResource::Pointer _collisionGeometryResource;
126  std::vector<int> _jointMap;
127  QVariantMap _originalTextures;
128  bool _jointMapCompleted { false };
129  bool _originalTexturesRead { false };
130  bool _dimensionsInitialized { true };
131  bool _needsJointSimulation { false };
132  bool _needsToRescaleModel { false };
133 };
134 
135 namespace render { namespace entities {
136 
137 class ModelEntityRenderer : public TypedEntityRenderer<RenderableModelEntityItem>, public MetaModelPayload {
138  using Parent = TypedEntityRenderer<RenderableModelEntityItem>;
139  friend class EntityRenderer;
140  Q_OBJECT
141 
142 public:
143  ModelEntityRenderer(const EntityItemPointer& entity);
144  virtual scriptable::ScriptableModelBase getScriptableModel() override;
145  virtual bool canReplaceModelMeshPart(int meshIndex, int partIndex) override;
146  virtual bool replaceScriptableModelMeshPart(scriptable::ScriptableModelBasePointer model, int meshIndex, int partIndex) override;
147 
148  void addMaterial(graphics::MaterialLayer material, const std::string& parentMaterialName) override;
149  void removeMaterial(graphics::MaterialPointer material, const std::string& parentMaterialName) override;
150 
151  void fade(render::Transaction& transaction, TransitionType type) override;
152 
153 protected:
154  virtual void removeFromScene(const ScenePointer& scene, Transaction& transaction) override;
155  virtual void onRemoveFromSceneTyped(const TypedEntityPointer& entity) override;
156 
157  void setKey(bool didVisualGeometryRequestSucceed, const ModelPointer& model);
158  virtual ItemKey getKey() override;
159  virtual uint32_t metaFetchMetaSubItems(ItemIDs& subItems) const override;
160  virtual void handleBlendedVertices(int blendshapeNumber, const QVector<BlendshapeOffset>& blendshapeOffsets,
161  const QVector<int>& blendedMeshSizes, const render::ItemIDs& subItemIDs) override;
162 
163  virtual bool needsRenderUpdateFromTypedEntity(const TypedEntityPointer& entity) const override;
164  virtual void doRenderUpdateSynchronousTyped(const ScenePointer& scene, Transaction& transaction, const TypedEntityPointer& entity) override;
165  virtual void doRenderUpdateAsynchronousTyped(const TypedEntityPointer& entity) override;
166  virtual void doRender(RenderArgs* args) override;
167 
168  void setIsVisibleInSecondaryCamera(bool value) override;
169  void setRenderLayer(RenderLayer value) override;
170  void setCullWithParent(bool value) override;
171  void setMirrorMode(MirrorMode value) override;
172  void setPortalExitID(const QUuid& value) override;
173 
174 private:
175  void animate(const TypedEntityPointer& entity, const ModelPointer& model);
176  void updateJointData(const QVector<glm::vec3>& translations, const QVector<glm::quat>& rotations, const TypedEntityPointer& entity, const ModelPointer& model);
177  void mapJoints(const TypedEntityPointer& entity, const ModelPointer& model);
178 
179  // Transparency is handled in ModelMeshPartPayload
180  virtual bool isTransparent() const override { return false; }
181 
182  bool _hasModel { false };
183  ModelPointer _model;
184  QString _textures;
185  bool _texturesLoaded { false };
186  int _lastKnownCurrentIntegerFrame { -1 };
187 
188  QUrl _parsedModelURL;
189  bool _jointMappingCompleted { false };
190  QVector<int> _jointMapping; // domain is index into model-joints, range is index into animation-joints
191  AnimationPointer _animation;
192  QString _animationURL;
193  uint64_t _lastAnimated { 0 };
194 
195  render::ItemKey _itemKey { render::ItemKey::Builder().withTypeMeta() };
196 
197  bool _didLastVisualGeometryRequestSucceed { true };
198 
199  void processMaterials();
200  bool _allProceduralMaterialsLoaded { false };
201 
202  static void metaBlendshapeOperator(render::ItemID renderItemID, int blendshapeNumber, const QVector<BlendshapeOffset>& blendshapeOffsets,
203  const QVector<int>& blendedMeshSizes, const render::ItemIDs& subItemIDs);
204 };
205 
206 } } // namespace
207 
208 #endif // hifi_RenderableModelEntityItem_h
Abstract ID for editing model items. Used in EntityItem JS API.
Definition: EntityItemID.h:28
A generic 3D model displaying geometry loaded from a URL.
Definition: Model.h:85