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