Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
spatial_hash.h
1 #pragma once
2 
3 
4 #include "../partitioner.h"
5 #include "./impl/spatial_hash.h"
6 #include "../threads/shared_mutex.h"
7 
8 namespace smlt {
9 
10 enum PartitionerEntryType {
11  PARTITIONER_ENTRY_TYPE_LIGHT,
12  PARTITIONER_ENTRY_TYPE_ACTOR,
13  PARTITIONER_ENTRY_TYPE_GEOM,
14  PARTITIONER_ENTRY_TYPE_PARTICLE_SYSTEM
15 };
16 
18  PartitionerEntry(ActorID actor_id):
19  type(PARTITIONER_ENTRY_TYPE_ACTOR),
20  actor_id(actor_id) {}
21 
22  PartitionerEntry(LightID light_id):
23  type(PARTITIONER_ENTRY_TYPE_LIGHT),
24  light_id(light_id) {}
25 
26  PartitionerEntry(GeomID geom_id):
27  type(PARTITIONER_ENTRY_TYPE_GEOM),
28  geom_id(geom_id) {}
29 
31  type(PARTITIONER_ENTRY_TYPE_PARTICLE_SYSTEM),
32  particle_system_id(ps_id) {}
33 
34  virtual ~PartitionerEntry() {}
35 
36  PartitionerEntryType type;
37  union {
38  ActorID actor_id;
39  LightID light_id;
40  GeomID geom_id;
41  ParticleSystemID particle_system_id;
42  };
43 };
44 
46 public:
48  virtual ~SpatialHashPartitioner();
49 
50  void lights_and_geometry_visible_from(
51  CameraID camera_id,
52  std::vector<LightID> &lights_out,
53  std::vector<StageNode*> &geom_out
54  );
55 
56 private:
57  void stage_add_actor(ActorID obj);
58  void stage_remove_actor(ActorID obj);
59 
60  void stage_add_geom(GeomID geom_id);
61  void stage_remove_geom(GeomID geom_id);
62 
63  void stage_add_light(LightID obj);
64  void stage_remove_light(LightID obj);
65 
66  void stage_add_particle_system(ParticleSystemID ps);
67  void stage_remove_particle_system(ParticleSystemID ps);
68 
69  void _update_actor(const AABB& bounds, ActorID actor);
70  void _update_particle_system(const AABB& bounds, ParticleSystemID ps);
71  void _update_light(const AABB& bounds, LightID light);
72 
73  void apply_staged_write(const UniqueIDKey& key, const StagedWrite& write) override;
74 
75  SpatialHash* hash_ = nullptr;
76 
77  typedef std::shared_ptr<PartitionerEntry> PartitionerEntryPtr;
78 
79  std::unordered_map<ActorID, PartitionerEntryPtr> actor_entries_;
80  std::unordered_map<LightID, PartitionerEntryPtr> light_entries_;
81  std::unordered_map<ParticleSystemID, PartitionerEntryPtr> particle_system_entries_;
82  std::unordered_map<GeomID, PartitionerEntryPtr> geom_entries_;
83 
84  std::unordered_set<LightID> directional_lights_;
85 
86  thread::SharedMutex lock_;
87 };
88 
89 }
smlt::SpatialHashPartitioner
Definition: spatial_hash.h:45
smlt::SpatialHashEntry
Definition: spatial_hash.h:88
smlt::PartitionerEntry
Definition: spatial_hash.h:17
smlt::StagedWrite
Definition: partitioner.h:45
smlt::thread::SharedMutex
Definition: shared_mutex.h:46
smlt::Stage
Definition: stage.h:92
smlt::Partitioner
Definition: partitioner.h:54
smlt
Definition: animation.cpp:25
smlt::UniqueID< ActorPtr >
smlt::SpatialHash
Definition: spatial_hash.h:120
smlt::AABB
Definition: aabb.h:22