Unit.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "Unit.h"
  24. #include "World.h"
  25. #include "Allocator.h"
  26. #include "Log.h"
  27. #include "UnitResource.h"
  28. #include "SceneGraphManager.h"
  29. #include "Actor.h"
  30. #include "Controller.h"
  31. #include "PhysicsResource.h"
  32. #include "Device.h"
  33. #include "ResourceManager.h"
  34. #include "Sprite.h"
  35. namespace crown
  36. {
  37. //-----------------------------------------------------------------------------
  38. Unit::Unit(World& w, const ResourceId id, const UnitResource* ur, const Matrix4x4& pose)
  39. : m_world(w)
  40. , m_scene_graph(*w.scene_graph_manager()->create_scene_graph())
  41. , m_resource_id(id)
  42. , m_resource(ur)
  43. , m_num_cameras(0)
  44. , m_num_meshes(0)
  45. , m_num_sprites(0)
  46. , m_num_actors(0)
  47. , m_num_materials(0)
  48. {
  49. m_controller.component.id = INVALID_ID;
  50. create_objects(pose);
  51. }
  52. //-----------------------------------------------------------------------------
  53. Unit::~Unit()
  54. {
  55. destroy_objects();
  56. m_world.scene_graph_manager()->destroy_scene_graph(&m_scene_graph);
  57. }
  58. //-----------------------------------------------------------------------------
  59. void Unit::set_id(const UnitId id)
  60. {
  61. m_id = id;
  62. }
  63. //-----------------------------------------------------------------------------
  64. UnitId Unit::id()
  65. {
  66. return m_id;
  67. }
  68. //-----------------------------------------------------------------------------
  69. const UnitResource* Unit::resource() const
  70. {
  71. return m_resource;
  72. }
  73. //-----------------------------------------------------------------------------
  74. void Unit::create_objects(const Matrix4x4& pose)
  75. {
  76. // Create the scene graph
  77. m_scene_graph.create(pose, m_resource->num_scene_graph_nodes(), m_resource->scene_graph_nodes());
  78. create_camera_objects();
  79. create_renderable_objects();
  80. create_physics_objects();
  81. set_default_material();
  82. }
  83. //-----------------------------------------------------------------------------
  84. void Unit::destroy_objects()
  85. {
  86. // Destroy cameras
  87. for (uint32_t i = 0; i < m_num_cameras; i++)
  88. {
  89. m_world.destroy_camera(m_cameras[i].component);
  90. }
  91. m_num_cameras = 0;
  92. // Destroy meshes
  93. for (uint32_t i = 0; i < m_num_meshes; i++)
  94. {
  95. m_world.render_world()->destroy_mesh(m_meshes[i].component);
  96. }
  97. m_num_meshes = 0;
  98. // Destroy sprites
  99. for (uint32_t i = 0; i < m_num_sprites; i++)
  100. {
  101. m_world.render_world()->destroy_sprite(m_sprites[i].component);
  102. }
  103. m_num_sprites = 0;
  104. // Destroy actors
  105. for (uint32_t i = 0; i < m_num_actors; i++)
  106. {
  107. m_world.physics_world()->destroy_actor(m_actors[i].component);
  108. }
  109. m_num_actors = 0;
  110. // Destroy controller
  111. if (m_controller.component.id != INVALID_ID)
  112. {
  113. m_world.physics_world()->destroy_controller(m_controller.component);
  114. m_controller.component.id = INVALID_ID;
  115. }
  116. // Destroy materials
  117. for (uint32_t i = 0; i < m_num_materials; i++)
  118. {
  119. m_world.render_world()->destroy_material(m_materials[i].component);
  120. }
  121. m_num_materials = 0;
  122. // Destroy scene graph
  123. m_scene_graph.destroy();
  124. }
  125. //-----------------------------------------------------------------------------
  126. void Unit::create_camera_objects()
  127. {
  128. for (uint32_t i = 0; i < m_resource->num_cameras(); i++)
  129. {
  130. const UnitCamera camera = m_resource->get_camera(i);
  131. const CameraId cam = m_world.create_camera(m_scene_graph, camera.node);
  132. add_camera(camera.name, cam);
  133. }
  134. }
  135. //-----------------------------------------------------------------------------
  136. void Unit::create_renderable_objects()
  137. {
  138. // Create renderables
  139. for (uint32_t i = 0; i < m_resource->num_renderables(); i++)
  140. {
  141. const UnitRenderable renderable = m_resource->get_renderable(i);
  142. if (renderable.type == UnitRenderable::MESH)
  143. {
  144. MeshResource* mr = (MeshResource*) device()->resource_manager()->data(renderable.resource);
  145. MeshId mesh = m_world.render_world()->create_mesh(mr, m_scene_graph, renderable.node);
  146. add_mesh(renderable.name, mesh);
  147. }
  148. else if (renderable.type == UnitRenderable::SPRITE)
  149. {
  150. SpriteResource* sr = (SpriteResource*) device()->resource_manager()->data(renderable.resource);
  151. SpriteId sprite = m_world.render_world()->create_sprite(sr, m_scene_graph, renderable.node);
  152. add_sprite(renderable.name, sprite);
  153. }
  154. else
  155. {
  156. CE_FATAL("Oops, bad renderable type");
  157. }
  158. }
  159. // Create materials
  160. if (m_resource->material_resource().id != 0)
  161. {
  162. MaterialResource* mr = (MaterialResource*) device()->resource_manager()->data(m_resource->material_resource());
  163. add_material(string::murmur2_32("default", string::strlen("default"), 0), m_world.render_world()->create_material(mr));
  164. }
  165. }
  166. //-----------------------------------------------------------------------------
  167. void Unit::create_physics_objects()
  168. {
  169. if (m_resource->physics_resource().id != 0)
  170. {
  171. const PhysicsResource* pr = (PhysicsResource*) device()->resource_manager()->data(m_resource->physics_resource());
  172. // Create controller if any
  173. if (pr->has_controller())
  174. {
  175. set_controller(pr->controller().name, m_world.physics_world()->create_controller(pr, m_scene_graph, 0));
  176. }
  177. // Create actors if any
  178. for (uint32_t i = 0; i < pr->num_actors(); i++)
  179. {
  180. const PhysicsActor& actor = pr->actor(i);
  181. ActorId id = m_world.physics_world()->create_actor(pr, i, m_scene_graph, m_scene_graph.node(actor.node), this);
  182. add_actor(actor.name, id);
  183. }
  184. // Create joints if any
  185. for (uint32_t i = 0; i < pr->num_joints(); i++)
  186. {
  187. const PhysicsJoint& joint = pr->joint(i);
  188. Actor* a1 = actor_by_index(joint.actor_0);
  189. Actor* a2 = actor_by_index(joint.actor_1);
  190. JointId id = m_world.physics_world()->create_joint(pr, i, *a1, *a2);
  191. }
  192. }
  193. }
  194. //-----------------------------------------------------------------------------
  195. void Unit::set_default_material()
  196. {
  197. if (m_num_materials == 0) return;
  198. for (uint32_t i = 0; i < m_num_sprites; i++)
  199. {
  200. Sprite* s = m_world.render_world()->lookup_sprite(m_sprites[i].component);
  201. s->set_material(m_materials[0].component);
  202. }
  203. }
  204. //-----------------------------------------------------------------------------
  205. int32_t Unit::node(const char* name) const
  206. {
  207. return m_scene_graph.node(name);
  208. }
  209. //-----------------------------------------------------------------------------
  210. bool Unit::has_node(const char* name) const
  211. {
  212. return m_scene_graph.has_node(name);
  213. }
  214. //-----------------------------------------------------------------------------
  215. uint32_t Unit::num_nodes() const
  216. {
  217. return m_scene_graph.num_nodes();
  218. }
  219. //-----------------------------------------------------------------------------
  220. Vector3 Unit::local_position(int32_t node) const
  221. {
  222. return m_scene_graph.local_position(node);
  223. }
  224. //-----------------------------------------------------------------------------
  225. Quaternion Unit::local_rotation(int32_t node) const
  226. {
  227. return m_scene_graph.local_rotation(node);
  228. }
  229. //-----------------------------------------------------------------------------
  230. Matrix4x4 Unit::local_pose(int32_t node) const
  231. {
  232. return m_scene_graph.local_pose(node);
  233. }
  234. //-----------------------------------------------------------------------------
  235. Vector3 Unit::world_position(int32_t node) const
  236. {
  237. return m_scene_graph.world_position(node);
  238. }
  239. //-----------------------------------------------------------------------------
  240. Quaternion Unit::world_rotation(int32_t node) const
  241. {
  242. return m_scene_graph.world_rotation(node);
  243. }
  244. //-----------------------------------------------------------------------------
  245. Matrix4x4 Unit::world_pose(int32_t node) const
  246. {
  247. return m_scene_graph.world_pose(node);
  248. }
  249. //-----------------------------------------------------------------------------
  250. void Unit::set_local_position(int32_t node, const Vector3& pos)
  251. {
  252. m_scene_graph.set_local_position(node, pos);
  253. }
  254. //-----------------------------------------------------------------------------
  255. void Unit::set_local_rotation(int32_t node, const Quaternion& rot)
  256. {
  257. m_scene_graph.set_local_rotation(node, rot);
  258. }
  259. //-----------------------------------------------------------------------------
  260. void Unit::set_local_pose(int32_t node, const Matrix4x4& pose)
  261. {
  262. m_scene_graph.set_local_pose(node, pose);
  263. }
  264. //-----------------------------------------------------------------------------
  265. void Unit::link_node(int32_t child, int32_t parent)
  266. {
  267. m_scene_graph.link(child, parent);
  268. }
  269. //-----------------------------------------------------------------------------
  270. void Unit::unlink_node(int32_t child)
  271. {
  272. m_scene_graph.unlink(child);
  273. }
  274. //-----------------------------------------------------------------------------
  275. void Unit::update()
  276. {
  277. }
  278. //-----------------------------------------------------------------------------
  279. void Unit::reload(UnitResource* new_ur)
  280. {
  281. Matrix4x4 m = m_scene_graph.world_pose(0);
  282. destroy_objects();
  283. m_resource = new_ur;
  284. create_objects(m);
  285. }
  286. //-----------------------------------------------------------------------------
  287. void Unit::add_component(StringId32 name, Id component, uint32_t& size, Component* array)
  288. {
  289. Component comp;
  290. comp.name = name;
  291. comp.component = component;
  292. array[size] = comp;
  293. size++;
  294. }
  295. //-----------------------------------------------------------------------------
  296. Id Unit::find_component(const char* name, uint32_t size, Component* array)
  297. {
  298. uint32_t name_hash = string::murmur2_32(name, string::strlen(name), 0);
  299. Id comp;
  300. comp.id = INVALID_ID;
  301. for (uint32_t i = 0; i < size; i++)
  302. {
  303. if (name_hash == array[i].name)
  304. {
  305. comp = array[i].component;
  306. }
  307. }
  308. return comp;
  309. }
  310. //-----------------------------------------------------------------------------
  311. Id Unit::find_component(uint32_t index, uint32_t size, Component* array)
  312. {
  313. Id comp;
  314. comp.id = INVALID_ID;
  315. if (index < size)
  316. {
  317. comp = array[index].component;
  318. }
  319. return comp;
  320. }
  321. //-----------------------------------------------------------------------------
  322. Id Unit::find_component_by_index(StringId32 name, uint32_t size, Component* array)
  323. {
  324. Id comp;
  325. comp.id = INVALID_ID;
  326. for (uint32_t i = 0; i < size; i++)
  327. {
  328. if (name == array[i].name)
  329. {
  330. comp = array[i].component;
  331. }
  332. }
  333. return comp;
  334. }
  335. //-----------------------------------------------------------------------------
  336. void Unit::add_camera(StringId32 name, CameraId camera)
  337. {
  338. CE_ASSERT(m_num_cameras < CE_MAX_CAMERA_COMPONENTS, "Max camera number reached");
  339. add_component(name, camera, m_num_cameras, m_cameras);
  340. }
  341. //-----------------------------------------------------------------------------
  342. void Unit::add_mesh(StringId32 name, MeshId mesh)
  343. {
  344. CE_ASSERT(m_num_meshes < CE_MAX_MESH_COMPONENTS, "Max mesh number reached");
  345. add_component(name, mesh, m_num_meshes, m_meshes);
  346. }
  347. //-----------------------------------------------------------------------------
  348. void Unit::add_sprite(StringId32 name, SpriteId sprite)
  349. {
  350. CE_ASSERT(m_num_sprites < CE_MAX_SPRITE_COMPONENTS, "Max sprite number reached");
  351. add_component(name, sprite, m_num_sprites, m_sprites);
  352. }
  353. //-----------------------------------------------------------------------------
  354. void Unit::add_actor(StringId32 name, ActorId actor)
  355. {
  356. CE_ASSERT(m_num_actors < CE_MAX_ACTOR_COMPONENTS, "Max actor number reached");
  357. add_component(name, actor, m_num_actors, m_actors);
  358. }
  359. //-----------------------------------------------------------------------------
  360. void Unit::add_material(StringId32 name, MaterialId material)
  361. {
  362. CE_ASSERT(m_num_materials < CE_MAX_MATERIAL_COMPONENTS, "Max material number reached");
  363. add_component(name, material, m_num_materials, m_materials);
  364. }
  365. //-----------------------------------------------------------------------------
  366. void Unit::set_controller(StringId32 name, ControllerId controller)
  367. {
  368. m_controller.name = name;
  369. m_controller.component = controller;
  370. }
  371. //-----------------------------------------------------------------------------
  372. Camera* Unit::camera(const char* name)
  373. {
  374. CameraId cam = find_component(name, m_num_cameras, m_cameras);
  375. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with name '%s'", name);
  376. return m_world.lookup_camera(cam);
  377. }
  378. //-----------------------------------------------------------------------------
  379. Camera* Unit::camera(uint32_t i)
  380. {
  381. CameraId cam = find_component(i, m_num_cameras, m_cameras);
  382. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with index '%d'", i);
  383. return m_world.lookup_camera(cam);
  384. }
  385. //-----------------------------------------------------------------------------
  386. Mesh* Unit::mesh(const char* name)
  387. {
  388. MeshId mesh = find_component(name, m_num_meshes, m_meshes);
  389. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with name '%s'", name);
  390. return m_world.render_world()->lookup_mesh(mesh);
  391. }
  392. //-----------------------------------------------------------------------------
  393. Mesh* Unit::mesh(uint32_t i)
  394. {
  395. MeshId mesh = find_component(i, m_num_meshes, m_meshes);
  396. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with index '%d'", i);
  397. return m_world.render_world()->lookup_mesh(mesh);
  398. }
  399. //-----------------------------------------------------------------------------
  400. Sprite* Unit::sprite(const char* name)
  401. {
  402. SpriteId sprite = find_component(name, m_num_sprites, m_sprites);
  403. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with name '%s'", name);
  404. return m_world.render_world()->lookup_sprite(sprite);
  405. }
  406. //-----------------------------------------------------------------------------
  407. Sprite* Unit::sprite(uint32_t i)
  408. {
  409. SpriteId sprite = find_component(i, m_num_sprites, m_sprites);
  410. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with index '%d'", i);
  411. return m_world.render_world()->lookup_sprite(sprite);
  412. }
  413. //-----------------------------------------------------------------------------
  414. Actor* Unit::actor(const char* name)
  415. {
  416. ActorId actor = find_component(name, m_num_actors, m_actors);
  417. CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%s'", name);
  418. return m_world.physics_world()->lookup_actor(actor);
  419. }
  420. //-----------------------------------------------------------------------------
  421. Actor* Unit::actor(uint32_t i)
  422. {
  423. ActorId actor = find_component(i, m_num_actors, m_actors);
  424. CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%d'", i);
  425. return m_world.physics_world()->lookup_actor(actor);
  426. }
  427. //-----------------------------------------------------------------------------
  428. Actor* Unit::actor_by_index(StringId32 name)
  429. {
  430. ActorId actor = find_component_by_index(name, m_num_actors, m_actors);
  431. // CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%d'", name);
  432. return m_world.physics_world()->lookup_actor(actor);
  433. }
  434. //-----------------------------------------------------------------------------
  435. Controller* Unit::controller()
  436. {
  437. if (m_controller.component.id != INVALID_ID)
  438. {
  439. return m_world.physics_world()->lookup_controller(m_controller.component);
  440. }
  441. return NULL;
  442. }
  443. //-----------------------------------------------------------------------------
  444. Material* Unit::material(const char* name)
  445. {
  446. MaterialId material = find_component(name, m_num_materials, m_materials);
  447. CE_ASSERT(material.id != INVALID_ID, "Unit does not have material with name '%s'", name);
  448. return m_world.render_world()->lookup_material(material);
  449. }
  450. //-----------------------------------------------------------------------------
  451. Material* Unit::material(uint32_t i)
  452. {
  453. MaterialId material = find_component(i, m_num_materials, m_materials);
  454. CE_ASSERT(material.id != INVALID_ID, "Unit does not have material with name '%d'", i);
  455. return m_world.render_world()->lookup_material(material);
  456. }
  457. //-----------------------------------------------------------------------------
  458. bool Unit::is_a(const char* name)
  459. {
  460. DynamicString unit(name);
  461. unit += ".unit";
  462. return m_resource_id.id == string::murmur2_64(unit.c_str(), string::strlen(unit.c_str()), 0);
  463. }
  464. } // namespace crown