fork(5) download
  1. #ifndef __C_GRID_SCENE_NODE_H__
  2. #define __C_GRID_SCENE_NODE_H__
  3.  
  4. #include "irrlicht.h"
  5.  
  6. //! Grid scene node
  7. /*! If you need a grid on the XY or ZY axis, simply rotate this node by 90
  8. degrees in the appropiate axis.
  9. This node creates an XZ grid by default, which should be fine for normal use.
  10. Axis Lines are a default Red and Blue for the X and Z axis respectively.
  11.  
  12. Please note that the internal meshbuffer used for the grid has a max size of 65535 indices.
  13.  
  14. Code is based on the class from here: http://i...content-available-to-author-only...e.net/forum/viewtopic.php?f=9&t=24938
  15. Thanks goes to MasterGod for helping to clean up the code and for a few bug fixes.
  16.  
  17. Additional thanks to:
  18. JP for optimising the rendering.
  19. Vins for fixing a nasty crash bug and optimising memory usage.
  20. */
  21.  
  22. class CGridSceneNode : public irr::scene::ISceneNode
  23. {
  24. public:
  25.  
  26. enum EAlign
  27. {
  28. CENTER,
  29. MIN_X_MIN_Y
  30. };
  31.  
  32. struct SGrid
  33. {
  34. friend class CGridSceneNode;
  35.  
  36. SGrid();
  37. ~SGrid();
  38.  
  39. SGrid(const SGrid& other);
  40. SGrid& operator=(const SGrid& other);
  41.  
  42. void setVisible(bool visible);
  43. bool isVisible() const;
  44.  
  45. //! If size/spacing doesn't have a modulo of 0 we need to align the grid inside the given size
  46. void setAlignment(EAlign align);
  47.  
  48. //! return grid alignment inside space
  49. EAlign getAlignment() const;
  50.  
  51. //! Sets Spacing
  52. void setSpacing(irr::f32 newspacing);
  53.  
  54. //! Returns the Spacing of the grid
  55. irr::f32 getSpacing() const;
  56.  
  57. //! Sets size
  58. void setSize(const irr::core::dimension2df& newsize);
  59.  
  60. //! Returns the total size of the grid
  61. const irr::core::dimension2df& getSize() const;
  62.  
  63. //! lines are drawn with some offset
  64. void setOffset(const irr::core::vector3df& offset);
  65.  
  66. //! get line offset
  67. const irr::core::vector3df& getOffset() const;
  68.  
  69. //! Sets the general grid color
  70. void setGridColor(irr::video::SColor newcolor);
  71.  
  72. //! Returns the grid color
  73. irr::video::SColor getGridColor() const;
  74.  
  75. //! Set the maximal distance the grid can have to the camera to still be rendered
  76. void setMaxRenderDistance(irr::f32 dist);
  77.  
  78. //! Get the maximal distance the grid can have to the camera to still be rendered
  79. irr::f32 getMaxRenderDistance() const;
  80.  
  81. //! Set a new material
  82. void setMaterial(const irr::video::SMaterial& newMaterial);
  83. const irr::video::SMaterial& getMaterial() const;
  84. irr::video::SMaterial& getMaterial();
  85.  
  86. // get all grid-lines (tranformed when a matrix is passed as well)
  87. void getGridLines(irr::core::array<irr::core::line2df> &lines, const irr::core::matrix4* transformation=0) const;
  88.  
  89. const irr::core::aabbox3d<irr::f32>& getBoundingBox() const;
  90.  
  91. protected:
  92. //! rebuild the grid
  93. void regenerateMesh(const CGridSceneNode* const gridNode);
  94. void setDefaultMaterial();
  95.  
  96. // render this grid
  97. void render(irr::video::IVideoDriver* driver);
  98.  
  99. bool canUseGridLine(irr::f32 pos, bool axisX, const CGridSceneNode* const gridNode);
  100.  
  101. // First and last line of a grid are for each axis usually a bit inside the grid unless the
  102. // grid-size divides exactly by the Spacing. This gives the corners of that inner grid.
  103. void calcGridInside(irr::core::vector2df& leftTop, irr::core::vector2df& rightBottom) const;
  104.  
  105. bool IsVisible;
  106. EAlign Alignment;
  107. irr::f32 Spacing;
  108. irr::core::dimension2df Size;
  109. irr::core::vector3df Offset;
  110. irr::video::SColor GridColor;
  111. irr::f32 MaxRenderDist; // grid not rendered when distance of the gridplane to the camera is larger
  112. bool GridDirty; // grid settings have been changed and all grids need to regenerate the mesh.
  113. bool BoundingBoxDirty; // boundingbox was changed, CGridSceneNode needs to recalculate it's boundingbox
  114. irr::scene::IDynamicMeshBuffer * MeshBuffer;
  115. };
  116.  
  117. CGridSceneNode(irr::scene::ISceneNode* parent, irr::scene::ISceneManager* smgr, irr::s32 id = -1, irr::u32 numGrids=1);
  118. ~CGridSceneNode();
  119.  
  120. //! Will create a copy of this scenenode and it's settings
  121. virtual CGridSceneNode* clone(irr::scene::ISceneNode* newParent = 0, irr::scene::ISceneManager* newSceneManager = 0);
  122.  
  123. //! Pre-Register stuff
  124. virtual void OnRegisterSceneNode();
  125.  
  126. //! Render our grid using 3D lines stored inside the internal meshbuffer
  127. virtual void render();
  128.  
  129. //! Returns our bounding box
  130. virtual const irr::core::aabbox3d<irr::f32>& getBoundingBox() const;
  131.  
  132. //! Returns the total number of materials (identical to number of grids)
  133. virtual irr::u32 getMaterialCount() const;
  134.  
  135. //! Returns the only material
  136. virtual irr::video::SMaterial& getMaterial(irr::u32 i);
  137.  
  138. // ! get the number of grids in this node
  139. irr::u32 getNumberOfGrids() const;
  140.  
  141. //! add a new grid and return it's index
  142. irr::u32 addGrid();
  143.  
  144. //! remove an existing grid
  145. void removeGrid(irr::u32 index);
  146.  
  147. //! access a grid
  148. //* Note: reference no longer valid when grids are added/removed */
  149. SGrid& getGrid(irr::u32 index);
  150.  
  151. //! access a grid
  152. //* Note: reference no longer valid when grids are added/removed */
  153. const SGrid& getGrid(irr::u32 index) const;
  154.  
  155. //! Sets the size for all grids
  156. void setGridsSize(const irr::core::dimension2df& newsize);
  157.  
  158. //! Get the size used in setGridsSize
  159. //! Note that individual grids can overwrite that size
  160. const irr::core::dimension2df& getGridsSize() const;
  161.  
  162. //! For debugging: show the selection buffer on next render
  163. void showSelectionOnce()
  164. {
  165. ShowSelectionOnce = true;
  166. }
  167.  
  168. protected:
  169. bool hasDirtyBoundingBox() const;
  170. bool hasDirtyGrid() const;
  171. void regenerateMeshes();
  172. void rebuildBoundingBox();
  173. void rebuildSelectionMesh();
  174.  
  175. // automatic enabling/disabling of grids based on distance of the plane to the camera
  176. void setGridVisibilityByDistToCamera();
  177.  
  178. private:
  179. irr::scene::SMesh* SelectionMesh;
  180. irr::scene::IDynamicMeshBuffer * SelectionMeshBuffer;
  181. bool ShowSelectionOnce;
  182. irr::core::aabbox3d<irr::f32> BoundingBox;
  183. irr::core::dimension2df GridsSize;
  184. irr::core::array<SGrid> Grids;
  185. };
  186.  
  187. #endif // __C_GRID_SCENE_NODE_H__
  188.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty