CustomGeometry.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // Copyright (c) 2008-2014 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 "Drawable.h"
  24. namespace Urho3D
  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 URHO3D_API CustomGeometry : public Drawable
  43. {
  44. OBJECT(CustomGeometry);
  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. /// Begin defining a geometry. Clears existing geometry in that index.
  65. void BeginGeometry(unsigned index, PrimitiveType type);
  66. /// Define a vertex position. This begins a new vertex.
  67. void DefineVertex(const Vector3& position);
  68. /// Define a vertex normal.
  69. void DefineNormal(const Vector3& normal);
  70. /// Define a vertex tangent.
  71. void DefineTangent(const Vector4& tangent);
  72. /// Define a vertex color.
  73. void DefineColor(const Color& color);
  74. /// Define a vertex UV coordinate.
  75. void DefineTexCoord(const Vector2& texCoord);
  76. /// Update vertex buffer and calculate the bounding box. Call after finishing defining geometry.
  77. void Commit();
  78. /// Set material on all geometries.
  79. void SetMaterial(Material* material);
  80. /// Set material on one geometry. Return true if successful.
  81. bool SetMaterial(unsigned index, Material* material);
  82. /// Return number of geometries.
  83. unsigned GetNumGeometries() const { return geometries_.Size(); }
  84. /// Return material by geometry index.
  85. Material* GetMaterial(unsigned index) const;
  86. /// Set geometry data attribute.
  87. void SetGeometryDataAttr(PODVector<unsigned char> value);
  88. /// Set materials attribute.
  89. void SetMaterialsAttr(const ResourceRefList& value);
  90. /// Return geometry data attribute.
  91. PODVector<unsigned char> GetGeometryDataAttr() const;
  92. /// Return materials attribute.
  93. const ResourceRefList& GetMaterialsAttr() const;
  94. protected:
  95. /// Recalculate the world-space bounding box.
  96. virtual void OnWorldBoundingBoxUpdate();
  97. private:
  98. /// Primitive type per geometry.
  99. PODVector<PrimitiveType> primitiveTypes_;
  100. /// Source vertices per geometry.
  101. Vector<PODVector<CustomGeometryVertex> > vertices_;
  102. /// All geometries.
  103. Vector<SharedPtr<Geometry> > geometries_;
  104. /// Vertex buffer.
  105. SharedPtr<VertexBuffer> vertexBuffer_;
  106. /// Element mask used so far.
  107. unsigned elementMask_;
  108. /// Current geometry being updated.
  109. unsigned geometryIndex_;
  110. /// Material list attribute.
  111. mutable ResourceRefList materialsAttr_;
  112. };
  113. }