immediate_geometry.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "immediate_geometry.h"
  2. void ImmediateGeometry::begin(Mesh::PrimitiveType p_primitive,const Ref<Texture>& p_texture) {
  3. VS::get_singleton()->immediate_begin(im,(VS::PrimitiveType)p_primitive,p_texture.is_valid()?p_texture->get_rid():RID());
  4. if (p_texture.is_valid())
  5. cached_textures.push_back(p_texture);
  6. }
  7. void ImmediateGeometry::set_normal(const Vector3& p_normal){
  8. VS::get_singleton()->immediate_normal(im,p_normal);
  9. }
  10. void ImmediateGeometry::set_tangent(const Plane& p_tangent){
  11. VS::get_singleton()->immediate_tangent(im,p_tangent);
  12. }
  13. void ImmediateGeometry::set_color(const Color& p_color){
  14. VS::get_singleton()->immediate_color(im,p_color);
  15. }
  16. void ImmediateGeometry::set_uv(const Vector2& p_uv){
  17. VS::get_singleton()->immediate_uv(im,p_uv);
  18. }
  19. void ImmediateGeometry::set_uv2(const Vector2& p_uv2){
  20. VS::get_singleton()->immediate_uv2(im,p_uv2);
  21. }
  22. void ImmediateGeometry::add_vertex(const Vector3& p_vertex){
  23. VS::get_singleton()->immediate_vertex(im,p_vertex);
  24. if (empty) {
  25. aabb.pos=p_vertex;
  26. aabb.size=Vector3();
  27. } else {
  28. aabb.expand_to(p_vertex);
  29. }
  30. }
  31. void ImmediateGeometry::end(){
  32. VS::get_singleton()->immediate_end(im);
  33. }
  34. void ImmediateGeometry::clear(){
  35. VS::get_singleton()->immediate_clear(im);
  36. empty=true;
  37. cached_textures.clear();
  38. }
  39. AABB ImmediateGeometry::get_aabb() const {
  40. return aabb;
  41. }
  42. DVector<Face3> ImmediateGeometry::get_faces(uint32_t p_usage_flags) const {
  43. return DVector<Face3>();
  44. }
  45. void ImmediateGeometry::add_sphere(int p_lats,int p_lons,float p_radius) {
  46. for(int i = 1; i <= p_lats; i++) {
  47. double lat0 = Math_PI * (-0.5 + (double) (i - 1) / p_lats);
  48. double z0 = Math::sin(lat0);
  49. double zr0 = Math::cos(lat0);
  50. double lat1 = Math_PI * (-0.5 + (double) i / p_lats);
  51. double z1 = Math::sin(lat1);
  52. double zr1 = Math::cos(lat1);
  53. for(int j = p_lons; j >= 1; j--) {
  54. double lng0 = 2 * Math_PI * (double) (j - 1) / p_lons;
  55. double x0 = Math::cos(lng0);
  56. double y0 = Math::sin(lng0);
  57. double lng1 = 2 * Math_PI * (double) (j) / p_lons;
  58. double x1 = Math::cos(lng1);
  59. double y1 = Math::sin(lng1);
  60. Vector3 v[4]={
  61. Vector3(x1 * zr0, z0, y1 *zr0),
  62. Vector3(x1 * zr1, z1, y1 *zr1),
  63. Vector3(x0 * zr1, z1, y0 *zr1),
  64. Vector3(x0 * zr0, z0, y0 *zr0)
  65. };
  66. #define ADD_POINT(m_idx)\
  67. set_normal(v[m_idx]);\
  68. add_vertex(v[m_idx]*p_radius);
  69. ADD_POINT(0);
  70. ADD_POINT(1);
  71. ADD_POINT(2);
  72. ADD_POINT(2);
  73. ADD_POINT(3);
  74. ADD_POINT(0);
  75. }
  76. }
  77. }
  78. void ImmediateGeometry::_bind_methods() {
  79. ObjectTypeDB::bind_method(_MD("begin","primitive","texture:Texture"),&ImmediateGeometry::begin);
  80. ObjectTypeDB::bind_method(_MD("set_normal","normal"),&ImmediateGeometry::set_normal);
  81. ObjectTypeDB::bind_method(_MD("set_tangent","tangent"),&ImmediateGeometry::set_tangent);
  82. ObjectTypeDB::bind_method(_MD("set_color","color"),&ImmediateGeometry::set_color);
  83. ObjectTypeDB::bind_method(_MD("set_uv","uv"),&ImmediateGeometry::set_uv);
  84. ObjectTypeDB::bind_method(_MD("set_uv2","uv"),&ImmediateGeometry::set_uv2);
  85. ObjectTypeDB::bind_method(_MD("add_vertex","pos"),&ImmediateGeometry::add_vertex);
  86. ObjectTypeDB::bind_method(_MD("add_sphere","lats","lons","radius"),&ImmediateGeometry::add_sphere);
  87. ObjectTypeDB::bind_method(_MD("end"),&ImmediateGeometry::end);
  88. ObjectTypeDB::bind_method(_MD("clear"),&ImmediateGeometry::clear);
  89. }
  90. ImmediateGeometry::ImmediateGeometry() {
  91. im = VisualServer::get_singleton()->immediate_create();
  92. set_base(im);
  93. empty=true;
  94. }
  95. ImmediateGeometry::~ImmediateGeometry() {
  96. VisualServer::get_singleton()->free(im);
  97. }