RigsofRods
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  std::vector<ForvertTempData>& forvert_data
59  );
60 
61 public:
62 
63  enum class PlaceholderType
64  {
69  };
70  static const char* PlaceholderTypeToString(PlaceholderType type);
71 
76 
78  FlexBody(PlaceholderType, FlexbodyID_t id, const std::string& orig_meshname);
79  ~FlexBody();
80 
81  void reset();
82  void updateBlend();
83  void writeBlend();
84 
85  void computeFlexbody();
87 
88  bool isVisible() const;
89  void setVisible(bool visible);
90 
91  void setFlexbodyCastShadow(bool val);
92 
93  int getVertexCount() { return static_cast<int>(m_vertex_count); };
94  Locator_t& getVertexLocator(int vert) { ROR_ASSERT((size_t)vert < m_vertex_count); return m_locators[vert]; }
95  Ogre::Vector3 getVertexPos(int vert) { ROR_ASSERT((size_t)vert < m_vertex_count); return m_dst_pos[vert] + m_flexit_center; }
96  Ogre::Entity* getEntity() { return m_scene_entity; }
97  const std::string& getOrigMeshName() const { return m_orig_mesh_name; }
98  std::vector<NodeNum_t>& getForsetNodes() { return m_forset_nodes; };
99  std::string getOrigMeshInfo() { return m_orig_mesh_info; }
100  std::string getLiveMeshInfo() { return RoR::PrintMeshInfo("Live", m_scene_entity->getMesh()); }
101 
105 
106  FlexbodyID_t getID() const { return m_id; }
108  void destroyOgreObjects();
109 
110 private:
111 
112  void defragmentFlexbodyMesh();
113 
115  size_t m_vertex_count = 0;
116  Ogre::Vector3 m_flexit_center = Ogre::Vector3::ZERO;
117  FlexbodyID_t m_id = FLEXBODYID_INVALID; // Filled by FlexFactory
119 
120  Ogre::Vector3* m_dst_pos = nullptr;
121  Ogre::Vector3* m_src_normals = nullptr;
122  Ogre::Vector3* m_dst_normals = nullptr;
123  Ogre::ARGB* m_src_colors = nullptr;
124  Locator_t* m_locators = nullptr;
125 
129  Ogre::Vector3 m_center_offset = Ogre::Vector3::ZERO;
130  Ogre::SceneNode* m_scene_node = nullptr;
131  Ogre::Entity* m_scene_entity = nullptr;
133 
135  Ogre::HardwareVertexBufferSharedPtr m_shared_vbuf_pos;
136  Ogre::HardwareVertexBufferSharedPtr m_shared_vbuf_norm;
137  Ogre::HardwareVertexBufferSharedPtr m_shared_vbuf_color;
138 
141  Ogre::HardwareVertexBufferSharedPtr m_submesh_vbufs_pos[16];
142  Ogre::HardwareVertexBufferSharedPtr m_submesh_vbufs_norm[16];
143  Ogre::HardwareVertexBufferSharedPtr m_submesh_vbufs_color[16];
144 
146  bool m_has_texture = true;
147  bool m_has_texture_blend = true;
148  bool m_blend_changed = false;
149 
150  // Diagnostic data, not used for calculations
151  std::vector<NodeNum_t> m_forset_nodes;
152  std::string m_orig_mesh_info;
153  std::string m_orig_mesh_name;
154 };
155 
158 
159 } // 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:692
RoR::FlexBody::destroyOgreObjects
void destroyOgreObjects()
Definition: FlexBody.cpp:572
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, std::vector< ForvertTempData > &forvert_data)
Definition: FlexBody.cpp:38
RoR::FlexBody::m_scene_node
Ogre::SceneNode * m_scene_node
Definition: FlexBody.h:130
RoR::FlexBody::m_placeholder_type
PlaceholderType m_placeholder_type
Definition: FlexBody.h:118
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:151
RoR::FlexBody::m_submesh_vbufs_vertex_counts
int m_submesh_vbufs_vertex_counts[16]
Definition: FlexBody.h:140
RoR::FlexBody::m_uses_shared_vertex_data
bool m_uses_shared_vertex_data
Definition: FlexBody.h:145
RigDef_Prerequisites.h
RoR::FlexBody::updateFlexbodyVertexBuffers
void updateFlexbodyVertexBuffers()
Definition: FlexBody.cpp:661
RoR::FlexBody::m_orig_mesh_name
std::string m_orig_mesh_name
Definition: FlexBody.h:153
RoR::NODENUM_INVALID
static const NodeNum_t NODENUM_INVALID
Definition: ForwardDeclarations.h:55
RoR::FlexBody::defragmentFlexbodyMesh
void defragmentFlexbodyMesh()
Definition: FlexBody.cpp:793
RoR::FlexBody::m_shared_vbuf_pos
Ogre::HardwareVertexBufferSharedPtr m_shared_vbuf_pos
Definition: FlexBody.h:135
RoR::PrintMeshInfo
std::string PrintMeshInfo(std::string const &title, Ogre::MeshPtr mesh)
RoR::FlexBody::getForsetNodes
std::vector< NodeNum_t > & getForsetNodes()
Definition: FlexBody.h:98
RoR::FlexBody::getPlaceholderType
PlaceholderType getPlaceholderType() const
Definition: FlexBody.h:107
RoR::FlexBody::getYNode
NodeNum_t getYNode()
Definition: FlexBody.h:104
RoR::FlexBody::setVisible
void setVisible(bool visible)
Definition: FlexBody.cpp:606
RoR::FlexBody::getVertexLocator
Locator_t & getVertexLocator(int vert)
Definition: FlexBody.h:94
RoR::FlexBody::m_blend_changed
bool m_blend_changed
Definition: FlexBody.h:148
Utils.h
RoR::FlexBody::m_dst_pos
Ogre::Vector3 * m_dst_pos
Definition: FlexBody.h:120
RoR::FlexBody::m_dst_normals
Ogre::Vector3 * m_dst_normals
Definition: FlexBody.h:122
RoR::FlexBody::getVertexCount
int getVertexCount()
Definition: FlexBody.h:93
RoR::FlexFactory
Definition: FlexFactory.h:163
RoR::FlexBody::updateBlend
void updateBlend()
Definition: FlexBody.cpp:717
RoR::FlexBody::PlaceholderType::FAULTY_MESH_PLACEHOLDER
@ FAULTY_MESH_PLACEHOLDER
RoR::FlexBody::getLiveMeshInfo
std::string getLiveMeshInfo()
Definition: FlexBody.h:100
RoR::FlexBody::getOrigMeshName
const std::string & getOrigMeshName() const
Definition: FlexBody.h:97
RoR::FlexBody::m_has_texture
bool m_has_texture
Definition: FlexBody.h:146
RoR::FlexBody::m_camera_mode
int m_camera_mode
Visibility control {-2 = always, -1 = 3rdPerson only, 0+ = cinecam index}.
Definition: FlexBody.h:132
RoR::FLEXBODYID_INVALID
static const FlexbodyID_t FLEXBODYID_INVALID
Definition: ForwardDeclarations.h:65
RoR::NodeNum_t
uint16_t NodeNum_t
Node position within Actor::ar_nodes; use RoR::NODENUM_INVALID as empty value.
Definition: ForwardDeclarations.h:54
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:143
RoR::FlexBody::PlaceholderType::FAULTY_FORSET_PLACEHOLDER
@ FAULTY_FORSET_PLACEHOLDER
RoR::FlexBody::~FlexBody
~FlexBody()
Definition: FlexBody.cpp:547
RoR::FlexBody::m_src_colors
Ogre::ARGB * m_src_colors
Definition: FlexBody.h:123
RoR::FlexBody::m_shared_buf_num_verts
int m_shared_buf_num_verts
Definition: FlexBody.h:134
RoR::FlexBody::fb_camera_mode_active
CameraMode_t fb_camera_mode_active
Dynamic visibility mode {0 and higher = cinecam index}.
Definition: FlexBody.h:74
RoR::CameraMode_t
int CameraMode_t
Definition: Application.h:563
RoR::FlexBody::getOrigMeshInfo
std::string getOrigMeshInfo()
Definition: FlexBody.h:99
RoR::FlexBody::m_num_submesh_vbufs
int m_num_submesh_vbufs
Definition: FlexBody.h:139
RoR::FlexBody::isVisible
bool isVisible() const
Definition: FlexBody.cpp:597
Application.h
Central state/object manager and communications hub.
RoR::FlexBody::PlaceholderType
PlaceholderType
Definition: FlexBody.h:63
RoR::FlexBody::getEntity
Ogre::Entity * getEntity()
Definition: FlexBody.h:96
RoR::FlexBody::m_gfx_actor
RoR::GfxActor * m_gfx_actor
Definition: FlexBody.h:114
RoR::FlexBody::setFlexbodyCastShadow
void setFlexbodyCastShadow(bool val)
Definition: FlexBody.cpp:613
Locator_t.h
RoR::FlexBody::PlaceholderTypeToString
static const char * PlaceholderTypeToString(PlaceholderType type)
Definition: FlexBody.cpp:560
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:75
RoR::CAMERA_MODE_ALWAYS_VISIBLE
static CameraMode_t CAMERA_MODE_ALWAYS_VISIBLE
Definition: Application.h:565
RoR::FlexBody::m_flexit_center
Ogre::Vector3 m_flexit_center
Updated per frame.
Definition: FlexBody.h:116
RoR::FlexBody::getID
FlexbodyID_t getID() const
Definition: FlexBody.h:106
RoR::FlexBody::m_scene_entity
Ogre::Entity * m_scene_entity
Definition: FlexBody.h:131
GfxData.h
RoR::FlexBody::getVertexPos
Ogre::Vector3 getVertexPos(int vert)
Definition: FlexBody.h:95
RoR::FlexBody::m_locators
Locator_t * m_locators
1 loc per vertex
Definition: FlexBody.h:124
RoR::FlexBody::m_center_offset
Ogre::Vector3 m_center_offset
Definition: FlexBody.h:129
RoR::GfxActor
Definition: GfxActor.h:52
RoR::FlexBody::m_node_y
NodeNum_t m_node_y
Definition: FlexBody.h:128
RoR::FlexBody::m_src_normals
Ogre::Vector3 * m_src_normals
Definition: FlexBody.h:121
RoR::FlexBody::m_node_center
NodeNum_t m_node_center
Definition: FlexBody.h:126
RoR::FlexBody::m_has_texture_blend
bool m_has_texture_blend
Definition: FlexBody.h:147
RoR::FlexbodyID_t
int FlexbodyID_t
Index to GfxActor::m_flexbodies, use RoR::FLEXBODYID_INVALID as empty value.
Definition: ForwardDeclarations.h:64
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:117
RoR::FlexBody::getRefNode
NodeNum_t getRefNode()
Definition: FlexBody.h:102
RoR::FlexBody::computeFlexbody
void computeFlexbody()
Updates mesh deformation; works on CPU using local copy of vertex data.
Definition: FlexBody.cpp:620
RoR::FlexBody::m_shared_vbuf_color
Ogre::HardwareVertexBufferSharedPtr m_shared_vbuf_color
Definition: FlexBody.h:137
RoR::FlexBody::m_submesh_vbufs_pos
Ogre::HardwareVertexBufferSharedPtr m_submesh_vbufs_pos[16]
positions
Definition: FlexBody.h:141
RoR::FlexBody::getXNode
NodeNum_t getXNode()
Definition: FlexBody.h:103
RoR::FlexBody::writeBlend
void writeBlend()
Definition: FlexBody.cpp:701
RoR::FlexBody::m_shared_vbuf_norm
Ogre::HardwareVertexBufferSharedPtr m_shared_vbuf_norm
Definition: FlexBody.h:136
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:142
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:152
RoR::FlexBody::m_node_x
NodeNum_t m_node_x
Definition: FlexBody.h:127
RoR::FlexBody::m_vertex_count
size_t m_vertex_count
Definition: FlexBody.h:115