fork download
  1. #include "EDE_GridSceneNode.h"
  2. #include <stdio.h>
  3.  
  4.  
  5. /*
  6.   ##############################
  7. # Setting Up the Cell Struct #
  8. ##############################
  9. */
  10.  
  11. EDE_GridSceneNode::Cell::Cell()
  12. {
  13. this->topLeft = vector3df();
  14. this->topRight = vector3df();
  15. this->bottomLeft = vector3df();
  16. this->bottomRight = vector3df();
  17. this->id = 0;
  18. this->visible = false;
  19. }
  20.  
  21. EDE_GridSceneNode::Cell::Cell(vector3df topLeft, vector3df topRight, vector3df bottomLeft, vector3df bottomRight, int id, bool visible, SMaterial material)
  22. {
  23. this->id = id;
  24. this->visible = visible;
  25.  
  26. this->bottomLeft = bottomLeft;
  27. this->bottomRight = bottomRight;
  28. this->topLeft = topLeft;
  29. this->topRight = topRight;
  30. this->center = (bottomLeft + bottomRight + topLeft + topRight) / 4;
  31. this->Material = material;
  32. }
  33.  
  34. EDE_GridSceneNode::Cell::~Cell()
  35. {
  36.  
  37. }
  38.  
  39. void EDE_GridSceneNode::Cell::RenderCell(IVideoDriver* driver, SColor color = SColor(255, 255, 255, 255))
  40. {
  41. if (!visible)
  42. return;
  43.  
  44.  
  45. driver->setMaterial(this->Material);
  46. driver->setTransform(ETS_WORLD, matrix4());
  47. // Draw the Box
  48. driver->draw3DLine(topLeft, topRight, color); // TopLeft -> TopRight
  49. driver->draw3DLine(bottomLeft, bottomRight, color); // BottomLeft -> BottomRight
  50. driver->draw3DLine(topLeft, bottomLeft, color); // TopLeft -> BottomLeft
  51. driver->draw3DLine(topRight, bottomRight, color); // TopRight -> BottomRight
  52.  
  53. }
  54.  
  55. /*
  56. ##############################
  57. # Setting Up the Grid Class #
  58. ##############################
  59. */
  60. EDE_GridSceneNode::EDE_GridSceneNode(ISceneNode* parent, ISceneManager* smgr, s32 id)
  61. : scene::ISceneNode(parent, smgr, id)
  62. {
  63. gridCells = NULL;
  64.  
  65. Material.Wireframe = false;
  66. Material.Lighting = false;
  67. Material.Thickness = 1;
  68. Material.FogEnable = false;
  69. Material.ZWriteEnable = true;
  70. Material.ZBuffer = true;
  71. Material.BackfaceCulling = true;
  72. Material.AntiAliasing = true;
  73. }
  74.  
  75. void EDE_GridSceneNode::init( int width, int height, SColor color, vector3df gridPos, float cellSize)
  76. {
  77. this->cellSize = cellSize;
  78. this->width = floor32(width / cellSize);
  79. this->height = floor32(height / cellSize);
  80. this->gridPos = gridPos;
  81. this->color = color;
  82.  
  83. // Setting Up my 2D Grid Array
  84. gridCells = new Cell*[this->width];
  85. for (int i = 0; i < this->width; i++)
  86. {
  87. gridCells[i] = new Cell[this->height];
  88. }
  89.  
  90. Box.reset(gridPos);
  91.  
  92. }
  93.  
  94. void EDE_GridSceneNode::createGridFlat()
  95. {
  96. for (int z = 0; z < height; z++)
  97. {
  98. for (int x = 0; x < width; x++)
  99. {
  100. vector3df v1 = vector3df((gridPos.X * (x+1)) + (x*this->cellSize), gridPos.Y, (gridPos.Z * (z+1)) + (z*this->cellSize));
  101. vector3df v2 = vector3df((gridPos.X * (x+1)) + (x*this->cellSize) + this->cellSize, gridPos.Y, (gridPos.Z * (z+1)) + (z*this->cellSize));
  102. vector3df v3 = vector3df((gridPos.X * (x+1)) + (x*this->cellSize), gridPos.Y, (gridPos.Z * (z+1)) + (z*this->cellSize) + this->cellSize);
  103. vector3df v4 = vector3df((gridPos.X * (x+1)) + (x*this->cellSize) + this->cellSize, gridPos.Y, (gridPos.Z * (z+1)) + (z*this->cellSize) + this->cellSize);
  104.  
  105. Box.addInternalPoint(v1);
  106. Box.addInternalPoint(v2);
  107. Box.addInternalPoint(v3);
  108. Box.addInternalPoint(v4);
  109.  
  110. gridCells[x][z] = Cell(v1, v2, v3, v4, z + x, true, Material);
  111. }
  112. }
  113. }
  114.  
  115. void EDE_GridSceneNode::createGridOnTerrain(ITerrainSceneNode* terrainSceneNode)
  116. {
  117. for (int z = 0; z < height; z++)
  118. {
  119. for (int x = 0; x < width; x++)
  120. {
  121. vector3df v1 = vector3df((terrainSceneNode->getPosition().X * (x+1)) + (x*this->cellSize),
  122. terrainSceneNode->getHeight((terrainSceneNode->getPosition().X * (x+1)) + (x*this->cellSize), (terrainSceneNode->getPosition().Z * (z+1)) + (z*this->cellSize)),
  123. (terrainSceneNode->getPosition().Z * (z+1)) + (z*this->cellSize));
  124. vector3df v2 = vector3df((terrainSceneNode->getPosition().X * (x+1)) + (x*this->cellSize) + this->cellSize,
  125. terrainSceneNode->getHeight((terrainSceneNode->getPosition().X * (x+1)) + (x*this->cellSize) + this->cellSize, (terrainSceneNode->getPosition().Z * (z+1)) + (z*this->cellSize)),
  126. (terrainSceneNode->getPosition().Z * (z+1)) + (z*this->cellSize));
  127. vector3df v3 = vector3df((terrainSceneNode->getPosition().X * (x+1)) + (x*this->cellSize),
  128. terrainSceneNode->getHeight((terrainSceneNode->getPosition().X * (x+1)) + (x*this->cellSize), (terrainSceneNode->getPosition().Z * (z+1)) + (z*this->cellSize) + this->cellSize),
  129. (terrainSceneNode->getPosition().Z * (z+1)) + (z*this->cellSize) + this->cellSize);
  130. vector3df v4 = vector3df((terrainSceneNode->getPosition().X * (x+1)) + (x*this->cellSize) + this->cellSize,
  131. terrainSceneNode->getHeight((terrainSceneNode->getPosition().X * (x+1)) + (x*this->cellSize) + this->cellSize, (terrainSceneNode->getPosition().Z * (z+1)) + (z*this->cellSize) + this->cellSize),
  132. (terrainSceneNode->getPosition().Z * (z+1)) + (z*this->cellSize) + this->cellSize);
  133.  
  134. Box.addInternalPoint(v1);
  135. Box.addInternalPoint(v2);
  136. Box.addInternalPoint(v3);
  137. Box.addInternalPoint(v4);
  138.  
  139. gridCells[x][z] = Cell(v1, v2, v3, v4, z + x, true, Material);
  140. }
  141. }
  142. }
  143.  
  144. void EDE_GridSceneNode::renderGrid(IVideoDriver* driver, SColor color)
  145. {
  146. for (int z = 0; z < height; z++)
  147. {
  148. for (int x = 0; x < width; x++)
  149. {
  150. gridCells[x][z].RenderCell(driver, color);
  151. }
  152. }
  153. }
  154.  
  155. void EDE_GridSceneNode::OnRegisterSceneNode()
  156. {
  157. if (IsVisible)
  158. SceneManager->registerNodeForRendering(this);
  159.  
  160. ISceneNode::OnRegisterSceneNode();
  161. }
  162.  
  163. void EDE_GridSceneNode::render()
  164. {
  165. IVideoDriver* driver = SceneManager->getVideoDriver();
  166.  
  167. if ( !driver )
  168. return;
  169.  
  170. renderGrid(driver, this->color);
  171. }
  172.  
  173. const aabbox3d<f32>& EDE_GridSceneNode::getBoundingBox() const
  174. {
  175. return Box;
  176. }
  177.  
  178. u32 EDE_GridSceneNode::getMaterialCount() const
  179. {
  180. return 1;
  181. }
  182.  
  183. SMaterial& EDE_GridSceneNode::getMaterial(u32 i)
  184. {
  185. return Material;
  186. }
  187.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty