Geometry.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/ArrayPtr.h"
  24. #include "../Core/Object.h"
  25. #include "../Graphics/GraphicsDefs.h"
  26. namespace Atomic
  27. {
  28. class IndexBuffer;
  29. class Ray;
  30. class Graphics;
  31. class VertexBuffer;
  32. /// Defines one or more vertex buffers, an index buffer and a draw range.
  33. class ATOMIC_API Geometry : public Object
  34. {
  35. ATOMIC_OBJECT(Geometry, Object);
  36. public:
  37. /// Construct with one empty vertex buffer.
  38. Geometry(Context* context);
  39. /// Destruct.
  40. virtual ~Geometry();
  41. /// Set number of vertex buffers.
  42. bool SetNumVertexBuffers(unsigned num);
  43. /// Set a vertex buffer by index.
  44. bool SetVertexBuffer(unsigned index, VertexBuffer* buffer);
  45. /// Set the index buffer.
  46. void SetIndexBuffer(IndexBuffer* buffer);
  47. /// Set the draw range.
  48. bool SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, bool getUsedVertexRange = true);
  49. /// Set the draw range.
  50. bool SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned vertexStart, unsigned vertexCount,
  51. bool checkIllegal = true);
  52. /// Set the LOD distance.
  53. void SetLodDistance(float distance);
  54. /// Override raw vertex data to be returned for CPU-side operations.
  55. void SetRawVertexData(SharedArrayPtr<unsigned char> data, const PODVector<VertexElement>& elements);
  56. /// Override raw vertex data to be returned for CPU-side operations using a legacy vertex bitmask.
  57. void SetRawVertexData(SharedArrayPtr<unsigned char> data, unsigned elementMask);
  58. /// Override raw index data to be returned for CPU-side operations.
  59. void SetRawIndexData(SharedArrayPtr<unsigned char> data, unsigned indexSize);
  60. /// Draw.
  61. void Draw(Graphics* graphics);
  62. /// Return all vertex buffers.
  63. const Vector<SharedPtr<VertexBuffer> >& GetVertexBuffers() const { return vertexBuffers_; }
  64. /// Return number of vertex buffers.
  65. unsigned GetNumVertexBuffers() const { return vertexBuffers_.Size(); }
  66. /// Return vertex buffer by index.
  67. VertexBuffer* GetVertexBuffer(unsigned index) const;
  68. /// Return the index buffer.
  69. IndexBuffer* GetIndexBuffer() const { return indexBuffer_; }
  70. /// Return primitive type.
  71. PrimitiveType GetPrimitiveType() const { return primitiveType_; }
  72. /// Return start index.
  73. unsigned GetIndexStart() const { return indexStart_; }
  74. /// Return number of indices.
  75. unsigned GetIndexCount() const { return indexCount_; }
  76. /// Return first used vertex.
  77. unsigned GetVertexStart() const { return vertexStart_; }
  78. /// Return number of used vertices.
  79. unsigned GetVertexCount() const { return vertexCount_; }
  80. /// Return LOD distance.
  81. float GetLodDistance() const { return lodDistance_; }
  82. /// Return buffers' combined hash value for state sorting.
  83. unsigned short GetBufferHash() const;
  84. /// Return raw vertex and index data for CPU operations, or null pointers if not available. Will return data of the first vertex buffer if override data not set.
  85. void GetRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData, unsigned& indexSize, const PODVector<VertexElement>*& elements) const;
  86. /// Return raw vertex and index data for CPU operations, or null pointers if not available. Will return data of the first vertex buffer if override data not set.
  87. void GetRawDataShared(SharedArrayPtr<unsigned char>& vertexData, unsigned& vertexSize, SharedArrayPtr<unsigned char>& indexData,
  88. unsigned& indexSize, const PODVector<VertexElement>*& elements) const;
  89. /// Return ray hit distance or infinity if no hit. Requires raw data to be set. Optionally return hit normal and hit uv coordinates at intersect point.
  90. float GetHitDistance(const Ray& ray, Vector3* outNormal = 0, Vector2* outUV = 0) const;
  91. /// Return whether or not the ray is inside geometry.
  92. bool IsInside(const Ray& ray) const;
  93. /// Return whether has empty draw range.
  94. bool IsEmpty() const { return indexCount_ == 0 && vertexCount_ == 0; }
  95. private:
  96. /// Vertex buffers.
  97. Vector<SharedPtr<VertexBuffer> > vertexBuffers_;
  98. /// Index buffer.
  99. SharedPtr<IndexBuffer> indexBuffer_;
  100. /// Primitive type.
  101. PrimitiveType primitiveType_;
  102. /// Start index.
  103. unsigned indexStart_;
  104. /// Number of indices.
  105. unsigned indexCount_;
  106. /// First used vertex.
  107. unsigned vertexStart_;
  108. /// Number of used vertices.
  109. unsigned vertexCount_;
  110. /// LOD distance.
  111. float lodDistance_;
  112. /// Raw vertex data elements.
  113. PODVector<VertexElement> rawElements_;
  114. /// Raw vertex data override.
  115. SharedArrayPtr<unsigned char> rawVertexData_;
  116. /// Raw index data override.
  117. SharedArrayPtr<unsigned char> rawIndexData_;
  118. /// Raw vertex data override size.
  119. unsigned rawVertexSize_;
  120. /// Raw index data override size.
  121. unsigned rawIndexSize_;
  122. };
  123. }