room_instance.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*************************************************************************/
  2. /* room_instance.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 "room_instance.h"
  30. #include "servers/visual_server.h"
  31. #include "geometry.h"
  32. #include "globals.h"
  33. #include "scene/resources/surface_tool.h"
  34. void Room::_notification(int p_what) {
  35. switch(p_what) {
  36. case NOTIFICATION_ENTER_WORLD: {
  37. // go find parent level
  38. Node *parent_room=get_parent();
  39. level=0;
  40. while(parent_room) {
  41. Room *r = parent_room->cast_to<Room>();
  42. if (r) {
  43. level=r->level+1;
  44. break;
  45. }
  46. parent_room=parent_room->get_parent();
  47. }
  48. if (sound_enabled)
  49. SpatialSoundServer::get_singleton()->room_set_space(sound_room,get_world()->get_sound_space());
  50. } break;
  51. case NOTIFICATION_TRANSFORM_CHANGED: {
  52. SpatialSoundServer::get_singleton()->room_set_transform(sound_room,get_global_transform());
  53. } break;
  54. case NOTIFICATION_EXIT_WORLD: {
  55. if (sound_enabled)
  56. SpatialSoundServer::get_singleton()->room_set_space(sound_room,RID());
  57. } break;
  58. }
  59. }
  60. RES Room::_get_gizmo_geometry() const {
  61. DVector<Face3> faces;
  62. if (!room.is_null())
  63. faces=room->get_geometry_hint();
  64. int count=faces.size();
  65. if (count==0)
  66. return RES();
  67. DVector<Face3>::Read facesr=faces.read();
  68. const Face3* facesptr=facesr.ptr();
  69. DVector<Vector3> points;
  70. Ref<SurfaceTool> surface_tool( memnew( SurfaceTool ));
  71. Ref<FixedMaterial> mat( memnew( FixedMaterial ));
  72. mat->set_parameter( FixedMaterial::PARAM_DIFFUSE,Color(0.2,0.8,0.9,0.3) );
  73. mat->set_line_width(4);
  74. mat->set_flag(Material::FLAG_DOUBLE_SIDED,true);
  75. mat->set_flag(Material::FLAG_UNSHADED,true);
  76. // mat->set_hint(Material::HINT_NO_DEPTH_DRAW,true);
  77. surface_tool->begin(Mesh::PRIMITIVE_LINES);
  78. surface_tool->set_material(mat);
  79. for (int i=0;i<count;i++) {
  80. surface_tool->add_vertex(facesptr[i].vertex[0]);
  81. surface_tool->add_vertex(facesptr[i].vertex[1]);
  82. surface_tool->add_vertex(facesptr[i].vertex[1]);
  83. surface_tool->add_vertex(facesptr[i].vertex[2]);
  84. surface_tool->add_vertex(facesptr[i].vertex[2]);
  85. surface_tool->add_vertex(facesptr[i].vertex[0]);
  86. }
  87. return surface_tool->commit();
  88. }
  89. AABB Room::get_aabb() const {
  90. if (room.is_null())
  91. return AABB();
  92. return room->get_bounds().get_aabb();
  93. }
  94. DVector<Face3> Room::get_faces(uint32_t p_usage_flags) const {
  95. return DVector<Face3>();
  96. }
  97. void Room::set_room( const Ref<RoomBounds>& p_room ) {
  98. room=p_room;
  99. update_gizmo();
  100. if (room.is_valid()) {
  101. set_base(room->get_rid());
  102. } else {
  103. set_base(RID());
  104. }
  105. if (!is_inside_tree())
  106. return;
  107. propagate_notification(NOTIFICATION_AREA_CHANGED);
  108. update_gizmo();
  109. if (room.is_valid())
  110. SpatialSoundServer::get_singleton()->room_set_bounds(sound_room,room->get_bounds());
  111. }
  112. Ref<RoomBounds> Room::get_room() const {
  113. return room;
  114. }
  115. void Room::_parse_node_faces(DVector<Face3> &all_faces,const Node *p_node) const {
  116. const VisualInstance *vi=p_node->cast_to<VisualInstance>();
  117. if (vi) {
  118. DVector<Face3> faces=vi->get_faces(FACES_ENCLOSING);
  119. if (faces.size()) {
  120. int old_len=all_faces.size();
  121. all_faces.resize( all_faces.size() + faces.size() );
  122. int new_len=all_faces.size();
  123. DVector<Face3>::Write all_facesw=all_faces.write();
  124. Face3 * all_facesptr=all_facesw.ptr();
  125. DVector<Face3>::Read facesr=faces.read();
  126. const Face3 * facesptr=facesr.ptr();
  127. Transform tr=vi->get_relative_transform(this);
  128. for(int i=old_len;i<new_len;i++) {
  129. Face3 f=facesptr[i-old_len];
  130. for (int j=0;j<3;j++)
  131. f.vertex[j]=tr.xform(f.vertex[j]);
  132. all_facesptr[i]=f;
  133. }
  134. }
  135. }
  136. for (int i=0;i<p_node->get_child_count();i++) {
  137. _parse_node_faces(all_faces,p_node->get_child(i));
  138. }
  139. }
  140. void Room::compute_room_from_subtree() {
  141. DVector<Face3> all_faces;
  142. _parse_node_faces(all_faces,this);
  143. if (all_faces.size()==0)
  144. return;
  145. float error;
  146. DVector<Face3> wrapped_faces = Geometry::wrap_geometry(all_faces,&error);
  147. if (wrapped_faces.size()==0)
  148. return;
  149. BSP_Tree tree(wrapped_faces,error);
  150. Ref<RoomBounds> room( memnew( RoomBounds ) );
  151. room->set_bounds(tree);
  152. room->set_geometry_hint(wrapped_faces);
  153. set_room(room);
  154. }
  155. void Room::set_simulate_acoustics(bool p_enable) {
  156. if (sound_enabled==p_enable)
  157. return;
  158. sound_enabled=p_enable;
  159. if (!is_inside_world())
  160. return; //nothing to do
  161. if (sound_enabled)
  162. SpatialSoundServer::get_singleton()->room_set_space(sound_room,get_world()->get_sound_space());
  163. else
  164. SpatialSoundServer::get_singleton()->room_set_space(sound_room,RID());
  165. }
  166. void Room::_bounds_changed() {
  167. update_gizmo();
  168. }
  169. bool Room::is_simulating_acoustics() const {
  170. return sound_enabled;
  171. }
  172. RID Room::get_sound_room() const {
  173. return RID();
  174. }
  175. void Room::_bind_methods() {
  176. ObjectTypeDB::bind_method(_MD("set_room","room:Room"),&Room::set_room );
  177. ObjectTypeDB::bind_method(_MD("get_room:Room"),&Room::get_room );
  178. ObjectTypeDB::bind_method(_MD("compute_room_from_subtree"),&Room::compute_room_from_subtree);
  179. ObjectTypeDB::bind_method(_MD("set_simulate_acoustics","enable"),&Room::set_simulate_acoustics );
  180. ObjectTypeDB::bind_method(_MD("is_simulating_acoustics"),&Room::is_simulating_acoustics );
  181. ADD_PROPERTY( PropertyInfo( Variant::OBJECT, "room/room", PROPERTY_HINT_RESOURCE_TYPE, "Area" ), _SCS("set_room"), _SCS("get_room") );
  182. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "room/simulate_acoustics"), _SCS("set_simulate_acoustics"), _SCS("is_simulating_acoustics") );
  183. }
  184. Room::Room() {
  185. sound_enabled=false;
  186. sound_room=SpatialSoundServer::get_singleton()->room_create();
  187. level=0;
  188. }
  189. Room::~Room() {
  190. SpatialSoundServer::get_singleton()->free(sound_room);
  191. }