NAP
mesh.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "vertexattribute.h"
9 
10 // External includes
11 #include <gpumesh.h>
12 #include <memory>
13 #include <utility/dllexport.h>
14 #include <rtti/object.h>
15 #include <rtti/objectptr.h>
16 #include <nap/numeric.h>
17 #include <rtti/factory.h>
18 #include <nap/numeric.h>
19 
20 namespace nap
21 {
22  class RenderService;
23  class Core;
24 
28  enum class EDrawMode : int32
29  {
30  Points = 0,
31  Lines = 1,
32  LineStrip = 2,
33  Triangles = 3,
34  TriangleStrip = 4,
35  TriangleFan = 5,
36  Unknown = 0x7FFFFFFF,
37  };
38 
39 
43  enum class ECullMode : int32
44  {
45  None = 0,
46  Front = 1,
47  Back = 2,
48  FrontAndBack = 3
49  };
50 
51 
55  enum class EPolygonMode : int32
56  {
57  Fill = 0,
58  Line = 1,
59  Point = 2
60  };
61 
62 
64  // MeshShape
66 
71  struct NAPAPI MeshShape
72  {
76  int getNumIndices() const { return mIndices.size(); }
77 
82  void clearIndices() { mIndices.clear(); }
83 
88  void reserveIndices(size_t numIndices);
89 
96  void setIndices(uint32* indices, int numIndices);
97 
101  const std::vector<uint32>& getIndices() const { return mIndices; }
102 
106  std::vector<uint32>& getIndices() { return mIndices; }
107 
115  void addIndices(uint32* indices, int numIndices);
116 
122  void addIndex(uint32 index) { mIndices.emplace_back(index); }
123 
129  uint32& operator[](std::size_t index) { return mIndices[index]; }
130 
136  const uint32& operator[](std::size_t index) const { return mIndices[index]; }
137 
138  std::vector<uint32> mIndices;
139  };
140 
141 
143  // MeshProperties
145 
156  template<typename VERTEX_ATTRIBUTE_PTR>
158  {
159  using VertexAttributeList = std::vector<VERTEX_ATTRIBUTE_PTR>;
160 
161  int mNumVertices = 0;
167  std::vector<MeshShape> mShapes;
168  };
169 
170  // ObjectPtr based mesh properties, used in serializable Mesh format (json/binary)
172 
173 
175  // MeshInstance
177 
196  class NAPAPI MeshInstance
197  {
198  RTTI_ENABLE()
199  public:
200  // Default constructor
201  MeshInstance(RenderService& renderService);
202 
203  // destructor
204  virtual ~MeshInstance();
205 
211  bool init(utility::ErrorState& errorState);
212 
218  void copyMeshProperties(RTTIMeshProperties& meshProperties);
219 
223  GPUMesh& getGPUMesh() const;
224 
230  template<typename T>
231  VertexAttribute<T>* findAttribute(const std::string& id);
232 
238  template<typename T>
239  const VertexAttribute<T>* findAttribute(const std::string& id) const;
240 
246  template<typename T>
247  VertexAttribute<T>& getAttribute(const std::string& id);
248 
254  template<typename T>
255  const VertexAttribute<T>& getAttribute(const std::string& id) const;
256 
262  template<typename T>
263  VertexAttribute<T>& getOrCreateAttribute(const std::string& id);
264 
270  void reserveVertices(size_t numVertices);
271 
277  void setNumVertices(int numVertices) { mProperties.mNumVertices = numVertices; }
278 
282  int getNumVertices() const { return mProperties.mNumVertices; }
283 
287  int getNumShapes() const { return mProperties.mShapes.size(); }
288 
292  void setDrawMode(EDrawMode mode) { mProperties.mDrawMode = mode; }
293 
297  EDrawMode getDrawMode() const { return mProperties.mDrawMode; }
298 
302  void setCullMode(ECullMode mode) { mProperties.mCullMode = mode; }
303 
307  ECullMode getCullMode() const { return mProperties.mCullMode; }
308 
315  void setPolygonMode(EPolygonMode mode);
316 
320  EPolygonMode getPolygonMode() const { return mProperties.mPolygonMode; }
321 
327  MeshShape& getShape(int index) { return mProperties.mShapes[index]; }
328 
334  const MeshShape& getShape(int index) const { return mProperties.mShapes[index]; }
335 
340  MeshShape& createShape();
341 
346  void setUsage(EMemoryUsage inUsage) { mProperties.mUsage = inUsage; }
347 
351  EMemoryUsage getUsage() const { return mProperties.mUsage; }
352 
361  bool update(utility::ErrorState& errorState);
362 
372  bool update(nap::BaseVertexAttribute& attribute, utility::ErrorState& errorState);
373 
374  protected:
375  bool initGPUData(utility::ErrorState& errorState);
376 
377  private:
378  RenderService& mRenderService;
380  std::unique_ptr<GPUMesh> mGPUMesh;
381  bool mInitialized = false;
382  };
383 
384 
386  // IMesh
388 
394  class IMesh : public Resource
395  {
396  RTTI_ENABLE(Resource)
397  public:
401  virtual MeshInstance& getMeshInstance() = 0;
402 
406  virtual const MeshInstance& getMeshInstance() const = 0;
407  };
408 
409 
411  // Mesh
413 
424  class Mesh : public IMesh
425  {
426  RTTI_ENABLE(IMesh)
427  public:
431  Mesh() = default;
432 
436  Mesh(Core& core);
437 
443  virtual bool init(utility::ErrorState& errorState) override;
444 
448  virtual MeshInstance& getMeshInstance() override;
449 
453  virtual const MeshInstance& getMeshInstance() const override;
454 
460  template<typename T>
461  const VertexAttribute<T>* FindAttribute(const std::string& id) const;
462 
468  template<typename T>
469  const VertexAttribute<T>& GetAttribute(const std::string& id) const;
470 
472 
473  private:
474  std::unique_ptr<MeshInstance> mMeshInstance;
475  RenderService* mRenderService = nullptr;
476  };
477 
478 
480  // Template definitions
482 
483  template<typename T>
485  {
486  for (auto& attribute : mProperties.mAttributes)
487  if (attribute->mAttributeID == id)
488  return rtti_cast<VertexAttribute<T>>(attribute.get());
489  return nullptr;
490  }
491 
492  template<typename T>
493  const VertexAttribute<T>* nap::MeshInstance::findAttribute(const std::string& id) const
494  {
495  for (auto& attribute : mProperties.mAttributes)
496  {
497  if (attribute->mAttributeID == id)
498  {
499  assert(attribute->get_type().is_derived_from(RTTI_OF(VertexAttribute<T>)));
500  return static_cast<VertexAttribute<T>*>(attribute.get());
501  }
502  }
503  return nullptr;
504  }
505 
506  template<typename T>
508  {
509  VertexAttribute<T>* attribute = findAttribute<T>(id);
510  assert(attribute != nullptr);
511  return *attribute;
512  }
513 
514  template<typename T>
515  const VertexAttribute<T>& nap::MeshInstance::getAttribute(const std::string& id) const
516  {
517  const VertexAttribute<T>* attribute = findAttribute<T>(id);
518  assert(attribute != nullptr);
519  return *attribute;
520  }
521 
522  template<typename T>
524  {
525  for (auto& attribute : mProperties.mAttributes)
526  {
527  if (attribute->mAttributeID == id)
528  {
529  VertexAttribute<T>* result = rtti_cast<VertexAttribute<T>>(attribute.get());
530  assert(result != nullptr); // Attribute found, but has wrong type!
531  return *result;
532  }
533  }
534 
535  std::unique_ptr<VertexAttribute<T>> new_attribute = std::make_unique<VertexAttribute<T>>();
536  new_attribute->mAttributeID = id;
537  VertexAttribute<T>* result = new_attribute.get();
538  mProperties.mAttributes.emplace_back(std::move(new_attribute));
539 
540  return *result;
541  }
542 
543  template<typename T>
544  const VertexAttribute<T>* nap::Mesh::FindAttribute(const std::string& id) const
545  {
546  for (auto& attribute : mProperties.mAttributes)
547  if (attribute->mAttributeID == id)
548  return static_cast<VertexAttribute<T>*>(attribute.get());
549 
550  return nullptr;
551  }
552 
553 
554  template<typename T>
555  const VertexAttribute<T>& nap::Mesh::GetAttribute(const std::string& id) const
556  {
557  const VertexAttribute<T>* attribute = FindAttribute<T>(id);
558  assert(attribute != nullptr);
559  return *attribute;
560  }
561 }
nap::MeshShape::getIndices
std::vector< uint32 > & getIndices()
Definition: mesh.h:106
nap::Mesh::GetAttribute
const VertexAttribute< T > & GetAttribute(const std::string &id) const
Definition: mesh.h:555
nap::MeshInstance::findAttribute
VertexAttribute< T > * findAttribute(const std::string &id)
nap::MeshInstance::getDrawMode
EDrawMode getDrawMode() const
Definition: mesh.h:297
nap::EDrawMode::TriangleFan
@ TriangleFan
Interpret the vertex data as a fan of triangles.
nap::Line
Definition: polyline.h:172
nap::MeshShape::clearIndices
void clearIndices()
Definition: mesh.h:82
nap::MeshShape::getIndices
const std::vector< uint32 > & getIndices() const
Definition: mesh.h:101
nap::MeshInstance::getShape
MeshShape & getShape(int index)
Definition: mesh.h:327
nap::IMesh::getMeshInstance
virtual MeshInstance & getMeshInstance()=0
nap::MeshInstance::getOrCreateAttribute
VertexAttribute< T > & getOrCreateAttribute(const std::string &id)
nap::MeshShape::addIndex
void addIndex(uint32 index)
Definition: mesh.h:122
nap::VertexAttribute
Definition: vertexattribute.h:65
nap::EMemoryUsage::Static
@ Static
Buffer data is uploaded only once from the CPU to the GPU.
nap::EPolygonMode
EPolygonMode
Definition: mesh.h:55
nap::ECullMode::Front
@ Front
Cull front facing triangles.
nap::BaseVertexAttribute
Definition: vertexattribute.h:20
nap::GPUMesh
Definition: gpumesh.h:38
nap::EDrawMode::TriangleStrip
@ TriangleStrip
Interpret the vertex data as a strip of triangles.
nap::ECullMode
ECullMode
Definition: mesh.h:43
nap::Mesh::Mesh
Mesh()=default
nap::EMemoryUsage
EMemoryUsage
Definition: gpubuffer.h:41
nap::MeshShape::getNumIndices
int getNumIndices() const
Definition: mesh.h:76
nap::MeshProperties::mAttributes
VertexAttributeList mAttributes
Property: 'Attributes' vertex attributes.
Definition: mesh.h:166
nap::MeshInstance::getShape
const MeshShape & getShape(int index) const
Definition: mesh.h:334
nap::utility::ErrorState
Definition: errorstate.h:19
nap::BaseVertexAttribute::mAttributeID
std::string mAttributeID
Name/ID of the attribute.
Definition: vertexattribute.h:52
nap::MeshProperties
Definition: mesh.h:157
nap::IMesh
Definition: mesh.h:394
nap::MeshInstance::setDrawMode
void setDrawMode(EDrawMode mode)
Definition: mesh.h:292
nap::EDrawMode::Points
@ Points
Interpret the vertex data as single points.
nap::MeshInstance::getNumShapes
int getNumShapes() const
Definition: mesh.h:287
nap::EDrawMode::Unknown
@ Unknown
Invalid vertex interpretation.
nap::Mesh::mProperties
RTTIMeshProperties mProperties
Property: 'Properties' RTTI mesh CPU data.
Definition: mesh.h:471
nap::MeshProperties< rtti::ObjectPtr< BaseVertexAttribute > >::VertexAttributeList
std::vector< rtti::ObjectPtr< BaseVertexAttribute > > VertexAttributeList
Definition: mesh.h:159
nap::uint32
uint32_t uint32
Definition: numeric.h:20
nap::MeshShape::operator[]
const uint32 & operator[](std::size_t index) const
Definition: mesh.h:136
nap::MeshInstance::getCullMode
ECullMode getCullMode() const
Definition: mesh.h:307
nap::RenderService
Definition: renderservice.h:275
nap::MeshInstance::getAttribute
VertexAttribute< T > & getAttribute(const std::string &id)
nap::Core
Definition: core.h:82
nap::Mesh::FindAttribute
const VertexAttribute< T > * FindAttribute(const std::string &id) const
Definition: mesh.h:544
nap::MeshInstance::getPolygonMode
EPolygonMode getPolygonMode() const
Definition: mesh.h:320
nap::MeshProperties::mPolygonMode
EPolygonMode mPolygonMode
Property: 'PolygonMode' The polygon mode to use, fill is always available and should be the default.
Definition: mesh.h:165
nap::ECullMode::Back
@ Back
Cull back facing triangles.
nap::ECullMode::FrontAndBack
@ FrontAndBack
Cull front and back facing triangles.
nap::EPolygonMode::Fill
@ Fill
Polygons are interpreted and rendered using the specified 'EDrawMode'.
nap::MeshShape
Definition: mesh.h:71
nap::EDrawMode::Lines
@ Lines
Interpret the vertex data as individual lines.
nap::MeshInstance::setNumVertices
void setNumVertices(int numVertices)
Definition: mesh.h:277
nap::MeshShape::mIndices
std::vector< uint32 > mIndices
Property: 'Indices' into the mesh's vertex data.
Definition: mesh.h:138
nap::EDrawMode::Triangles
@ Triangles
Interpret the vertex data as a set of triangles.
nap
Definition: templateapp.h:17
nap::MeshInstance::getUsage
EMemoryUsage getUsage() const
Definition: mesh.h:351
nap::MeshInstance::setUsage
void setUsage(EMemoryUsage inUsage)
Definition: mesh.h:346
nap::MeshInstance::getNumVertices
int getNumVertices() const
Definition: mesh.h:282
nap::Mesh::getMeshInstance
virtual MeshInstance & getMeshInstance() override
nap::MeshShape::operator[]
uint32 & operator[](std::size_t index)
Definition: mesh.h:129
nap::Mesh::init
virtual bool init(utility::ErrorState &errorState) override
nap::MeshInstance
Definition: mesh.h:196
nap::Resource
Definition: resource.h:19
nap::MeshInstance::setCullMode
void setCullMode(ECullMode mode)
Definition: mesh.h:302
nap::MeshProperties::mUsage
EMemoryUsage mUsage
Property: 'Usage' GPU memory usage.
Definition: mesh.h:162
nap::MeshProperties::mNumVertices
int mNumVertices
Property: 'NumVertices' number of mesh vertices.
Definition: mesh.h:161
nap::MeshProperties::mCullMode
ECullMode mCullMode
Property: 'CullMode' The triangle cull mode to use.
Definition: mesh.h:164
nap::ECullMode::None
@ None
No culling.
nap::EDrawMode::LineStrip
@ LineStrip
Interpret the vertex data as a single connected line.
nap::MeshProperties::mShapes
std::vector< MeshShape > mShapes
Property: 'Shapes' list of managed shapes.
Definition: mesh.h:167
nap::EPolygonMode::Point
@ Point
Polygon vertices are drawn as points.
nap::EDrawMode
EDrawMode
Definition: mesh.h:28
nap::Mesh
Definition: mesh.h:424
nap::int32
int32_t int32
Definition: numeric.h:19
nap::MeshProperties::mDrawMode
EDrawMode mDrawMode
Property: 'DrawMode' The draw mode that should be used to draw the shapes.
Definition: mesh.h:163