CustomGeometry.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // Copyright (c) 2008-2020 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. /// @fakeref
  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. static void RegisterObject(Context* context);
  53. /// Process octree raycast. May be called from a worker thread.
  54. void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results) override;
  55. /// Return the geometry for a specific LOD level.
  56. Geometry* GetLodGeometry(unsigned batchIndex, unsigned level) override;
  57. /// Return number of occlusion geometry triangles.
  58. unsigned GetNumOccluderTriangles() override;
  59. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  60. bool DrawOcclusion(OcclusionBuffer* buffer) override;
  61. // TODO: Extract this code from CustomGeometry
  62. // {
  63. /// Geometry Shape Helper Functions (normal defaults to (0,1,0)
  64. /// Shape Creation Functions, can be used as input into protrude shape
  65. static Vector<Vector3> GetCircleShape(float radius = 1, size_t iterations = 100, float startTheta = 0, float endTheta = 2 * M_PI);
  66. static Vector<Vector3> GetSquareShape(float size);
  67. /// Make the custom geometry into a circle, change start and stop to make a segment instead
  68. /// Set clear=false and geomNum for making multiple circle segments
  69. void MakeCircle(float radius = 1, size_t iterations = 300, float startTheta = 0, float endTheta = 2 * M_PI,
  70. bool clear = true, int geomNum = 0);
  71. /// Draws a shape generated by connecting the points in the input vector (the end point will connect to the start)
  72. void MakeShape(const Vector<Urho3D::Vector3>& pointList, bool connectTail = true);
  73. /// Makes this custom geometry into a square shape
  74. void MakeSquare(float size);
  75. /// Produces a circle graph given a vector of paired (weights and materials)
  76. void MakeCircleGraph(const Vector<Pair<float, Urho3D::SharedPtr<Urho3D::Material> > >& parts,
  77. int radius = 1, int iterations = 300);
  78. void MakeSphere(float radius = 1, size_t iterations = 200);
  79. /// Protrode a shape along a line (note: the input vector must be a complete shape with the tail connecting to head)
  80. /// This function makes this object into the newly generated 3D mesh (works best if the line (point list) is also complete)
  81. void ProtrudeShape(const Vector<Vector3> &mShapeList, const Vector<Vector3> &mPointList, bool connectTail = false);
  82. /// Helper Function Creating 3D Meshes (connectTail = true), connect the endLinePoint to the start)
  83. void CreateQuadsFromBuffer(const Vector<Vector3>& pointList, size_t zIterations, size_t thetaIterations, bool connectTail = false);
  84. /// Fills a point List shape with triangles
  85. void FillShape(const Vector<Vector3>& shapeList, bool connectTail = true, bool clear = true, int geomNum = 0);
  86. /// End Shape Generation Helper Functions
  87. // }
  88. /// Clear all geometries.
  89. void Clear();
  90. /// Set number of geometries.
  91. /// @property
  92. void SetNumGeometries(unsigned num);
  93. /// Set vertex buffer dynamic mode. A dynamic buffer should be faster to update frequently. Effective at the next Commit() call.
  94. /// @property
  95. void SetDynamic(bool enable);
  96. /// Begin defining a geometry. Clears existing vertices in that index.
  97. void BeginGeometry(unsigned index, PrimitiveType type);
  98. /// Define a vertex position. This begins a new vertex.
  99. void DefineVertex(const Vector3& position);
  100. /// Define a vertex normal.
  101. void DefineNormal(const Vector3& normal);
  102. /// Define a vertex color.
  103. void DefineColor(const Color& color);
  104. /// Define a vertex UV coordinate.
  105. void DefineTexCoord(const Vector2& texCoord);
  106. /// Define a vertex tangent.
  107. void DefineTangent(const Vector4& tangent);
  108. /// 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().
  109. void DefineGeometry
  110. (unsigned index, PrimitiveType type, unsigned numVertices, bool hasNormals, bool hasColors, bool hasTexCoords,
  111. bool hasTangents);
  112. /// Update vertex buffer and calculate the bounding box. Call after finishing defining geometry.
  113. void Commit();
  114. /// Set material on all geometries.
  115. /// @property
  116. void SetMaterial(Material* material);
  117. /// Set material on one geometry. Return true if successful.
  118. /// @property{set_materials}
  119. bool SetMaterial(unsigned index, Material* material);
  120. /// Return number of geometries.
  121. /// @property
  122. unsigned GetNumGeometries() const { return geometries_.Size(); }
  123. /// Return number of vertices in a geometry.
  124. /// @property
  125. unsigned GetNumVertices(unsigned index) const;
  126. /// Return whether vertex buffer dynamic mode is enabled.
  127. /// @property
  128. bool IsDynamic() const { return dynamic_; }
  129. /// Return material by geometry index.
  130. /// @property{get_materials}
  131. Material* GetMaterial(unsigned index = 0) const;
  132. /// Return all vertices. These can be edited; calling Commit() updates the vertex buffer.
  133. Vector<PODVector<CustomGeometryVertex> >& GetVertices() { return vertices_; }
  134. /// 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.
  135. CustomGeometryVertex* GetVertex(unsigned geometryIndex, unsigned vertexNum);
  136. /// Set geometry data attribute.
  137. void SetGeometryDataAttr(const PODVector<unsigned char>& value);
  138. /// Set materials attribute.
  139. void SetMaterialsAttr(const ResourceRefList& value);
  140. /// Return geometry data attribute.
  141. PODVector<unsigned char> GetGeometryDataAttr() const;
  142. /// Return materials attribute.
  143. const ResourceRefList& GetMaterialsAttr() const;
  144. protected:
  145. /// Recalculate the world-space bounding box.
  146. void OnWorldBoundingBoxUpdate() override;
  147. private:
  148. /// Primitive type per geometry.
  149. PODVector<PrimitiveType> primitiveTypes_;
  150. /// Source vertices per geometry.
  151. Vector<PODVector<CustomGeometryVertex> > vertices_;
  152. /// All geometries.
  153. Vector<SharedPtr<Geometry> > geometries_;
  154. /// Vertex buffer.
  155. SharedPtr<VertexBuffer> vertexBuffer_;
  156. /// Element mask used so far.
  157. VertexMaskFlags elementMask_;
  158. /// Current geometry being updated.
  159. unsigned geometryIndex_;
  160. /// Material list attribute.
  161. mutable ResourceRefList materialsAttr_;
  162. /// Vertex buffer dynamic flag.
  163. bool dynamic_;
  164. };
  165. }