Unit.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 "PhysicsGraphManager.h"
  30. namespace crown
  31. {
  32. typedef Id CameraId;
  33. typedef Id SpriteId;
  34. Unit::Unit(World& w, SceneGraph& sg, const UnitResource* ur, const Matrix4x4& pose)
  35. : m_world(w)
  36. , m_scene_graph(sg)
  37. , m_resource(ur)
  38. , m_num_cameras(0)
  39. , m_num_meshes(0)
  40. , m_num_sprites(0)
  41. {
  42. // Log debug info
  43. Log::d("Creating unit...");
  44. Log::d("Num renderables = %d", ur->num_renderables());
  45. Log::d("Num cameras = %d", ur->num_cameras());
  46. Log::d("Num actors = %d", ur->num_actors());
  47. Log::d("Num scene graph nodes = %d", ur->num_scene_graph_nodes());
  48. create(pose);
  49. }
  50. //-----------------------------------------------------------------------------
  51. void Unit::set_id(const UnitId id)
  52. {
  53. m_id = id;
  54. }
  55. //-----------------------------------------------------------------------------
  56. UnitId Unit::id()
  57. {
  58. return m_id;
  59. }
  60. //-----------------------------------------------------------------------------
  61. void Unit::create(const Matrix4x4& pose)
  62. {
  63. // Create the scene graph
  64. m_scene_graph.create(m_resource->num_scene_graph_nodes(), m_resource->scene_graph_names(),
  65. m_resource->scene_graph_poses(), m_resource->scene_graph_parents());
  66. // Set root node pose
  67. m_scene_graph.set_local_pose(0, pose);
  68. // Create renderables
  69. for (uint32_t i = 0; i < m_resource->num_renderables(); i++)
  70. {
  71. UnitRenderable renderable = m_resource->get_renderable(i);
  72. switch (renderable.type)
  73. {
  74. case UnitRenderable::MESH:
  75. {
  76. MeshId mesh = m_world.create_mesh(renderable.resource, m_scene_graph, renderable.node);
  77. add_mesh(renderable.name, mesh);
  78. break;
  79. }
  80. case UnitRenderable::SPRITE:
  81. {
  82. SpriteId sprite = m_world.create_sprite(renderable.resource, m_scene_graph, renderable.node);
  83. add_sprite(renderable.name, sprite);
  84. break;
  85. }
  86. default:
  87. {
  88. CE_FATAL("Oops, bad renderable type");
  89. break;
  90. }
  91. }
  92. }
  93. // Create cameras
  94. for (uint32_t i = 0; i < m_resource->num_cameras(); i++)
  95. {
  96. UnitCamera camera = m_resource->get_camera(i);
  97. CameraId cam = m_world.create_camera(m_scene_graph, camera.node);
  98. add_camera(camera.name, cam);
  99. }
  100. }
  101. //-----------------------------------------------------------------------------
  102. void Unit::destroy()
  103. {
  104. // Destroy cameras
  105. for (uint32_t i = 0; i < m_num_cameras; i++)
  106. {
  107. m_world.destroy_camera(m_cameras[i].component);
  108. }
  109. // Destroy meshes
  110. for (uint32_t i = 0; i < m_num_meshes; i++)
  111. {
  112. m_world.destroy_mesh(m_meshes[i].component);
  113. }
  114. // Destroy sprites
  115. for (uint32_t i = 0; i < m_num_sprites; i++)
  116. {
  117. m_world.destroy_sprite(m_sprites[i].component);
  118. }
  119. }
  120. //-----------------------------------------------------------------------------
  121. Vector3 Unit::local_position(int32_t node) const
  122. {
  123. return m_scene_graph.local_position(node);
  124. }
  125. //-----------------------------------------------------------------------------
  126. Quaternion Unit::local_rotation(int32_t node) const
  127. {
  128. return m_scene_graph.local_rotation(node);
  129. }
  130. //-----------------------------------------------------------------------------
  131. Matrix4x4 Unit::local_pose(int32_t node) const
  132. {
  133. return m_scene_graph.local_pose(node);
  134. }
  135. //-----------------------------------------------------------------------------
  136. Vector3 Unit::world_position(int32_t node) const
  137. {
  138. return m_scene_graph.world_position(node);
  139. }
  140. //-----------------------------------------------------------------------------
  141. Quaternion Unit::world_rotation(int32_t node) const
  142. {
  143. return m_scene_graph.world_rotation(node);
  144. }
  145. //-----------------------------------------------------------------------------
  146. Matrix4x4 Unit::world_pose(int32_t node) const
  147. {
  148. return m_scene_graph.world_pose(node);
  149. }
  150. //-----------------------------------------------------------------------------
  151. void Unit::set_local_position(int32_t node, const Vector3& pos)
  152. {
  153. m_scene_graph.set_local_position(node, pos);
  154. }
  155. //-----------------------------------------------------------------------------
  156. void Unit::set_local_rotation(int32_t node, const Quaternion& rot)
  157. {
  158. m_scene_graph.set_local_rotation(node, rot);
  159. }
  160. //-----------------------------------------------------------------------------
  161. void Unit::set_local_pose(int32_t node, const Matrix4x4& pose)
  162. {
  163. m_scene_graph.set_local_pose(node, pose);
  164. }
  165. //-----------------------------------------------------------------------------
  166. void Unit::link_node(int32_t child, int32_t parent)
  167. {
  168. m_scene_graph.link(child, parent);
  169. }
  170. //-----------------------------------------------------------------------------
  171. void Unit::unlink_node(int32_t child)
  172. {
  173. m_scene_graph.unlink(child);
  174. }
  175. //-----------------------------------------------------------------------------
  176. void Unit::add_component(StringId32 name, Id component, uint32_t& size, Component* array)
  177. {
  178. Component comp;
  179. comp.name = name;
  180. comp.component = component;
  181. array[size] = comp;
  182. size++;
  183. }
  184. //-----------------------------------------------------------------------------
  185. Id Unit::find_component(const char* name, uint32_t size, Component* array)
  186. {
  187. uint32_t name_hash = hash::murmur2_32(name, string::strlen(name), 0);
  188. Id comp;
  189. comp.id = INVALID_ID;
  190. for (uint32_t i = 0; i < size; i++)
  191. {
  192. if (name_hash == array[i].name)
  193. {
  194. comp = array[i].component;
  195. }
  196. }
  197. return comp;
  198. }
  199. //-----------------------------------------------------------------------------
  200. Id Unit::find_component(uint32_t index, uint32_t size, Component* array)
  201. {
  202. Id comp;
  203. comp.id = INVALID_ID;
  204. if (index < size)
  205. {
  206. comp = array[index].component;
  207. }
  208. return comp;
  209. }
  210. //-----------------------------------------------------------------------------
  211. void Unit::add_camera(StringId32 name, CameraId camera)
  212. {
  213. CE_ASSERT(m_num_cameras < MAX_CAMERA_COMPONENTS, "Max camera number reached");
  214. add_component(name, camera, m_num_cameras, m_cameras);
  215. }
  216. //-----------------------------------------------------------------------------
  217. void Unit::add_mesh(StringId32 name, MeshId mesh)
  218. {
  219. CE_ASSERT(m_num_meshes < MAX_MESH_COMPONENTS, "Max mesh number reached");
  220. add_component(name, mesh, m_num_meshes, m_meshes);
  221. }
  222. //-----------------------------------------------------------------------------
  223. void Unit::add_sprite(StringId32 name, SpriteId sprite)
  224. {
  225. CE_ASSERT(m_num_sprites < MAX_SPRITE_COMPONENTS, "Max sprite number reached");
  226. add_component(name, sprite, m_num_sprites, m_sprites);
  227. }
  228. //-----------------------------------------------------------------------------
  229. void Unit::add_actor(StringId32 name, ActorId actor)
  230. {
  231. CE_ASSERT(m_num_actors < MAX_ACTOR_COMPONENTS, "Max actor number reached");
  232. add_component(name, actor, m_num_actors, m_actors);
  233. }
  234. //-----------------------------------------------------------------------------
  235. Camera* Unit::camera(const char* name)
  236. {
  237. CameraId cam = find_component(name, m_num_cameras, m_cameras);
  238. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with name '%s'", name);
  239. return m_world.lookup_camera(cam);
  240. }
  241. //-----------------------------------------------------------------------------
  242. Camera* Unit::camera(uint32_t i)
  243. {
  244. CameraId cam = find_component(i, m_num_cameras, m_cameras);
  245. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with index '%d'", i);
  246. return m_world.lookup_camera(cam);
  247. }
  248. //-----------------------------------------------------------------------------
  249. Mesh* Unit::mesh(const char* name)
  250. {
  251. MeshId mesh = find_component(name, m_num_meshes, m_meshes);
  252. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with name '%s'", name);
  253. return m_world.lookup_mesh(mesh);
  254. }
  255. //-----------------------------------------------------------------------------
  256. Mesh* Unit::mesh(uint32_t i)
  257. {
  258. MeshId mesh = find_component(i, m_num_meshes, m_meshes);
  259. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with index '%d'", i);
  260. return m_world.lookup_mesh(mesh);
  261. }
  262. //-----------------------------------------------------------------------------
  263. Sprite* Unit::sprite(const char* name)
  264. {
  265. SpriteId sprite = find_component(name, m_num_sprites, m_sprites);
  266. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with name '%s'", name);
  267. return m_world.lookup_sprite(sprite);
  268. }
  269. //-----------------------------------------------------------------------------
  270. Sprite* Unit::sprite(uint32_t i)
  271. {
  272. SpriteId sprite = find_component(i, m_num_sprites, m_sprites);
  273. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with index '%d'", i);
  274. return m_world.lookup_sprite(sprite);
  275. }
  276. //-----------------------------------------------------------------------------
  277. Actor* Unit::actor(const char* name)
  278. {
  279. ActorId actor = find_component(name, m_num_actors, m_actors);
  280. CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%s'", name);
  281. return m_world.lookup_actor(actor);
  282. }
  283. //-----------------------------------------------------------------------------
  284. Actor* Unit::actor(uint32_t i)
  285. {
  286. ActorId actor = find_component(i, m_num_actors, m_actors);
  287. CE_ASSERT(actor.id != INVALID_ID, "Unit does not have actor with name '%d'", i);
  288. return m_world.lookup_actor(actor);
  289. }
  290. } // namespace crown