fork download
  1. #include "CGridSceneNode.h"
  2.  
  3. using namespace irr;
  4.  
  5. CGridSceneNode::CGridSceneNode(ISceneNode* parent, scene::ISceneManager* smgr, s32 id,
  6. u32 spacing, u32 size, video::SColor gridcolor, u32 accentlineoffset,
  7. video::SColor accentgridcolor, bool axislinestate) : ISceneNode(parent, smgr, id),
  8. m_spacing(spacing), m_size(size),
  9. m_gridcolor(gridcolor), m_accentgridcolor(accentgridcolor),
  10. m_accentlineoffset(accentlineoffset), m_AxisLineState(axislinestate),
  11. m_XLineColor(video::SColor(255,255,0,0)), m_ZLineColor(video::SColor(255,0,0,255)), m_YLineColor(video::SColor(255,0,255,0)),
  12. m_buffer(0)
  13. {
  14. // Set the material
  15. m_buffer = new irr::scene::CDynamicMeshBuffer(irr::video::EVT_STANDARD, irr::video::EIT_16BIT);
  16. m_buffer->getMaterial().Wireframe = false;
  17. m_buffer->getMaterial().Lighting = false;
  18. m_buffer->getMaterial().Thickness = 1;
  19. m_buffer->getMaterial().FogEnable = false;
  20. m_buffer->getMaterial().ZWriteEnable = false;
  21. m_buffer->getMaterial().BackfaceCulling = true;
  22. m_buffer->getMaterial().AntiAliasing = true;
  23.  
  24. // Set the default culling state to Frustum Box
  25. AutomaticCullingState = scene::EAC_FRUSTUM_BOX;
  26.  
  27. RegenerateGrid();
  28. }
  29.  
  30. CGridSceneNode::~CGridSceneNode()
  31. {
  32. if ( m_buffer )
  33. m_buffer->drop();
  34. }
  35.  
  36. CGridSceneNode* CGridSceneNode::clone(scene::ISceneNode *newParent, scene::ISceneManager *newSceneManager)
  37. {
  38. if (!newParent) newParent = Parent;
  39. if (!newSceneManager) newSceneManager = SceneManager;
  40.  
  41. CGridSceneNode* clone = new CGridSceneNode(
  42. Parent,
  43. SceneManager,
  44. ID,
  45. m_spacing,
  46. m_size*2,
  47. m_gridcolor,
  48. m_accentlineoffset,
  49. m_accentgridcolor,
  50. m_AxisLineState);
  51.  
  52. if (!clone)
  53. return 0x0;
  54.  
  55. clone->SetAxisLineXColor(m_XLineColor);
  56. clone->SetAxisLineZColor(m_ZLineColor);
  57. clone->SetMaterial(m_buffer->getMaterial());
  58.  
  59. clone->drop();
  60. return clone;
  61. }
  62.  
  63. void CGridSceneNode::OnRegisterSceneNode()
  64. {
  65. if (IsVisible)
  66. SceneManager->registerNodeForRendering(this);
  67.  
  68. ISceneNode::OnRegisterSceneNode();
  69. }
  70.  
  71. void CGridSceneNode::RegenerateGrid()
  72. {
  73. //Clean up memory
  74. m_buffer->getIndexBuffer().set_used(0);
  75. m_buffer->getVertexBuffer().set_used(0);
  76.  
  77. u32 m_numVertices = ((m_size / m_spacing) + 1) * 2 * 2;
  78. if (m_accentlineoffset) m_numVertices += ((m_size / (m_spacing * m_accentlineoffset)) + 1) * 2 * 2;
  79.  
  80. if ( m_numVertices > 65535)
  81. {
  82. //Too many vertices on 16 bit for for 16bit indices of SMeshBuffer
  83. //Returning with a blank buffer to avoid segfaulting the entire application
  84. return;
  85. }
  86.  
  87. //Set our left corner
  88. core::vector3df leftMost = core::vector3df(0,0,0);
  89. leftMost.X -= m_size/2;
  90. leftMost.Z -= m_size/2;
  91.  
  92. //Set our right corner
  93. core::vector3df rightMost = core::vector3df(0,0,0);
  94. rightMost.X += m_size/2;
  95. rightMost.Z += m_size/2;
  96.  
  97. u32 indexIndex = 0;
  98.  
  99. //X-axis lines
  100. for (u32 x = 0; x <= m_size; x+= m_spacing)
  101. {
  102. core::vector3df start = leftMost;
  103. start.X += x ;
  104.  
  105. core::vector3df end = rightMost;
  106. end.X = start.X;
  107.  
  108. m_buffer->getVertexBuffer().push_back(video::S3DVertex(start, core::vector3df(0,1,0), m_gridcolor, core::vector2df(0.0f, 0.0f)));
  109. m_buffer->getVertexBuffer().push_back(video::S3DVertex(end, core::vector3df(0,1,0), m_gridcolor, core::vector2df(0.0f, 0.0f)));
  110.  
  111. m_buffer->getIndexBuffer().push_back(indexIndex++);
  112. m_buffer->getIndexBuffer().push_back(indexIndex++);
  113. }
  114.  
  115. //Z-axis lines
  116. for (u32 z = 0; z <= m_size; z+= m_spacing)
  117. {
  118. core::vector3df start = leftMost;
  119. start.Z += z ;
  120.  
  121. core::vector3df end = rightMost;
  122. end.Z = start.Z;
  123.  
  124. m_buffer->getVertexBuffer().push_back(video::S3DVertex(start, core::vector3df(0,1,0), m_gridcolor, core::vector2df(0.0f, 0.0f)));
  125. m_buffer->getVertexBuffer().push_back(video::S3DVertex(end, core::vector3df(0,1,0), m_gridcolor, core::vector2df(0.0f, 0.0f)));
  126.  
  127. m_buffer->getIndexBuffer().push_back(indexIndex++);
  128. m_buffer->getIndexBuffer().push_back(indexIndex++);
  129. }
  130.  
  131. //Accent lines are only drawn if the offset is greater than 0
  132. if (m_accentlineoffset > 0)
  133. {
  134. //X-axis
  135. for (u32 x = 0; x <= m_size; x+= m_spacing*m_accentlineoffset)
  136. {
  137. core::vector3df start = leftMost;
  138. start.X += x ;
  139.  
  140. core::vector3df end = rightMost;
  141. end.X = start.X;
  142.  
  143. m_buffer->getVertexBuffer().push_back(video::S3DVertex(start, core::vector3df(0,1,0), m_accentgridcolor, core::vector2df(0.0f, 0.0f)));
  144. m_buffer->getVertexBuffer().push_back(video::S3DVertex(end, core::vector3df(0,1,0), m_accentgridcolor, core::vector2df(0.0f, 0.0f)));
  145.  
  146. m_buffer->getIndexBuffer().push_back(indexIndex++);
  147. m_buffer->getIndexBuffer().push_back(indexIndex++);
  148. }
  149.  
  150. //Z-axis
  151. for (u32 z = 0; z <= m_size; z+= m_spacing*m_accentlineoffset)
  152. {
  153. core::vector3df start = leftMost;
  154. start.Z += z ;
  155.  
  156. core::vector3df end = rightMost;
  157. end.Z = start.Z;
  158. m_buffer->getVertexBuffer().push_back(video::S3DVertex(start, core::vector3df(0,1,0), m_accentgridcolor, core::vector2df(0.0f, 0.0f)));
  159. m_buffer->getVertexBuffer().push_back(video::S3DVertex(end, core::vector3df(0,1,0), m_accentgridcolor, core::vector2df(0.0f, 0.0f)));
  160.  
  161. m_buffer->getIndexBuffer().push_back(indexIndex++);
  162. m_buffer->getIndexBuffer().push_back(indexIndex++);
  163. }
  164. }
  165.  
  166.  
  167. // Create our box, it is the size of the grid exactly, plus 1 in the Y axis
  168. m_buffer->setBoundingBox( core::aabbox3df(-(f32)m_size/2,-0.5f,-(f32)m_size/2,(f32)m_size/2,0.5f,(f32)m_size/2) );
  169. }
  170.  
  171. void CGridSceneNode::render()
  172. {
  173. video::IVideoDriver* driver = SceneManager->getVideoDriver();
  174.  
  175. //Prep to render
  176. if (driver)
  177. {
  178. driver->setMaterial(m_buffer->getMaterial());
  179. driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
  180.  
  181. driver->drawVertexPrimitiveList(m_buffer->getVertexBuffer().getData(), // const void* vertices
  182. m_buffer->getVertexBuffer().size(), //u32 vertexCount
  183. m_buffer->getIndexBuffer().getData(), // const void* indexList
  184. m_buffer->getVertexBuffer().size()/2, // u32 primCount
  185. m_buffer->getVertexType(), // E_VERTEX_TYPE vType
  186. scene::EPT_LINES, // scene::E_PRIMITIVE_TYPE pType
  187. m_buffer->getIndexType() ); // E_INDEX_TYPE iType
  188.  
  189. // Axis Lines are only drawn if the State is true
  190. if (m_AxisLineState)
  191. {
  192. m_buffer->getMaterial().Thickness = 3;
  193. driver->setMaterial(m_buffer->getMaterial());
  194. driver->draw3DLine(core::vector3df(0,0,0),core::vector3df(0.5f,0,0), m_XLineColor);
  195. driver->draw3DLine(core::vector3df(0,0,0),core::vector3df(0,0.5f,0), m_YLineColor);
  196. driver->draw3DLine(core::vector3df(0,0,0),core::vector3df(0,0,0.5f), m_ZLineColor);
  197. m_buffer->getMaterial().Thickness = 1;
  198. driver->setMaterial(m_buffer->getMaterial());
  199. }
  200. }
  201. }
  202.  
  203. const core::aabbox3d<f32>& CGridSceneNode::getBoundingBox() const
  204. {
  205. return m_buffer->getBoundingBox();
  206. }
  207.  
  208. u32 CGridSceneNode::getMaterialCount() const
  209. {
  210. return 1;
  211. }
  212.  
  213. video::SMaterial& CGridSceneNode::getMaterial(u32 i)
  214. {
  215. return m_buffer->getMaterial();
  216. }
  217.  
  218. u32 CGridSceneNode::GetSpacing()
  219. {
  220. return m_spacing;
  221. }
  222.  
  223. u32 CGridSceneNode::GetSize()
  224. {
  225. return m_size;
  226. }
  227.  
  228. u32 CGridSceneNode::GetAccentlineOffset()
  229. {
  230. return m_accentlineoffset;
  231. }
  232.  
  233. video::SColor CGridSceneNode::GetAccentlineColor()
  234. {
  235. return m_accentgridcolor;
  236. }
  237.  
  238. video::SColor CGridSceneNode::GetGridColor()
  239. {
  240. return m_gridcolor;
  241. }
  242.  
  243. bool CGridSceneNode::AreAxisLineActive()
  244. {
  245. return m_AxisLineState;
  246. }
  247.  
  248. video::SColor CGridSceneNode::GetAxisLineXColor()
  249. {
  250. return m_XLineColor;
  251. }
  252.  
  253. video::SColor CGridSceneNode::GetAxisLineZColor()
  254. {
  255. return m_ZLineColor;
  256. }
  257.  
  258. void CGridSceneNode::SetSpacing(u32 newspacing)
  259. {
  260. m_spacing = newspacing;
  261. RegenerateGrid();
  262. }
  263.  
  264. void CGridSceneNode::SetSize(u32 newsize)
  265. {
  266. m_size = newsize;
  267. RegenerateGrid();
  268. }
  269.  
  270. void CGridSceneNode::SetAccentlineColor(video::SColor newcolor)
  271. {
  272. m_accentgridcolor = newcolor;
  273. RegenerateGrid();
  274. }
  275.  
  276. void CGridSceneNode::SetAccentlineOffset(u32 newoffset)
  277. {
  278. m_accentlineoffset = newoffset;
  279. RegenerateGrid();
  280. }
  281.  
  282. void CGridSceneNode::SetGridColor(video::SColor newcolor)
  283. {
  284. m_gridcolor = newcolor;
  285. RegenerateGrid();
  286. }
  287.  
  288. void CGridSceneNode::SetAxisLineActive(bool active)
  289. {
  290. m_AxisLineState = active;
  291. }
  292.  
  293. void CGridSceneNode::SetAxisLineXColor(video::SColor XLine)
  294. {
  295. m_XLineColor = XLine;
  296. }
  297.  
  298. void CGridSceneNode::SetAxisLineZColor(video::SColor ZLine)
  299. {
  300. m_ZLineColor = ZLine;
  301. }
  302.  
  303. void CGridSceneNode::SetMaterial(video::SMaterial newMaterial)
  304. {
  305. m_buffer->getMaterial() = newMaterial;
  306. }
  307.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty