world.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*************************************************************************/
  2. /* world.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "world.h"
  30. #include "scene/3d/camera.h"
  31. #include "scene/3d/visibility_notifier.h"
  32. #include "scene/3d/spatial_indexer.h"
  33. #include "scene/scene_string_names.h"
  34. #include "octree.h"
  35. #include "camera_matrix.h"
  36. struct SpatialIndexer {
  37. Octree<VisibilityNotifier> octree;
  38. struct NotifierData {
  39. AABB aabb;
  40. OctreeElementID id;
  41. };
  42. Map<VisibilityNotifier*,NotifierData> notifiers;
  43. struct CameraData {
  44. Map<VisibilityNotifier*,uint64_t> notifiers;
  45. };
  46. Map<Camera*,CameraData> cameras;
  47. enum {
  48. VISIBILITY_CULL_MAX=32768
  49. };
  50. Vector<VisibilityNotifier*> cull;
  51. bool changed;
  52. uint64_t pass;
  53. uint64_t last_frame;
  54. void _notifier_add(VisibilityNotifier* p_notifier,const AABB& p_rect) {
  55. ERR_FAIL_COND(notifiers.has(p_notifier));
  56. notifiers[p_notifier].aabb=p_rect;
  57. notifiers[p_notifier].id = octree.create(p_notifier,p_rect);
  58. changed=true;
  59. }
  60. void _notifier_update(VisibilityNotifier* p_notifier,const AABB& p_rect) {
  61. Map<VisibilityNotifier*,NotifierData>::Element *E=notifiers.find(p_notifier);
  62. ERR_FAIL_COND(!E);
  63. if (E->get().aabb==p_rect)
  64. return;
  65. E->get().aabb=p_rect;
  66. octree.move(E->get().id,E->get().aabb);
  67. changed=true;
  68. }
  69. void _notifier_remove(VisibilityNotifier* p_notifier) {
  70. Map<VisibilityNotifier*,NotifierData>::Element *E=notifiers.find(p_notifier);
  71. ERR_FAIL_COND(!E);
  72. octree.erase(E->get().id);
  73. notifiers.erase(p_notifier);
  74. List<Camera*> removed;
  75. for (Map<Camera*,CameraData>::Element*F=cameras.front();F;F=F->next()) {
  76. Map<VisibilityNotifier*,uint64_t>::Element*G=F->get().notifiers.find(p_notifier);
  77. if (G) {
  78. F->get().notifiers.erase(G);
  79. removed.push_back(F->key());
  80. }
  81. }
  82. while(!removed.empty()) {
  83. p_notifier->_exit_camera(removed.front()->get());
  84. removed.pop_front();
  85. }
  86. changed=true;
  87. }
  88. void _add_camera(Camera* p_camera) {
  89. ERR_FAIL_COND(cameras.has(p_camera));
  90. CameraData vd;
  91. cameras[p_camera]=vd;
  92. changed=true;
  93. }
  94. void _update_camera(Camera* p_camera) {
  95. Map<Camera*,CameraData>::Element *E= cameras.find(p_camera);
  96. ERR_FAIL_COND(!E);
  97. changed=true;
  98. }
  99. void _remove_camera(Camera* p_camera) {
  100. ERR_FAIL_COND(!cameras.has(p_camera));
  101. List<VisibilityNotifier*> removed;
  102. for(Map<VisibilityNotifier*,uint64_t>::Element *E=cameras[p_camera].notifiers.front();E;E=E->next()) {
  103. removed.push_back(E->key());
  104. }
  105. while(!removed.empty()) {
  106. removed.front()->get()->_exit_camera(p_camera);
  107. removed.pop_front();
  108. }
  109. cameras.erase(p_camera);
  110. }
  111. void _update(uint64_t p_frame) {
  112. if (p_frame==last_frame)
  113. return;
  114. last_frame=p_frame;
  115. if (!changed)
  116. return;
  117. for (Map<Camera*,CameraData>::Element *E=cameras.front();E;E=E->next()) {
  118. pass++;
  119. Camera *c=E->key();
  120. Vector<Plane> planes = c->get_frustum();
  121. int culled = octree.cull_convex(planes,cull.ptr(),cull.size());
  122. VisibilityNotifier**ptr=cull.ptr();
  123. List<VisibilityNotifier*> added;
  124. List<VisibilityNotifier*> removed;
  125. for(int i=0;i<culled;i++) {
  126. //notifiers in frustum
  127. Map<VisibilityNotifier*,uint64_t>::Element *H=E->get().notifiers.find(ptr[i]);
  128. if (!H) {
  129. E->get().notifiers.insert(ptr[i],pass);
  130. added.push_back(ptr[i]);
  131. } else {
  132. H->get()=pass;
  133. }
  134. }
  135. for (Map<VisibilityNotifier*,uint64_t>::Element *F=E->get().notifiers.front();F;F=F->next()) {
  136. if (F->get()!=pass)
  137. removed.push_back(F->key());
  138. }
  139. while(!added.empty()) {
  140. added.front()->get()->_enter_camera(E->key());
  141. added.pop_front();
  142. }
  143. while(!removed.empty()) {
  144. E->get().notifiers.erase(removed.front()->get());
  145. removed.front()->get()->_exit_camera(E->key());
  146. removed.pop_front();
  147. }
  148. }
  149. changed=false;
  150. }
  151. SpatialIndexer() {
  152. pass=0;
  153. last_frame=0;
  154. changed=false;
  155. cull.resize(VISIBILITY_CULL_MAX);
  156. }
  157. };
  158. void World::_register_camera(Camera* p_camera) {
  159. #ifndef _3D_DISABLED
  160. indexer->_add_camera(p_camera);
  161. #endif
  162. }
  163. void World::_update_camera(Camera* p_camera){
  164. #ifndef _3D_DISABLED
  165. indexer->_update_camera(p_camera);
  166. #endif
  167. }
  168. void World::_remove_camera(Camera* p_camera){
  169. #ifndef _3D_DISABLED
  170. indexer->_remove_camera(p_camera);
  171. #endif
  172. }
  173. void World::_register_notifier(VisibilityNotifier* p_notifier,const AABB& p_rect){
  174. #ifndef _3D_DISABLED
  175. indexer->_notifier_add(p_notifier,p_rect);
  176. #endif
  177. }
  178. void World::_update_notifier(VisibilityNotifier* p_notifier,const AABB& p_rect){
  179. #ifndef _3D_DISABLED
  180. indexer->_notifier_update(p_notifier,p_rect);
  181. #endif
  182. }
  183. void World::_remove_notifier(VisibilityNotifier* p_notifier){
  184. #ifndef _3D_DISABLED
  185. indexer->_notifier_remove(p_notifier);
  186. #endif
  187. }
  188. void World::_update(uint64_t p_frame) {
  189. #ifndef _3D_DISABLED
  190. indexer->_update(p_frame);
  191. #endif
  192. }
  193. RID World::get_space() const {
  194. return space;
  195. }
  196. RID World::get_scenario() const{
  197. return scenario;
  198. }
  199. RID World::get_sound_space() const{
  200. return sound_space;
  201. }
  202. void World::set_environment(const Ref<Environment>& p_environment) {
  203. environment=p_environment;
  204. if (environment.is_valid())
  205. VS::get_singleton()->scenario_set_environment(scenario,environment->get_rid());
  206. else
  207. VS::get_singleton()->scenario_set_environment(scenario,RID());
  208. }
  209. Ref<Environment> World::get_environment() const {
  210. return environment;
  211. }
  212. PhysicsDirectSpaceState *World::get_direct_space_state() {
  213. return PhysicsServer::get_singleton()->space_get_direct_state(space);
  214. }
  215. void World::_bind_methods() {
  216. ObjectTypeDB::bind_method(_MD("get_space"),&World::get_space);
  217. ObjectTypeDB::bind_method(_MD("get_scenario"),&World::get_scenario);
  218. ObjectTypeDB::bind_method(_MD("get_sound_space"),&World::get_sound_space);
  219. ObjectTypeDB::bind_method(_MD("set_environment","env:Environment"),&World::set_environment);
  220. ObjectTypeDB::bind_method(_MD("get_environment:Environment"),&World::get_environment);
  221. ObjectTypeDB::bind_method(_MD("get_direct_space_state:PhysicsDirectSpaceState"),&World::get_direct_space_state);
  222. ADD_PROPERTY(PropertyInfo(Variant::OBJECT,"environment",PROPERTY_HINT_RESOURCE_TYPE,"Environment"),_SCS("set_environment"),_SCS("get_environment"));
  223. }
  224. World::World() {
  225. space = PhysicsServer::get_singleton()->space_create();
  226. scenario = VisualServer::get_singleton()->scenario_create();
  227. sound_space = SpatialSoundServer::get_singleton()->space_create();
  228. PhysicsServer::get_singleton()->space_set_active(space,true);
  229. #ifdef _3D_DISABLED
  230. indexer = NULL;
  231. #else
  232. indexer = memnew( SpatialIndexer );
  233. #endif
  234. }
  235. World::~World() {
  236. PhysicsServer::get_singleton()->free(space);
  237. VisualServer::get_singleton()->free(scenario);
  238. SpatialSoundServer::get_singleton()->free(sound_space);
  239. #ifndef _3D_DISABLED
  240. memdelete( indexer );
  241. #endif
  242. }