Simulant  21.12-194
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 private:
92  MeshPtr mesh_;
93 
94  /* The axis-aligned box containing all mesh instances */
95  AABB aabb_;
96 
97  void recalc_aabb();
98 
99  void on_transformation_changed();
100 
101  struct MeshInstance {
102  uint32_t id = 0;
103  bool is_visible = true;
104  Mat4 transformation;
105  Mat4 abs_transformation;
106  AABB aabb;
107 
108  /* Recalc the aabb from the transformation */
109  void recalc_aabb(MeshPtr mesh);
110  };
111 
112  static uint32_t id_counter_;
113 
114  /* FIXME: Convert to ContiguousMap when it has erase... */
115  std::unordered_map<uint32_t, MeshInstance> instances_;
116 };
117 
118 
119 }
smlt::Mat4
Definition: mat4.h:25
smlt::Vec3
Definition: vec3.h:23
smlt::Quaternion
Definition: quaternion.h:24
smlt::AudioSource
Definition: sound.h:103
smlt::Stage
Definition: stage.h:92
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:33
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:61
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