CustomGeometry.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // Copyright (c) 2008-2013 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. /// Custom geometry component.
  41. class CustomGeometry : public Drawable
  42. {
  43. OBJECT(CustomGeometry);
  44. public:
  45. /// Construct.
  46. CustomGeometry(Context* context);
  47. /// Destruct.
  48. virtual ~CustomGeometry();
  49. /// Register object factory. Drawable must be registered first.
  50. static void RegisterObject(Context* context);
  51. /// Process octree raycast. May be called from a worker thread.
  52. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  53. /// Return the geometry for a specific LOD level.
  54. virtual Geometry* GetLodGeometry(unsigned batchIndex, unsigned level);
  55. /// Return number of occlusion geometry triangles.
  56. virtual unsigned GetNumOccluderTriangles();
  57. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  58. virtual bool DrawOcclusion(OcclusionBuffer* buffer);
  59. /// Clear all geometries.
  60. void Clear();
  61. /// Set number of geometries.
  62. void SetNumGeometries(unsigned num);
  63. /// Begin defining a geometry. Clears existing geometry in that index.
  64. void BeginGeometry(unsigned index, PrimitiveType type);
  65. /// Define a vertex position. This begins a new vertex.
  66. void DefineVertex(const Vector3& position);
  67. /// Define a vertex normal.
  68. void DefineNormal(const Vector3& normal);
  69. /// Define a vertex tangent.
  70. void DefineTangent(const Vector4& tangent);
  71. /// Define a vertex color.
  72. void DefineColor(const Color& color);
  73. /// Define a vertex UV coordinate.
  74. void DefineTexCoord(const Vector2& texCoord);
  75. /// Commit all geometries to the vertex buffer and calculate the bounding box.
  76. void Commit();
  77. /// Set material on all geometries.
  78. void SetMaterial(Material* material);
  79. /// Set material on one geometry. Return true if successful.
  80. bool SetMaterial(unsigned index, Material* material);
  81. /// Return local space bounding box.
  82. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  83. /// Return number of geometries.
  84. unsigned GetNumGeometries() const { return geometries_.Size(); }
  85. /// Return material by geometry index.
  86. Material* GetMaterial(unsigned index) const;
  87. protected:
  88. /// Recalculate the world-space bounding box.
  89. virtual void OnWorldBoundingBoxUpdate();
  90. private:
  91. /// Local-space bounding box.
  92. BoundingBox boundingBox_;
  93. /// Primitive type per geometry.
  94. PODVector<PrimitiveType> primitiveTypes_;
  95. /// Source vertices per geometry.
  96. Vector<PODVector<CustomGeometryVertex> > vertices_;
  97. /// All geometries.
  98. Vector<SharedPtr<Geometry> > geometries_;
  99. /// Vertex buffer.
  100. SharedPtr<VertexBuffer> vertexBuffer_;
  101. /// Element mask used so far.
  102. unsigned elementMask_;
  103. /// Current geometry being updated.
  104. unsigned geometryIndex_;
  105. };
  106. }