Overte C++ Documentation
PolyVoxEntityItem.h
1 //
2 // PolyVoxEntityItem.h
3 // libraries/entities/src
4 //
5 // Created by Seth Alves on 5/11/15.
6 // Copyright 2015 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_PolyVoxEntityItem_h
13 #define hifi_PolyVoxEntityItem_h
14 
15 #include "EntityItem.h"
16 
17 class PolyVoxEntityItem : public EntityItem {
18  public:
19  static EntityItemPointer factory(const EntityItemID& entityID, const EntityItemProperties& properties);
20 
21  PolyVoxEntityItem(const EntityItemID& entityItemID);
22 
23  ALLOW_INSTANTIATION // This class can be instantiated
24 
25  // methods for getting/setting all properties of an entity
26  virtual EntityItemProperties getProperties(const EntityPropertyFlags& desiredProperties, bool allowEmptyDesiredProperties) const override;
27  virtual bool setSubClassProperties(const EntityItemProperties& properties) override;
28 
29  virtual EntityPropertyFlags getEntityProperties(EncodeBitstreamParams& params) const override;
30 
31  virtual void appendSubclassData(OctreePacketData* packetData, EncodeBitstreamParams& params,
32  EntityTreeElementExtraEncodeDataPointer modelTreeElementExtraEncodeData,
33  EntityPropertyFlags& requestedProperties,
34  EntityPropertyFlags& propertyFlags,
35  EntityPropertyFlags& propertiesDidntFit,
36  int& propertyCount,
37  OctreeElement::AppendState& appendState) const override;
38 
39  virtual int readEntitySubclassDataFromBuffer(const unsigned char* data, int bytesLeftToRead,
40  ReadBitstreamToTreeParams& args,
41  EntityPropertyFlags& propertyFlags, bool overwriteLocalData,
42  bool& somethingChanged) override;
43 
44  // never have a ray intersection pick a PolyVoxEntityItem.
45  virtual bool supportsDetailedIntersection() const override { return true; }
46  virtual bool findDetailedRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
47  const glm::vec3& viewFrustumPos, OctreeElementPointer& element, float& distance,
48  BoxFace& face, glm::vec3& surfaceNormal,
49  QVariantMap& extraInfo, bool precisionPicking) const override { return false; }
50  virtual bool findDetailedParabolaIntersection(const glm::vec3& origin, const glm::vec3& velocity,
51  const glm::vec3& acceleration, const glm::vec3& viewFrustumPos, OctreeElementPointer& element,
52  float& parabolicDistance, BoxFace& face, glm::vec3& surfaceNormal,
53  QVariantMap& extraInfo, bool precisionPicking) const override { return false; }
54 
55  virtual void debugDump() const override;
56 
57  virtual void setVoxelVolumeSize(const glm::vec3& voxelVolumeSize);
58  virtual glm::vec3 getVoxelVolumeSize() const;
59 
60  virtual void setVoxelData(const QByteArray& voxelData);
61  virtual QByteArray getVoxelData() const;
62 
63  virtual int getOnCount() const { return 0; }
64 
65  /*@jsdoc
66  * <p>The surface of a {@link Entities.EntityProperties-PolyVox|PolyVox} entity may be one of the following styles:</p>
67  * <table>
68  * <thead>
69  * <tr><th>Value</th><th>Type</th><th>Description</th></tr>
70  * </thead>
71  * <tbody>
72  * <tr><td><code>0</code></td><td>Marching cubes.</td><td>Chamfered edges. Open volume.
73  * Joins neighboring PolyVox entities reasonably well.</td></tr>
74  * <tr><td><code>1</code></td><td>Cubic.</td><td>Square edges. Open volume.
75  * Joins neighboring PolyVox entities cleanly.</td></tr>
76  * <tr><td><code>2</code></td><td>Edged cubic.</td><td>Square edges. Enclosed volume.
77  * Joins neighboring PolyVox entities cleanly.</td></tr>
78  * <tr><td><code>3</code></td><td>Edged marching cubes.</td><td>Chamfered edges. Enclosed volume.
79  * Doesn't join neighboring PolyVox entities.</td></tr>
80  * </tbody>
81  * </table>
82  * @typedef {number} Entities.PolyVoxSurfaceStyle
83  */
84  enum PolyVoxSurfaceStyle {
85  SURFACE_MARCHING_CUBES,
86  SURFACE_CUBIC,
87  SURFACE_EDGED_CUBIC,
88  SURFACE_EDGED_MARCHING_CUBES
89  };
90  static bool isEdged(PolyVoxSurfaceStyle surfaceStyle);
91 
92 
93  virtual void setVoxelSurfaceStyle(PolyVoxSurfaceStyle voxelSurfaceStyle) { _voxelSurfaceStyle = voxelSurfaceStyle; }
94  // this other version of setVoxelSurfaceStyle is needed for SET_ENTITY_PROPERTY_FROM_PROPERTIES
95  void setVoxelSurfaceStyle(uint16_t voxelSurfaceStyle) { setVoxelSurfaceStyle((PolyVoxSurfaceStyle) voxelSurfaceStyle); }
96  virtual PolyVoxSurfaceStyle getVoxelSurfaceStyle() const { return _voxelSurfaceStyle; }
97 
98  static const glm::vec3 DEFAULT_VOXEL_VOLUME_SIZE;
99  static const float MAX_VOXEL_DIMENSION;
100 
101  static const QByteArray DEFAULT_VOXEL_DATA;
102  static const PolyVoxSurfaceStyle DEFAULT_VOXEL_SURFACE_STYLE;
103 
104  glm::vec3 voxelCoordsToWorldCoords(const glm::vec3& voxelCoords) const;
105  glm::vec3 worldCoordsToVoxelCoords(const glm::vec3& worldCoords) const;
106  glm::vec3 voxelCoordsToLocalCoords(const glm::vec3& voxelCoords) const;
107  glm::vec3 localCoordsToVoxelCoords(const glm::vec3& localCoords) const;
108 
109  // coords are in voxel-volume space
110  virtual bool setSphereInVolume(const vec3& center, float radius, uint8_t toValue) { return false; }
111  virtual bool setVoxelInVolume(const vec3& position, uint8_t toValue) { return false; }
112  // coords are in world-space
113  virtual bool setSphere(const vec3& center, float radius, uint8_t toValue) { return false; }
114  virtual bool setCapsule(const vec3& startWorldCoords, const vec3& endWorldCoords,
115  float radiusWorldCoords, uint8_t toValue) { return false; }
116  virtual bool setAll(uint8_t toValue) { return false; }
117  virtual bool setCuboid(const vec3& lowPosition, const vec3& cuboidSize, int value) { return false; }
118 
119  virtual uint8_t getVoxel(int x, int y, int z) const final { return getVoxel({ x, y, z }); }
120  virtual bool setVoxel(int x, int y, int z, uint8_t toValue) final{ return setVoxel({ x, y, z }, toValue); }
121 
122  virtual uint8_t getVoxel(const ivec3& v) const { return 0; }
123  virtual bool setVoxel(const ivec3& v, uint8_t toValue) { return false; }
124 
125  static QByteArray makeEmptyVoxelData(quint16 voxelXSize = 16, quint16 voxelYSize = 16, quint16 voxelZSize = 16);
126 
127  static const QString DEFAULT_X_TEXTURE_URL;
128  void setXTextureURL(const QString& xTextureURL);
129  QString getXTextureURL() const;
130 
131  static const QString DEFAULT_Y_TEXTURE_URL;
132  void setYTextureURL(const QString& yTextureURL);
133  QString getYTextureURL() const;
134 
135  static const QString DEFAULT_Z_TEXTURE_URL;
136  void setZTextureURL(const QString& zTextureURL);
137  QString getZTextureURL() const;
138 
139  virtual void setXNNeighborID(const EntityItemID& xNNeighborID);
140  void setXNNeighborID(const QString& xNNeighborID);
141  EntityItemID getXNNeighborID() const;
142  virtual void setYNNeighborID(const EntityItemID& yNNeighborID);
143  void setYNNeighborID(const QString& yNNeighborID);
144  EntityItemID getYNNeighborID() const;
145  virtual void setZNNeighborID(const EntityItemID& zNNeighborID);
146  void setZNNeighborID(const QString& zNNeighborID);
147  EntityItemID getZNNeighborID() const;
148 
149  std::array<EntityItemID, 3> getNNeigborIDs() const;
150 
151 
152  virtual void setXPNeighborID(const EntityItemID& xPNeighborID);
153  void setXPNeighborID(const QString& xPNeighborID);
154  EntityItemID getXPNeighborID() const;
155  virtual void setYPNeighborID(const EntityItemID& yPNeighborID);
156  void setYPNeighborID(const QString& yPNeighborID);
157  EntityItemID getYPNeighborID() const;
158  virtual void setZPNeighborID(const EntityItemID& zPNeighborID);
159  void setZPNeighborID(const QString& zPNeighborID);
160  EntityItemID getZPNeighborID() const;
161 
162  std::array<EntityItemID, 3> getPNeigborIDs() const;
163 
164  glm::vec3 getSurfacePositionAdjustment() const;
165 
166  virtual ShapeType getShapeType() const override;
167 
168  bool isEdged() const;
169 
170  glm::mat4 voxelToWorldMatrix(bool includeBillboard = false) const;
171  glm::mat4 worldToVoxelMatrix(bool includeBillboard = false) const;
172  glm::mat4 voxelToLocalMatrix() const;
173  glm::mat4 localToVoxelMatrix() const;
174 
175  protected:
176  void setVoxelDataDirty(bool value) { withWriteLock([&] { _voxelDataDirty = value; }); }
177 
178  glm::vec3 _voxelVolumeSize { DEFAULT_VOXEL_VOLUME_SIZE }; // this is always 3 bytes
179 
180  QByteArray _voxelData { DEFAULT_VOXEL_DATA };
181  bool _voxelDataDirty { true }; // _voxelData has changed, things that depend on it should be updated
182 
183  PolyVoxSurfaceStyle _voxelSurfaceStyle { DEFAULT_VOXEL_SURFACE_STYLE };
184 
185  QString _xTextureURL { DEFAULT_X_TEXTURE_URL };
186  QString _yTextureURL { DEFAULT_Y_TEXTURE_URL };
187  QString _zTextureURL { DEFAULT_Z_TEXTURE_URL };
188 
189  // for non-edged surface styles, these are used to compute the high-axis edges
190  EntityItemID _xNNeighborID{UNKNOWN_ENTITY_ID};
191  EntityItemID _yNNeighborID{UNKNOWN_ENTITY_ID};
192  EntityItemID _zNNeighborID{UNKNOWN_ENTITY_ID};
193 
194  EntityItemID _xPNeighborID{UNKNOWN_ENTITY_ID};
195  EntityItemID _yPNeighborID{UNKNOWN_ENTITY_ID};
196  EntityItemID _zPNeighborID{UNKNOWN_ENTITY_ID};
197 };
198 
199 #endif // hifi_PolyVoxEntityItem_h
Definition: EntityItem.h:82
virtual ShapeType getShapeType() const
return preferred shape type (actual physical shape may differ)
Definition: EntityItem.h:390
Abstract ID for editing model items. Used in EntityItem JS API.
Definition: EntityItemID.h:28
Definition: EntityItemProperties.h:106
Handles packing of the data portion of PacketType_OCTREE_DATA messages.
Definition: OctreePacketData.h:93