RigsofRods
Soft-body Physics Simulation
FlexBody.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 #pragma once
23 
24 #include "RigDef_Prerequisites.h"
25 #include "Application.h"
26 #include "Locator_t.h"
27 #include "SimData.h"
28 #include "GfxData.h"
29 #include "RigDef_File.h"
30 #include "Utils.h"
31 
32 #include <Ogre.h>
33 
34 namespace RoR {
35 
38 
41 
43 class FlexBody
44 {
45  friend class RoR::FlexFactory;
46  friend class RoR::FlexBodyFileIO;
47 
48  FlexBody( // Private, for FlexFactory
49  RoR::FlexBodyCacheData* preloaded_from_cache,
50  RoR::GfxActor* gfx_actor,
51  Ogre::Entity* entity,
52  NodeNum_t ref,
53  NodeNum_t nx,
54  NodeNum_t ny,
55  Ogre::Vector3 offset,
56  Ogre::Quaternion const & rot,
57  std::vector<unsigned int> & node_indices
58  );
59 
60 public:
61 
62  enum class PlaceholderType
63  {
68  };
69  static const char* PlaceholderTypeToString(PlaceholderType type);
70 
75 
77  FlexBody(PlaceholderType, FlexbodyID_t id, const std::string& orig_meshname);
78  ~FlexBody();
79 
80  void reset();
81  void updateBlend();
82  void writeBlend();
83 
84  void computeFlexbody();
86 
87  bool isVisible() const;
88  void setVisible(bool visible);
89 
90  void setFlexbodyCastShadow(bool val);
91 
92  int getVertexCount() { return static_cast<int>(m_vertex_count); };
93  Locator_t& getVertexLocator(int vert) { ROR_ASSERT((size_t)vert < m_vertex_count); return m_locators[vert]; }
94  Ogre::Vector3 getVertexPos(int vert) { ROR_ASSERT((size_t)vert < m_vertex_count); return m_dst_pos[vert] + m_flexit_center; }
95  Ogre::Entity* getEntity() { return m_scene_entity; }
96  const std::string& getOrigMeshName() const { return m_orig_mesh_name; }
97  std::vector<NodeNum_t>& getForsetNodes() { return m_forset_nodes; };
98  std::string getOrigMeshInfo() { return m_orig_mesh_info; }
99  std::string getLiveMeshInfo() { return RoR::PrintMeshInfo("Live", m_scene_entity->getMesh()); }
100 
104 
105  FlexbodyID_t getID() const { return m_id; }
107  void destroyOgreObjects();
108 
109 private:
110 
111  void defragmentFlexbodyMesh();
112 
114  size_t m_vertex_count = 0;
115  Ogre::Vector3 m_flexit_center = Ogre::Vector3::ZERO;
116  FlexbodyID_t m_id = FLEXBODYID_INVALID; // Filled by FlexFactory
118 
119  Ogre::Vector3* m_dst_pos = nullptr;
120  Ogre::Vector3* m_src_normals = nullptr;
121  Ogre::Vector3* m_dst_normals = nullptr;
122  Ogre::ARGB* m_src_colors = nullptr;
123  Locator_t* m_locators = nullptr;
124 
128  Ogre::Vector3 m_center_offset = Ogre::Vector3::ZERO;
129  Ogre::SceneNode* m_scene_node = nullptr;
130  Ogre::Entity* m_scene_entity = nullptr;
132 
134  Ogre::HardwareVertexBufferSharedPtr m_shared_vbuf_pos;
135  Ogre::HardwareVertexBufferSharedPtr m_shared_vbuf_norm;
136  Ogre::HardwareVertexBufferSharedPtr m_shared_vbuf_color;
137 
140  Ogre::HardwareVertexBufferSharedPtr m_submesh_vbufs_pos[16];
141  Ogre::HardwareVertexBufferSharedPtr m_submesh_vbufs_norm[16];
142  Ogre::HardwareVertexBufferSharedPtr m_submesh_vbufs_color[16];
143 
145  bool m_has_texture = true;
146  bool m_has_texture_blend = true;
147  bool m_blend_changed = false;
148 
149  // Diagnostic data, not used for calculations
150  std::vector<NodeNum_t> m_forset_nodes;
151  std::string m_orig_mesh_info;
152  std::string m_orig_mesh_name;
153 };
154 
157 
158 } // namespace RoR
ROR_ASSERT
#define ROR_ASSERT(_EXPR)
Definition: Application.h:40
RoR::FlexBodyCacheData
Definition: FlexFactory.h:66
RoR::FlexBody::reset
void reset()
Definition: FlexBody.cpp:675
RoR::FlexBody::destroyOgreObjects
void destroyOgreObjects()
Definition: FlexBody.cpp:555
RoR::FlexBody::m_scene_node
Ogre::SceneNode * m_scene_node
Definition: FlexBody.h:129
RoR::FlexBody::m_placeholder_type
PlaceholderType m_placeholder_type
Definition: FlexBody.h:117
RoR::FlexBody::PlaceholderType::NOT_A_PLACEHOLDER
@ NOT_A_PLACEHOLDER
RoR::Locator_t
Definition: Locator_t.h:10
RoR::FlexBody::m_forset_nodes
std::vector< NodeNum_t > m_forset_nodes
Definition: FlexBody.h:150
RoR::FlexBody::m_submesh_vbufs_vertex_counts
int m_submesh_vbufs_vertex_counts[16]
Definition: FlexBody.h:139
RoR::FlexBody::m_uses_shared_vertex_data
bool m_uses_shared_vertex_data
Definition: FlexBody.h:144
RoR::CameraMode_t
int CameraMode_t
Definition: GfxData.h:123
RigDef_Prerequisites.h
RoR::FlexBody::updateFlexbodyVertexBuffers
void updateFlexbodyVertexBuffers()
Definition: FlexBody.cpp:644
RoR::FlexBody::m_orig_mesh_name
std::string m_orig_mesh_name
Definition: FlexBody.h:152
RoR::NODENUM_INVALID
static const NodeNum_t NODENUM_INVALID
Definition: ForwardDeclarations.h:53
RoR::FlexBody::defragmentFlexbodyMesh
void defragmentFlexbodyMesh()
Definition: FlexBody.cpp:776
RoR::FlexBody::m_shared_vbuf_pos
Ogre::HardwareVertexBufferSharedPtr m_shared_vbuf_pos
Definition: FlexBody.h:134
RoR::PrintMeshInfo
std::string PrintMeshInfo(std::string const &title, Ogre::MeshPtr mesh)
RoR::FlexBody::getForsetNodes
std::vector< NodeNum_t > & getForsetNodes()
Definition: FlexBody.h:97
RoR::FlexBody::getPlaceholderType
PlaceholderType getPlaceholderType() const
Definition: FlexBody.h:106
RoR::FlexBody::getYNode
NodeNum_t getYNode()
Definition: FlexBody.h:103
RoR::FlexBody::setVisible
void setVisible(bool visible)
Definition: FlexBody.cpp:589
RoR::FlexBody::getVertexLocator
Locator_t & getVertexLocator(int vert)
Definition: FlexBody.h:93
RoR::FlexBody::FlexBody
FlexBody(RoR::FlexBodyCacheData *preloaded_from_cache, RoR::GfxActor *gfx_actor, Ogre::Entity *entity, NodeNum_t ref, NodeNum_t nx, NodeNum_t ny, Ogre::Vector3 offset, Ogre::Quaternion const &rot, std::vector< unsigned int > &node_indices)
Definition: FlexBody.cpp:38
RoR::FlexBody::m_blend_changed
bool m_blend_changed
Definition: FlexBody.h:147
Utils.h
RoR::FlexBody::m_dst_pos
Ogre::Vector3 * m_dst_pos
Definition: FlexBody.h:119
RoR::FlexBody::m_dst_normals
Ogre::Vector3 * m_dst_normals
Definition: FlexBody.h:121
RoR::FlexBody::getVertexCount
int getVertexCount()
Definition: FlexBody.h:92
RoR::FlexFactory
Definition: FlexFactory.h:163
RoR::FlexBody::updateBlend
void updateBlend()
Definition: FlexBody.cpp:700
RoR::FlexBody::PlaceholderType::FAULTY_MESH_PLACEHOLDER
@ FAULTY_MESH_PLACEHOLDER
RoR::FlexBody::getLiveMeshInfo
std::string getLiveMeshInfo()
Definition: FlexBody.h:99
RoR::FlexBody::getOrigMeshName
const std::string & getOrigMeshName() const
Definition: FlexBody.h:96
RoR::FlexBody::m_has_texture
bool m_has_texture
Definition: FlexBody.h:145
RoR::FlexBody::m_camera_mode
int m_camera_mode
Visibility control {-2 = always, -1 = 3rdPerson only, 0+ = cinecam index}.
Definition: FlexBody.h:131
RoR::FLEXBODYID_INVALID
static const FlexbodyID_t FLEXBODYID_INVALID
Definition: ForwardDeclarations.h:63
RoR::NodeNum_t
uint16_t NodeNum_t
Node position within Actor::ar_nodes; use RoR::NODENUM_INVALID as empty value.
Definition: ForwardDeclarations.h:52
SimData.h
Core data structures for simulation; Everything affected by by either physics, network or user intera...
RoR::FlexBody::m_submesh_vbufs_color
Ogre::HardwareVertexBufferSharedPtr m_submesh_vbufs_color[16]
colors
Definition: FlexBody.h:142
RoR::FlexBody::PlaceholderType::FAULTY_FORSET_PLACEHOLDER
@ FAULTY_FORSET_PLACEHOLDER
RoR::FlexBody::~FlexBody
~FlexBody()
Definition: FlexBody.cpp:530
RoR::FlexBody::m_src_colors
Ogre::ARGB * m_src_colors
Definition: FlexBody.h:122
RoR::FlexBody::m_shared_buf_num_verts
int m_shared_buf_num_verts
Definition: FlexBody.h:133
RoR::FlexBody::fb_camera_mode_active
CameraMode_t fb_camera_mode_active
Dynamic visibility mode {0 and higher = cinecam index}.
Definition: FlexBody.h:73
RoR::FlexBody::getOrigMeshInfo
std::string getOrigMeshInfo()
Definition: FlexBody.h:98
RoR::FlexBody::m_num_submesh_vbufs
int m_num_submesh_vbufs
Definition: FlexBody.h:138
RoR::FlexBody::isVisible
bool isVisible() const
Definition: FlexBody.cpp:580
Application.h
Central state/object manager and communications hub.
RoR::FlexBody::PlaceholderType
PlaceholderType
Definition: FlexBody.h:62
RoR::FlexBody::getEntity
Ogre::Entity * getEntity()
Definition: FlexBody.h:95
RoR::FlexBody::m_gfx_actor
RoR::GfxActor * m_gfx_actor
Definition: FlexBody.h:113
RoR::FlexBody::setFlexbodyCastShadow
void setFlexbodyCastShadow(bool val)
Definition: FlexBody.cpp:596
Locator_t.h
RoR::FlexBody::PlaceholderTypeToString
static const char * PlaceholderTypeToString(PlaceholderType type)
Definition: FlexBody.cpp:543
RoR::FlexBody::PlaceholderType::TUNING_REMOVED_PLACEHOLDER
@ TUNING_REMOVED_PLACEHOLDER
RoR::FlexBody::fb_camera_mode_orig
CameraMode_t fb_camera_mode_orig
Dynamic visibility mode {0 and higher = cinecam index}.
Definition: FlexBody.h:74
RoR::FlexBody::m_flexit_center
Ogre::Vector3 m_flexit_center
Updated per frame.
Definition: FlexBody.h:115
RoR::FlexBody::getID
FlexbodyID_t getID() const
Definition: FlexBody.h:105
RoR::FlexBody::m_scene_entity
Ogre::Entity * m_scene_entity
Definition: FlexBody.h:130
GfxData.h
RoR::FlexBody::getVertexPos
Ogre::Vector3 getVertexPos(int vert)
Definition: FlexBody.h:94
RoR::CAMERA_MODE_ALWAYS_VISIBLE
static CameraMode_t CAMERA_MODE_ALWAYS_VISIBLE
Definition: GfxData.h:125
RoR::FlexBody::m_locators
Locator_t * m_locators
1 loc per vertex
Definition: FlexBody.h:123
RoR::FlexBody::m_center_offset
Ogre::Vector3 m_center_offset
Definition: FlexBody.h:128
RoR::GfxActor
Definition: GfxActor.h:52
RoR::FlexBody::m_node_y
NodeNum_t m_node_y
Definition: FlexBody.h:127
RoR::FlexBody::m_src_normals
Ogre::Vector3 * m_src_normals
Definition: FlexBody.h:120
RoR::FlexBody::m_node_center
NodeNum_t m_node_center
Definition: FlexBody.h:125
RoR::FlexBody::m_has_texture_blend
bool m_has_texture_blend
Definition: FlexBody.h:146
RoR::FlexbodyID_t
int FlexbodyID_t
Index to GfxActor::m_flexbodies, use RoR::FLEXBODYID_INVALID as empty value.
Definition: ForwardDeclarations.h:62
RoR::FlexBody
Flexbody = A deformable mesh; updated on CPU every frame, then uploaded to video memory.
Definition: FlexBody.h:43
RoR::FlexBody::m_id
FlexbodyID_t m_id
Definition: FlexBody.h:116
RoR::FlexBody::getRefNode
NodeNum_t getRefNode()
Definition: FlexBody.h:101
RoR::FlexBody::computeFlexbody
void computeFlexbody()
Updates mesh deformation; works on CPU using local copy of vertex data.
Definition: FlexBody.cpp:603
RoR::FlexBody::m_shared_vbuf_color
Ogre::HardwareVertexBufferSharedPtr m_shared_vbuf_color
Definition: FlexBody.h:136
RoR::FlexBody::m_submesh_vbufs_pos
Ogre::HardwareVertexBufferSharedPtr m_submesh_vbufs_pos[16]
positions
Definition: FlexBody.h:140
RoR::FlexBody::getXNode
NodeNum_t getXNode()
Definition: FlexBody.h:102
RoR::FlexBody::writeBlend
void writeBlend()
Definition: FlexBody.cpp:684
RoR::FlexBody::m_shared_vbuf_norm
Ogre::HardwareVertexBufferSharedPtr m_shared_vbuf_norm
Definition: FlexBody.h:135
RigDef_File.h
Data structures representing 'truck' file format, see https://docs.rigsofrods.org/vehicle-creation/fi...
RoR
Definition: AppContext.h:36
RoR::FlexBody::m_submesh_vbufs_norm
Ogre::HardwareVertexBufferSharedPtr m_submesh_vbufs_norm[16]
normals
Definition: FlexBody.h:141
RoR::FlexBodyFileIO
Enables saving and loading flexbodies from/to binary file.
Definition: FlexFactory.h:97
RoR::FlexBody::m_orig_mesh_info
std::string m_orig_mesh_info
Definition: FlexBody.h:151
RoR::FlexBody::m_node_x
NodeNum_t m_node_x
Definition: FlexBody.h:126
RoR::FlexBody::m_vertex_count
size_t m_vertex_count
Definition: FlexBody.h:114