Geometry.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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. class IndexBuffer;
  28. class Ray;
  29. class Graphics;
  30. class VertexBuffer;
  31. /// Defines one or more vertex buffers, an index buffer and a draw range.
  32. class Geometry : public Object
  33. {
  34. OBJECT(Geometry);
  35. public:
  36. /// Construct with one empty vertex buffer.
  37. Geometry(Context* context);
  38. /// Destruct.
  39. virtual ~Geometry();
  40. /// %Set number of vertex buffer.
  41. bool SetNumVertexBuffers(unsigned num);
  42. /// %Set a vertex buffer by index.
  43. bool SetVertexBuffer(unsigned index, VertexBuffer* buffer, unsigned elementMask = MASK_DEFAULT);
  44. /// %Set the index buffer.
  45. void SetIndexBuffer(IndexBuffer* buffer);
  46. /// %Set the draw range.
  47. bool SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, bool getUsedVertexRange = true);
  48. /// %Set the draw range.
  49. bool SetDrawRange(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned vertexStart, unsigned vertexCount);
  50. /// %Set the LOD distance.
  51. void SetLodDistance(float distance);
  52. /// Draw.
  53. void Draw(Graphics* graphics);
  54. /// Return all vertex buffers.
  55. const Vector<SharedPtr<VertexBuffer> >& GetVertexBuffers() const { return vertexBuffers_; }
  56. /// Return vertex element masks.
  57. const PODVector<unsigned>& GetVertexElementMasks() const { return elementMasks_; }
  58. /// Return number of vertex buffers.
  59. unsigned GetNumVertexBuffers() const { return vertexBuffers_.Size(); }
  60. /// Return vertex buffer by index.
  61. VertexBuffer* GetVertexBuffer(unsigned index) const;
  62. /// Return vertex element mask by index.
  63. unsigned GetVertexElementMask(unsigned index) const;
  64. /// Return the index buffer.
  65. IndexBuffer* GetIndexBuffer() const { return indexBuffer_; }
  66. /// Return primitive type.
  67. PrimitiveType GetPrimitiveType() const { return primitiveType_; }
  68. /// Return start index.
  69. unsigned GetIndexStart() const { return indexStart_; }
  70. /// Return number of indices.
  71. unsigned GetIndexCount() const { return indexCount_; }
  72. /// Return first used vertex.
  73. unsigned GetVertexStart() const { return vertexStart_; }
  74. /// Return number of used vertices.
  75. unsigned GetVertexCount() const { return vertexCount_; }
  76. /// Return LOD distance.
  77. float GetLodDistance() const { return lodDistance_; }
  78. /// Return buffers' combined hash value for state sorting.
  79. unsigned short GetBufferHash() const;
  80. /// Return raw vertex and index data for CPU operations, or null pointers if not available.
  81. void GetRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData, unsigned& indexSize);
  82. /// Return ray hit distance or infinity if no hit. Requires raw data to be set.
  83. float GetDistance(const Ray& ray);
  84. private:
  85. /// Locate vertex buffer with position data.
  86. void GetPositionBufferIndex();
  87. /// Vertex buffers.
  88. Vector<SharedPtr<VertexBuffer> > vertexBuffers_;
  89. /// Vertex element masks.
  90. PODVector<unsigned> elementMasks_;
  91. /// Index buffer.
  92. SharedPtr<IndexBuffer> indexBuffer_;
  93. /// Primitive type.
  94. PrimitiveType primitiveType_;
  95. /// Start index.
  96. unsigned indexStart_;
  97. /// Number of indices.
  98. unsigned indexCount_;
  99. /// First used vertex.
  100. unsigned vertexStart_;
  101. /// Number of used vertices.
  102. unsigned vertexCount_;
  103. /// Index of vertex buffer with position data.
  104. unsigned positionBufferIndex_;
  105. /// LOD distance.
  106. float lodDistance_;
  107. };