Polyphase Game Engine
Loading...
Searching...
No Matches
World.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <vector>
5#include <unordered_map>
6#include <unordered_set>
7
8#include "Assets/StaticMesh.h"
9#include "Assets/Material.h"
10#include "Assets/Texture.h"
11#include "Nodes/Node.h"
13#include "Clock.h"
14#include "Line.h"
15#include "EngineTypes.h"
16#include "Nodes/3D/Camera3d.h"
18
19class OcclusionData;
20
21class Node;
22class Audio3D;
23class Particle3D;
24
26{
27public:
28
29 World();
30 ~World();
31
32 void Destroy();
33
34 void Update(float deltaTime);
35
36 Camera3D* GetMainCamera();
37
38 Camera3D* GetActiveCamera();
39 Camera3D* FindCameraForScreen(uint8_t targetScreen);
40 Node3D* GetAudioReceiver();
41
42 void SetActiveCamera(Camera3D* activeCamera);
43
44 // Keeps a transient stand-in camera alive whenever the loaded scene has no
45 // Camera3D of its own, and retires it the moment a real one registers.
46 // Called once per World::Update. See the implementation for why a scene
47 // without any camera renders its UI incorrectly.
48 void EnsureFallbackCamera();
49 Camera3D* GetFallbackCamera() const { return mFallbackCamera; }
50 void SetCameraOverride(Camera3D* camera) { mCameraOverride = camera; }
51 Camera3D* GetCameraOverride() const { return mCameraOverride; }
52 void SetAudioReceiver(Node3D* newReceiver);
53
54 Node* SpawnNode(TypeId actorType, glm::vec3 position = {});
55 Node* SpawnNode(const char* typeName, glm::vec3 position = {});
56 Node* SpawnScene(const char* sceneName, glm::vec3 position = {});
57 Node* SpawnScene(Scene* scene, glm::vec3 position = {});
58 void DespawnScene(Node* sceneRoot);
59 Particle3D* SpawnParticle(ParticleSystem* sys, glm::vec3 position);
60 Particle3D* SpawnParticle(ParticleSystem* sys, glm::vec3 position, glm::vec3 velocity);
61
62 template<class NodeClass>
63 NodeClass* SpawnNode(glm::vec3 position = {})
64 {
65 return (NodeClass*)SpawnNode(NodeClass::GetStaticType(), position);
66 }
67
68 Node* GetRootNode();
69 void SetRootNode(Node* node);
70 NodePtr GetRootNodePtr();
71 void DestroyRootNode();
72 Node* FindNode(const std::string& name);
73 Node* GetNetNode(NetId netId);
74 std::vector<Node*> FindNodesWithTag(const char* tag);
75 std::vector<Node*> FindNodesWithName(const char* name);
76 std::vector<Node*> GatherNodes();
77 void GatherNodes(std::vector<Node*>& outNodes);
78
79
80
81 Node* FindNodeByUuid(uint64_t uuid);
82 void RegisterNodeUuid(Node* node);
83 void UnregisterNodeUuid(Node* node);
84
85 bool FindNavPath(glm::vec3 start, glm::vec3 end, std::vector<glm::vec3>& outPath);
86 bool FindRandomNavPoint(glm::vec3& outPoint);
87 bool FindClosestNavPoint(glm::vec3 inPoint, glm::vec3& outPoint);
88 void BuildNavigationData();
89 void EnableAutoNavRebuild(bool enable);
90 bool IsAutoNavRebuildEnabled() const;
91 void InvalidateNavMesh();
92 void Clear();
93
94 int32_t GetIndex() const;
95
96 void AddLine(const Line& line);
97 void RemoveLine(const Line& line);
98 void RemoveAllLines();
99 const std::vector<Line>& GetLines() const;
100
101 const std::vector<class Light3D*>& GetLights();
102 std::vector<FadingLight>& GetFadingLights();
103
104 void SetAmbientLightColor(glm::vec4 color);
105 glm::vec4 GetAmbientLightColor() const;
106
107 void SetShadowColor(glm::vec4 shadowColor);
108 glm::vec4 GetShadowColor() const;
109
110 void SetFogSettings(const FogSettings& settings);
111 const FogSettings& GetFogSettings() const;
112
113 // Baked occlusion culling. The data is owned by the root node's Scene.
114 void SetOcclusionData(const OcclusionData* data, bool enabled);
115 const OcclusionData* GetOcclusionData() const;
116 bool IsOcclusionCullingEnabled() const;
117 bool IsOcclusionDataStale() const;
118 float GetOcclusionMoveEpsilon2() const;
119 void RequestOccludeeResolve();
120 bool IsOccludeeResolvePending() const;
121 void ResolveOccludees();
122
123 // Gameplay visibility queries: frustum test on the node's bounds plus the
124 // baked occlusion tables (static bit for baked occludees, coarse dynamic
125 // table otherwise). Evaluated on demand against the active camera unless
126 // one is passed, so results never lag a frame. Hidden nodes and the
127 // camera itself are never "seen".
128 bool IsSeenByCamera(Node3D* node, Camera3D* camera = nullptr);
129 // Every visible Node3D, optionally restricted to nodes carrying at least
130 // one of the given tags.
131 void GetSeenByCamera(std::vector<Node3D*>& outNodes, const std::vector<std::string>* tags = nullptr, Camera3D* camera = nullptr);
132
133 void SetGravity(glm::vec3 gravity);
134 glm::vec3 GetGravity() const;
135
136 btDynamicsWorld* GetDynamicsWorld();
137 btDbvtBroadphase* GetBroadphase();
138 void PurgeOverlaps(Primitive3D* prim);
139
140 void RayTest(
141 glm::vec3 start,
142 glm::vec3 end,
143 uint8_t collisionMask,
144 RayTestResult& outResult,
145 uint32_t numIgnoredObjects = 0,
146 btCollisionObject** ignoreObjects = nullptr,
147 bool ignorePureOverlap = true);
148
149 void RayTestMulti(
150 glm::vec3 start,
151 glm::vec3 end,
152 uint8_t collisionMask,
153 bool ignorePureOverlap,
154 RayTestMultiResult& outResult);
155
156 void SweepTest(
157 Primitive3D* primComp,
158 glm::vec3 start,
159 glm::vec3 end,
160 uint8_t collisionMask,
161 SweepTestResult& outResult);
162
163 void SweepTest(
164 btConvexShape* convexShape,
165 glm::vec3 start,
166 glm::vec3 end,
167 glm::quat rotation,
168 uint8_t collisionMask,
169 SweepTestResult& outResult,
170 uint32_t numIgnoreObjects = 0,
171 btCollisionObject** ignoreObjects = nullptr);
172
173 void RegisterNode(Node* node, bool subRoot);
174 void UnregisterNode(Node* node, bool subRoot);
175 const std::vector<Audio3D*>& GetAudios() const;
176
177 void LoadScene(const char* name, bool instant);
178 void QueueRootScene(const char* name);
179 void QueueRootNode(Node* node);
180 const std::string& GetLoadedSceneName() const { return mLoadedSceneName; }
181 void GetLoadedSceneNames(std::vector<std::string>& outNames);
182
183 void EnableInternalEdgeSmoothing(bool enable);
184 bool IsInternalEdgeSmoothingEnabled() const;
185
186 void DirtyAllWidgets();
187
188 void UpdateRenderSettings();
189 void AddNewlyRegisteredNode(Node* node);
190
191 Camera3D* SpawnDefaultCamera();
192 Node* SpawnDefaultRoot();
193 void PlaceNewlySpawnedNode(NodePtr node, glm::vec3 position);
194
195 void OverrideDynamicsWorld(btDiscreteDynamicsWorld* world);
196 void RestoreDynamicsWorld();
197
198 template<typename T>
200 {
201 T* ret = nullptr;
202
203 auto typedNodeFind = [&](Node* node) -> bool
204 {
205 if (node->Is(T::ClassRuntimeId()))
206 {
207 ret = static_cast<T*>(node);
208 return false;
209 }
210
211 return true;
212 };
213
214 if (mRootNode != nullptr)
215 {
216 mRootNode->ForEach(typedNodeFind);
217 }
218
219 return ret;
220 }
221
222 template<typename T>
223 void FindNodes(std::vector<T*>& outNodes)
224 {
225 auto typedNodeGather = [&](Node* node) -> bool
226 {
227 if (node->Is(T::ClassRuntimeId()))
228 {
229 outNodes.push_back(static_cast<T*>(node));
230 }
231
232 return true;
233 };
234
235 if (mRootNode != nullptr)
236 {
237 mRootNode->Traverse(typedNodeGather);
238 }
239 }
240
241private:
242
243 void UpdateLines(float deltaTime);
244 void ExtractPersistingNodes();
245
246private:
247
248 static std::unordered_set<NodePtrWeak> sNewlyRegisteredNodes;
249
250 NodePtr mRootNode;
251 std::vector<NodePtr> mPersistingNodes;
252 std::vector<Line> mLines;
253 std::vector<class Light3D*> mLights;
254 std::vector<class Audio3D*> mAudios;
255 std::vector<FadingLight> mFadingLights;
256 NodePtr mQueuedRootNode;
257 std::string mLoadedSceneName;
258 std::string mQueuedSceneName;
259 glm::vec4 mAmbientLightColor;
260 glm::vec4 mShadowColor;
261 FogSettings mFogSettings;
262 const OcclusionData* mOcclusionData = nullptr;
263 bool mOcclusionEnabled = false;
264 bool mOcclusionStale = false;
265 bool mOcclusionResolvePending = false;
266 float mOcclusionMoveEps2 = 0.0f;
267 Camera3D* mActiveCamera;
268 Camera3D* mCameraOverride = nullptr;
269 // Transient stand-in spawned by EnsureFallbackCamera() when the scene has
270 // no camera. Raw pointer, cleared in UnregisterNode like mActiveCamera.
271 Camera3D* mFallbackCamera = nullptr;
272 Node3D* mAudioReceiver;
273 bool mPendingClear = false;
274 bool mAutoNavRebuild = false;
275
276 // Physics
277 btDefaultCollisionConfiguration* mCollisionConfig = nullptr;
278 btCollisionDispatcher* mCollisionDispatcher = nullptr;
279 btDbvtBroadphase* mBroadphase = nullptr;
280 btSequentialImpulseConstraintSolver* mSolver = nullptr;
281 btDiscreteDynamicsWorld* mDynamicsWorld = nullptr;
282 btDiscreteDynamicsWorld* mDefaultDynamicsWorld = nullptr;;
283 std::vector<PrimitivePair> mCurrentOverlaps;
284 std::vector<PrimitivePair> mPreviousOverlaps;
285
286 // Node UUID lookup
287 std::unordered_map<uint64_t, Node*> mUuidMap;
288};
289
uint32_t NetId
Definition EngineTypes.h:76
uint32_t TypeId
Definition EngineTypes.h:75
bool Update()
Definition Engine.cpp:995
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
Definition Audio3d.h:8
Definition Camera3d.h:12
Definition Node3d.h:14
Definition Node.h:67
Definition OcclusionData.h:30
Definition Particle3d.h:34
Definition ParticleSystem.h:66
Definition Primitive3d.h:46
Definition Scene.h:42
Definition World.h:26
Camera3D * GetCameraOverride() const
Definition World.h:51
NodeClass * SpawnNode(glm::vec3 position={})
Definition World.h:63
void SetCameraOverride(Camera3D *camera)
Definition World.h:50
T * FindNode()
Definition World.h:199
void FindNodes(std::vector< T * > &outNodes)
Definition World.h:223
const std::string & GetLoadedSceneName() const
Definition World.h:180
Camera3D * GetFallbackCamera() const
Definition World.h:49
Definition EngineTypes.h:606
Definition Line.h:6
Definition EngineTypes.h:556
Definition EngineTypes.h:546
Definition EngineTypes.h:567