Geometry.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // Copyright (c) 2008-2014 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 "ArrayPtr.h"
  24. #include "GraphicsDefs.h"
  25. #include "Object.h"
  26. namespace Urho3D
  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 URHO3D_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, bool checkIllegal = true);
  51. /// Set the LOD distance.
  52. void SetLodDistance(float distance);
  53. /// Override raw vertex data to be returned for CPU-side operations.
  54. void SetRawVertexData(SharedArrayPtr<unsigned char> data, unsigned vertexSize, unsigned elementMask);
  55. /// Override raw index data to be returned for CPU-side operations.
  56. void SetRawIndexData(SharedArrayPtr<unsigned char> data, unsigned indexSize);
  57. /// Draw.
  58. void Draw(Graphics* graphics);
  59. /// Return all vertex buffers.
  60. const Vector<SharedPtr<VertexBuffer> >& GetVertexBuffers() const { return vertexBuffers_; }
  61. /// Return vertex element masks.
  62. const PODVector<unsigned>& GetVertexElementMasks() const { return elementMasks_; }
  63. /// Return number of vertex buffers.
  64. unsigned GetNumVertexBuffers() const { return vertexBuffers_.Size(); }
  65. /// Return vertex buffer by index.
  66. VertexBuffer* GetVertexBuffer(unsigned index) const;
  67. /// Return vertex element mask by index.
  68. unsigned GetVertexElementMask(unsigned index) const;
  69. /// Return the index buffer.
  70. IndexBuffer* GetIndexBuffer() const { return indexBuffer_; }
  71. /// Return primitive type.
  72. PrimitiveType GetPrimitiveType() const { return primitiveType_; }
  73. /// Return start index.
  74. unsigned GetIndexStart() const { return indexStart_; }
  75. /// Return number of indices.
  76. unsigned GetIndexCount() const { return indexCount_; }
  77. /// Return first used vertex.
  78. unsigned GetVertexStart() const { return vertexStart_; }
  79. /// Return number of used vertices.
  80. unsigned GetVertexCount() const { return vertexCount_; }
  81. /// Return LOD distance.
  82. float GetLodDistance() const { return lodDistance_; }
  83. /// Return buffers' combined hash value for state sorting.
  84. unsigned short GetBufferHash() const;
  85. /// Return raw vertex and index data for CPU operations, or null pointers if not available.
  86. void GetRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData, unsigned& indexSize, unsigned& elementMask) const;
  87. /// Return raw vertex and index data for CPU operations, or null pointers if not available.
  88. void GetRawDataShared(SharedArrayPtr<unsigned char>& vertexData, unsigned& vertexSize, SharedArrayPtr<unsigned char>& indexData, unsigned& indexSize, unsigned& elementMask) const;
  89. /// Return ray hit distance or infinity if no hit. Requires raw data to be set. Optionally return hit normal.
  90. float GetHitDistance(const Ray& ray, Vector3* outNormal = 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. /// Locate vertex buffer with position data.
  97. void GetPositionBufferIndex();
  98. /// Vertex buffers.
  99. Vector<SharedPtr<VertexBuffer> > vertexBuffers_;
  100. /// Vertex element masks.
  101. PODVector<unsigned> elementMasks_;
  102. /// Index buffer.
  103. SharedPtr<IndexBuffer> indexBuffer_;
  104. /// Raw vertex data override.
  105. SharedArrayPtr<unsigned char> rawVertexData_;
  106. /// Raw index data override.
  107. SharedArrayPtr<unsigned char> rawIndexData_;
  108. /// Primitive type.
  109. PrimitiveType primitiveType_;
  110. /// Start index.
  111. unsigned indexStart_;
  112. /// Number of indices.
  113. unsigned indexCount_;
  114. /// First used vertex.
  115. unsigned vertexStart_;
  116. /// Number of used vertices.
  117. unsigned vertexCount_;
  118. /// Index of vertex buffer with position data.
  119. unsigned positionBufferIndex_;
  120. /// Raw vertex data override size.
  121. unsigned rawVertexSize_;
  122. /// Raw vertex data override element mask.
  123. unsigned rawElementMask_;
  124. /// Raw index data override size.
  125. unsigned rawIndexSize_;
  126. /// LOD distance.
  127. float lodDistance_;
  128. };
  129. }