Guest User

EDE_GridSceneNode.cpp

a guest
Feb 19th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.09 KB | None | 0 0
  1. #include "EDE_GridSceneNode.h"
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. /*  
  7.     ##############################
  8.     # Setting Up the Grid Class  #
  9.     ##############################
  10. */
  11. EDE_GridSceneNode::EDE_GridSceneNode(ISceneNode* parent, ISceneManager* smgr, s32 id)
  12.     : scene::ISceneNode(parent, smgr, id)
  13. {
  14.     Material.Wireframe = false;
  15.     Material.Lighting = false;
  16.     Material.Thickness = 1;
  17.     Material.FogEnable = false;
  18.     Material.ZWriteEnable = true;
  19.     Material.ZBuffer = true;
  20.     Material.BackfaceCulling = true;
  21.     Material.AntiAliasing = true;
  22. }
  23.  
  24. void EDE_GridSceneNode::initGridOverFullTerrain( ITerrainSceneNode* terrainSceneNode, ITexture* heightmapTexture, vector3df gridPos, float cellSize, SColor color)
  25. {
  26.     this->cellSize = cellSize;
  27.     this->widthInCells = (int)((heightmapTexture->getSize().Width  - 8) / cellSize);
  28.     this->heightInCells = (int)((heightmapTexture->getSize().Height - 8) / cellSize);
  29.     this->gridPos = gridPos;
  30.     this->color = color;
  31.    
  32.     setupVertexArrays();
  33.    
  34.     Box.reset(gridPos);
  35.  
  36.     createGridOnFullTerrain(terrainSceneNode);
  37. }
  38.  
  39. void EDE_GridSceneNode::initGridOverPositionTerrain( ITerrainSceneNode* terrainSceneNode, int widthInCells, int heightInCells, vector3df gridPos, float cellSize, SColor color)
  40. {
  41.     this->cellSize = cellSize;
  42.     this->widthInCells = widthInCells;
  43.     this->heightInCells = heightInCells;
  44.     this->gridPos = gridPos;
  45.     this->color = color;
  46.  
  47.     setupVertexArrays();
  48.  
  49.     Box.reset(gridPos);
  50.  
  51.     createGridOnPositionTerrain(terrainSceneNode);
  52. }
  53.  
  54. void EDE_GridSceneNode::initGridFlat(int widthInCells, int heightInCells, vector3df gridPos, float cellSize, SColor color)
  55. {
  56.     this->cellSize = cellSize;
  57.     this->widthInCells = widthInCells;
  58.     this->heightInCells = heightInCells;
  59.     this->gridPos = gridPos;
  60.     this->color = color;
  61.  
  62.     setupVertexArrays();
  63.  
  64.     Box.reset(gridPos);
  65.  
  66.     createGridFlat();
  67. }
  68.  
  69. void EDE_GridSceneNode::createGridFlat()
  70. {
  71.     for (int x = 0; x < widthInCells + 1; x++)
  72.     {
  73.         for (int z = 0; z < heightInCells + 1; z++)
  74.         {
  75.             vector3df v1 = vector3df((gridPos.X * (x+1)) + (x*this->cellSize), gridPos.Y, (gridPos.Z * (z+1)) + (z*this->cellSize));
  76.  
  77.             verticesVertical[z][x].Pos = v1;   
  78.             verticesVertical[z][x].Color = this->color;
  79.             uIndicesVertical[z][x] = x;
  80.  
  81.             verticesHorizontal[x][z].Pos = v1;
  82.             verticesHorizontal[x][z].Color = this->color;
  83.             uIndicesHorizontal[x][z] = z;
  84.  
  85.             Box.addInternalPoint(v1);
  86.         }
  87.     }
  88. }
  89.  
  90. void EDE_GridSceneNode::createGridOnFullTerrain(ITerrainSceneNode* terrainSceneNode)
  91. {
  92.     for (int x = 0; x < widthInCells + 1; x++)
  93.     {
  94.         for (int z = 0; z < heightInCells + 1; z++)
  95.         {
  96.             vector3df v1 = vector3df((terrainSceneNode->getAbsolutePosition().X * (x+1)) + (x*this->cellSize),
  97.                 terrainSceneNode->getHeight((terrainSceneNode->getAbsolutePosition().X * (x+1)) + (x*this->cellSize), (terrainSceneNode->getAbsolutePosition().Z * (z+1)) + (z*this->cellSize)),
  98.                 (terrainSceneNode->getAbsolutePosition().Z * (z+1)) + (z*this->cellSize));
  99.  
  100.            
  101.             verticesVertical[z][x].Pos = v1;
  102.             verticesVertical[z][x].Color = this->color;
  103.             uIndicesVertical[z][x] = x;
  104.  
  105.             verticesHorizontal[x][z].Pos = v1;
  106.             verticesHorizontal[x][z].Color = this->color;
  107.             uIndicesHorizontal[x][z] = z;
  108.  
  109.             Box.addInternalPoint(v1);
  110.         }
  111.     }
  112. }
  113.  
  114. void EDE_GridSceneNode::createGridOnPositionTerrain(ITerrainSceneNode* terrainSceneNode)
  115. {
  116.     std::ofstream debugFile;
  117.     debugFile.open("debugFile.txt");
  118.  
  119.     for (int x = 0; x < widthInCells + 1; x++)
  120.     {
  121.         for (int z = 0; z < heightInCells + 1; z++)
  122.         {
  123.             vector3df v1 = vector3df(
  124.                 (terrainSceneNode->getAbsolutePosition().X + gridPos.X) + ((x+1)*this->cellSize),
  125.                 terrainSceneNode->getHeight((terrainSceneNode->getAbsolutePosition().X + gridPos.X) + ((x+1)*this->cellSize),
  126.                                             (terrainSceneNode->getAbsolutePosition().Z + gridPos.Z) + ((z+1)*this->cellSize)),
  127.                 (terrainSceneNode->getAbsolutePosition().Z + gridPos.Z) + ((z+1)*this->cellSize));
  128.  
  129.             verticesVertical[z][x].Pos = v1;
  130.             verticesVertical[z][x].Color = this->color;
  131.             uIndicesVertical[z][x] = x;
  132.  
  133.             verticesHorizontal[x][z].Pos = v1;
  134.             verticesHorizontal[x][z].Color = this->color;
  135.             uIndicesHorizontal[x][z] = z;
  136.  
  137.             Box.addInternalPoint(v1);
  138.  
  139.             debugFile << "X=" << x << " Z=" << z << " Vec3=("<< v1.X << ", " << v1.Y << ", " << v1.Z << ")\n";
  140.         }
  141.     }
  142.     debugFile.close();
  143. }
  144.  
  145. void EDE_GridSceneNode::renderGrid(IVideoDriver* driver, SColor color)
  146. {
  147.    
  148.     driver->setMaterial(this->Material);
  149.     driver->setTransform(ETS_WORLD, matrix4());
  150.  
  151.     for (int z = 0; z < heightInCells + 1; z++)
  152.     {
  153.         driver->drawVertexPrimitiveList(&verticesVertical[z][0], widthInCells, &uIndicesVertical[z][0], widthInCells, EVT_STANDARD, EPT_LINE_STRIP);
  154.     }
  155.    
  156.     for (int x = 0; x < widthInCells + 1; x++)
  157.     {
  158.         driver->drawVertexPrimitiveList(&verticesHorizontal[x][0], heightInCells, &uIndicesHorizontal[x][0], heightInCells, EVT_STANDARD, EPT_LINE_STRIP);
  159.     }
  160. }
  161.  
  162. void EDE_GridSceneNode::setupVertexArrays()
  163. {
  164.     // Setting Up my 2D Grid Array
  165.     //////////////////////////////
  166.  
  167.     verticesVertical = new S3DVertex*[this->heightInCells + 1];
  168.     for (int i = 0; i < this->heightInCells + 1; i++)
  169.     {
  170.         try
  171.         {
  172.             verticesVertical[i] = new S3DVertex[this->widthInCells + 1];
  173.         }
  174.         catch (std::bad_alloc e)
  175.         {
  176.             char buffer[512];
  177.             sprintf_s(buffer, "Out of Memory: %d bytes",  sizeof(S3DVertex)*i);
  178.  
  179.             MessageBoxA(NULL, buffer, "Out of Memory!", MB_OK);
  180.             exit(1);
  181.         }
  182.     }
  183.  
  184.     uIndicesVertical = new u16*[this->heightInCells + 1];
  185.     for (int i = 0; i < this->heightInCells + 1; i++)
  186.     {
  187.         try
  188.         {
  189.             uIndicesVertical[i] = new u16[this->widthInCells + 1];
  190.         }
  191.         catch (std::bad_alloc e)
  192.         {
  193.             char buffer[512];
  194.             sprintf_s(buffer, "Out of Memory: %d bytes",  sizeof(u16)*i);
  195.  
  196.             MessageBoxA(NULL, buffer, "Out of Memory!", MB_OK);
  197.             exit(1);
  198.         }
  199.     }
  200.  
  201.     verticesHorizontal = new S3DVertex*[this->widthInCells + 1];
  202.     for (int i = 0; i < this->widthInCells + 1; i++)
  203.     {
  204.         try
  205.         {
  206.             verticesHorizontal[i] = new S3DVertex[this->heightInCells + 1];
  207.         }
  208.         catch (std::bad_alloc e)
  209.         {
  210.             char buffer[512];
  211.             sprintf_s(buffer, "Out of Memory: %d bytes",  sizeof(S3DVertex)*i);
  212.  
  213.             MessageBoxA(NULL, buffer, "Out of Memory!", MB_OK);
  214.             exit(1);
  215.         }
  216.     }
  217.  
  218.     uIndicesHorizontal = new u16*[this->widthInCells + 1];
  219.     for (int i = 0; i < this->widthInCells + 1; i++)
  220.     {
  221.         try
  222.         {
  223.             uIndicesHorizontal[i] = new u16[this->heightInCells + 1];
  224.         }
  225.         catch (std::bad_alloc e)
  226.         {
  227.             char buffer[512];
  228.             sprintf_s(buffer, "Out of Memory: %d bytes",  sizeof(u16)*i);
  229.  
  230.             MessageBoxA(NULL, buffer, "Out of Memory!", MB_OK);
  231.             exit(1);
  232.         }
  233.     }
  234. }
  235.  
  236. void EDE_GridSceneNode::OnRegisterSceneNode()
  237. {
  238.    
  239.     SceneManager->registerNodeForRendering(this);
  240.  
  241.     ISceneNode::OnRegisterSceneNode();
  242. }
  243.  
  244. void EDE_GridSceneNode::render()
  245. {
  246.     IVideoDriver* driver = SceneManager->getVideoDriver();
  247.  
  248.     if ( !driver )
  249.         return;
  250.  
  251.     if (IsVisible)
  252.         renderGrid(driver, this->color);   
  253. }
  254.  
  255. const aabbox3d<f32>& EDE_GridSceneNode::getBoundingBox() const
  256. {
  257.     return Box;
  258. }
  259.  
  260. u32 EDE_GridSceneNode::getMaterialCount() const
  261. {
  262.     return 1;
  263. }
  264.  
  265. SMaterial& EDE_GridSceneNode::getMaterial(u32 i)
  266. {
  267.     return Material;
  268. }
Advertisement
Add Comment
Please, Sign In to add comment