Geometry.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // Copyright (c) 2008-2015 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. OBJECT(Geometry);
  36. public:
  37. /// Construct with one empty vertex buffer.
  38. Geometry(Context* context);
  39. /// Destruct.
  40. virtual ~Geometry();
  41. /// Set number of vertex buffer.
  42. bool SetNumVertexBuffers(unsigned num);
  43. /// Set a vertex buffer by index.
  44. bool SetVertexBuffer(unsigned index, VertexBuffer* buffer, unsigned elementMask = MASK_DEFAULT);
  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, unsigned vertexSize, unsigned elementMask);
  56. /// Override raw index data to be returned for CPU-side operations.
  57. void SetRawIndexData(SharedArrayPtr<unsigned char> data, unsigned indexSize);
  58. /// Draw.
  59. void Draw(Graphics* graphics);
  60. /// Return all vertex buffers.
  61. const Vector<SharedPtr<VertexBuffer> >& GetVertexBuffers() const { return vertexBuffers_; }
  62. /// Return vertex element masks.
  63. const PODVector<unsigned>& GetVertexElementMasks() const { return elementMasks_; }
  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 vertex element mask by index.
  69. unsigned GetVertexElementMask(unsigned index) const;
  70. /// Return the index buffer.
  71. IndexBuffer* GetIndexBuffer() const { return indexBuffer_; }
  72. /// Return primitive type.
  73. PrimitiveType GetPrimitiveType() const { return primitiveType_; }
  74. /// Return start index.
  75. unsigned GetIndexStart() const { return indexStart_; }
  76. /// Return number of indices.
  77. unsigned GetIndexCount() const { return indexCount_; }
  78. /// Return first used vertex.
  79. unsigned GetVertexStart() const { return vertexStart_; }
  80. /// Return number of used vertices.
  81. unsigned GetVertexCount() const { return vertexCount_; }
  82. /// Return LOD distance.
  83. float GetLodDistance() const { return lodDistance_; }
  84. /// Return buffers' combined hash value for state sorting.
  85. unsigned short GetBufferHash() const;
  86. /// Return raw vertex and index data for CPU operations, or null pointers if not available.
  87. void GetRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData, unsigned& indexSize,
  88. unsigned& elementMask) const;
  89. /// Return raw vertex and index data for CPU operations, or null pointers if not available.
  90. void GetRawDataShared(SharedArrayPtr<unsigned char>& vertexData, unsigned& vertexSize, SharedArrayPtr<unsigned char>& indexData,
  91. unsigned& indexSize, unsigned& elementMask) const;
  92. /// 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.
  93. float GetHitDistance(const Ray& ray, Vector3* outNormal = 0, Vector2* outUV = 0) const;
  94. /// Return whether or not the ray is inside geometry.
  95. bool IsInside(const Ray& ray) const;
  96. /// Return whether has empty draw range.
  97. bool IsEmpty() const { return indexCount_ == 0 && vertexCount_ == 0; }
  98. private:
  99. /// Locate vertex buffer with position data.
  100. void GetPositionBufferIndex();
  101. /// Vertex buffers.
  102. Vector<SharedPtr<VertexBuffer> > vertexBuffers_;
  103. /// Vertex element masks.
  104. PODVector<unsigned> elementMasks_;
  105. /// Index buffer.
  106. SharedPtr<IndexBuffer> indexBuffer_;
  107. /// Raw vertex data override.
  108. SharedArrayPtr<unsigned char> rawVertexData_;
  109. /// Raw index data override.
  110. SharedArrayPtr<unsigned char> rawIndexData_;
  111. /// Primitive type.
  112. PrimitiveType primitiveType_;
  113. /// Start index.
  114. unsigned indexStart_;
  115. /// Number of indices.
  116. unsigned indexCount_;
  117. /// First used vertex.
  118. unsigned vertexStart_;
  119. /// Number of used vertices.
  120. unsigned vertexCount_;
  121. /// Index of vertex buffer with position data.
  122. unsigned positionBufferIndex_;
  123. /// Raw vertex data override size.
  124. unsigned rawVertexSize_;
  125. /// Raw vertex data override element mask.
  126. unsigned rawElementMask_;
  127. /// Raw index data override size.
  128. unsigned rawIndexSize_;
  129. /// LOD distance.
  130. float lodDistance_;
  131. };
  132. }