BaseMesh.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // This code is in the public domain -- Ignacio Castaño <castano@gmail.com>
  2. #pragma once
  3. #ifndef NV_MESH_BASEMESH_H
  4. #define NV_MESH_BASEMESH_H
  5. #include "nvmesh.h"
  6. #include "nvmath/Vector.h"
  7. #include "nvcore/Array.h"
  8. #include "nvcore/Hash.h"
  9. namespace nv
  10. {
  11. /// Base mesh without connectivity.
  12. class BaseMesh
  13. {
  14. public:
  15. struct Vertex;
  16. BaseMesh() {}
  17. BaseMesh(uint vertexNum) :
  18. m_vertexArray(vertexNum) {}
  19. // Vertex methods.
  20. uint vertexCount() const { return m_vertexArray.count(); }
  21. const Vertex & vertexAt(uint i) const { return m_vertexArray[i]; }
  22. Vertex & vertexAt(uint i) { return m_vertexArray[i]; }
  23. const Array<Vertex> & vertices() const { return m_vertexArray; }
  24. Array<Vertex> & vertices() { return m_vertexArray; }
  25. friend Stream & operator<< (Stream & s, BaseMesh & obj);
  26. protected:
  27. Array<Vertex> m_vertexArray;
  28. };
  29. /// BaseMesh vertex.
  30. struct BaseMesh::Vertex
  31. {
  32. Vertex() : id(NIL), pos(0.0f), nor(0.0f), tex(0.0f) {}
  33. uint id; // @@ Vertex should be an index into the vertex data.
  34. Vector3 pos;
  35. Vector3 nor;
  36. Vector2 tex;
  37. };
  38. inline bool operator==(const BaseMesh::Vertex & a, const BaseMesh::Vertex & b)
  39. {
  40. return a.pos == b.pos && a.nor == b.nor && a.tex == b.tex;
  41. }
  42. inline bool operator!=(const BaseMesh::Vertex & a, const BaseMesh::Vertex & b)
  43. {
  44. return a.pos != b.pos && a.nor != b.nor && a.tex != b.tex;
  45. }
  46. template <> struct Hash<BaseMesh::Vertex>
  47. {
  48. uint operator()(const BaseMesh::Vertex & v) const
  49. {
  50. return Hash<Vector3>()(v.pos);
  51. }
  52. };
  53. } // nv namespace
  54. #endif // NV_MESH_BASEMESH_H