quad.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*************************************************************************/
  2. /* quad.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 "quad.h"
  30. #include "servers/visual_server.h"
  31. void Quad::_update() {
  32. if (!is_inside_tree())
  33. return;
  34. Vector3 normal;
  35. normal[axis]=1.0;
  36. const int axis_order_1[3]={1,2,0};
  37. const int axis_order_2[3]={2,0,1};
  38. const int a1=axis_order_1[axis];
  39. const int a2=axis_order_2[axis];
  40. DVector<Vector3> points;
  41. points.resize(4);
  42. DVector<Vector3>::Write pointsw = points.write();
  43. Vector2 s2 = size*0.5;
  44. Vector2 o = offset;
  45. if (!centered)
  46. o+=s2;
  47. pointsw[0][a1]=-s2.x+offset.x;
  48. pointsw[0][a2]=s2.y+offset.y;
  49. pointsw[1][a1]=s2.x+offset.x;
  50. pointsw[1][a2]=s2.y+offset.y;
  51. pointsw[2][a1]=s2.x+offset.x;
  52. pointsw[2][a2]=-s2.y+offset.y;
  53. pointsw[3][a1]=-s2.x+offset.x;
  54. pointsw[3][a2]=-s2.y+offset.y;
  55. aabb=AABB(pointsw[0],Vector3());
  56. for(int i=1;i<4;i++)
  57. aabb.expand_to(pointsw[i]);
  58. pointsw = DVector<Vector3>::Write();
  59. DVector<Vector3> normals;
  60. normals.resize(4);
  61. DVector<Vector3>::Write normalsw = normals.write();
  62. for(int i=0;i<4;i++)
  63. normalsw[i]=normal;
  64. normalsw=DVector<Vector3>::Write();
  65. DVector<Vector2> uvs;
  66. uvs.resize(4);
  67. DVector<Vector2>::Write uvsw = uvs.write();
  68. uvsw[0]=Vector2(0,0);
  69. uvsw[1]=Vector2(1,0);
  70. uvsw[2]=Vector2(1,1);
  71. uvsw[3]=Vector2(0,1);
  72. uvsw = DVector<Vector2>::Write();
  73. DVector<int> indices;
  74. indices.resize(6);
  75. DVector<int>::Write indicesw = indices.write();
  76. indicesw[0]=0;
  77. indicesw[1]=1;
  78. indicesw[2]=2;
  79. indicesw[3]=2;
  80. indicesw[4]=3;
  81. indicesw[5]=0;
  82. indicesw=DVector<int>::Write();
  83. Array arr;
  84. arr.resize(VS::ARRAY_MAX);
  85. arr[VS::ARRAY_VERTEX]=points;
  86. arr[VS::ARRAY_NORMAL]=normals;
  87. arr[VS::ARRAY_TEX_UV]=uvs;
  88. arr[VS::ARRAY_INDEX]=indices;
  89. if (configured) {
  90. VS::get_singleton()->mesh_remove_surface(mesh,0);
  91. } else {
  92. configured=true;
  93. }
  94. VS::get_singleton()->mesh_add_surface(mesh,VS::PRIMITIVE_TRIANGLES,arr);
  95. pending_update=false;
  96. }
  97. void Quad::set_axis(Vector3::Axis p_axis) {
  98. axis=p_axis;
  99. _update();
  100. }
  101. Vector3::Axis Quad::get_axis() const{
  102. return axis;
  103. }
  104. void Quad::set_size(const Vector2& p_size){
  105. size=p_size;
  106. _update();
  107. }
  108. Vector2 Quad::get_size() const{
  109. return size;
  110. }
  111. void Quad::set_offset(const Vector2& p_offset){
  112. offset=p_offset;
  113. _update();
  114. }
  115. Vector2 Quad::get_offset() const{
  116. return offset;
  117. }
  118. void Quad::set_centered(bool p_enabled){
  119. centered=p_enabled;
  120. _update();
  121. }
  122. bool Quad::is_centered() const{
  123. return centered;
  124. }
  125. void Quad::_notification(int p_what) {
  126. switch(p_what) {
  127. case NOTIFICATION_ENTER_TREE: {
  128. if (pending_update)
  129. _update();
  130. } break;
  131. case NOTIFICATION_EXIT_TREE: {
  132. pending_update=true;
  133. } break;
  134. }
  135. }
  136. DVector<Face3> Quad::get_faces(uint32_t p_usage_flags) const {
  137. return DVector<Face3>();
  138. }
  139. AABB Quad::get_aabb() const {
  140. return aabb;
  141. }
  142. void Quad::_bind_methods(){
  143. ObjectTypeDB::bind_method(_MD("set_axis","axis"),&Quad::set_axis);
  144. ObjectTypeDB::bind_method(_MD("get_axis"),&Quad::get_axis);
  145. ObjectTypeDB::bind_method(_MD("set_size","size"),&Quad::set_size);
  146. ObjectTypeDB::bind_method(_MD("get_size"),&Quad::get_size);
  147. ObjectTypeDB::bind_method(_MD("set_centered","centered"),&Quad::set_centered);
  148. ObjectTypeDB::bind_method(_MD("is_centered"),&Quad::is_centered);
  149. ObjectTypeDB::bind_method(_MD("set_offset","offset"),&Quad::set_offset);
  150. ObjectTypeDB::bind_method(_MD("get_offset"),&Quad::get_offset);
  151. ADD_PROPERTY( PropertyInfo( Variant::INT, "quad/axis", PROPERTY_HINT_ENUM,"X,Y,Z" ), _SCS("set_axis"), _SCS("get_axis"));
  152. ADD_PROPERTY( PropertyInfo( Variant::VECTOR2, "quad/size" ), _SCS("set_size"), _SCS("get_size"));
  153. ADD_PROPERTY( PropertyInfo( Variant::VECTOR2, "quad/offset" ), _SCS("set_offset"), _SCS("get_offset"));
  154. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "quad/centered" ), _SCS("set_centered"), _SCS("is_centered"));
  155. }
  156. Quad::Quad() {
  157. pending_update=true;
  158. centered=true;
  159. //offset=0;
  160. size=Vector2(1,1);
  161. axis=Vector3::AXIS_Z;
  162. mesh=VisualServer::get_singleton()->mesh_create();
  163. set_base(mesh);
  164. configured=false;
  165. }