Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "EDE_GridSceneNode.h"
- #include <stdio.h>
- #include <iostream>
- #include <fstream>
- /*
- ##############################
- # Setting Up the Grid Class #
- ##############################
- */
- EDE_GridSceneNode::EDE_GridSceneNode(ISceneNode* parent, ISceneManager* smgr, s32 id)
- : scene::ISceneNode(parent, smgr, id)
- {
- Material.Wireframe = false;
- Material.Lighting = false;
- Material.Thickness = 1;
- Material.FogEnable = false;
- Material.ZWriteEnable = true;
- Material.ZBuffer = true;
- Material.BackfaceCulling = true;
- Material.AntiAliasing = true;
- }
- void EDE_GridSceneNode::initGridOverFullTerrain( ITerrainSceneNode* terrainSceneNode, ITexture* heightmapTexture, float cellSize, SColor color)
- {
- this->cellSize = cellSize;
- this->widthInCells = (int)(((heightmapTexture->getSize().Width - 8)*terrainSceneNode->getScale().X) / cellSize);
- this->heightInCells = (int)(((heightmapTexture->getSize().Height - 8)*terrainSceneNode->getScale().Z) / cellSize);
- this->gridPos = vector3df(0.0f, 0.0f, 0.0f);
- this->color = color;
- setupVertexArrays();
- Box.reset(gridPos);
- createGridOnFullTerrain(terrainSceneNode);
- }
- void EDE_GridSceneNode::initGridOverPositionTerrain( ITerrainSceneNode* terrainSceneNode, int widthInCells, int heightInCells, vector3df gridPos, float cellSize, SColor color)
- {
- this->cellSize = cellSize;
- this->widthInCells = widthInCells;
- this->heightInCells = heightInCells;
- this->gridPos = gridPos;
- this->color = color;
- setupVertexArrays();
- Box.reset(gridPos);
- createGridOnPositionTerrain(terrainSceneNode);
- }
- void EDE_GridSceneNode::initGridFlat(int widthInCells, int heightInCells, vector3df gridPos, float cellSize, SColor color)
- {
- this->cellSize = cellSize;
- this->widthInCells = widthInCells;
- this->heightInCells = heightInCells;
- this->gridPos = gridPos;
- this->color = color;
- setupVertexArrays();
- Box.reset(gridPos);
- createGridFlat();
- }
- void EDE_GridSceneNode::createGridFlat()
- {
- for (int x = 0; x < widthInCells + 1; x++)
- {
- for (int z = 0; z < heightInCells + 1; z++)
- {
- vector3df v1 = vector3df(
- (gridPos.X * (x+1)) + (x*this->cellSize),
- gridPos.Y,
- (gridPos.Z * (z+1)) + (z*this->cellSize));
- verticesVertical[z][x].Pos = v1;
- verticesVertical[z][x].Color = this->color;
- uIndicesVertical[z][x] = x;
- verticesHorizontal[x][z].Pos = v1;
- verticesHorizontal[x][z].Color = this->color;
- uIndicesHorizontal[x][z] = z;
- Box.addInternalPoint(v1);
- }
- }
- }
- void EDE_GridSceneNode::createGridOnFullTerrain(ITerrainSceneNode* terrainSceneNode)
- {
- for (int x = 0; x < widthInCells + 1; x++)
- {
- for (int z = 0; z < heightInCells + 1; z++)
- {
- vector3df v1 = vector3df(
- x*this->cellSize,
- terrainSceneNode->getHeight(x*this->cellSize, z*this->cellSize),
- z*this->cellSize);
- verticesVertical[z][x].Pos = v1;
- verticesVertical[z][x].Color = this->color;
- uIndicesVertical[z][x] = x;
- verticesHorizontal[x][z].Pos = v1;
- verticesHorizontal[x][z].Color = this->color;
- uIndicesHorizontal[x][z] = z;
- Box.addInternalPoint(v1);
- }
- }
- }
- void EDE_GridSceneNode::createGridOnPositionTerrain(ITerrainSceneNode* terrainSceneNode)
- {
- for (int x = 0; x < widthInCells + 1; x++)
- {
- for (int z = 0; z < heightInCells + 1; z++)
- {
- vector3df v1 = vector3df(
- gridPos.X + ((x+1)*this->cellSize),
- terrainSceneNode->getHeight(gridPos.X + ((x+1)*this->cellSize),
- gridPos.Z + ((z+1)*this->cellSize)),
- gridPos.Z + ((z+1)*this->cellSize));
- verticesVertical[z][x].Pos = v1;
- verticesVertical[z][x].Color = this->color;
- uIndicesVertical[z][x] = x;
- verticesHorizontal[x][z].Pos = v1;
- verticesHorizontal[x][z].Color = this->color;
- uIndicesHorizontal[x][z] = z;
- Box.addInternalPoint(v1);
- }
- }
- }
- void EDE_GridSceneNode::renderGrid(IVideoDriver* driver, SColor color)
- {
- driver->setMaterial(this->Material);
- driver->setTransform(ETS_WORLD, AbsoluteTransformation);
- for (int z = 0; z < heightInCells + 1; z++)
- {
- driver->drawVertexPrimitiveList(&verticesVertical[z][0], widthInCells, &uIndicesVertical[z][0], widthInCells, EVT_STANDARD, EPT_LINE_STRIP);
- }
- for (int x = 0; x < widthInCells + 1; x++)
- {
- driver->drawVertexPrimitiveList(&verticesHorizontal[x][0], heightInCells, &uIndicesHorizontal[x][0], heightInCells, EVT_STANDARD, EPT_LINE_STRIP);
- }
- }
- void EDE_GridSceneNode::setupVertexArrays()
- {
- // Setting Up my 2D Grid Array
- //////////////////////////////
- verticesVertical = new S3DVertex*[this->heightInCells + 1];
- for (int i = 0; i < this->heightInCells + 1; i++)
- {
- try
- {
- verticesVertical[i] = new S3DVertex[this->widthInCells + 1];
- }
- catch (std::bad_alloc e)
- {
- char buffer[512];
- sprintf_s(buffer, "Out of Memory: %d bytes", sizeof(S3DVertex)*i);
- MessageBoxA(NULL, buffer, "Out of Memory!", MB_OK);
- exit(1);
- }
- }
- uIndicesVertical = new u16*[this->heightInCells + 1];
- for (int i = 0; i < this->heightInCells + 1; i++)
- {
- try
- {
- uIndicesVertical[i] = new u16[this->widthInCells + 1];
- }
- catch (std::bad_alloc e)
- {
- char buffer[512];
- sprintf_s(buffer, "Out of Memory: %d bytes", sizeof(u16)*i);
- MessageBoxA(NULL, buffer, "Out of Memory!", MB_OK);
- exit(1);
- }
- }
- verticesHorizontal = new S3DVertex*[this->widthInCells + 1];
- for (int i = 0; i < this->widthInCells + 1; i++)
- {
- try
- {
- verticesHorizontal[i] = new S3DVertex[this->heightInCells + 1];
- }
- catch (std::bad_alloc e)
- {
- char buffer[512];
- sprintf_s(buffer, "Out of Memory: %d bytes", sizeof(S3DVertex)*i);
- MessageBoxA(NULL, buffer, "Out of Memory!", MB_OK);
- exit(1);
- }
- }
- uIndicesHorizontal = new u16*[this->widthInCells + 1];
- for (int i = 0; i < this->widthInCells + 1; i++)
- {
- try
- {
- uIndicesHorizontal[i] = new u16[this->heightInCells + 1];
- }
- catch (std::bad_alloc e)
- {
- char buffer[512];
- sprintf_s(buffer, "Out of Memory: %d bytes", sizeof(u16)*i);
- MessageBoxA(NULL, buffer, "Out of Memory!", MB_OK);
- exit(1);
- }
- }
- }
- void EDE_GridSceneNode::OnRegisterSceneNode()
- {
- SceneManager->registerNodeForRendering(this);
- ISceneNode::OnRegisterSceneNode();
- }
- void EDE_GridSceneNode::render()
- {
- IVideoDriver* driver = SceneManager->getVideoDriver();
- if ( !driver )
- return;
- if (IsVisible)
- renderGrid(driver, this->color);
- }
- const aabbox3d<f32>& EDE_GridSceneNode::getBoundingBox() const
- {
- return Box;
- }
- u32 EDE_GridSceneNode::getMaterialCount() const
- {
- return 1;
- }
- SMaterial& EDE_GridSceneNode::getMaterial(u32 i)
- {
- return Material;
- }
Advertisement
Add Comment
Please, Sign In to add comment