CustomGeometry.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  25. {
  26. /// Custom geometry vertex.
  27. struct CustomGeometryVertex
  28. {
  29. /// Position.
  30. Vector3 position_;
  31. /// Normal.
  32. Vector3 normal_;
  33. /// Color.
  34. unsigned color_;
  35. /// Texture coordinates.
  36. Vector2 texCoord_;
  37. /// Tangent.
  38. Vector4 tangent_;
  39. };
  40. class VertexBuffer;
  41. /// Custom geometry component.
  42. class ATOMIC_API CustomGeometry : public Drawable
  43. {
  44. ATOMIC_OBJECT(CustomGeometry, Drawable);
  45. public:
  46. /// Construct.
  47. CustomGeometry(Context* context);
  48. /// Destruct.
  49. virtual ~CustomGeometry();
  50. /// Register object factory. Drawable must be registered first.
  51. static void RegisterObject(Context* context);
  52. /// Process octree raycast. May be called from a worker thread.
  53. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  54. /// Return the geometry for a specific LOD level.
  55. virtual Geometry* GetLodGeometry(unsigned batchIndex, unsigned level);
  56. /// Return number of occlusion geometry triangles.
  57. virtual unsigned GetNumOccluderTriangles();
  58. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  59. virtual bool DrawOcclusion(OcclusionBuffer* buffer);
  60. /// Clear all geometries.
  61. void Clear();
  62. /// Set number of geometries.
  63. void SetNumGeometries(unsigned num);
  64. /// Set vertex buffer dynamic mode. A dynamic buffer should be faster to update frequently. Effective at the next Commit() call.
  65. void SetDynamic(bool enable);
  66. /// Begin defining a geometry. Clears existing vertices in that index.
  67. void BeginGeometry(unsigned index, PrimitiveType type);
  68. /// Define a vertex position. This begins a new vertex.
  69. void DefineVertex(const Vector3& position);
  70. /// Define a vertex normal.
  71. void DefineNormal(const Vector3& normal);
  72. /// Define a vertex color.
  73. void DefineColor(const Color& color);
  74. /// Define a vertex UV coordinate.
  75. void DefineTexCoord(const Vector2& texCoord);
  76. /// Define a vertex tangent.
  77. void DefineTangent(const Vector4& tangent);
  78. /// 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().
  79. void DefineGeometry
  80. (unsigned index, PrimitiveType type, unsigned numVertices, bool hasNormals, bool hasColors, bool hasTexCoords,
  81. bool hasTangents);
  82. /// Update vertex buffer and calculate the bounding box. Call after finishing defining geometry.
  83. void Commit();
  84. /// Set material on all geometries.
  85. void SetMaterial(Material* material);
  86. /// Set material on one geometry. Return true if successful.
  87. bool SetMaterial(unsigned index, Material* material);
  88. /// Return number of geometries.
  89. unsigned GetNumGeometries() const { return geometries_.Size(); }
  90. /// Return number of vertices in a geometry.
  91. unsigned GetNumVertices(unsigned index) const;
  92. /// Return whether vertex buffer dynamic mode is enabled.
  93. bool IsDynamic() const { return dynamic_; }
  94. /// Return material by geometry index.
  95. Material* GetMaterial(unsigned index = 0) const;
  96. /// Return all vertices. These can be edited; calling Commit() updates the vertex buffer.
  97. Vector<PODVector<CustomGeometryVertex> >& GetVertices() { return vertices_; }
  98. /// 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.
  99. CustomGeometryVertex* GetVertex(unsigned geometryIndex, unsigned vertexNum);
  100. /// Set geometry data attribute.
  101. void SetGeometryDataAttr(const PODVector<unsigned char>& value);
  102. /// Set materials attribute.
  103. void SetMaterialsAttr(const ResourceRefList& value);
  104. /// Return geometry data attribute.
  105. PODVector<unsigned char> GetGeometryDataAttr() const;
  106. /// Return materials attribute.
  107. const ResourceRefList& GetMaterialsAttr() const;
  108. protected:
  109. /// Recalculate the world-space bounding box.
  110. virtual void OnWorldBoundingBoxUpdate();
  111. private:
  112. /// Primitive type per geometry.
  113. PODVector<PrimitiveType> primitiveTypes_;
  114. /// Source vertices per geometry.
  115. Vector<PODVector<CustomGeometryVertex> > vertices_;
  116. /// All geometries.
  117. Vector<SharedPtr<Geometry> > geometries_;
  118. /// Vertex buffer.
  119. SharedPtr<VertexBuffer> vertexBuffer_;
  120. /// Element mask used so far.
  121. unsigned elementMask_;
  122. /// Current geometry being updated.
  123. unsigned geometryIndex_;
  124. /// Material list attribute.
  125. mutable ResourceRefList materialsAttr_;
  126. /// Vertex buffer dynamic flag.
  127. bool dynamic_;
  128. };
  129. }