Geometry.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "GraphicsDefs.h"
  25. #include "Object.h"
  26. #include "ArrayPtr.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 to report
  51. void SetLodDistance(float distance);
  52. /// Set vertex and index raw data. 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 ray hit distance or infinity if no hit. Requires raw data to be set
  85. float GetDistance(const Ray& ray);
  86. private:
  87. /// Locate vertex buffer with position data
  88. void GetPositionBufferIndex();
  89. /// Vertex buffers
  90. Vector<SharedPtr<VertexBuffer> > vertexBuffers_;
  91. /// Vertex element masks
  92. PODVector<unsigned> elementMasks_;
  93. /// Index buffer
  94. SharedPtr<IndexBuffer> indexBuffer_;
  95. /// Primitive type
  96. PrimitiveType primitiveType_;
  97. /// Start index
  98. unsigned indexStart_;
  99. /// Number of indices
  100. unsigned indexCount_;
  101. /// First used vertex
  102. unsigned vertexStart_;
  103. /// Number of used vertices
  104. unsigned vertexCount_;
  105. /// Index of vertex buffer with position data
  106. unsigned positionBufferIndex_;
  107. /// LOD distance
  108. float lodDistance_;
  109. /// Raw vertex data (positions only) for CPU operations
  110. SharedArrayPtr<unsigned char> rawVertexData_;
  111. /// Raw index data for CPU operations
  112. SharedArrayPtr<unsigned char> rawIndexData_;
  113. };