RigsofRods
Soft-body Physics Simulation
ProceduralManager.cpp
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  Copyright 2013-2022 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "ProceduralManager.h"
23 
24 #include "Application.h"
25 #include "ProceduralRoad.h"
26 
27 using namespace Ogre;
28 using namespace RoR;
29 
30 #pragma region ProceduralObject
31 
32 ProceduralPointPtr ProceduralObject::getPoint(int pos)
33 {
34  if (pos >= 0 && pos < (int)points.size())
35  {
36  return points[pos];
37  }
38  else
39  {
40  return ProceduralPointPtr();
41  }
42 }
43 
44 void ProceduralObject::insertPoint(int pos, ProceduralPointPtr p)
45 {
46  if (pos >= 0 && pos < (int)points.size())
47  {
48  points.insert(points.begin() + pos, p);
49  }
50 }
51 
52 void ProceduralObject::deletePoint(int pos)
53 {
54  if (pos >= 0 && pos < (int)points.size())
55  {
56  points.erase(points.begin() + pos);
57  }
58 }
59 
60 #pragma endregion
61 
62 #pragma region ProceduralManager
63 
64 ProceduralManager::ProceduralManager(Ogre::SceneNode* groupingSceneNode)
65  : pGroupingSceneNode(groupingSceneNode)
66 {
67 }
68 
70 {
71  this->removeAllObjects();
72 }
73 
75 {
76  if (pos >= 0 && pos < (int)pObjects.size())
77  {
78  return pObjects[pos];
79  }
80  else
81  {
82  return ProceduralObjectPtr();
83  }
84 }
85 
87 {
88  for (ProceduralObjectPtr obj : pObjects)
89  {
90  this->deleteObject(obj);
91  }
92  pObjects.clear(); // delete (unreference) all objects.
93 }
94 
96 {
97  if (po->road)
98  {
99  // loaded already, delete (unreference) old object
100  po->road = ProceduralRoadPtr();
101  }
102 }
103 
105 {
106  for (size_t i = 0; i < pObjects.size(); i++)
107  {
108  if (pObjects[i] == po)
109  {
110  pObjects.erase(pObjects.begin() + i);
111  }
112  }
113 }
114 
116 {
117  if (po->road)
118  this->deleteObject(po);
119 
120  po->road = new ProceduralRoad();
121  // In diagnostic mode, disable collisions (speeds up terrain loading)
123 
124  Ogre::SimpleSpline spline;
125  if (po->smoothing_num_splits > 0)
126  {
127  // Init smoothing
128  spline.setAutoCalculate(false);
129  for (ProceduralPointPtr& pp : po->points)
130  {
131  spline.addPoint(pp->position);
132  }
133  spline.recalcTangents();
134  }
135 
136  for (int i_point = 0; i_point < po->getNumPoints(); i_point++)
137  {
138  ProceduralPointPtr pp = po->getPoint(i_point);
139  if (po->smoothing_num_splits > 0)
140  {
141  const int num_segments = po->smoothing_num_splits + 1;
142 
143  // smoothing on
144  for (int i_seg = 1; i_seg <= num_segments; i_seg++)
145  {
146  if (i_point == 0)
147  {
148  po->road->addBlock(pp->position, pp->rotation, pp->type, pp->width, pp->bwidth, pp->bheight, pp->pillartype);
149  }
150  else
151  {
152  const float progress = static_cast<float>(i_seg) / static_cast<float>(num_segments);
153  ProceduralPointPtr prev_pp = po->getPoint(i_point - 1);
154 
155  const Ogre::Vector3 smooth_pos = spline.interpolate(i_point - 1, progress);
156  const Ogre::Quaternion smooth_rot = Quaternion::nlerp(progress, prev_pp->rotation, pp->rotation);
157  const float smooth_width = Math::lerp(prev_pp->width, pp->width, progress);
158  const float smooth_bwidth = Math::lerp(prev_pp->bwidth, pp->bwidth, progress);
159  const float smooth_bheight = Math::lerp(prev_pp->bheight, pp->bheight, progress);
160 
161  po->road->addBlock(smooth_pos, smooth_rot, pp->type, smooth_width, smooth_bwidth, smooth_bheight, pp->pillartype);
162  }
163  }
164  }
165  else
166  {
167  // smoothing off
168  po->road->addBlock(pp->position, pp->rotation, pp->type, pp->width, pp->bwidth, pp->bheight, pp->pillartype);
169  }
170  }
172 }
173 
175 {
176  updateObject(po);
177  pObjects.push_back(po);
178 }
179 
181 {
182  Log("[RoR] Procedural road diagnostic.\n"
183  " types: 0=ROAD_AUTOMATIC, 1=ROAD_FLAT, 2=ROAD_LEFT, 3=ROAD_RIGHT, 4=ROAD_BOTH, 5=ROAD_BRIDGE, 6=ROAD_MONORAIL\n"
184  " pillartypes: 0=none, 1=road bridge, 2=monorail");
185  for (int i=0; i< (int) pObjects.size(); ++i)
186  {
187  LogFormat("~~~~~~ ProceduralObject %d ~~~~~~", i);
189  for (int j = 0; j<(int)po->points.size(); ++j)
190  {
191  ProceduralPointPtr& pp = po->points[j];
192  LogFormat("\t Point [%d] posXYZ %f %f %f, type %d, width %f, bwidth %f, bheight %f, pillartype %i",
193  j, pp->position.x, pp->position.y, pp->position.z,
194  pp->type, pp->width, pp->bwidth, pp->bheight, pp->pillartype);
195  }
196  }
197 }
198 
199 #pragma endregion
RoR::ProceduralManager::addObject
void addObject(ProceduralObjectPtr po)
Generates road mesh and adds to internal list.
Definition: ProceduralManager.cpp:174
RoR::ProceduralManager::removeObject
void removeObject(ProceduralObjectPtr po)
Clears road mesh and removes from internal list.
Definition: ProceduralManager.cpp:104
RoR::ProceduralObject::points
std::vector< ProceduralPointPtr > points
Definition: ProceduralManager.h:74
RoR::ProceduralManager::pObjects
std::vector< ProceduralObjectPtr > pObjects
Definition: ProceduralManager.h:105
RoR::LogFormat
void LogFormat(const char *format,...)
Improved logging utility. Uses fixed 2Kb buffer.
Definition: Application.cpp:424
RoR::ProceduralRoad::setCollisionEnabled
void setCollisionEnabled(bool v)
Definition: ProceduralRoad.h:77
ScriptValueTypes::nlerp
quaternion nlerp(float, const quaternion &in, const quaternion &in, bool &in)
RoR::ProceduralManager::~ProceduralManager
virtual ~ProceduralManager() override
Definition: ProceduralManager.cpp:69
RoR::ProceduralRoad::finish
void finish(Ogre::SceneNode *snode)
Definition: ProceduralRoad.cpp:61
RoR::ProceduralObject::getNumPoints
int getNumPoints() const
Definition: ProceduralManager.h:68
RoR::ProceduralManager::updateObject
void updateObject(ProceduralObjectPtr po)
Rebuilds the road mesh.
Definition: ProceduralManager.cpp:115
RefCountingObjectPtr
Definition: RefCountingObjectPtr.h:24
RoR::ProceduralObjectPtr
RefCountingObjectPtr< ProceduralObject > ProceduralObjectPtr
Definition: ForwardDeclarations.h:200
ProceduralRoad.h
RoR::ProceduralManager::removeAllObjects
void removeAllObjects()
Definition: ProceduralManager.cpp:86
ProceduralManager.h
RoR::ProceduralObject::road
ProceduralRoadPtr road
Definition: ProceduralManager.h:75
RoR::ProceduralRoad::addBlock
void addBlock(Ogre::Vector3 pos, Ogre::Quaternion rot, RoadType type, float width, float bwidth, float bheight, int pillartype=1)
Definition: ProceduralRoad.cpp:81
RoR::ProceduralManager::pGroupingSceneNode
Ogre::SceneNode * pGroupingSceneNode
Definition: ProceduralManager.h:106
RoR::ProceduralManager::getObject
ProceduralObjectPtr getObject(int pos)
Definition: ProceduralManager.cpp:74
Application.h
Central state/object manager and communications hub.
RoR::ProceduralObject::smoothing_num_splits
int smoothing_num_splits
Definition: ProceduralManager.h:76
RoR::ProceduralRoad
Definition: ProceduralRoad.h:60
RoR::ProceduralObject::getPoint
ProceduralPointPtr getPoint(int pos)
Definition: ProceduralManager.cpp:32
RoR::ProceduralManager::deleteObject
void deleteObject(ProceduralObjectPtr po)
Deletes the road mesh.
Definition: ProceduralManager.cpp:95
RoR::ProceduralManager::logDiagnostics
void logDiagnostics()
Definition: ProceduralManager.cpp:180
Ogre
Definition: ExtinguishableFireAffector.cpp:35
RoR::App::diag_terrn_log_roads
CVar * diag_terrn_log_roads
Definition: Application.cpp:157
RoR::ProceduralPointPtr
RefCountingObjectPtr< ProceduralPoint > ProceduralPointPtr
Definition: ForwardDeclarations.h:199
RoR
Definition: AppContext.h:36
RoR::Log
void Log(const char *msg)
The ultimate, application-wide logging function. Adds a line (any length) in 'RoR....
Definition: Application.cpp:419
RoR::ProceduralRoadPtr
RefCountingObjectPtr< ProceduralRoad > ProceduralRoadPtr
Definition: ForwardDeclarations.h:201