RigsofRods
Soft-body Physics Simulation
TObjFileFormat.h
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2016-2017 Petr Ohlidal
4 
5  For more information, see http://www.rigsofrods.org/
6 
7  Rigs of Rods is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License version 3, as
9  published by the Free Software Foundation.
10 
11  Rigs of Rods is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
25 
26 #include "Collisions.h"
27 #include "ProceduralManager.h"
28 
29 #include <Ogre.h>
30 
31 #include <memory>
32 
33 namespace RoR {
34 
35 namespace TObj {
36 
37  const int STR_LEN = 300;
38  const int LINE_BUF_LEN = 4000;
39 
40  enum class SpecialObject
41  {
42  NONE,
43  TRUCK,
44  LOAD,
45  MACHINE,
46  BOAT,
47  TRUCK2,
48  GRID,
49  // Road types
50  ROAD,
56  };
57 
58 } // namespace TObj
59 
60 // -----------------------------------------------------------------------------
61 struct TObjTree
62 {
64  yaw_from(0.f), yaw_to(0.f),
65  scale_from(0.f), scale_to(0.f),
66  min_distance(90), max_distance(700),
67  high_density(1.f),
68  grid_spacing(0.f)
69  {
70  tree_mesh [0] = '\0';
71  color_map [0] = '\0';
72  density_map [0] = '\0';
73  collision_mesh[0] = '\0';
74  }
75 
76  float yaw_from, yaw_to;
79  float high_density;
80  float grid_spacing;
81 
86 };
87 
88 // -----------------------------------------------------------------------------
90 struct TObjGrass
91 {
93  range(80),
94  technique(1), // GRASSTECH_CROSSQUADS
95  grow_techniq(0),
96  sway_speed(0.5f),
97  sway_length(0.05f),
98  sway_distrib(10.f),
99 
100  min_x(0.2f), min_y(0.2f), min_h(-9999.f),
101  max_x(1.0f), max_y(0.6f), max_h(+9999.f)
102  {
103  material_name [0] = '\0';
104  color_map_filename [0] = '\0';
105  density_map_filename [0] = '\0';
106  }
107 
108  int range;
111  float sway_speed;
112  float sway_length;
114  float density;
115 
116  float min_x, min_y, min_h;
117  float max_x, max_y, max_h;
118 
122 };
123 
124 // -----------------------------------------------------------------------------
126 {
127  Ogre::Vector3 position;
128  Ogre::Quaternion rotation;
131 };
132 
133 // -----------------------------------------------------------------------------
134 struct TObjEntry
135 {
136  TObjEntry() {};
137  TObjEntry(
138  Ogre::Vector3 pos, Ogre::Vector3 rot, const char* instance_name,
139  TObj::SpecialObject special, const char* type, const char* name);
140 
141  bool IsActor() const;
142  bool IsRoad() const;
143 
144  Ogre::Vector3 position = Ogre::Vector3::ZERO;
145  Ogre::Vector3 rotation = Ogre::Vector3::ZERO;
147  char type[TObj::STR_LEN] = {};
150  float rendering_distance = 0.f; // 0 means 'always rendered', see https://ogrecave.github.io/ogre/api/1.11/class_ogre_1_1_movable_object.html#afe1f2a1009e3f14f36e1bcc9b1b9557e
151 };
152 
153 // -----------------------------------------------------------------------------
154 struct TObjFile
155 {
157  grid_position(),
158  grid_enabled(false)
159  {}
160 
161  Ogre::Vector3 grid_position;
163  std::vector<TObjTree> trees;
164  std::vector<TObjGrass> grass;
165  std::vector<TObjVehicle> vehicles;
166  std::vector<TObjEntry> objects;
167  std::vector<ProceduralObjectPtr> proc_objects;
168 };
169 
170 // -----------------------------------------------------------------------------
172 {
173 public:
174  void Prepare();
175  bool ProcessLine(const char* line);
176  void ProcessOgreStream(Ogre::DataStream* stream);
177  std::shared_ptr<TObjFile> Finalize();
178 
179 private:
180  // Processing:
181  bool ProcessCurrentLine();
182  void ProcessProceduralLine();
183  void ProcessGridLine();
184  void ProcessTreesLine();
185  void ProcessGrassLine();
186  void ProcessActorObject(const TObjEntry& object);
187  void ProcessRoadObject(const TObjEntry& object);
188 
189  // Helpers:
190  void ImportProceduralPoint(Ogre::Vector3 const& pos, Ogre::Vector3 const& rot, TObj::SpecialObject special);
191  Ogre::Quaternion CalcRotation(Ogre::Vector3 const& rot) const;
192  bool ParseObjectLine(TObjEntry& object);
193  void FlushProceduralObject();
194 
195  std::shared_ptr<TObjFile> m_def;
196  std::string m_filename;
198  const char* m_cur_line;
199  const char* m_cur_line_trimmed;
201 
202  // Procedural roads
206 
207  // Auto-importing legacy road blocks (ODEF) as procedural roads
208  Ogre::Vector3 m_road2_last_pos;
209  Ogre::Vector3 m_road2_last_rot;
211 
212  bool m_rot_yxz;
213 };
214 
215 } // namespace RoR
RoR::TObj::SpecialObject::BOAT
@ BOAT
RoR::TObjParser::ProcessLine
bool ProcessLine(const char *line)
Definition: TObjFileFormat.cpp:72
RoR::TObj::SpecialObject::MACHINE
@ MACHINE
RoR::TObjParser::ProcessProceduralLine
void ProcessProceduralLine()
Definition: TObjFileFormat.cpp:218
RoR::TObjFile
Definition: TObjFileFormat.h:154
RoR::TObjGrass::density
float density
Definition: TObjFileFormat.h:114
RoR::TObjVehicle::type
TObj::SpecialObject type
Definition: TObjFileFormat.h:130
RoR::TObjParser::ProcessActorObject
void ProcessActorObject(const TObjEntry &object)
Definition: TObjFileFormat.cpp:303
RoR::TObjGrass::min_y
float min_y
Definition: TObjFileFormat.h:116
RoR::TObjParser
Definition: TObjFileFormat.h:171
RoR::TObjGrass::range
int range
Definition: TObjFileFormat.h:108
RoR::TObjEntry::IsActor
bool IsActor() const
Definition: TObjFileFormat.cpp:48
RoR::TObjGrass::max_y
float max_y
Definition: TObjFileFormat.h:117
RoR::TObjParser::m_filename
std::string m_filename
Definition: TObjFileFormat.h:196
RoR::TObjVehicle::rotation
Ogre::Quaternion rotation
Definition: TObjFileFormat.h:128
RoR::TObjGrass::sway_distrib
float sway_distrib
Definition: TObjFileFormat.h:113
RoR::TObjParser::Prepare
void Prepare()
Definition: TObjFileFormat.cpp:58
RoR::TObjTree::yaw_to
float yaw_to
Definition: TObjFileFormat.h:76
RoR::TObjFile::TObjFile
TObjFile()
Definition: TObjFileFormat.h:156
RoR::TObjFile::grid_position
Ogre::Vector3 grid_position
Definition: TObjFileFormat.h:161
RoR::TObj::SpecialObject::TRUCK2
@ TRUCK2
RoR::TObj::SpecialObject::LOAD
@ LOAD
RoR::TObjGrass::min_x
float min_x
Definition: TObjFileFormat.h:116
RoR::TObjEntry::special
TObj::SpecialObject special
Definition: TObjFileFormat.h:146
RoR::TObjEntry::rendering_distance
float rendering_distance
Definition: TObjFileFormat.h:150
RoR::TObjEntry::instance_name
char instance_name[TObj::STR_LEN]
Definition: TObjFileFormat.h:148
RoR::TObjFile::grid_enabled
bool grid_enabled
Definition: TObjFileFormat.h:162
RoR::TObjParser::m_in_procedural_road
bool m_in_procedural_road
Definition: TObjFileFormat.h:203
RoR::TObjTree::min_distance
float min_distance
Definition: TObjFileFormat.h:78
RoR::TObjEntry::odef_name
char odef_name[TObj::STR_LEN]
Definition: TObjFileFormat.h:149
RoR::TObjGrass::material_name
char material_name[TObj::STR_LEN]
Definition: TObjFileFormat.h:119
RoR::TObj::LINE_BUF_LEN
const int LINE_BUF_LEN
Definition: TObjFileFormat.h:38
RefCountingObjectPtr< ProceduralObject >
RoR::TObjFile::grass
std::vector< TObjGrass > grass
Definition: TObjFileFormat.h:164
RoR::TObjParser::ProcessCurrentLine
bool ProcessCurrentLine()
Definition: TObjFileFormat.cpp:90
RoR::TObjGrass::max_h
float max_h
Definition: TObjFileFormat.h:117
RoR::TObjParser::m_def
std::shared_ptr< TObjFile > m_def
Definition: TObjFileFormat.h:195
RoR::TObjParser::m_cur_procedural_obj_start_line
int m_cur_procedural_obj_start_line
Definition: TObjFileFormat.h:205
RoR::TObjTree::density_map
char density_map[TObj::STR_LEN]
Definition: TObjFileFormat.h:84
RoR::TObjParser::ProcessGridLine
void ProcessGridLine()
Definition: TObjFileFormat.cpp:243
RoR::TObjEntry
Definition: TObjFileFormat.h:134
RoR::TObjGrass::sway_length
float sway_length
Definition: TObjFileFormat.h:112
RoR::TObjEntry::rotation
Ogre::Vector3 rotation
Definition: TObjFileFormat.h:145
RoR::TObjParser::ImportProceduralPoint
void ImportProceduralPoint(Ogre::Vector3 const &pos, Ogre::Vector3 const &rot, TObj::SpecialObject special)
Definition: TObjFileFormat.cpp:343
RoR::TObjParser::m_line_number
int m_line_number
Definition: TObjFileFormat.h:197
RoR::TObjParser::ParseObjectLine
bool ParseObjectLine(TObjEntry &object)
Definition: TObjFileFormat.cpp:377
ProceduralManager.h
RoR::TObj::SpecialObject::ROAD
@ ROAD
RoR::TObjGrass::technique
int technique
Definition: TObjFileFormat.h:109
RoR::TObj::STR_LEN
const int STR_LEN
Definition: TObjFileFormat.h:37
RoR::TObjGrass::density_map_filename
char density_map_filename[TObj::STR_LEN]
Definition: TObjFileFormat.h:121
RoR::TObj::SpecialObject::GRID
@ GRID
RoR::TObjParser::m_road2_num_blocks
int m_road2_num_blocks
Definition: TObjFileFormat.h:210
RoR::TObjParser::m_road2_last_rot
Ogre::Vector3 m_road2_last_rot
Definition: TObjFileFormat.h:209
RoR::TObjFile::proc_objects
std::vector< ProceduralObjectPtr > proc_objects
Definition: TObjFileFormat.h:167
RoR::TObjVehicle::position
Ogre::Vector3 position
Definition: TObjFileFormat.h:127
RoR::TObj::SpecialObject::ROAD_BORDER_LEFT
@ ROAD_BORDER_LEFT
RoR::TObjTree::tree_mesh
char tree_mesh[TObj::STR_LEN]
Definition: TObjFileFormat.h:82
RoR::TObjGrass::sway_speed
float sway_speed
Definition: TObjFileFormat.h:111
RoR::TObjEntry::type
char type[TObj::STR_LEN]
Definition: TObjFileFormat.h:147
RoR::TObjEntry::IsRoad
bool IsRoad() const
Definition: TObjFileFormat.cpp:43
RoR::TObjFile::vehicles
std::vector< TObjVehicle > vehicles
Definition: TObjFileFormat.h:165
RoR::TObjTree::scale_from
float scale_from
Definition: TObjFileFormat.h:77
RoR::TObjParser::m_cur_procedural_obj
ProceduralObjectPtr m_cur_procedural_obj
Definition: TObjFileFormat.h:204
RoR::TObjTree::max_distance
float max_distance
Definition: TObjFileFormat.h:78
RoR::TObjParser::ProcessRoadObject
void ProcessRoadObject(const TObjEntry &object)
Definition: TObjFileFormat.cpp:314
RoR::TObjParser::m_rot_yxz
bool m_rot_yxz
Definition: TObjFileFormat.h:212
RoR::TObjEntry::TObjEntry
TObjEntry()
Definition: TObjFileFormat.h:136
RoR::TObjParser::ProcessOgreStream
void ProcessOgreStream(Ogre::DataStream *stream)
Definition: TObjFileFormat.cpp:203
RoR::TObjTree::grid_spacing
float grid_spacing
Definition: TObjFileFormat.h:80
RoR::TObjFile::trees
std::vector< TObjTree > trees
Definition: TObjFileFormat.h:163
RoR::TObj::SpecialObject::ROAD_BORDER_RIGHT
@ ROAD_BORDER_RIGHT
RoR::TObjTree::TObjTree
TObjTree()
Definition: TObjFileFormat.h:63
RoR::TObjParser::m_road2_last_pos
Ogre::Vector3 m_road2_last_pos
Definition: TObjFileFormat.h:208
RoR::TObjParser::m_cur_line
const char * m_cur_line
Definition: TObjFileFormat.h:198
RoR::TObjGrass::color_map_filename
char color_map_filename[TObj::STR_LEN]
Definition: TObjFileFormat.h:120
RoR::TObjTree::high_density
float high_density
Definition: TObjFileFormat.h:79
RoR::TObjVehicle::name
char name[TObj::STR_LEN]
Definition: TObjFileFormat.h:129
RoR::TObjGrass::max_x
float max_x
Definition: TObjFileFormat.h:117
RoR::TObjGrass
Unified 'grass' and 'grass2'.
Definition: TObjFileFormat.h:90
RoR::TObjParser::m_default_rendering_distance
float m_default_rendering_distance
Definition: TObjFileFormat.h:200
RoR::TObjGrass::grow_techniq
int grow_techniq
Definition: TObjFileFormat.h:110
RoR::TObjTree::color_map
char color_map[TObj::STR_LEN]
Definition: TObjFileFormat.h:83
RoR::TObjTree
Definition: TObjFileFormat.h:61
RoR::TObj::SpecialObject::NONE
@ NONE
RoR::TObj::SpecialObject::ROAD_BORDER_BOTH
@ ROAD_BORDER_BOTH
RoR::TObjTree::scale_to
float scale_to
Definition: TObjFileFormat.h:77
RoR::TObj::SpecialObject::ROAD_BRIDGE
@ ROAD_BRIDGE
RoR::TObjEntry::position
Ogre::Vector3 position
Definition: TObjFileFormat.h:144
RoR::TObjParser::Finalize
std::shared_ptr< TObjFile > Finalize()
Passes ownership.
Definition: TObjFileFormat.cpp:185
RoR::TObjVehicle
Definition: TObjFileFormat.h:125
RoR::TObjFile::objects
std::vector< TObjEntry > objects
Definition: TObjFileFormat.h:166
RoR::TObjParser::ProcessTreesLine
void ProcessTreesLine()
Definition: TObjFileFormat.cpp:250
RoR::TObjParser::CalcRotation
Ogre::Quaternion CalcRotation(Ogre::Vector3 const &rot) const
Definition: TObjFileFormat.cpp:361
Collisions.h
RoR::TObjParser::FlushProceduralObject
void FlushProceduralObject()
Definition: TObjFileFormat.cpp:410
RoR::TObjGrass::min_h
float min_h
Definition: TObjFileFormat.h:116
RoR::TObj::SpecialObject::ROAD_BRIDGE_NO_PILLARS
@ ROAD_BRIDGE_NO_PILLARS
RoR::TObjGrass::TObjGrass
TObjGrass()
Definition: TObjFileFormat.h:92
RoR
Definition: AppContext.h:36
RoR::TObj::SpecialObject
SpecialObject
Definition: TObjFileFormat.h:40
RoR::TObjParser::m_cur_line_trimmed
const char * m_cur_line_trimmed
Definition: TObjFileFormat.h:199
RoR::TObj::SpecialObject::TRUCK
@ TRUCK
RoR::TObjTree::collision_mesh
char collision_mesh[TObj::STR_LEN]
Definition: TObjFileFormat.h:85
RoR::TObjTree::yaw_from
float yaw_from
Definition: TObjFileFormat.h:76
RoR::TObjParser::ProcessGrassLine
void ProcessGrassLine()
Definition: TObjFileFormat.cpp:264