Geometry.h 4.7 KB

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