immediate_geometry.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**************************************************************************/
  2. /* immediate_geometry.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. }
  37. void ImmediateGeometry::set_normal(const Vector3 &p_normal) {
  38. VS::get_singleton()->immediate_normal(im, p_normal);
  39. }
  40. void ImmediateGeometry::set_tangent(const Plane &p_tangent) {
  41. VS::get_singleton()->immediate_tangent(im, p_tangent);
  42. }
  43. void ImmediateGeometry::set_color(const Color &p_color) {
  44. VS::get_singleton()->immediate_color(im, p_color);
  45. }
  46. void ImmediateGeometry::set_uv(const Vector2 &p_uv) {
  47. VS::get_singleton()->immediate_uv(im, p_uv);
  48. }
  49. void ImmediateGeometry::set_uv2(const Vector2 &p_uv2) {
  50. VS::get_singleton()->immediate_uv2(im, p_uv2);
  51. }
  52. void ImmediateGeometry::add_vertex(const Vector3 &p_vertex) {
  53. VS::get_singleton()->immediate_vertex(im, p_vertex);
  54. if (empty) {
  55. aabb.position = p_vertex;
  56. aabb.size = Vector3();
  57. empty = false;
  58. } else {
  59. aabb.expand_to(p_vertex);
  60. }
  61. }
  62. void ImmediateGeometry::end() {
  63. VS::get_singleton()->immediate_end(im);
  64. }
  65. void ImmediateGeometry::clear() {
  66. VS::get_singleton()->immediate_clear(im);
  67. empty = true;
  68. cached_textures.clear();
  69. }
  70. AABB ImmediateGeometry::get_aabb() const {
  71. return aabb;
  72. }
  73. PoolVector<Face3> ImmediateGeometry::get_faces(uint32_t p_usage_flags) const {
  74. return PoolVector<Face3>();
  75. }
  76. void ImmediateGeometry::add_sphere(int p_lats, int p_lons, float p_radius, bool p_add_uv) {
  77. for (int i = 1; i <= p_lats; i++) {
  78. double lat0 = Math_PI * (-0.5 + (double)(i - 1) / p_lats);
  79. double z0 = Math::sin(lat0);
  80. double zr0 = Math::cos(lat0);
  81. double lat1 = Math_PI * (-0.5 + (double)i / p_lats);
  82. double z1 = Math::sin(lat1);
  83. double zr1 = Math::cos(lat1);
  84. for (int j = p_lons; j >= 1; j--) {
  85. double lng0 = 2 * Math_PI * (double)(j - 1) / p_lons;
  86. double x0 = Math::cos(lng0);
  87. double y0 = Math::sin(lng0);
  88. double lng1 = 2 * Math_PI * (double)(j) / p_lons;
  89. double x1 = Math::cos(lng1);
  90. double y1 = Math::sin(lng1);
  91. Vector3 v[4] = {
  92. Vector3(x1 * zr0, z0, y1 * zr0),
  93. Vector3(x1 * zr1, z1, y1 * zr1),
  94. Vector3(x0 * zr1, z1, y0 * zr1),
  95. Vector3(x0 * zr0, z0, y0 * zr0)
  96. };
  97. #define ADD_POINT(m_idx) \
  98. if (p_add_uv) { \
  99. 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)); \
  100. set_tangent(Plane(Vector3(-v[m_idx].z, v[m_idx].y, v[m_idx].x), 1)); \
  101. } \
  102. set_normal(v[m_idx]); \
  103. add_vertex(v[m_idx] * p_radius);
  104. ADD_POINT(0);
  105. ADD_POINT(1);
  106. ADD_POINT(2);
  107. ADD_POINT(2);
  108. ADD_POINT(3);
  109. ADD_POINT(0);
  110. }
  111. }
  112. }
  113. void ImmediateGeometry::_bind_methods() {
  114. ClassDB::bind_method(D_METHOD("begin", "primitive", "texture"), &ImmediateGeometry::begin, DEFVAL(Ref<Texture>()));
  115. ClassDB::bind_method(D_METHOD("set_normal", "normal"), &ImmediateGeometry::set_normal);
  116. ClassDB::bind_method(D_METHOD("set_tangent", "tangent"), &ImmediateGeometry::set_tangent);
  117. ClassDB::bind_method(D_METHOD("set_color", "color"), &ImmediateGeometry::set_color);
  118. ClassDB::bind_method(D_METHOD("set_uv", "uv"), &ImmediateGeometry::set_uv);
  119. ClassDB::bind_method(D_METHOD("set_uv2", "uv"), &ImmediateGeometry::set_uv2);
  120. ClassDB::bind_method(D_METHOD("add_vertex", "position"), &ImmediateGeometry::add_vertex);
  121. ClassDB::bind_method(D_METHOD("add_sphere", "lats", "lons", "radius", "add_uv"), &ImmediateGeometry::add_sphere, DEFVAL(true));
  122. ClassDB::bind_method(D_METHOD("end"), &ImmediateGeometry::end);
  123. ClassDB::bind_method(D_METHOD("clear"), &ImmediateGeometry::clear);
  124. }
  125. ImmediateGeometry::ImmediateGeometry() {
  126. im = RID_PRIME(VisualServer::get_singleton()->immediate_create());
  127. set_base(im);
  128. empty = true;
  129. }
  130. ImmediateGeometry::~ImmediateGeometry() {
  131. VisualServer::get_singleton()->free(im);
  132. }