Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
mesh_instancer.h
1 #pragma once
2 
3 #include <unordered_map>
4 
5 #include "../generic/identifiable.h"
6 
7 #include "stage_node.h"
8 #include "../interfaces.h"
9 #include "../sound.h"
10 #include "../generic/manual_object.h"
11 #include "../generic/containers/contiguous_map.h"
12 
13 namespace smlt {
14 
15 typedef std::size_t MeshInstanceID;
16 
38  public TypedDestroyableObject<MeshInstancer, Stage>,
39  public StageNode,
40  public virtual Boundable,
41  public generic::Identifiable<MeshInstancerID>,
42  public AudioSource,
44  public ChainNameable<MeshInstancer> {
45 
46 public:
47  MeshInstancer(Stage* stage, SoundDriver* sound_driver, MeshPtr mesh);
48  virtual ~MeshInstancer();
49 
50  const AABB& aabb() const override;
51  void set_mesh(MeshPtr mesh);
52  MeshPtr mesh() const;
53 
59  MeshInstanceID new_mesh_instance(
60  const smlt::Vec3& position,
61  const smlt::Quaternion& rotation=smlt::Quaternion()
62  );
63 
70  bool destroy_mesh_instance(MeshInstanceID mid);
71 
77  bool show_mesh_instance(MeshInstanceID mid);
78 
84  bool hide_mesh_instance(MeshInstanceID mid);
85 
86  void _get_renderables(
87  batcher::RenderQueue* render_queue,
88  const CameraPtr camera,
89  const DetailLevel detail_level
90  ) override;
91 
92 private:
93  UniqueIDKey make_key() const override {
94  return make_unique_id_key(id());
95  }
96 
97  MeshPtr mesh_;
98 
99  /* The axis-aligned box containing all mesh instances */
100  AABB aabb_;
101 
102  void recalc_aabb();
103 
104  void on_transformation_changed() override;
105 
106  struct MeshInstance {
107  uint32_t id = 0;
108  bool is_visible = true;
109  Mat4 transformation;
110  Mat4 abs_transformation;
111  AABB aabb;
112 
113  /* Recalc the aabb from the transformation */
114  void recalc_aabb(MeshPtr mesh);
115  };
116 
117  static uint32_t id_counter_;
118 
119  /* FIXME: Convert to ContiguousMap when it has erase... */
120  std::unordered_map<uint32_t, MeshInstance> instances_;
121 };
122 
123 
124 }
smlt::Mat4
Definition: mat4.h:29
smlt::Vec3
Definition: vec3.h:23
smlt::Quaternion
Definition: quaternion.h:20
smlt::AudioSource
Definition: sound.h:106
smlt::Stage
Definition: stage.h:80
smlt
Definition: animation.cpp:25
smlt::HasMutableRenderPriority
Definition: renderable.h:38
smlt::MeshInstancer::hide_mesh_instance
bool hide_mesh_instance(MeshInstanceID mid)
hide_mesh_instance
Definition: mesh_instancer.cpp:78
smlt::batcher::RenderQueue
Definition: render_queue.h:156
smlt::generic::Identifiable
Definition: identifiable.h:26
smlt::TypedDestroyableObject
Definition: manual_object.h:34
smlt::MeshInstancer
The MeshInstancer class.
Definition: mesh_instancer.h:44
smlt::MeshInstancer::show_mesh_instance
bool show_mesh_instance(MeshInstanceID mid)
show_mesh_instance
Definition: mesh_instancer.cpp:68
smlt::ChainNameable
Definition: nameable.h:34
smlt::MeshInstancer::destroy_mesh_instance
bool destroy_mesh_instance(MeshInstanceID mid)
destroy_mesh_instance
Definition: mesh_instancer.cpp:56
smlt::AABB
Definition: aabb.h:22
smlt::StageNode
Definition: stage_node.h:62
smlt::default_init_ptr< Camera >
smlt::SoundDriver
Definition: sound_driver.h:74
smlt::MeshInstancer::new_mesh_instance
MeshInstanceID new_mesh_instance(const smlt::Vec3 &position, const smlt::Quaternion &rotation=smlt::Quaternion())
Definition: mesh_instancer.cpp:41
smlt::Boundable
The Boundable class.
Definition: boundable.h:12