Geometry.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "ArrayPtr.h"
  25. #include "GraphicsDefs.h"
  26. #include "Object.h"
  27. namespace Urho3D
  28. {
  29. class IndexBuffer;
  30. class Ray;
  31. class Graphics;
  32. class VertexBuffer;
  33. /// Defines one or more vertex buffers, an index buffer and a draw range.
  34. class Geometry : public Object
  35. {
  36. OBJECT(Geometry);
  37. public:
  38. /// Construct with one empty vertex buffer.
  39. Geometry(Context* context);
  40. /// Destruct.
  41. virtual ~Geometry();
  42. /// Set number of vertex buffer.
  43. bool SetNumVertexBuffers(unsigned num);
  44. /// Set a vertex buffer by index.
  45. bool SetVertexBuffer(unsigned index, VertexBuffer* buffer, unsigned elementMask = MASK_DEFAULT);
  46. /// Set the index buffer.
  47. void SetIndexBuffer(IndexBuffer* buffer);
  48. /// Set the draw range.
  49. bool SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, bool getUsedVertexRange = true);
  50. /// Set the draw range.
  51. bool SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned vertexStart, unsigned vertexCount);
  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, unsigned& elementMask);
  88. /// Return ray hit distance or infinity if no hit. Requires raw data to be set.
  89. float GetHitDistance(const Ray& ray);
  90. /// Return whether has empty draw range.
  91. bool IsEmpty() const { return indexCount_ == 0 && vertexCount_ == 0; }
  92. private:
  93. /// Locate vertex buffer with position data.
  94. void GetPositionBufferIndex();
  95. /// Vertex buffers.
  96. Vector<SharedPtr<VertexBuffer> > vertexBuffers_;
  97. /// Vertex element masks.
  98. PODVector<unsigned> elementMasks_;
  99. /// Index buffer.
  100. SharedPtr<IndexBuffer> indexBuffer_;
  101. /// Raw vertex data override.
  102. SharedArrayPtr<unsigned char> rawVertexData_;
  103. /// Raw index data override.
  104. SharedArrayPtr<unsigned char> rawIndexData_;
  105. /// Primitive type.
  106. PrimitiveType primitiveType_;
  107. /// Start index.
  108. unsigned indexStart_;
  109. /// Number of indices.
  110. unsigned indexCount_;
  111. /// First used vertex.
  112. unsigned vertexStart_;
  113. /// Number of used vertices.
  114. unsigned vertexCount_;
  115. /// Index of vertex buffer with position data.
  116. unsigned positionBufferIndex_;
  117. /// Raw vertex data override size.
  118. unsigned rawVertexSize_;
  119. /// Raw vertex data override element mask.
  120. unsigned rawElementMask_;
  121. /// Raw index data override size.
  122. unsigned rawIndexSize_;
  123. /// LOD distance.
  124. float lodDistance_;
  125. };
  126. }