immediate_geometry.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*************************************************************************/
  2. /* immediate_geometry.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "immediate_geometry.h"
  31. void ImmediateGeometry::begin(Mesh::PrimitiveType p_primitive, const Ref<Texture> &p_texture) {
  32. VS::get_singleton()->immediate_begin(im, (VS::PrimitiveType)p_primitive, p_texture.is_valid() ? p_texture->get_rid() : RID());
  33. if (p_texture.is_valid())
  34. cached_textures.push_back(p_texture);
  35. }
  36. void ImmediateGeometry::set_normal(const Vector3 &p_normal) {
  37. VS::get_singleton()->immediate_normal(im, p_normal);
  38. }
  39. void ImmediateGeometry::set_tangent(const Plane &p_tangent) {
  40. VS::get_singleton()->immediate_tangent(im, p_tangent);
  41. }
  42. void ImmediateGeometry::set_color(const Color &p_color) {
  43. VS::get_singleton()->immediate_color(im, p_color);
  44. }
  45. void ImmediateGeometry::set_uv(const Vector2 &p_uv) {
  46. VS::get_singleton()->immediate_uv(im, p_uv);
  47. }
  48. void ImmediateGeometry::set_uv2(const Vector2 &p_uv2) {
  49. VS::get_singleton()->immediate_uv2(im, p_uv2);
  50. }
  51. void ImmediateGeometry::add_vertex(const Vector3 &p_vertex) {
  52. VS::get_singleton()->immediate_vertex(im, p_vertex);
  53. if (empty) {
  54. aabb.position = p_vertex;
  55. aabb.size = Vector3();
  56. empty = false;
  57. } else {
  58. aabb.expand_to(p_vertex);
  59. }
  60. }
  61. void ImmediateGeometry::end() {
  62. VS::get_singleton()->immediate_end(im);
  63. }
  64. void ImmediateGeometry::clear() {
  65. VS::get_singleton()->immediate_clear(im);
  66. empty = true;
  67. cached_textures.clear();
  68. }
  69. AABB ImmediateGeometry::get_aabb() const {
  70. return aabb;
  71. }
  72. PoolVector<Face3> ImmediateGeometry::get_faces(uint32_t p_usage_flags) const {
  73. return PoolVector<Face3>();
  74. }
  75. void ImmediateGeometry::add_sphere(int p_lats, int p_lons, float p_radius, bool p_add_uv) {
  76. for (int i = 1; i <= p_lats; i++) {
  77. double lat0 = Math_PI * (-0.5 + (double)(i - 1) / p_lats);
  78. double z0 = Math::sin(lat0);
  79. double zr0 = Math::cos(lat0);
  80. double lat1 = Math_PI * (-0.5 + (double)i / p_lats);
  81. double z1 = Math::sin(lat1);
  82. double zr1 = Math::cos(lat1);
  83. for (int j = p_lons; j >= 1; j--) {
  84. double lng0 = 2 * Math_PI * (double)(j - 1) / p_lons;
  85. double x0 = Math::cos(lng0);
  86. double y0 = Math::sin(lng0);
  87. double lng1 = 2 * Math_PI * (double)(j) / p_lons;
  88. double x1 = Math::cos(lng1);
  89. double y1 = Math::sin(lng1);
  90. Vector3 v[4] = {
  91. Vector3(x1 * zr0, z0, y1 * zr0),
  92. Vector3(x1 * zr1, z1, y1 * zr1),
  93. Vector3(x0 * zr1, z1, y0 * zr1),
  94. Vector3(x0 * zr0, z0, y0 * zr0)
  95. };
  96. #define ADD_POINT(m_idx) \
  97. if (p_add_uv) { \
  98. set_uv(Vector2(Math::atan2(v[m_idx].x, v[m_idx].z) / Math_PI * 0.5 + 0.5, v[m_idx].y * 0.5 + 0.5)); \
  99. set_tangent(Plane(Vector3(-v[m_idx].z, v[m_idx].y, v[m_idx].x), 1)); \
  100. } \
  101. set_normal(v[m_idx]); \
  102. add_vertex(v[m_idx] * p_radius);
  103. ADD_POINT(0);
  104. ADD_POINT(1);
  105. ADD_POINT(2);
  106. ADD_POINT(2);
  107. ADD_POINT(3);
  108. ADD_POINT(0);
  109. }
  110. }
  111. }
  112. void ImmediateGeometry::_bind_methods() {
  113. ClassDB::bind_method(D_METHOD("begin", "primitive", "texture"), &ImmediateGeometry::begin, DEFVAL(Ref<Texture>()));
  114. ClassDB::bind_method(D_METHOD("set_normal", "normal"), &ImmediateGeometry::set_normal);
  115. ClassDB::bind_method(D_METHOD("set_tangent", "tangent"), &ImmediateGeometry::set_tangent);
  116. ClassDB::bind_method(D_METHOD("set_color", "color"), &ImmediateGeometry::set_color);
  117. ClassDB::bind_method(D_METHOD("set_uv", "uv"), &ImmediateGeometry::set_uv);
  118. ClassDB::bind_method(D_METHOD("set_uv2", "uv"), &ImmediateGeometry::set_uv2);
  119. ClassDB::bind_method(D_METHOD("add_vertex", "position"), &ImmediateGeometry::add_vertex);
  120. ClassDB::bind_method(D_METHOD("add_sphere", "lats", "lons", "radius", "add_uv"), &ImmediateGeometry::add_sphere, DEFVAL(true));
  121. ClassDB::bind_method(D_METHOD("end"), &ImmediateGeometry::end);
  122. ClassDB::bind_method(D_METHOD("clear"), &ImmediateGeometry::clear);
  123. }
  124. ImmediateGeometry::ImmediateGeometry() {
  125. im = VisualServer::get_singleton()->immediate_create();
  126. set_base(im);
  127. empty = true;
  128. }
  129. ImmediateGeometry::~ImmediateGeometry() {
  130. VisualServer::get_singleton()->free(im);
  131. }