CustomGeometry.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Copyright (c) 2008-2022 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Graphics/Drawable.h"
  24. namespace Urho3D
  25. {
  26. /// Custom geometry vertex.
  27. /// @nocount
  28. struct CustomGeometryVertex
  29. {
  30. /// Position.
  31. Vector3 position_;
  32. /// Normal.
  33. Vector3 normal_;
  34. /// Color.
  35. unsigned color_;
  36. /// Texture coordinates.
  37. Vector2 texCoord_;
  38. /// Tangent.
  39. Vector4 tangent_;
  40. };
  41. class VertexBuffer;
  42. /// Custom geometry component.
  43. class URHO3D_API CustomGeometry : public Drawable
  44. {
  45. URHO3D_OBJECT(CustomGeometry, Drawable);
  46. public:
  47. /// Construct.
  48. explicit CustomGeometry(Context* context);
  49. /// Destruct.
  50. ~CustomGeometry() override;
  51. /// Register object factory. Drawable must be registered first.
  52. /// @nobind
  53. static void RegisterObject(Context* context);
  54. /// Process octree raycast. May be called from a worker thread.
  55. void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results) override;
  56. /// Return the geometry for a specific LOD level.
  57. Geometry* GetLodGeometry(unsigned batchIndex, unsigned level) override;
  58. /// Return number of occlusion geometry triangles.
  59. unsigned GetNumOccluderTriangles() override;
  60. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  61. bool DrawOcclusion(OcclusionBuffer* buffer) override;
  62. /// Clear all geometries.
  63. void Clear();
  64. /// Set number of geometries.
  65. /// @property
  66. void SetNumGeometries(unsigned num);
  67. /// Set vertex buffer dynamic mode. A dynamic buffer should be faster to update frequently. Effective at the next Commit() call.
  68. /// @property
  69. void SetDynamic(bool enable);
  70. /// Begin defining a geometry. Clears existing vertices in that index.
  71. void BeginGeometry(unsigned index, PrimitiveType type);
  72. /// Define a vertex position. This begins a new vertex.
  73. void DefineVertex(const Vector3& position);
  74. /// Define a vertex normal.
  75. void DefineNormal(const Vector3& normal);
  76. /// Define a vertex color.
  77. void DefineColor(const Color& color);
  78. /// Define a vertex UV coordinate.
  79. void DefineTexCoord(const Vector2& texCoord);
  80. /// Define a vertex tangent.
  81. void DefineTangent(const Vector4& tangent);
  82. /// Set the primitive type, number of vertices and elements in a geometry, after which the vertices can be edited with GetVertex(). An alternative to BeginGeometry() / DefineVertex().
  83. void DefineGeometry
  84. (unsigned index, PrimitiveType type, unsigned numVertices, bool hasNormals, bool hasColors, bool hasTexCoords,
  85. bool hasTangents);
  86. /// Update vertex buffer and calculate the bounding box. Call after finishing defining geometry.
  87. void Commit();
  88. /// Set material on all geometries.
  89. /// @property
  90. void SetMaterial(Material* material);
  91. /// Set material on one geometry. Return true if successful.
  92. /// @property{set_materials}
  93. bool SetMaterial(unsigned index, Material* material);
  94. /// Return number of geometries.
  95. /// @property
  96. unsigned GetNumGeometries() const { return geometries_.Size(); }
  97. /// Return number of vertices in a geometry.
  98. /// @property
  99. unsigned GetNumVertices(unsigned index) const;
  100. /// Return whether vertex buffer dynamic mode is enabled.
  101. /// @property
  102. bool IsDynamic() const { return dynamic_; }
  103. /// Return material by geometry index.
  104. /// @property{get_materials}
  105. Material* GetMaterial(unsigned index = 0) const;
  106. /// Return all vertices. These can be edited; calling Commit() updates the vertex buffer.
  107. Vector<PODVector<CustomGeometryVertex> >& GetVertices() { return vertices_; }
  108. /// Return a vertex in a geometry for editing, or null if out of bounds. After the edits are finished, calling Commit() updates the vertex buffer.
  109. CustomGeometryVertex* GetVertex(unsigned geometryIndex, unsigned vertexNum);
  110. /// Set geometry data attribute.
  111. void SetGeometryDataAttr(const PODVector<unsigned char>& value);
  112. /// Set materials attribute.
  113. void SetMaterialsAttr(const ResourceRefList& value);
  114. /// Return geometry data attribute.
  115. PODVector<unsigned char> GetGeometryDataAttr() const;
  116. /// Return materials attribute.
  117. const ResourceRefList& GetMaterialsAttr() const;
  118. protected:
  119. /// Recalculate the world-space bounding box.
  120. void OnWorldBoundingBoxUpdate() override;
  121. private:
  122. /// Primitive type per geometry.
  123. PODVector<PrimitiveType> primitiveTypes_;
  124. /// Source vertices per geometry.
  125. Vector<PODVector<CustomGeometryVertex> > vertices_;
  126. /// All geometries.
  127. Vector<SharedPtr<Geometry> > geometries_;
  128. /// Vertex buffer.
  129. SharedPtr<VertexBuffer> vertexBuffer_;
  130. /// Element mask used so far.
  131. VertexMaskFlags elementMask_;
  132. /// Current geometry being updated.
  133. unsigned geometryIndex_;
  134. /// Material list attribute.
  135. mutable ResourceRefList materialsAttr_;
  136. /// Vertex buffer dynamic flag.
  137. bool dynamic_;
  138. };
  139. }