RigsofRods
Soft-body Physics Simulation
RandomTreeLoader.h
Go to the documentation of this file.
1 /*
2 This source file is part of Rigs of Rods
3 Copyright 2005-2012 Pierre-Michel Ricordel
4 Copyright 2007-2012 Thomas Fischer
5 
6 For more information, see http://www.rigsofrods.org/
7 
8 Rigs of Rods is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 3, as
10 published by the Free Software Foundation.
11 
12 Rigs of Rods is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #pragma once
21 #ifndef __RandomTreeLoader_H__
22 #define __RandomTreeLoader_H__
23 
24 #include "RoRPrerequisites.h"
25 #include "PagedGeometry.h"
26 #include "PropertyMaps.h"
27 #include "TreeLoader2D.h"
28 
29 
30 
31 
32 namespace Forests {
33 
34 class TreeIterator3D;
35 class TreeIterator2D;
36 
37 class RandomTreeLoader : public TreeLoader2D, public ZeroedMemoryAllocator
38 {
39 public:
40  RandomTreeLoader(PagedGeometry *geom, const TBounds &bounds) : TreeLoader2D(geom, bounds)
41  {
42  }
43 
45  {
46  }
47 
48  void loadPage(PageInfo &page)
49  {
50  //Calculate x/z indexes for the grid array
51  page.xIndex -= Math::Floor(gridBounds.left / pageSize);
52  page.zIndex -= Math::Floor(gridBounds.top / pageSize);
53 
54  //Check if the requested page is in bounds
55  if (page.xIndex < 0 || page.zIndex < 0 || page.xIndex >= pageGridX || page.zIndex >= pageGridZ)
56  return;
57 
58  // just take the first tree registered as an example
59  std::vector<TreeDef> *pageGrid = pageGridList.begin()->second;
60  std::vector<TreeDef> &treeList = _getGridPage(pageGrid, page.xIndex, page.zIndex);
61 
62  // example values
63  minimumScale=0.07;
64  maximumScale=0.13;
65 
66  // if there are no tress in the page to be loaded, add some randomly
67  if (treeList.size() == 0)
68  {
69  // only add trees if there are none in this page...
70  for (int n=0; n < pageSize/10; n++)
71  {
72  //Create the new tree
73  Real xrel = Math::RangeRandom(0, pageSize);
74  Real zrel = Math::RangeRandom(0, pageSize);
75  Degree yaw(Math::RangeRandom(0, 360));
76  Real scale = Math::RangeRandom(minimumScale, maximumScale);
77 
78  TreeDef tree;
79  tree.xPos = 65535 * (xrel - (page.xIndex * pageSize)) / pageSize;
80  tree.zPos = 65535 * (zrel - (page.zIndex * pageSize)) / pageSize;
81  tree.rotation = 255 * (yaw.valueDegrees() / 360.0f);
82  tree.scale = 255 * ((scale - minimumScale) / maximumScale);
83  treeList.push_back(tree);
84  }
85  }
86 
87  // now load page as normal
88  TreeLoader2D::loadPage(page);
89  }
90 
91  void unloadPage(PageInfo &page)
92  {
93  //Calculate x/z indexes for the grid array
94  page.xIndex -= Math::Floor(gridBounds.left / pageSize);
95  page.zIndex -= Math::Floor(gridBounds.top / pageSize);
96 
97  //Check if the requested page is in bounds
98  if (page.xIndex < 0 || page.zIndex < 0 || page.xIndex >= pageGridX || page.zIndex >= pageGridZ)
99  return;
100 
101  // just take the first tree registered as an example
102  std::vector<TreeDef> *pageGrid = pageGridList.begin()->second;
103  std::vector<TreeDef> &treeList = _getGridPage(pageGrid, page.xIndex, page.zIndex);
104 
105  // clear all existing trees
106  uint32 i = 0;
107  while (i < treeList.size()){
108  //Check if tree is in bounds
109 #ifdef USE_PAGEDGEOMETRY_USER_DATA
110  deletedUserData.push_back(treeList[i].userData);
111 #endif
112  // delete it
113  treeList[i] = treeList.back();
114  treeList.pop_back();
115  ++i;
116  }
117 
118  // now unload page as normal
119  TreeLoader2D::unloadPage(page);
120  }
121 };
122 
123 }
124 #endif
Forests::RandomTreeLoader::~RandomTreeLoader
~RandomTreeLoader()
Definition: RandomTreeLoader.h:44
Forests::RandomTreeLoader::unloadPage
void unloadPage(PageInfo &page)
Definition: RandomTreeLoader.h:91
Forests::RandomTreeLoader::loadPage
void loadPage(PageInfo &page)
Definition: RandomTreeLoader.h:48
Forests::RandomTreeLoader
Definition: RandomTreeLoader.h:37
Forests::RandomTreeLoader::RandomTreeLoader
RandomTreeLoader(PagedGeometry *geom, const TBounds &bounds)
Definition: RandomTreeLoader.h:40
Forests
Definition: RandomTreeLoader.h:32