Overte C++ Documentation
ScriptableMesh.h
1 //
2 // Copyright 2018 High Fidelity, Inc.
3 // Copyright 2023 Overte e.V.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 // SPDX-License-Identifier: Apache-2.0
8 //
9 
10 #pragma once
11 
12 #include "ScriptableModel.h"
13 
14 #include <glm/glm.hpp>
15 #include <graphics/BufferViewHelpers.h>
16 #include <DependencyManager.h>
17 
18 #include <memory>
19 #include <QPointer>
20 #include <QtCore/QList>
21 #include <QtCore/QObject>
22 #include <QtCore/QUuid>
23 #include <QtCore/QVariant>
24 #include <QtCore/QVector>
25 
26 #include "GraphicsScriptingUtil.h"
27 
28 #include <graphics/Geometry.h>
29 #include <Scriptable.h>
30 
31 class ScriptValue;
32 
33 namespace scriptable {
34  /*@jsdoc
35  * A handle to in-memory mesh data in a {@link GraphicsModel}.
36  *
37  * <p>Create using the {@link Graphics} API, {@link GraphicsModel.cloneModel}, or {@link GraphicsMesh.cloneMesh}.</p>
38  *
39  * @class GraphicsMesh
40  * @hideconstructor
41  *
42  * @hifi-interface
43  * @hifi-client-entity
44  * @hifi-avatar
45  *
46  * @property {number} numParts - The number of mesh parts.
47  * <em>Read-only.</em>
48  * @property {GraphicsMeshPart[]} parts - The mesh parts.
49  * <em>Read-only.</em>
50  * @property {number} numIndices - The total number of vertex indices in the mesh.
51  * <em>Read-only.</em>
52  * @property {number} numVertices - The total number of vertices in the mesh.
53  * <em>Read-only.</em>
54  * @property {number} numAttributes - The number of vertex attributes.
55  * <em>Read-only.</em>
56  * @property {Graphics.BufferTypeName[]} attributeNames - The names of the vertex attributes.
57  * <em>Read-only.</em>
58  * @property {boolean} valid - <code>true</code> if the mesh is valid, <code>false</code> if it isn't.
59  * <em>Read-only.</em>
60  * @property {boolean} strong - <code>true</code> if the mesh is valid and able to be used, <code>false</code> if it isn't.
61  * <em>Read-only.</em>
62  * @property {Graphics.MeshExtents} extents - The mesh extents, in model coordinates.
63  * <em>Read-only.</em>
64  * @property {Object<Graphics.BufferTypeName, Graphics.BufferFormat>} bufferFormats - Details of the buffers used for the
65  * mesh.
66  * <em>Read-only.</em>
67  *
68  * @borrows GraphicsMesh.getVertextAttributes as getVertextAttributes
69  * @borrows GraphicsMesh.setVertextAttributes as setVertextAttributes
70  */
71  class ScriptableMesh : public ScriptableMeshBase, Scriptable {
72  Q_OBJECT
73  public:
74  Q_PROPERTY(glm::uint32 numParts READ getNumParts)
75  Q_PROPERTY(glm::uint32 numAttributes READ getNumAttributes)
76  Q_PROPERTY(glm::uint32 numVertices READ getNumVertices)
77  Q_PROPERTY(glm::uint32 numIndices READ getNumIndices)
78  Q_PROPERTY(QVector<QString> attributeNames READ getAttributeNames)
79  Q_PROPERTY(QVector<scriptable::ScriptableMeshPartPointer> parts READ getMeshParts)
80  Q_PROPERTY(bool valid READ isValid)
81  Q_PROPERTY(bool strong READ hasValidStrongMesh)
82  Q_PROPERTY(QVariantMap extents READ getMeshExtents)
83  Q_PROPERTY(QVariantMap bufferFormats READ getBufferFormats)
84  QVariantMap getBufferFormats() const;
85 
86  operator const ScriptableMeshBase*() const { return (qobject_cast<const scriptable::ScriptableMeshBase*>(this)); }
87 
88  ScriptableMesh(WeakModelProviderPointer provider, ScriptableModelBasePointer model, MeshPointer mesh, QObject* parent)
89  : ScriptableMeshBase(provider, model, mesh, parent), Scriptable() { strongMesh = mesh; }
90  ScriptableMesh(MeshPointer mesh, QObject* parent)
91  : ScriptableMeshBase(WeakModelProviderPointer(), nullptr, mesh, parent), Scriptable() { strongMesh = mesh; }
92  ScriptableMesh(const ScriptableMeshBase& other);
93  ScriptableMesh(const ScriptableMesh& other) : ScriptableMeshBase(other), Scriptable() {};
94  virtual ~ScriptableMesh();
95 
96  const scriptable::MeshPointer getOwnedMeshPointer() const { return strongMesh; }
97  scriptable::ScriptableMeshPointer getSelf() const { return const_cast<scriptable::ScriptableMesh*>(this); }
98  bool isValid() const { return !weakMesh.expired(); }
99  bool hasValidStrongMesh() const { return (bool)strongMesh; }
100  glm::uint32 getNumParts() const;
101  glm::uint32 getNumVertices() const;
102  glm::uint32 getNumAttributes() const;
103  glm::uint32 getNumIndices() const;
104  QVector<QString> getAttributeNames() const;
105  QVector<scriptable::ScriptableMeshPartPointer> getMeshParts() const;
106  QVariantMap getMeshExtents() const;
107 
108  operator bool() const { return !weakMesh.expired(); }
109  int getSlotNumber(const QString& attributeName) const;
110 
111  public slots:
112 
113  /*@jsdoc
114  * Gets the model the mesh is part of.
115  * <p><em>Currently doesn't work.</em></p>
116  * @function GraphicsMesh.getParentModel
117  * @returns {GraphicsModel} The model the mesh is part of, <code>null</code> if it isn't part of a model.
118  */
119  const scriptable::ScriptableModelPointer getParentModel() const { return qobject_cast<scriptable::ScriptableModel*>(model); }
120 
121  /*@jsdoc
122  * Gets the vertex indices.
123  * @function GraphicsMesh.getIndices
124  * @returns {number[]} The vertex indices.
125  */
126  QVector<glm::uint32> getIndices() const;
127 
128  /*@jsdoc
129  * Gets the indices of nearby vertices.
130  * @function GraphicsMesh.findNearbyVertexIndices
131  * @param {Vec3} origin - The search position, in model coordinates.
132  * @param {number} [epsilon=1e-6] - The search distance. If a vertex is within this distance from the
133  * <code>origin</code> it is considered to be "nearby".
134  * @returns {number[]} The indices of nearby vertices.
135  */
136  QVector<glm::uint32> findNearbyVertexIndices(const glm::vec3& origin, float epsilon = 1e-6) const;
137 
138  /*@jsdoc
139  * Adds an attribute for all vertices.
140  * @function GraphicsMesh.addAttribute
141  * @param {Graphics.BufferTypeName} name - The name of the attribute.
142  * @param {Graphics.BufferType} [defaultValue] - The value to give the attributes added to the vertices.
143  * @returns {number} The number of vertices the attribute was added to, <code>0</code> if the <code>name</code> was
144  * invalid or all vertices already had the attribute.
145  */
146  glm::uint32 addAttribute(const QString& attributeName, const QVariant& defaultValue = QVariant());
147 
148  /*@jsdoc
149  * Sets the value of an attribute for all vertices.
150  * @function GraphicsMesh.fillAttribute
151  * @param {Graphics.BufferTypeName} name - The name of the attribute. The attribute is added to the vertices if not
152  * already present.
153  * @param {Graphics.BufferType} value - The value to give the attributes.
154  * @returns {number} <code>1</code> if the attribute name was valid and the attribute values were set, <code>0</code>
155  * otherwise.
156  */
157  glm::uint32 fillAttribute(const QString& attributeName, const QVariant& value);
158 
159  /*@jsdoc
160  * Removes an attribute from all vertices.
161  * <p>Note: The <code>"position"</code> attribute cannot be removed.</p>
162  * @function GraphicsMesh.removeAttribute
163  * @param {Graphics.BufferTypeName} name - The name of the attribute to remove.
164  * @returns {boolean} <code>true</code> if the attribute existed and was removed, <code>false</code> otherwise.
165  */
166  bool removeAttribute(const QString& attributeName);
167 
168  /*@jsdoc
169  * Gets the value of an attribute for all vertices.
170  * @function GraphicsMesh.queryVertexAttributes
171  * @param {Graphics.BufferTypeName} name - The name of the attribute to get the vertex values of.
172  * @throws Throws an error if the <code>name</code> is invalid or isn't used in the mesh.
173  * @returns {Graphics.BufferType[]} The attribute values for all vertices.
174  */
175  QVariantList queryVertexAttributes(QVariant selector) const;
176 
177  /*@jsdoc
178  * Gets the attributes and attribute values of a vertex.
179  * @function GraphicsMesh.getVertexAttributes
180  * @param {number} index - The vertex to get the attributes for.
181  * @returns {Object<Graphics.BufferTypeName,Graphics.BufferType>} The attribute names and values for the vertex.
182  * @throws Throws an error if the <code>index</code> is invalid.
183  */
184  QVariantMap getVertexAttributes(glm::uint32 vertexIndex) const;
185 
186  /*@jsdoc
187  * Updates attribute values of a vertex.
188  * @function GraphicsMesh.setVertexAttributes
189  * @param {number} index - The vertex to set the attributes for.
190  * @param {Object<Graphics.BufferTypeNAme,Graphics.BufferType>} values - The attribute names and values. Need not
191  * specify unchanged values.
192  * @returns {boolean} <code>true</code> if the index and the attribute names and values were valid and the vertex was
193  * updated, <code>false</code> otherwise.
194  * @throws Throws an error if the <code>index</code> is invalid or one of the attribute names is invalid or isn't used
195  * in the mesh.
196  */
197  // @borrows jsdoc from GraphicsMesh
198  bool setVertexAttributes(glm::uint32 vertexIndex, const QVariantMap& attributeValues);
199 
200  /*@jsdoc
201  * Gets the value of a vertex's attribute.
202  * @function GraphicsMesh.getVertexProperty
203  * @param {number} index - The vertex index.
204  * @param {Graphics.BufferTypeName} name - The name of the vertex attribute to get.
205  * @returns {Graphics.BufferType} The value of the vertex attribute.
206  * @throws Throws an error if the <code>index</code> is invalid or <code>name</code> is invalid or isn't used in the
207  * mesh.
208  */
209  QVariant getVertexProperty(glm::uint32 vertexIndex, const QString& attributeName) const;
210 
211  /*@jsdoc
212  * Sets the value of a vertex's attribute.
213  * @function GraphicsMesh.setVertexProperty
214  * @param {number} index - The vertex index.
215  * @param {Graphics.BufferTypeName} name - The name of the vertex attribute to set.
216  * @param {Graphics.BufferType} value - The vertex attribute value to set.
217  * @returns {boolean} <code>true</code> if the vertex attribute value was set, <code>false</code> if it wasn't.
218  * @throws Throws an error if the <code>index</code> is invalid or <code>name</code> is invalid or isn't used in the
219  * mesh.
220  */
221  bool setVertexProperty(glm::uint32 vertexIndex, const QString& attributeName, const QVariant& value);
222 
223  /*@jsdoc
224  * Makes a copy of the mesh.
225  * @function GraphicsMesh.cloneMesh
226  * @returns {GraphicsMesh} A copy of the mesh.
227  */
228  scriptable::ScriptableMeshPointer cloneMesh();
229 
230  // ScriptEngine-specific wrappers
231 
232  /*@jsdoc
233  * Updates vertex attributes by calling a function for each vertex. The function can return modified attributes to
234  * update the vertex with.
235  * @function GraphicsMesh.updateVertexAttributes
236  * @param {GraphicsMesh~updateVertexAttributesCallback} callback - The function to call for each vertex.
237  * @returns {number} The number of vertices the callback was called for.
238  */
239  glm::uint32 updateVertexAttributes(const ScriptValue& callback);
240 
241  /*@jsdoc
242  * Calls a function for each vertex.
243  * @function GraphicsMesh.forEachVertex
244  * @param {GraphicsMesh~forEachVertexCallback} callback - The function to call for each vertex.
245  * @returns {number} The number of vertices the callback was called for.
246  */
247  glm::uint32 forEachVertex(const ScriptValue& callback);
248 
249  /*@jsdoc
250  * Checks if an index is valid and, optionally, that vertex has a particular attribute.
251  * @function GraphicsMesh.isValidIndex
252  * @param {number} index - The index to check.
253  * @param {Graphics.BufferTypeName} [attribute] - The attribute to check.
254  * @returns {boolean} <code>true</code> if the index is valid and that vertex has the attribute if specified.
255  * @throws Throws an error if the <code>index</code> if invalid or <code>name</code> is invalid or isn't used in the
256  * mesh.
257  */
258  // FIXME: Should return false rather than throw an error.
259  bool isValidIndex(glm::uint32 vertexIndex, const QString& attributeName = QString()) const;
260  };
261 
262 }
263 
264 Q_DECLARE_METATYPE(scriptable::ScriptableMeshPointer)
265 Q_DECLARE_METATYPE(QVector<scriptable::ScriptableMeshPointer>)
[ScriptInterface] Provides an engine-independent interface for QScriptValue
Definition: ScriptValue.h:40
[ScriptInterface] Provides an engine-independent interface for QScriptable
Definition: Scriptable.h:29