ViewerModule.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include "ViewerModule.h"
  2. //#include <Ogre.h>
  3. #include <OgreEntity.h>
  4. #include <OgreRoot.h>
  5. #include "core/Assert.h"
  6. #include "core/Logger.h"
  7. #include "core/Timer.h"
  8. #include "core/particles/ParticleSystemManager.h"
  9. #include "core/Walkmesh.h"
  10. #include "core/XmlWalkmeshFile.h"
  11. #include "core/XmlMapsFile.h"
  12. #include "core/XmlMapFile.h"
  13. #include "core/EntityManager.h"
  14. ViewerModule::ViewerModule():
  15. m_Entity( nullptr ),
  16. m_Walkmesh( nullptr ),
  17. m_Grid( nullptr ),
  18. m_Axis( nullptr )
  19. {
  20. m_SceneNode = Ogre::Root::getSingleton().getSceneManager( "Scene" )->getRootSceneNode()->createChildSceneNode( "ViewerModule" );
  21. //m_EntityState.SetPosition( 300, 5 );
  22. //m_EntityState.SetVisible( true );
  23. //m_EntityState.SetText( "Model: not loaded" );
  24. /*
  25. Ogre::ManualObject* grid = new Ogre::ManualObject("Grid");
  26. grid->begin("viewer/grid", Ogre::RenderOperation::OT_LINE_LIST);
  27. float grid_size = 1.0f;
  28. for (int j = -25; j < 25; ++j)
  29. {
  30. for (int i = -25; i < 25; ++i)
  31. {
  32. if (j == 24)
  33. {
  34. grid->position(grid_size * i, grid_size * (j + 1), 0); grid->position(grid_size * (i + 1), grid_size * (j + 1), 0);
  35. }
  36. if (i == -25)
  37. {
  38. grid->position(grid_size * i, grid_size * j, 0); grid->position(grid_size * i, grid_size * (j + 1), 0);
  39. }
  40. grid->position(grid_size * i, grid_size * j, 0); grid->position(grid_size * (i + 1), grid_size * j, 0);
  41. grid->position(grid_size * (i + 1), grid_size * j, 0); grid->position(grid_size * (i + 1), grid_size * (j + 1), 0);
  42. }
  43. }
  44. grid->end();
  45. Ogre::MeshPtr mesh = grid->convertToMesh("grid");
  46. Ogre::MeshSerializer ser;
  47. ser.exportMesh(mesh.getPointer(), "grid.mesh");
  48. Ogre::ManualObject* m_Axis = new Ogre::ManualObject("Axis");
  49. m_Axis->begin("viewer/axis", Ogre::RenderOperation::OT_LINE_LIST);
  50. float axis_size = 100.0f;
  51. m_Axis->position(0, 0, 0); m_Axis->colour(1.0f, 0, 0); m_Axis->position(axis_size, 0, 0); m_Axis->colour(1.0f, 0, 0);
  52. m_Axis->position(0, 0, 0); m_Axis->colour(0, 1.0f, 0); m_Axis->position(0, axis_size, 0); m_Axis->colour(0, 1.0f, 0);
  53. m_Axis->position(0, 0, 0); m_Axis->colour(0, 0, 1.0f); m_Axis->position(0, 0, axis_size); m_Axis->colour(0, 0, 1.0f);
  54. m_Axis->end();
  55. mesh = m_Axis->convertToMesh("axis");
  56. ser.exportMesh(mesh.getPointer(), "axis.mesh");
  57. */
  58. /*
  59. Ogre::ManualObject* no_model = new Ogre::ManualObject("NoModel");
  60. no_model->begin("system/no_model", Ogre::RenderOperation::OT_TRIANGLE_LIST);
  61. float cube_size = 1.0f;
  62. no_model->position(-cube_size / 2, cube_size / 2, 0); no_model->textureCoord(1, 0);
  63. no_model->position(cube_size / 2, cube_size / 2, 0); no_model->textureCoord(0, 0);
  64. no_model->position(-cube_size / 2, -cube_size / 2, 0); no_model->textureCoord(1, 1);
  65. no_model->position(cube_size / 2, -cube_size / 2, 0); no_model->textureCoord(0, 1);
  66. no_model->triangle(0, 1, 2);
  67. no_model->triangle(1, 3, 2);
  68. no_model->position(-cube_size / 2, cube_size / 2, cube_size); no_model->textureCoord(0, 0);
  69. no_model->position(cube_size / 2, cube_size / 2, cube_size); no_model->textureCoord(1, 0);
  70. no_model->position(-cube_size / 2, -cube_size / 2, cube_size); no_model->textureCoord(0, 1);
  71. no_model->position(cube_size / 2, -cube_size / 2, cube_size); no_model->textureCoord(1, 1);
  72. no_model->triangle(4, 6, 5);
  73. no_model->triangle(5, 6, 7);
  74. no_model->position(-cube_size / 2, -cube_size / 2, cube_size); no_model->textureCoord(1, 1);
  75. no_model->position(-cube_size / 2, cube_size / 2, cube_size); no_model->textureCoord(1, 0);
  76. no_model->position(-cube_size / 2, -cube_size / 2, 0); no_model->textureCoord(0, 1);
  77. no_model->position(-cube_size / 2, cube_size / 2, 0); no_model->textureCoord(0, 0);
  78. no_model->triangle(8, 9, 10);
  79. no_model->triangle(9, 11, 10);
  80. no_model->position(cube_size / 2, cube_size / 2, cube_size); no_model->textureCoord(0, 0);
  81. no_model->position(cube_size / 2, -cube_size / 2, cube_size); no_model->textureCoord(0, 1);
  82. no_model->position(cube_size / 2, cube_size / 2, 0); no_model->textureCoord(1, 0);
  83. no_model->position(cube_size / 2, -cube_size / 2, 0); no_model->textureCoord(1, 1);
  84. no_model->triangle(12, 13, 14);
  85. no_model->triangle(13, 15, 14);
  86. no_model->position(-cube_size / 2, cube_size / 2, cube_size); no_model->textureCoord(0, 0);
  87. no_model->position(cube_size / 2, cube_size / 2, cube_size); no_model->textureCoord(0, 1);
  88. no_model->position(-cube_size / 2, cube_size / 2, 0); no_model->textureCoord(1, 0);
  89. no_model->position(cube_size / 2, cube_size / 2, 0); no_model->textureCoord(1, 1);
  90. no_model->triangle(15, 16, 17);
  91. no_model->triangle(16, 18, 17);
  92. no_model->end();
  93. m_SceneNode->attachObject(no_model);
  94. // mesh = m_Axis->convertToMesh("axis");
  95. // ser.exportMesh(mesh.getPointer(), "axis.mesh");
  96. */
  97. m_Grid = Ogre::Root::getSingleton().getSceneManager( "Scene" )->createEntity( "Grid", "system/grid.mesh" );
  98. m_SceneNode->attachObject( m_Grid );
  99. m_Axis = Ogre::Root::getSingleton().getSceneManager( "Scene" )->createEntity( "Axis", "system/axis.mesh" );
  100. m_SceneNode->attachObject( m_Axis );
  101. m_SceneNode->setPosition( 0, 0, 0 );
  102. // m_ParticleSystem = ParticleSystemManager::getSingleton().CreateParticleSystem("Test1", "Test");
  103. // m_SceneNode->attachObject(m_ParticleSystem);
  104. LOG_TRIVIAL( "ViewerModule created." );
  105. }
  106. ViewerModule::~ViewerModule()
  107. {
  108. if( m_Grid != nullptr )
  109. {
  110. Ogre::Root::getSingleton().getSceneManager( "Scene" )->destroyEntity( m_Grid );
  111. }
  112. if( m_Axis != nullptr )
  113. {
  114. Ogre::Root::getSingleton().getSceneManager( "Scene" )->destroyEntity( m_Axis );
  115. }
  116. Clear();
  117. Ogre::Root::getSingleton().getSceneManager( "Scene" )->getRootSceneNode()->removeAndDestroyChild( "ViewerModule" );
  118. LOG_TRIVIAL( "ViewerModule destroyed." );
  119. }
  120. void
  121. ViewerModule::Input( const VGears::Event& event )
  122. {
  123. if (event.type == VGears::ET_KEY_PRESS && event.param1 == OIS::KC_DOWN)
  124. {
  125. bool change = false;
  126. if (m_Entity != nullptr)
  127. {
  128. if (m_Entity->getAllAnimationStates() != nullptr)
  129. {
  130. Ogre::AnimationStateIterator animations = m_Entity->getAllAnimationStates()->getAnimationStateIterator();
  131. while (animations.hasMoreElements() == true)
  132. {
  133. Ogre::AnimationState* state = animations.getNext();
  134. if (change == true)
  135. {
  136. state->setEnabled(true);
  137. state->setLoop(true);
  138. //m_EntityState.SetText("Model: " + m_Entity->getName() + ": " + state->getAnimationName());
  139. break;
  140. }
  141. if (state->getEnabled() == true && animations.hasMoreElements() != false)
  142. {
  143. state->setEnabled(false);
  144. state->setLoop(false);
  145. change = true;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. else if (event.type == VGears::ET_KEY_PRESS && event.param1 == OIS::KC_UP)
  152. {
  153. bool change = false;
  154. if (m_Entity != nullptr && m_Entity->getAllAnimationStates() != nullptr)
  155. {
  156. Ogre::AnimationStateIterator animations = m_Entity->getAllAnimationStates()->getAnimationStateIterator();
  157. Ogre::AnimationState* old_state;
  158. Ogre::AnimationState* new_state = animations.getNext();
  159. while (animations.hasMoreElements() == true)
  160. {
  161. old_state = new_state;
  162. new_state = animations.getNext();
  163. if (new_state->getEnabled() == true)
  164. {
  165. new_state->setEnabled(false);
  166. new_state->setLoop(false);
  167. old_state->setEnabled(true);
  168. old_state->setLoop(true);
  169. //m_EntityState.SetText("Model: " + m_Entity->getName() + ": " + old_state->getAnimationName());
  170. }
  171. }
  172. }
  173. }
  174. }
  175. void
  176. ViewerModule::Update()
  177. {
  178. float delta_time = Timer::getSingleton().GetGameTimeDelta();
  179. if (m_Entity != nullptr && m_Entity->getAllAnimationStates() != nullptr)
  180. {
  181. Ogre::AnimationStateIterator animations = m_Entity->getAllAnimationStates()->getAnimationStateIterator();
  182. while (animations.hasMoreElements() == true)
  183. {
  184. Ogre::AnimationState* state = animations.getNext();
  185. if (state->getEnabled() == true)
  186. {
  187. state->addTime(delta_time);
  188. }
  189. }
  190. }
  191. //m_ParticleSystem->Update(delta_time);
  192. }
  193. void
  194. ViewerModule::PlayAnimation(const Ogre::String& name)
  195. {
  196. if (m_Entity != nullptr)
  197. {
  198. Ogre::String info_text = "Model: " + m_Entity->getName();
  199. if (m_Entity->getAllAnimationStates() != nullptr && m_Entity->getAllAnimationStates()->hasAnimationState(name))
  200. {
  201. Ogre::AnimationStateIterator animations = m_Entity->getAllAnimationStates()->getAnimationStateIterator();
  202. while (animations.hasMoreElements() == true)
  203. {
  204. Ogre::AnimationState* state = animations.getNext();
  205. if (state->getEnabled() == true)
  206. {
  207. state->setEnabled(false);
  208. state->setLoop(false);
  209. }
  210. }
  211. Ogre::AnimationState* animation = m_Entity->getAnimationState(name);
  212. animation->setEnabled(true);
  213. animation->setLoop(true);
  214. info_text += ": " + name;
  215. }
  216. else
  217. {
  218. Ogre::LogManager::getSingletonPtr()->logMessage("[ERROR] ViewerModule: animation with name '" + name + "' doesn't exist in model '" + m_Entity->getName() + "'.");
  219. }
  220. //m_EntityState.SetText(info_text);
  221. }
  222. }
  223. void
  224. ViewerModule::Clear()
  225. {
  226. if (m_Entity != nullptr)
  227. {
  228. Ogre::Root::getSingleton().getSceneManager("Scene")->destroyEntity(m_Entity);
  229. m_Entity = nullptr;
  230. }
  231. if (m_Walkmesh != nullptr)
  232. {
  233. delete m_Walkmesh;
  234. m_Walkmesh = nullptr;
  235. }
  236. m_SceneNode->removeAndDestroyAllChildren();
  237. }
  238. void
  239. ViewerModule::SetModelToLoad( const Ogre::String& name )
  240. {
  241. Clear();
  242. if( Ogre::ResourceGroupManager::getSingleton().resourceExists( "Game", name ) == true)
  243. {
  244. m_Entity = Ogre::Root::getSingleton().getSceneManager( "Scene" )->createEntity( name, name, "Game" );
  245. //m_Entity->setDisplaySkeleton( true );
  246. //m_EntityState.SetText( "Model: '" + name + "': " );
  247. PlayAnimation( "idle" );
  248. m_SceneNode->attachObject( m_Entity );
  249. //m_SceneNode->showBoundingBox( true );
  250. }
  251. else
  252. {
  253. m_Entity = Ogre::Root::getSingleton().getSceneManager( "Scene" )->createEntity( Ogre::SceneManager::PT_CUBE );
  254. //m_EntityState.SetText( "Model: '" + name + "'" );
  255. m_SceneNode->attachObject( m_Entity );
  256. }
  257. }
  258. void
  259. ViewerModule::SetWalkmeshToLoad( const Ogre::String& name )
  260. {
  261. Clear();
  262. XmlMapsFile xml( "./data/game_data/maps.xml" );
  263. Ogre::String file_name = xml.GetMapFileNameByName( name );
  264. XmlMapFile xml_map( "./data/" + file_name );
  265. Ogre::String walkmeshfile_name_ = xml_map.GetWalkmeshFileName();
  266. if( walkmeshfile_name_ != "" )
  267. {
  268. XmlWalkmeshFile walkmesh_file( "./data/" + walkmeshfile_name_ );
  269. walkmesh_file.Load();
  270. m_Walkmesh = EntityManager::getSingleton().GetWalkmesh();
  271. //m_SceneNode->attachObject( m_Walkmesh );
  272. //m_Walkmesh->setVisible( true );
  273. }
  274. }