Guest User

EDE_GridSceneNode.cpp

a guest
Feb 20th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.60 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, float cellSize, SColor color)
  25. {
  26.     this->cellSize = cellSize;
  27.     this->widthInCells = (int)(((heightmapTexture->getSize().Width  - 8)*terrainSceneNode->getScale().X) / cellSize);
  28.     this->heightInCells = (int)(((heightmapTexture->getSize().Height - 8)*terrainSceneNode->getScale().Z) / cellSize);
  29.     this->gridPos = vector3df(0.0f, 0.0f, 0.0f);
  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(
  76.                 (gridPos.X * (x+1)) + (x*this->cellSize),
  77.                 gridPos.Y,
  78.                 (gridPos.Z * (z+1)) + (z*this->cellSize));
  79.  
  80.             verticesVertical[z][x].Pos = v1;   
  81.             verticesVertical[z][x].Color = this->color;
  82.             uIndicesVertical[z][x] = x;
  83.  
  84.             verticesHorizontal[x][z].Pos = v1;
  85.             verticesHorizontal[x][z].Color = this->color;
  86.             uIndicesHorizontal[x][z] = z;
  87.  
  88.             Box.addInternalPoint(v1);
  89.         }
  90.     }
  91. }
  92.  
  93. void EDE_GridSceneNode::createGridOnFullTerrain(ITerrainSceneNode* terrainSceneNode)
  94. {
  95.     for (int x = 0; x < widthInCells + 1; x++)
  96.     {
  97.         for (int z = 0; z < heightInCells + 1; z++)
  98.         {
  99.             vector3df v1 = vector3df(
  100.                 x*this->cellSize,
  101.                 terrainSceneNode->getHeight(x*this->cellSize, z*this->cellSize),
  102.                 z*this->cellSize);
  103.  
  104.             verticesVertical[z][x].Pos = v1;
  105.             verticesVertical[z][x].Color = this->color;
  106.             uIndicesVertical[z][x] = x;
  107.  
  108.             verticesHorizontal[x][z].Pos = v1;
  109.             verticesHorizontal[x][z].Color = this->color;
  110.             uIndicesHorizontal[x][z] = z;
  111.  
  112.             Box.addInternalPoint(v1);
  113.         }
  114.     }
  115. }
  116.  
  117. void EDE_GridSceneNode::createGridOnPositionTerrain(ITerrainSceneNode* terrainSceneNode)
  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.                 gridPos.X + ((x+1)*this->cellSize),
  125.                 terrainSceneNode->getHeight(gridPos.X + ((x+1)*this->cellSize),
  126.                                             gridPos.Z + ((z+1)*this->cellSize)),
  127.                 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.     }
  140. }
  141.  
  142. void EDE_GridSceneNode::renderGrid(IVideoDriver* driver, SColor color)
  143. {
  144.    
  145.     driver->setMaterial(this->Material);
  146.     driver->setTransform(ETS_WORLD, AbsoluteTransformation);
  147.  
  148.     for (int z = 0; z < heightInCells + 1; z++)
  149.     {
  150.         driver->drawVertexPrimitiveList(&verticesVertical[z][0], widthInCells, &uIndicesVertical[z][0], widthInCells, EVT_STANDARD, EPT_LINE_STRIP);
  151.     }
  152.    
  153.     for (int x = 0; x < widthInCells + 1; x++)
  154.     {
  155.         driver->drawVertexPrimitiveList(&verticesHorizontal[x][0], heightInCells, &uIndicesHorizontal[x][0], heightInCells, EVT_STANDARD, EPT_LINE_STRIP);
  156.     }
  157. }
  158.  
  159. void EDE_GridSceneNode::setupVertexArrays()
  160. {
  161.     // Setting Up my 2D Grid Array
  162.     //////////////////////////////
  163.  
  164.     verticesVertical = new S3DVertex*[this->heightInCells + 1];
  165.     for (int i = 0; i < this->heightInCells + 1; i++)
  166.     {
  167.         try
  168.         {
  169.             verticesVertical[i] = new S3DVertex[this->widthInCells + 1];
  170.         }
  171.         catch (std::bad_alloc e)
  172.         {
  173.             char buffer[512];
  174.             sprintf_s(buffer, "Out of Memory: %d bytes",  sizeof(S3DVertex)*i);
  175.            
  176.             MessageBoxA(NULL, buffer, "Out of Memory!", MB_OK);
  177.             exit(1);
  178.         }
  179.     }
  180.  
  181.     uIndicesVertical = new u16*[this->heightInCells + 1];
  182.     for (int i = 0; i < this->heightInCells + 1; i++)
  183.     {
  184.         try
  185.         {
  186.             uIndicesVertical[i] = new u16[this->widthInCells + 1];
  187.         }
  188.         catch (std::bad_alloc e)
  189.         {
  190.             char buffer[512];
  191.             sprintf_s(buffer, "Out of Memory: %d bytes",  sizeof(u16)*i);
  192.  
  193.             MessageBoxA(NULL, buffer, "Out of Memory!", MB_OK);
  194.             exit(1);
  195.         }
  196.     }
  197.  
  198.     verticesHorizontal = new S3DVertex*[this->widthInCells + 1];
  199.     for (int i = 0; i < this->widthInCells + 1; i++)
  200.     {
  201.         try
  202.         {
  203.             verticesHorizontal[i] = new S3DVertex[this->heightInCells + 1];
  204.         }
  205.         catch (std::bad_alloc e)
  206.         {
  207.             char buffer[512];
  208.             sprintf_s(buffer, "Out of Memory: %d bytes",  sizeof(S3DVertex)*i);
  209.  
  210.             MessageBoxA(NULL, buffer, "Out of Memory!", MB_OK);
  211.             exit(1);
  212.         }
  213.     }
  214.  
  215.     uIndicesHorizontal = new u16*[this->widthInCells + 1];
  216.     for (int i = 0; i < this->widthInCells + 1; i++)
  217.     {
  218.         try
  219.         {
  220.             uIndicesHorizontal[i] = new u16[this->heightInCells + 1];
  221.         }
  222.         catch (std::bad_alloc e)
  223.         {
  224.             char buffer[512];
  225.             sprintf_s(buffer, "Out of Memory: %d bytes",  sizeof(u16)*i);
  226.  
  227.             MessageBoxA(NULL, buffer, "Out of Memory!", MB_OK);
  228.             exit(1);
  229.         }
  230.     }
  231. }
  232.  
  233. void EDE_GridSceneNode::OnRegisterSceneNode()
  234. {
  235.    
  236.     SceneManager->registerNodeForRendering(this);
  237.  
  238.     ISceneNode::OnRegisterSceneNode();
  239. }
  240.  
  241. void EDE_GridSceneNode::render()
  242. {
  243.     IVideoDriver* driver = SceneManager->getVideoDriver();
  244.  
  245.     if ( !driver )
  246.         return;
  247.  
  248.     if (IsVisible)
  249.         renderGrid(driver, this->color);   
  250. }
  251.  
  252. const aabbox3d<f32>& EDE_GridSceneNode::getBoundingBox() const
  253. {
  254.     return Box;
  255. }
  256.  
  257. u32 EDE_GridSceneNode::getMaterialCount() const
  258. {
  259.     return 1;
  260. }
  261.  
  262. SMaterial& EDE_GridSceneNode::getMaterial(u32 i)
  263. {
  264.     return Material;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment