cat3d
A tiny OpenGL 3D engine library written in and for C++.
node.hpp
1 #pragma once
2 
3 #include <functional>
4 #include <glm/glm.hpp>
5 #include <vector>
6 #include <unordered_map>
7 
8 #include "cat3d/util/clock.hpp"
9 #include "cat3d/util/transform.hpp"
10 #include "cat3d/window.hpp"
11 
12 namespace cat3d::scene {
13 
17 enum node_bind_loc {
18  PRE_UPDATE,
19  POST_UPDATE,
20  PRE_RENDER,
21  POST_RENDER,
22 };
23 
28 class node {
29 public:
30  node();
31 
37  static node* new_root();
38 
40  virtual ~node();
41 
47  void delete_child(node* child);
48 
54 
60 
69 
74  void update(window& win);
75 
80  void render(window& win);
81 
87  node* parent() const;
88 
95 
105  template <typename Obj, typename... Args>
106  Obj* create(Args... args) {
107  Obj* o = new Obj(args...);
108  o->m_root = false;
109  o->m_parent = this;
110  m_children.push_back((node*)o);
111  return o;
112  }
113 
122  size_t bind(node_bind_loc loc, std::function<void()> fn);
123 
131  void unbind(size_t id);
132 
133 protected:
139  virtual void update_self(window& win);
140 
146  virtual void render_self(window& win);
147 
148 private:
150  bool m_root = false;
151 
153  node* m_parent = nullptr;
154 
156  std::vector<node*> m_children;
157 
159  std::unordered_map<size_t, std::pair<node_bind_loc, std::function<void()>>> m_hooks;
160 
162  size_t m_next_hook_id = 0;
163 
165  void run_hooks(node_bind_loc l);
166 
168  util::transform m_transform;
169 
171  void push_transform(window& win);
172 };
173 
174 }
cat3d::scene::node::new_root
static node * new_root()
Create a new root scene node.
cat3d::window
Manages internal window api / state.
Definition: window.hpp:17
cat3d::scene::node::create
Obj * create(Args... args)
Add a derived node class as a child.
Definition: node.hpp:106
cat3d::scene::node::delete_child
void delete_child(node *child)
Remove the given child node.
cat3d::scene::node::render_self
virtual void render_self(window &win)
Overridden by child classes to implement custom rendering.
cat3d::scene::node::render
void render(window &win)
Called once per render. Renders this node and all children.
cat3d::scene::node::~node
virtual ~node()
virtual destructor for polymorphism
cat3d::scene::node::global_transform
util::transform global_transform() const
Get this node's transform relative to the world.
cat3d::scene::node::update
void update(window &win)
Called once per frame. Updates this node.
cat3d::scene::node::update_self
virtual void update_self(window &win)
Overridden by child classes to implement custom behavior.
cat3d::scene::node::local_transform
util::transform local_transform() const
Get the node's transform relative to itself.
cat3d::scene::node
Basic, semi-abstract scene node class. Can have children which are other scene nodes,...
Definition: node.hpp:28
cat3d::util::transform
Transformation class to manage 3D space transforms.
Definition: transform.hpp:12
cat3d::scene::node::create_node
node * create_node()
Create a child node.
cat3d::scene::node::transform
util::transform & transform()
Access to the underlying transform.
cat3d::scene::node::bind
size_t bind(node_bind_loc loc, std::function< void()> fn)
Add a hook into the update cycle of the node.
cat3d::scene::node::unbind
void unbind(size_t id)
Unbind a hook from the node.
cat3d::scene::node::parent
node * parent() const
Get this node's parent.