Unit.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. namespace crown
  29. {
  30. typedef Id CameraId;
  31. Unit::Unit()
  32. : m_world(NULL)
  33. , m_num_cameras(0)
  34. , m_num_meshes(0)
  35. {
  36. }
  37. //-----------------------------------------------------------------------------
  38. void Unit::create(World& world, UnitId id, const Vector3& pos, const Quaternion& rot)
  39. {
  40. m_root_node = m_scene_graph.create_node(-1, pos, rot);
  41. m_world = &world;
  42. m_id = id;
  43. }
  44. //-----------------------------------------------------------------------------
  45. void Unit::destroy()
  46. {
  47. }
  48. //-----------------------------------------------------------------------------
  49. Vector3 Unit::local_position(int32_t node) const
  50. {
  51. return m_scene_graph.local_position(node);
  52. }
  53. //-----------------------------------------------------------------------------
  54. Quaternion Unit::local_rotation(int32_t node) const
  55. {
  56. return m_scene_graph.local_rotation(node);
  57. }
  58. //-----------------------------------------------------------------------------
  59. Matrix4x4 Unit::local_pose(int32_t node) const
  60. {
  61. return m_scene_graph.local_pose(node);
  62. }
  63. //-----------------------------------------------------------------------------
  64. Vector3 Unit::world_position(int32_t node) const
  65. {
  66. return m_scene_graph.world_position(node);
  67. }
  68. //-----------------------------------------------------------------------------
  69. Quaternion Unit::world_rotation(int32_t node) const
  70. {
  71. return m_scene_graph.world_rotation(node);
  72. }
  73. //-----------------------------------------------------------------------------
  74. Matrix4x4 Unit::world_pose(int32_t node) const
  75. {
  76. return m_scene_graph.world_pose(node);
  77. }
  78. //-----------------------------------------------------------------------------
  79. void Unit::set_local_position(const Vector3& pos, int32_t node)
  80. {
  81. m_scene_graph.set_local_position(node, pos);
  82. }
  83. //-----------------------------------------------------------------------------
  84. void Unit::set_local_rotation(const Quaternion& rot, int32_t node)
  85. {
  86. m_scene_graph.set_local_rotation(node, rot);
  87. }
  88. //-----------------------------------------------------------------------------
  89. void Unit::set_local_pose(const Matrix4x4& pose, int32_t node)
  90. {
  91. m_scene_graph.set_local_pose(node, pose);
  92. }
  93. //-----------------------------------------------------------------------------
  94. void Unit::link_node(int32_t child, int32_t parent)
  95. {
  96. m_scene_graph.link(child, parent);
  97. }
  98. //-----------------------------------------------------------------------------
  99. void Unit::unlink_node(int32_t child)
  100. {
  101. m_scene_graph.unlink(child);
  102. }
  103. //-----------------------------------------------------------------------------
  104. void Unit::add_component(const char* name, Id component, uint32_t& size, Component* array)
  105. {
  106. Component comp;
  107. comp.name = hash::murmur2_32(name, string::strlen(name), 0);
  108. comp.component = component;
  109. array[size] = comp;
  110. size++;
  111. }
  112. //-----------------------------------------------------------------------------
  113. Id Unit::find_component(const char* name, uint32_t size, Component* array)
  114. {
  115. uint32_t name_hash = hash::murmur2_32(name, string::strlen(name), 0);
  116. Id comp;
  117. comp.id = INVALID_ID;
  118. for (uint32_t i = 0; i < size; i++)
  119. {
  120. if (name_hash == array[i].name)
  121. {
  122. comp = array[i].component;
  123. }
  124. }
  125. return comp;
  126. }
  127. //-----------------------------------------------------------------------------
  128. Id Unit::find_component(uint32_t index, uint32_t size, Component* array)
  129. {
  130. Id comp;
  131. comp.id = INVALID_ID;
  132. if (index < size)
  133. {
  134. comp = array[index].component;
  135. }
  136. return comp;
  137. }
  138. //-----------------------------------------------------------------------------
  139. void Unit::add_camera(const char* name, CameraId camera)
  140. {
  141. CE_ASSERT(m_num_cameras < MAX_CAMERA_COMPONENTS, "Max camera number reached");
  142. add_component(name, camera, m_num_cameras, m_cameras);
  143. }
  144. //-----------------------------------------------------------------------------
  145. void Unit::add_mesh(const char* name, MeshId mesh)
  146. {
  147. CE_ASSERT(m_num_meshes < MAX_MESH_COMPONENTS, "Max mesh number reached");
  148. add_component(name, mesh, m_num_meshes, m_meshes);
  149. }
  150. //-----------------------------------------------------------------------------
  151. Camera* Unit::camera(const char* name)
  152. {
  153. CameraId cam = find_component(name, m_num_cameras, m_cameras);
  154. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with name '%s'", name);
  155. return m_world->lookup_camera(cam);
  156. }
  157. //-----------------------------------------------------------------------------
  158. Camera* Unit::camera(uint32_t i)
  159. {
  160. CameraId cam = find_component(i, m_num_cameras, m_cameras);
  161. CE_ASSERT(cam.id != INVALID_ID, "Unit does not have camera with index '%d'", i);
  162. return m_world->lookup_camera(cam);
  163. }
  164. //-----------------------------------------------------------------------------
  165. Mesh* Unit::mesh(const char* name)
  166. {
  167. MeshId mesh = find_component(name, m_num_meshes, m_meshes);
  168. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with name '%s'", name);
  169. return m_world->lookup_mesh(mesh);
  170. }
  171. //-----------------------------------------------------------------------------
  172. Mesh* Unit::mesh(uint32_t i)
  173. {
  174. MeshId mesh = find_component(i, m_num_meshes, m_meshes);
  175. CE_ASSERT(mesh.id != INVALID_ID, "Unit does not have mesh with index '%d'", i);
  176. return m_world->lookup_mesh(mesh);
  177. }
  178. } // namespace crown