Geometry.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /// %Set vertex and index raw data for CPU access. Vertex raw data should contain positions only.
  53. void SetRawData(const SharedArrayPtr<unsigned char>& vertexData, const SharedArrayPtr<unsigned char>& indexData);
  54. /// Draw.
  55. void Draw(Graphics* graphics);
  56. /// Return all vertex buffers.
  57. const Vector<SharedPtr<VertexBuffer> >& GetVertexBuffers() const { return vertexBuffers_; }
  58. /// Return vertex element masks.
  59. const PODVector<unsigned>& GetVertexElementMasks() const { return elementMasks_; }
  60. /// Return number of vertex buffers.
  61. unsigned GetNumVertexBuffers() const { return vertexBuffers_.Size(); }
  62. /// Return vertex buffer by index.
  63. VertexBuffer* GetVertexBuffer(unsigned index) const;
  64. /// Return vertex element mask by index.
  65. unsigned GetVertexElementMask(unsigned index) const;
  66. /// Return the index buffer.
  67. IndexBuffer* GetIndexBuffer() const { return indexBuffer_; }
  68. /// Return primitive type.
  69. PrimitiveType GetPrimitiveType() const { return primitiveType_; }
  70. /// Return start index.
  71. unsigned GetIndexStart() const { return indexStart_; }
  72. /// Return number of indices.
  73. unsigned GetIndexCount() const { return indexCount_; }
  74. /// Return first used vertex.
  75. unsigned GetVertexStart() const { return vertexStart_; }
  76. /// Return number of used vertices.
  77. unsigned GetVertexCount() const { return vertexCount_; }
  78. /// Return LOD distance.
  79. float GetLodDistance() const { return lodDistance_; }
  80. /// Return buffers' combined hash value for state sorting.
  81. unsigned short GetBufferHash() const;
  82. /// Return raw vertex and index data for CPU operations, or null pointers if not available.
  83. void GetRawData(const unsigned char*& vertexData, unsigned& vertexSize, const unsigned char*& indexData, unsigned& indexSize);
  84. /// Return the raw vertex data array.
  85. const SharedArrayPtr<unsigned char>& GetRawVertexData() const { return rawVertexData_; }
  86. /// Return the raw index data array.
  87. const SharedArrayPtr<unsigned char>& GetRawIndexData() const { return rawIndexData_; }
  88. /// Return ray hit distance or infinity if no hit. Requires raw data to be set.
  89. float GetDistance(const Ray& ray);
  90. private:
  91. /// Locate vertex buffer with position data.
  92. void GetPositionBufferIndex();
  93. /// Vertex buffers.
  94. Vector<SharedPtr<VertexBuffer> > vertexBuffers_;
  95. /// Vertex element masks.
  96. PODVector<unsigned> elementMasks_;
  97. /// Index buffer.
  98. SharedPtr<IndexBuffer> indexBuffer_;
  99. /// Primitive type.
  100. PrimitiveType primitiveType_;
  101. /// Start index.
  102. unsigned indexStart_;
  103. /// Number of indices.
  104. unsigned indexCount_;
  105. /// First used vertex.
  106. unsigned vertexStart_;
  107. /// Number of used vertices.
  108. unsigned vertexCount_;
  109. /// Index of vertex buffer with position data.
  110. unsigned positionBufferIndex_;
  111. /// LOD distance.
  112. float lodDistance_;
  113. /// Raw vertex data (positions only) for CPU operations.
  114. SharedArrayPtr<unsigned char> rawVertexData_;
  115. /// Raw index data for CPU operations.
  116. SharedArrayPtr<unsigned char> rawIndexData_;
  117. };