aboutsummaryrefslogtreecommitdiff
path: root/game/addons/zylann.hterrain/native/src/quad_tree_lod.h
blob: a4132ec21630f1105ec88e00589ed45d42538edd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef QUAD_TREE_LOD_H
#define QUAD_TREE_LOD_H

#include <CanvasItem.hpp>
#include <FuncRef.hpp>
#include <Godot.hpp>

#include <vector>

namespace godot {

class QuadTreeLod : public Reference {
   GODOT_CLASS(QuadTreeLod, Reference)
public:
   static void _register_methods();

   QuadTreeLod() {}
   ~QuadTreeLod() {}
   
   void _init() {}

   void set_callbacks(Ref<FuncRef> make_cb, Ref<FuncRef> recycle_cb, Ref<FuncRef> vbounds_cb);
   int get_lod_count();
   int get_lod_factor(int lod);
   int compute_lod_count(int base_size, int full_size);
   void set_split_scale(real_t p_split_scale);
   real_t get_split_scale();
   void clear();
   void create_from_sizes(int base_size, int full_size);
   void update(Vector3 view_pos);
   void debug_draw_tree(CanvasItem *ci);

private:
   static const unsigned int NO_CHILDREN = -1;
   static const unsigned int ROOT = -1;

   class Quad {
   public:
      unsigned int first_child = NO_CHILDREN;
      int origin_x = 0;
      int origin_y = 0;

      Quad() {
         init();
      }

      ~Quad() {
      }

      inline void init() {
         first_child = NO_CHILDREN;
         origin_x = 0;
         origin_y = 0;
         clear_data();
      }

      inline void clear_data() {
         _data = Variant();
      }

      inline bool has_children() {
         return first_child != NO_CHILDREN;
      }

      inline bool is_null() {
         return _data.get_type() == Variant::NIL;
      }

      inline bool is_valid() {
         return _data.get_type() != Variant::NIL;
      }

      inline Variant get_data() {
         return _data;
      }

      inline void set_data(Variant p_data) {
         _data = p_data;
      }

   private:
      Variant _data; // Type is HTerrainChunk.gd : Object
   };

   Quad _root;
   std::vector<Quad> _node_pool;
   std::vector<unsigned int> _free_indices;

   int _max_depth = 0;
   int _base_size = 16;
   real_t _split_scale = 2.0f;

   Ref<FuncRef> _make_func;
   Ref<FuncRef> _recycle_func;
   Ref<FuncRef> _vertical_bounds_func;

   inline Quad *_get_root() {
      return &_root;
   }

   inline Quad *_get_node(unsigned int index) {
      if (index == ROOT) {
         return &_root;
      } else {
         return &_node_pool[index];
      }
   }

   void _clear_children(unsigned int index);
   unsigned int _allocate_children();
   void _recycle_children(unsigned int i0);
   Variant _make_chunk(int lod, int origin_x, int origin_y);
   void _recycle_chunk(unsigned int quad_index, int lod);
   void _join_all_recursively(unsigned int quad_index, int lod);
   void _update(unsigned int quad_index, int lod, Vector3 view_pos);
   void _debug_draw_tree_recursive(CanvasItem *ci, unsigned int quad_index, int lod_index, int child_index);
}; // class QuadTreeLod

} // namespace godot

#endif // QUAD_TREE_LOD_H