RigsofRods
Soft-body Physics Simulation
MeshObject.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 
6  For more information, see http://www.rigsofrods.org/
7 
8  Rigs of Rods is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License version 3, as
10  published by the Free Software Foundation.
11 
12  Rigs of Rods is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
24 
25 #include "MeshObject.h"
26 
27 #include "Actor.h"
28 #include "Application.h"
29 #include "GfxScene.h"
30 #include "MeshLodGenerator/OgreMeshLodGenerator.h"
31 
32 using namespace Ogre;
33 using namespace RoR;
34 
35 MeshObject::MeshObject(Ogre::String meshName, Ogre::String entityRG, Ogre::String entityName, Ogre::SceneNode* m_scene_node)
36  : m_scene_node(m_scene_node)
37  , m_entity(nullptr)
38  , m_cast_shadows(true)
39 {
40  this->createEntity(meshName, entityRG, entityName);
41 }
42 
43 void MeshObject::setMaterialName(Ogre::String m)
44 {
45  if (m_entity)
46  {
47  m_entity->setMaterialName(m);
48  }
49 }
50 
52 {
53  m_cast_shadows = b;
54  if (m_scene_node && m_scene_node->numAttachedObjects())
55  {
56  m_scene_node->getAttachedObject(0)->setCastShadows(b);
57  }
58 }
59 
61 {
62  // Workaround: if the scenenode is not used (entity not attached) for some reason, try hiding the entity directly.
63  if (m_scene_node && m_scene_node->getAttachedObjects().size() > 0)
64  m_scene_node->setVisible(b);
65  else if (m_entity)
66  m_entity->setVisible(b);
67 }
68 
69 void MeshObject::createEntity(Ogre::String meshName, Ogre::String entityRG, Ogre::String entityName)
70 {
71  if (!m_scene_node)
72  return;
73 
74  try
75  {
76  m_mesh = Ogre::MeshManager::getSingleton().load(meshName, entityRG);
77 
78  // important: you need to add the LODs before creating the entity
79  // now find possible LODs, needs to be done before calling createEntity()
80  String basename, ext;
81  StringUtil::splitBaseFilename(meshName, basename, ext);
82 
83  // the classic LODs
84  FileInfoListPtr files = ResourceGroupManager::getSingleton().findResourceFileInfo(entityRG, basename + "_lod*.mesh");
85  for (FileInfoList::iterator iterFiles = files->begin(); iterFiles != files->end(); ++iterFiles)
86  {
87  String format = basename + "_lod%d.mesh";
88  int i = -1;
89  int r = sscanf(iterFiles->filename.c_str(), format.c_str(), &i);
90 
91  if (r <= 0 || i < 0)
92  continue;
93 
94  Ogre::MeshManager::getSingleton().load(iterFiles->filename, entityRG);
95  }
96 
97  // the custom LODs
98  FileInfoListPtr files2 = ResourceGroupManager::getSingleton().findResourceFileInfo(entityRG, basename + "_clod_*.mesh");
99  for (FileInfoList::iterator iterFiles = files2->begin(); iterFiles != files2->end(); ++iterFiles)
100  {
101  // and custom LODs
102  String format = basename + "_clod_%d.mesh";
103  int i = -1;
104  int r = sscanf(iterFiles->filename.c_str(), format.c_str(), &i);
105  if (r <= 0 || i < 0)
106  continue;
107 
108  Ogre::MeshManager::getSingleton().load(iterFiles->filename, entityRG);
109  }
110 
111  // now create an entity around the mesh and attach it to the scene graph
112 
113  m_entity = App::GetGfxScene()->GetSceneManager()->createEntity(entityName, meshName, entityRG);
114  m_entity->setCastShadows(m_cast_shadows);
115 
116  m_scene_node->attachObject(m_entity);
117  m_scene_node->setVisible(true);
118  }
119  catch (Ogre::Exception& e)
120  {
121  RoR::LogFormat("[RoR] Error creating entity of mesh '%s' (group: '%s'), message: %s",
122  meshName.c_str(), entityRG.c_str(), e.getFullDescription().c_str());
123  return;
124  }
125 }
MeshObject::MeshObject
MeshObject(Ogre::String meshName, Ogre::String entityRG, Ogre::String entityName, Ogre::SceneNode *sceneNode)
Definition: MeshObject.cpp:35
MeshObject::m_mesh
Ogre::MeshPtr m_mesh
Definition: MeshObject.h:50
MeshObject::createEntity
void createEntity(Ogre::String meshName, Ogre::String entityRG, Ogre::String entityName)
Definition: MeshObject.cpp:69
MeshObject::setMaterialName
void setMaterialName(Ogre::String m)
Definition: MeshObject.cpp:43
format
Truck file format(technical spec)
RoR::LogFormat
void LogFormat(const char *format,...)
Improved logging utility. Uses fixed 2Kb buffer.
Definition: Application.cpp:424
MeshObject.h
MeshObject::m_entity
Ogre::Entity * m_entity
Definition: MeshObject.h:49
MeshObject::m_cast_shadows
bool m_cast_shadows
Definition: MeshObject.h:51
Actor.h
RoR::GfxScene::GetSceneManager
Ogre::SceneManager * GetSceneManager()
Definition: GfxScene.h:64
MeshObject::m_scene_node
Ogre::SceneNode * m_scene_node
Definition: MeshObject.h:48
GfxScene.h
Application.h
Central state/object manager and communications hub.
files
This is a raw Ogre binding for Imgui No project cmake no just four source files
Definition: README-OgreImGui.txt:3
MeshObject::setVisible
void setVisible(bool b)
Definition: MeshObject.cpp:60
Ogre
Definition: ExtinguishableFireAffector.cpp:35
RoR
Definition: AppContext.h:36
MeshObject::setCastShadows
void setCastShadows(bool b)
Definition: MeshObject.cpp:51
RoR::App::GetGfxScene
GfxScene * GetGfxScene()
Definition: Application.cpp:276