RigsofRods
Soft-body Physics Simulation
FlexFactory.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  Copyright 2013-2020 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 
26 
27 
28 #pragma once
29 
30 #include "BitFlags.h"
31 #include "ForwardDeclarations.h"
32 #include "Locator_t.h"
33 #include "RigDef_Prerequisites.h"
34 
35 #include <OgreVector3.h>
36 #include <OgreColourValue.h>
37 #include <vector>
38 
39 namespace RoR
40 {
41 
44 
47 
49 {
52  int node_x;
53  int node_y;
54  Ogre::Vector3 center_offset;
59 
60  static const BitMask_t IS_FAULTY = BITMASK(1);
62  static const BitMask_t HAS_TEXTURE = BITMASK(3);
63  static const BitMask_t HAS_TEXTURE_BLEND = BITMASK(4);
64 };
65 
67 {
69  dst_pos(nullptr),
70  src_normals(nullptr),
71  src_colors(nullptr),
72  locators(nullptr)
73  {}
74 
75  // NOTE: No freeing of memory needed, pointers will be copied to FlexBody instances.
76 
78 
79  Ogre::Vector3* dst_pos;
80  Ogre::Vector3* src_normals;
81  Ogre::ARGB* src_colors;
83 };
84 
98 {
99 public:
101  {
110  };
111 
112  static const char* SIGNATURE;
113  static const unsigned int FILE_FORMAT_VERSION = 1;
114 
115  FlexBodyFileIO();
116 
117  std::vector<FlexBody*> & GetList();
118  inline void AddItemToSave(FlexBody* fb) { m_items_to_save.push_back(fb); }
119  inline FlexBodyCacheData* GetLoadedItem(unsigned index) { return & m_loaded_items[index]; }
122 
123 private:
125  {
126  unsigned int file_format_version;
127  unsigned int num_flexbodies;
128  };
129 
130  void OpenFile(const char* fopen_mode);
131  void WriteToFile(void* source, size_t length);
132  void ReadFromFile(void* dest, size_t length);
133  inline void CloseFile() { if (m_file != nullptr) { fclose(m_file); } }
134 
135  void WriteSignature();
136  void ReadAndCheckSignature();
137 
138  void WriteMetadata();
139  void ReadMetadata(FlexBodyFileMetadata* meta);
140 
141  void WriteFlexbodyHeader(FlexBody* flexbody);
142  void ReadFlexbodyHeader(FlexBodyCacheData* flexbody);
143 
144  void WriteFlexbodyLocatorList(FlexBody* flexbody);
146 
147  void WriteFlexbodyNormalsBuffer(FlexBody* flexbody);
149 
150  void WriteFlexbodyPositionsBuffer(FlexBody* flexbody);
152 
153  void WriteFlexbodyColorsBuffer(FlexBody* flexbody);
155 
156  std::vector<FlexBody*> m_items_to_save;
157  std::vector<FlexBodyCacheData> m_loaded_items;
158  FILE* m_file;
159  unsigned int m_fileformat_version;
161 };
162 
164 {
165 public:
167 
168  FlexFactory(ActorSpawner* spawner);
169 
171  FlexbodyID_t flexbody_id,
172  const NodeNum_t ref_node,
173  const NodeNum_t x_node,
174  const NodeNum_t y_node,
175  Ogre::Vector3 offset,
176  Ogre::Vector3 rotation,
177  std::vector<unsigned int> & node_indices,
178  const std::string& mesh_name,
179  const std::string& resource_group_name);
180 
182  unsigned int wheel_index,
183  int axis_node_1_index,
184  int axis_node_2_index,
185  int nstart,
186  int nrays,
187  float rim_radius,
188  bool rim_reverse,
189  std::string const & rim_mesh_name,
190  std::string const & rim_mesh_rg,
191  std::string const & tire_material_name,
192  std::string const & tire_material_rg);
193 
195  void SaveFlexbodiesToCache();
196 
197 private:
198 
200 
205 };
206 
209 
210 } // namespace RoR
RoR::FlexBodyCacheData
Definition: FlexFactory.h:66
RoR::FlexBodyFileIO::FlexBodyFileMetadata::num_flexbodies
unsigned int num_flexbodies
Definition: FlexFactory.h:127
RoR::FlexBodyRecordHeader::node_x
int node_x
Definition: FlexFactory.h:52
RoR::FlexBodyCacheData::header
FlexBodyRecordHeader header
Definition: FlexFactory.h:77
ForwardDeclarations.h
Global forward declarations.
RoR::FlexBodyFileIO::RESULT_CODE_FREAD_OUTPUT_INCOMPLETE
@ RESULT_CODE_FREAD_OUTPUT_INCOMPLETE
Definition: FlexFactory.h:108
RoR::Locator_t
Definition: Locator_t.h:10
RoR::FlexBodyFileIO::WriteFlexbodyColorsBuffer
void WriteFlexbodyColorsBuffer(FlexBody *flexbody)
Definition: FlexFactory.cpp:281
RoR::FlexBodyFileIO::OpenFile
void OpenFile(const char *fopen_mode)
Definition: FlexFactory.cpp:304
RoR::FlexBodyRecordHeader::HAS_TEXTURE
static const BitMask_t HAS_TEXTURE
Definition: FlexFactory.h:62
RigDef_Prerequisites.h
RoR::FlexBodyFileIO::ReadFlexbodyHeader
void ReadFlexbodyHeader(FlexBodyCacheData *flexbody)
Definition: FlexFactory.cpp:227
RoR::FlexFactory::CheckAndLoadFlexbodyCache
void CheckAndLoadFlexbodyCache()
Definition: FlexFactory.cpp:407
RoR::FlexBodyFileIO::WriteMetadata
void WriteMetadata()
Definition: FlexFactory.cpp:190
RoR::FlexBodyRecordHeader::USES_SHARED_VERTEX_DATA
static const BitMask_t USES_SHARED_VERTEX_DATA
Definition: FlexFactory.h:61
RoR::FlexBodyFileIO::GetLoadedItem
FlexBodyCacheData * GetLoadedItem(unsigned index)
Definition: FlexFactory.h:119
RoR::FlexBodyRecordHeader::shared_buf_num_verts
int shared_buf_num_verts
Definition: FlexFactory.h:56
RoR::FlexMeshWheel
Consists of static mesh, representing the rim, and dynamic mesh, representing the tire.
Definition: FlexMeshWheel.h:40
RoR::FlexBodyRecordHeader::HAS_TEXTURE_BLEND
static const BitMask_t HAS_TEXTURE_BLEND
Definition: FlexFactory.h:63
RoR::FlexBodyFileIO::ReadFlexbodyLocatorList
void ReadFlexbodyLocatorList(FlexBodyCacheData *flexbody)
Definition: FlexFactory.cpp:240
BITMASK
#define BITMASK(OFFSET)
Definition: BitFlags.h:10
RoR::FlexFactory
Definition: FlexFactory.h:163
RoR::FlexBodyFileIO::FlexBodyFileMetadata
Definition: FlexFactory.h:124
RoR::FlexBodyRecordHeader::node_center
int node_center
Definition: FlexFactory.h:51
RoR::FlexFactory::m_rig_spawner
ActorSpawner * m_rig_spawner
Definition: FlexFactory.h:199
RoR::FlexBodyRecordHeader::IS_FAULTY
static const BitMask_t IS_FAULTY
Definition: FlexFactory.h:60
RoR::FlexFactory::m_is_flexbody_cache_loaded
bool m_is_flexbody_cache_loaded
Definition: FlexFactory.h:203
RoR::FlexBodyFileIO::CloseFile
void CloseFile()
Definition: FlexFactory.h:133
RoR::ActorSpawner
Processes a RigDef::Document (parsed from 'truck' file format) into a simulated gameplay object (Acto...
Definition: ActorSpawner.h:70
RoR::FlexBodyFileIO::ReadFlexbodyNormalsBuffer
void ReadFlexbodyNormalsBuffer(FlexBodyCacheData *flexbody)
Definition: FlexFactory.cpp:255
RoR::FlexFactory::m_flexbody_cache_next_index
unsigned int m_flexbody_cache_next_index
Definition: FlexFactory.h:204
RoR::FlexBodyFileIO::SaveFile
ResultCode SaveFile()
Definition: FlexFactory.cpp:320
RoR::FlexFactory::m_flexbody_cache
FlexBodyFileIO m_flexbody_cache
Definition: FlexFactory.h:201
RoR::FlexBodyFileIO::m_items_to_save
std::vector< FlexBody * > m_items_to_save
Definition: FlexFactory.h:156
RoR::FlexBodyFileIO::RESULT_CODE_ERR_FOPEN_FAILED
@ RESULT_CODE_ERR_FOPEN_FAILED
Definition: FlexFactory.h:104
RoR::FlexBodyFileIO::SIGNATURE
static const char * SIGNATURE
Definition: FlexFactory.h:112
BitFlags.h
Bit operations.
RoR::NodeNum_t
uint16_t NodeNum_t
Node position within Actor::ar_nodes; use RoR::NODENUM_INVALID as empty value.
Definition: ForwardDeclarations.h:52
RoR::FlexBodyFileIO::WriteToFile
void WriteToFile(void *source, size_t length)
Definition: FlexFactory.cpp:153
RoR::FlexBodyFileIO::RESULT_CODE_GENERAL_ERROR
@ RESULT_CODE_GENERAL_ERROR
Definition: FlexFactory.h:103
RoR::FlexBodyFileIO::m_file
FILE * m_file
Definition: FlexFactory.h:158
RoR::FlexBodyRecordHeader::camera_mode
int camera_mode
Definition: FlexFactory.h:55
RoR::FlexBodyFileIO::LoadFile
ResultCode LoadFile()
Definition: FlexFactory.cpp:359
RoR::FlexBodyRecordHeader::center_offset
Ogre::Vector3 center_offset
Definition: FlexFactory.h:54
RoR::FlexBodyFileIO::WriteFlexbodyHeader
void WriteFlexbodyHeader(FlexBody *flexbody)
Definition: FlexFactory.cpp:207
RoR::FlexBodyFileIO::ResultCode
ResultCode
Definition: FlexFactory.h:100
RoR::FlexBodyFileIO::m_cache_entry_number
int m_cache_entry_number
Definition: FlexFactory.h:160
RoR::FlexBodyFileIO::ReadAndCheckSignature
void ReadAndCheckSignature()
Definition: FlexFactory.cpp:179
RoR::FlexBodyRecordHeader
Definition: FlexFactory.h:48
RoR::FlexFactory::FlexFactory
FlexFactory()
Definition: FlexFactory.h:166
RoR::FlexBodyFileIO::RESULT_CODE_ERR_SIGNATURE_MISMATCH
@ RESULT_CODE_ERR_SIGNATURE_MISMATCH
Definition: FlexFactory.h:105
RoR::FlexBodyFileIO::ReadFromFile
void ReadFromFile(void *dest, size_t length)
Definition: FlexFactory.cpp:163
RoR::FlexBodyCacheData::src_normals
Ogre::Vector3 * src_normals
Definition: FlexFactory.h:80
RoR::FlexBodyFileIO::WriteSignature
void WriteSignature()
Definition: FlexFactory.cpp:173
RoR::FlexBodyFileIO::m_loaded_items
std::vector< FlexBodyCacheData > m_loaded_items
Definition: FlexFactory.h:157
RoR::FlexFactory::SaveFlexbodiesToCache
void SaveFlexbodiesToCache()
Definition: FlexFactory.cpp:417
RoR::FlexBodyFileIO::FlexBodyFileIO
FlexBodyFileIO()
Definition: FlexFactory.cpp:401
RoR::FlexFactory::m_is_flexbody_cache_enabled
bool m_is_flexbody_cache_enabled
Definition: FlexFactory.h:202
RoR::FlexBodyFileIO::RESULT_CODE_OK
@ RESULT_CODE_OK
Definition: FlexFactory.h:102
Locator_t.h
RoR::FlexBodyFileIO::ReadFlexbodyPositionsBuffer
void ReadFlexbodyPositionsBuffer(FlexBodyCacheData *flexbody)
Definition: FlexFactory.cpp:271
RoR::FlexBodyFileIO::WriteFlexbodyLocatorList
void WriteFlexbodyLocatorList(FlexBody *flexbody)
Definition: FlexFactory.cpp:234
RoR::FlexBodyCacheData::src_colors
Ogre::ARGB * src_colors
Definition: FlexFactory.h:81
RoR::FlexBodyFileIO::RESULT_CODE_ERR_VERSION_MISMATCH
@ RESULT_CODE_ERR_VERSION_MISMATCH
Definition: FlexFactory.h:106
RoR::FlexBodyFileIO::m_fileformat_version
unsigned int m_fileformat_version
Definition: FlexFactory.h:159
RoR::FlexBodyCacheData::FlexBodyCacheData
FlexBodyCacheData()
Definition: FlexFactory.h:68
RoR::FlexBodyFileIO::ReadFlexbodyColorsBuffer
void ReadFlexbodyColorsBuffer(FlexBodyCacheData *flexbody)
Definition: FlexFactory.cpp:290
RoR::FlexBodyRecordHeader::flags
BitMask_t flags
Definition: FlexFactory.h:58
RoR::FlexFactory::CreateFlexMeshWheel
FlexMeshWheel * CreateFlexMeshWheel(unsigned int wheel_index, int axis_node_1_index, int axis_node_2_index, int nstart, int nrays, float rim_radius, bool rim_reverse, std::string const &rim_mesh_name, std::string const &rim_mesh_rg, std::string const &tire_material_name, std::string const &tire_material_rg)
Definition: FlexFactory.cpp:114
BitMask_t
uint32_t BitMask_t
Definition: BitFlags.h:7
RoR::FlexBodyFileIO::ReadMetadata
void ReadMetadata(FlexBodyFileMetadata *meta)
Definition: FlexFactory.cpp:200
RoR::FlexBodyFileIO::AddItemToSave
void AddItemToSave(FlexBody *fb)
Definition: FlexFactory.h:118
RoR::FlexBodyFileIO::GetList
std::vector< FlexBody * > & GetList()
RoR::FlexbodyID_t
int FlexbodyID_t
Index to GfxActor::m_flexbodies, use RoR::FLEXBODYID_INVALID as empty value.
Definition: ForwardDeclarations.h:62
RoR::FlexBodyFileIO::WriteFlexbodyPositionsBuffer
void WriteFlexbodyPositionsBuffer(FlexBody *flexbody)
Definition: FlexFactory.cpp:265
RoR::FlexBody
Flexbody = A deformable mesh; updated on CPU every frame, then uploaded to video memory.
Definition: FlexBody.h:43
RoR::FlexBodyRecordHeader::num_submesh_vbufs
int num_submesh_vbufs
Definition: FlexFactory.h:57
RoR::FlexBodyRecordHeader::vertex_count
int vertex_count
Definition: FlexFactory.h:50
RoR::FlexBodyCacheData::locators
Locator_t * locators
1 loc per vertex
Definition: FlexFactory.h:82
RoR::FlexBodyFileIO::FILE_FORMAT_VERSION
static const unsigned int FILE_FORMAT_VERSION
Definition: FlexFactory.h:113
RoR::FlexBodyFileIO::FlexBodyFileMetadata::file_format_version
unsigned int file_format_version
Definition: FlexFactory.h:126
RoR::FlexBodyCacheData::dst_pos
Ogre::Vector3 * dst_pos
Definition: FlexFactory.h:79
RoR
Definition: AppContext.h:36
RoR::FlexBodyRecordHeader::node_y
int node_y
Definition: FlexFactory.h:53
RoR::FlexBodyFileIO::RESULT_CODE_ERR_CACHE_NUMBER_UNDEFINED
@ RESULT_CODE_ERR_CACHE_NUMBER_UNDEFINED
Definition: FlexFactory.h:107
RoR::FlexBodyFileIO
Enables saving and loading flexbodies from/to binary file.
Definition: FlexFactory.h:97
RoR::FlexBodyFileIO::WriteFlexbodyNormalsBuffer
void WriteFlexbodyNormalsBuffer(FlexBody *flexbody)
Definition: FlexFactory.cpp:249
RoR::FlexBodyFileIO::RESULT_CODE_FWRITE_OUTPUT_INCOMPLETE
@ RESULT_CODE_FWRITE_OUTPUT_INCOMPLETE
Definition: FlexFactory.h:109
RoR::FlexFactory::CreateFlexBody
FlexBody * CreateFlexBody(FlexbodyID_t flexbody_id, const NodeNum_t ref_node, const NodeNum_t x_node, const NodeNum_t y_node, Ogre::Vector3 offset, Ogre::Vector3 rotation, std::vector< unsigned int > &node_indices, const std::string &mesh_name, const std::string &resource_group_name)
Definition: FlexFactory.cpp:63