CustomObject.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef RENDERER_CUSTOMOBJECT_H
  24. #define RENDERER_CUSTOMOBJECT_H
  25. #include "GeometryNode.h"
  26. #include "SharedArrayPtr.h"
  27. class IndexBuffer;
  28. class VertexBuffer;
  29. //! Custom geometry definition
  30. class CustomGeometry
  31. {
  32. public:
  33. //! Construct with empty geometry
  34. CustomGeometry() :
  35. mVertexCount(0),
  36. mVertexSize(0),
  37. mIndexCount(0),
  38. mIndexSize(sizeof(unsigned short))
  39. {
  40. }
  41. //! Return ray hit distance, or infinity if no hit
  42. float getDistance(const Ray& ray) const;
  43. //! Vertex data
  44. SharedArrayPtr<unsigned char> mVertexData;
  45. //! Index data
  46. SharedArrayPtr<unsigned char> mIndexData;
  47. //! Material
  48. SharedPtr<Material> mMaterial;
  49. //! Bounding box
  50. BoundingBox mBoundingBox;
  51. //! Number of vertices
  52. unsigned mVertexCount;
  53. //! Vertex size
  54. unsigned mVertexSize;
  55. //! Number of indices
  56. unsigned mIndexCount;
  57. //! Index size
  58. unsigned mIndexSize;
  59. };
  60. //! Scene node which defines custom geometry
  61. class CustomObject : public GeometryNode
  62. {
  63. DEFINE_TYPE(CustomObject);
  64. public:
  65. //! Construct with initial octant pointer and name
  66. CustomObject(Octant* octant = 0, const std::string& name = std::string());
  67. //! Destruct
  68. virtual ~CustomObject();
  69. //! Write component state to a stream
  70. virtual void save(Serializer& dest);
  71. //! Read component state from a stream
  72. virtual void load(Deserializer& source, ResourceCache* cache);
  73. //! Write component state to an XML element
  74. virtual void saveXML(XMLElement& dest);
  75. //! Read component state from an XML element
  76. virtual void loadXML(const XMLElement& source, ResourceCache* cache);
  77. //! Return resource references
  78. virtual void getResourceRefs(std::vector<Resource*>& dest);
  79. //! Process renderer raycast
  80. virtual void processRayQuery(RayOctreeQuery& query, float initialDistance);
  81. //! Prepare geometry for rendering
  82. virtual void updateGeometry(const FrameInfo& frame, Renderer* renderer);
  83. //! Return number of batches
  84. virtual unsigned getNumBatches();
  85. //! Return geometry by batch index
  86. virtual Geometry* getBatchGeometry(unsigned batchIndex);
  87. //! Return material by batch index
  88. virtual Material* getBatchMaterial(unsigned batchIndex);
  89. //! Draw to occlusion buffer
  90. virtual bool drawOcclusion(OcclusionBuffer* buffer);
  91. //! Set vertex data elements
  92. bool setVertexElementMask(unsigned mask);
  93. //! Set number of custom geometries
  94. void setNumGeometries(unsigned num);
  95. //! Set geometry vertex and index data
  96. bool setGeometryData(unsigned index, const void* vertexData, unsigned vertexCount, const void* indexData, unsigned indexCount, bool largeIndices = false);
  97. //! Set material of all geometries
  98. void setMaterial(Material* material);
  99. //! Set material of a geometry
  100. bool setMaterial(unsigned index, Material* material);
  101. //! Set optimization mode (combine geometries if same material)
  102. void setOptimization(bool enable);
  103. //! Return bounding box
  104. const BoundingBox& getBoundingBox() const { return mBoundingBox; }
  105. //! Return vertex element mask
  106. unsigned getVertexElementMask() const { return mVertexElementMask; }
  107. //! Return number of geometries
  108. unsigned getNumGeometries() const { return mCustomGeometries.size(); }
  109. //! Return geometry by index
  110. const CustomGeometry* getGeometry(unsigned index) const;
  111. //! Return material by index
  112. Material* getMaterial(unsigned index) const;
  113. //! Return optimization mode
  114. bool getOptimization() const { return mOptimization; }
  115. protected:
  116. //! Update world-space bounding box
  117. virtual void onWorldBoundingBoxUpdate(BoundingBox& worldBoundingBox);
  118. private:
  119. //! Calculate bounding box
  120. void calculateBoundingBox();
  121. //! Update vertex and index buffer
  122. void updateBuffer(Renderer* renderer);
  123. //! Custom geometry definitions
  124. std::vector<CustomGeometry> mCustomGeometries;
  125. //! Geometries
  126. std::vector<SharedPtr<Geometry> > mGeometries;
  127. //! Materials
  128. std::vector<SharedPtr<Material> > mMaterials;
  129. //! Bounding box
  130. BoundingBox mBoundingBox;
  131. //! Vertex buffer
  132. SharedPtr<VertexBuffer> mVertexBuffer;
  133. //! Index buffer
  134. SharedPtr<IndexBuffer> mIndexBuffer;
  135. //! Vertex data elements
  136. unsigned mVertexElementMask;
  137. //! Buffers need update flag
  138. bool mGeometriesDirty;
  139. //! Optimization flag
  140. bool mOptimization;
  141. };
  142. #endif // RENDERER_CUSTOMOBJECT_H