Unit.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 "IdTable.h"
  25. #include "World.h"
  26. #include "Allocator.h"
  27. #include "Log.h"
  28. #include "UnitResource.h"
  29. namespace crown
  30. {
  31. typedef Id CameraId;
  32. Unit::Unit()
  33. : m_world(NULL)
  34. , m_num_cameras(0)
  35. , m_num_meshes(0)
  36. {
  37. }
  38. //-----------------------------------------------------------------------------
  39. void Unit::create(World& world, UnitResource* ur, UnitId id, const Vector3& pos, const Quaternion& rot)
  40. {
  41. m_root_node = m_scene_graph.create_node(-1, pos, rot);
  42. // Create renderables
  43. for (uint32_t i = 0; i < ur->num_renderables(); i++)
  44. {
  45. int32_t node = m_scene_graph.create_node(m_root_node, Vector3::ZERO, Quaternion::IDENTITY);
  46. UnitRenderable renderable = ur->get_renderable(i);
  47. MeshId mesh = world.create_mesh(renderable.resource, node, Vector3::ZERO, Quaternion::IDENTITY);
  48. add_mesh(renderable.name, mesh);
  49. }
  50. // Create cameras
  51. for (uint32_t i = 0; i < ur->num_cameras(); i++)
  52. {
  53. int32_t cam_node = m_scene_graph.create_node(m_root_node, Vector3::ZERO, Quaternion::IDENTITY);
  54. UnitCamera camera = ur->get_camera(i);
  55. CameraId cam = world.create_camera(cam_node, Vector3::ZERO, Quaternion::IDENTITY);
  56. world.link_camera(cam, id, m_root_node);
  57. add_camera(camera.name, cam);
  58. }
  59. // FIXME FIXME FIXME - TEST CODE - FIXME FIXME FIXME
  60. SpriteId sprite = world.create_sprite("sprites/loading", m_root_node, Vector3::ZERO, Quaternion::IDENTITY);
  61. add_sprite(hash::murmur2_32("sprite", 6, 0), sprite);
  62. m_world = &world;
  63. m_resource = ur;
  64. m_id = id;
  65. }
  66. //-----------------------------------------------------------------------------
  67. void Unit::destroy()
  68. {
  69. }
  70. //-----------------------------------------------------------------------------
  71. Vector3 Unit::local_position(int32_t node) const
  72. {
  73. return m_scene_graph.local_position(node);
  74. }
  75. //-----------------------------------------------------------------------------
  76. Quaternion Unit::local_rotation(int32_t node) const
  77. {
  78. return m_scene_graph.local_rotation(node);
  79. }
  80. //-----------------------------------------------------------------------------
  81. Matrix4x4 Unit::local_pose(int32_t node) const
  82. {
  83. return m_scene_graph.local_pose(node);
  84. }
  85. //-----------------------------------------------------------------------------
  86. Vector3 Unit::world_position(int32_t node) const
  87. {
  88. return m_scene_graph.world_position(node);
  89. }
  90. //-----------------------------------------------------------------------------
  91. Quaternion Unit::world_rotation(int32_t node) const
  92. {
  93. return m_scene_graph.world_rotation(node);
  94. }
  95. //-----------------------------------------------------------------------------
  96. Matrix4x4 Unit::world_pose(int32_t node) const
  97. {
  98. return m_scene_graph.world_pose(node);
  99. }
  100. //-----------------------------------------------------------------------------
  101. void Unit::set_local_position(const Vector3& pos, int32_t node)
  102. {
  103. m_scene_graph.set_local_position(node, pos);
  104. }
  105. //-----------------------------------------------------------------------------
  106. void Unit::set_local_rotation(const Quaternion& rot, int32_t node)
  107. {
  108. m_scene_graph.set_local_rotation(node, rot);
  109. }
  110. //-----------------------------------------------------------------------------
  111. void Unit::set_local_pose(const Matrix4x4& pose, int32_t node)
  112. {
  113. m_scene_graph.set_local_pose(node, pose);
  114. }
  115. //-----------------------------------------------------------------------------
  116. void Unit::link_node(int32_t child, int32_t parent)
  117. {
  118. m_scene_graph.link(child, parent);
  119. }
  120. //-----------------------------------------------------------------------------
  121. void Unit::unlink_node(int32_t child)
  122. {
  123. m_scene_graph.unlink(child);
  124. }
  125. //-----------------------------------------------------------------------------
  126. void Unit::add_component(uint32_t name, Id component, uint32_t& size, Component* array)
  127. {
  128. Component comp;
  129. comp.name = name;
  130. comp.component = component;
  131. array[size] = comp;
  132. size++;
  133. }
  134. //-----------------------------------------------------------------------------
  135. Id Unit::find_component(const char* name, uint32_t size, Component* array)
  136. {
  137. uint32_t name_hash = hash::murmur2_32(name, string::strlen(name), 0);
  138. Id comp;
  139. comp.id = INVALID_ID;
  140. for (uint32_t i = 0; i < size; i++)
  141. {
  142. if (name_hash == array[i].name)
  143. {
  144. comp = array[i].component;
  145. }
  146. }
  147. return comp;
  148. }
  149. //-----------------------------------------------------------------------------
  150. Id Unit::find_component(uint32_t index, uint32_t size, Component* array)
  151. {
  152. Id comp;
  153. comp.id = INVALID_ID;
  154. if (index < size)
  155. {
  156. comp = array[index].component;
  157. }
  158. return comp;
  159. }
  160. //-----------------------------------------------------------------------------
  161. void Unit::add_camera(uint32_t name, CameraId camera)
  162. {
  163. CE_ASSERT(m_num_cameras < MAX_CAMERA_COMPONENTS, "Max camera number reached");
  164. add_component(name, camera, m_num_cameras, m_cameras);
  165. }
  166. //-----------------------------------------------------------------------------
  167. void Unit::add_mesh(uint32_t name, MeshId mesh)
  168. {
  169. CE_ASSERT(m_num_meshes < MAX_MESH_COMPONENTS, "Max mesh number reached");
  170. add_component(name, mesh, m_num_meshes, m_meshes);
  171. }
  172. //-----------------------------------------------------------------------------
  173. void Unit::add_sprite(uint32_t name, SpriteId sprite)
  174. {
  175. CE_ASSERT(m_num_sprites < MAX_SPRITE_COMPONENTS, "Max sprite number reached");
  176. add_component(name, sprite, m_num_sprites, m_sprites);
  177. }
  178. //-----------------------------------------------------------------------------
  179. Camera* Unit::camera(const char* name)
  180. {
  181. CameraId cam = find_component(name, m_num_cameras, m_cameras);
  182. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with name '%s'", name);
  183. return m_world->lookup_camera(cam);
  184. }
  185. //-----------------------------------------------------------------------------
  186. Camera* Unit::camera(uint32_t i)
  187. {
  188. CameraId cam = find_component(i, m_num_cameras, m_cameras);
  189. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with index '%d'", i);
  190. return m_world->lookup_camera(cam);
  191. }
  192. //-----------------------------------------------------------------------------
  193. Mesh* Unit::mesh(const char* name)
  194. {
  195. MeshId mesh = find_component(name, m_num_meshes, m_meshes);
  196. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with name '%s'", name);
  197. return m_world->lookup_mesh(mesh);
  198. }
  199. //-----------------------------------------------------------------------------
  200. Mesh* Unit::mesh(uint32_t i)
  201. {
  202. MeshId mesh = find_component(i, m_num_meshes, m_meshes);
  203. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with index '%d'", i);
  204. return m_world->lookup_mesh(mesh);
  205. }
  206. //-----------------------------------------------------------------------------
  207. Sprite* Unit::sprite(const char* name)
  208. {
  209. SpriteId sprite = find_component(name, m_num_sprites, m_sprites);
  210. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with name '%s'", name);
  211. return m_world->lookup_sprite(sprite);
  212. }
  213. //-----------------------------------------------------------------------------
  214. Sprite* Unit::sprite(uint32_t i)
  215. {
  216. SpriteId sprite = find_component(i, m_num_sprites, m_sprites);
  217. CE_ASSERT(sprite.id != INVALID_ID, "Unit does not have sprite with index '%d'", i);
  218. return m_world->lookup_sprite(sprite);
  219. }
  220. } // namespace crown