1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
//-----------------------------------------------------------------------------
// Name: scenegraph.h
// Developer: Wolfire Games LLC
// Description:
// License: Read below
//-----------------------------------------------------------------------------
//
// Copyright 2022 Wolfire Games LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-----------------------------------------------------------------------------
#pragma once
#include <Graphics/drawbatch.h>
#include <Graphics/texturepack.h>
#include <Graphics/lightprobecollection.hpp>
#include <Graphics/dynamiclightcollection.hpp>
#include <Graphics/flares.h>
#include <Graphics/navmeshrenderer.h>
#include <Editors/entity_type.h>
#include <Editors/object_sanity_state.h>
#include <Math/vec4.h>
#include <Objects/object_msg.h>
#include <Asset/Asset/material.h>
#include <Internal/collisiondetection.h>
#include <Scripting/scriptparams.h>
#include <list>
#include <map>
class Sound;
class Object;
class TerrainObject;
class DecalObject;
class Hotspot;
class Group;
struct SphereCollision;
class BulletWorld;
class NavMesh;
class ParticleSystem;
class MapEditor;
class Level;
class Sky;
class Textures;
class EnvObject;
class LightVolumeObject;
class MovementObject;
struct SceneLight {
vec3 pos;
vec3 color;
float intensity;
};
struct ShadowCacheObjectLightBounds {
float min_bounds[2];
float max_bounds[2];
bool is_calculated;
bool is_ignored;
};
class SceneGraph {
public:
enum SceneDrawType { kStaticOnly, kStaticAndDynamic };
SceneLight primary_light;
MapEditor* map_editor;
Level* level;
Sky* sky;
Flares flares;
ParticleSystem *particle_system;
TerrainObject* terrain_object_;
LightProbeCollection light_probe_collection;
DynamicLightCollection dynamic_light_collection;
std::vector<mat4> ref_cap_matrix;
std::vector<mat4> ref_cap_matrix_inverse;
TextureRef cubemaps;
std::vector<TextureRef> sub_cubemaps;
bool cubemaps_need_refresh;
bool reflection_data_loaded;
float fog_amount;
float haze_mult; // derived from fog_amount
unsigned infreq_update_index;
Path level_path_;
bool level_has_been_previously_saved_;
std::string level_name_;
std::string level_visible_name_;
std::string level_visible_description_;
// Objects that must be updated every timestep
static const int kMaxUpdateObjects = 1024;
Object* update_objects_[kMaxUpdateObjects];
int num_update_objects;
typedef std::vector<Object*> object_list;
object_list objects_;
object_list collide_objects_;
object_list visible_objects_; // Objects that have a Draw() function
std::vector<EnvObject*> visible_static_meshes_;
std::vector<ShadowCacheObjectLightBounds> visible_static_meshes_shadow_cache_bounds_;
std::vector<uint16_t> visible_static_mesh_indices_; // Will be used to keep both arrays in sorted order. Should never be more than 65536 visible static meshes
std::vector<TerrainObject*> terrain_objects_;
std::vector<ShadowCacheObjectLightBounds> terrain_objects_shadow_cache_bounds_;
object_list decal_objects_;
object_list movement_objects_;
object_list item_objects_;
object_list hotspots_;
object_list navmesh_hints_;
object_list navmesh_connections_;
object_list path_points_;
std::vector<LightVolumeObject*> light_volume_objects_;
std::vector<int> object_ids_to_delete;
bool hotspots_modified_;
static const size_t destruction_sanity_size = 256;
size_t destruction_sanity_insert_position;
Object* destruction_sanity[destruction_sanity_size];
static const size_t destruction_memory_size = 128;
static const size_t destruction_memory_string_size = 128;
size_t destruction_memory_insert_position;
int destruction_memory_ids[destruction_memory_size];
char destruction_memory_strings[destruction_memory_size*destruction_memory_string_size];
static const unsigned int kMaxWarnings = 64;
ObjectSanityState sanity_list[kMaxWarnings];
uint32_t partial_object_loop_counter;
// Maximum total number of static and dynamic decals
// This could be raised as high as 65535, after that refactoring is required to replace 16-bit uints
static const unsigned int kMaxDecals = 20000;
// Maximum number of decals added at run time
// When count exceeds this older ones are removed
static const unsigned int kMaxDynamicDecals = 1000;
// Maximum number of decals that can be added via editor
static const unsigned int kMaxStaticDecals = kMaxDecals - kMaxDynamicDecals;
typedef std::deque<DecalObject*> decal_deque;
decal_deque dynamic_decals;
BulletWorld *bullet_world_;
BulletWorld *abstract_bullet_world_;
BulletWorld *plant_bullet_world_;
SceneGraph();
~SceneGraph();
void UpdatePhysicsTransforms();
bool addObject(Object* new_object);
bool AddDynamicDecal(DecalObject *decal);
Collision lineCheck(const vec3 &start, const vec3 &end);
Collision lineCheckCollidable(const vec3 &start, const vec3 &end, Object* notHit = NULL);
void LineCheckAll(const vec3 &start, const vec3 &end, std::vector<Collision> *collisions);
void Update(float timestep, float curr_game_time);
void Draw(SceneGraph::SceneDrawType scene_draw_type);
Object* GetLastSelected();
void ReturnSelected(std::vector<Object*>* selected);
void UnselectAll();
Object* GetObjectFromID(int object_id);
bool DoesObjectWithIdExist(int object_id);
std::vector<Object*> GetObjectsOfType(enum EntityType type);
void UnlinkObject(Object *o);
void LinkObject(Object* new_object);
void CreateNavMesh();
void SaveNavMesh();
bool LoadNavMesh();
void AddSceneToNavmesh();
NavMesh* GetNavMesh();
const MaterialEvent *GetMaterialEvent( const std::string &the_event, const vec3 &event_pos );
const MaterialEvent *GetMaterialEvent( const std::string &the_event, const vec3 &event_pos, const std::string &mod );
const MaterialEvent *GetMaterialEvent( const std::string &the_event, const vec3 &event_pos, Object* excluded_object );
const MaterialEvent *GetMaterialEvent( const std::string &the_event, const vec3 &event_pos, const std::string &mod, Object* excluded_object );
Object* GetClosestObject(const vec3 &pos, Object* excluded_object = NULL, vec3 *hit_pos = NULL, int *hit_tri = NULL);
const MaterialDecal *GetMaterialDecal( const std::string &type, const vec3 &pos );
const MaterialParticle *GetMaterialParticle( const std::string &type, const vec3 &pos );
float GetMaterialHardness( const vec3 &pos, Object* excluded_object = NULL );
float GetMaterialFriction( const vec3 &pos, Object* excluded_object = NULL );
float GetMaterialSharpPenetration( const vec3 &pos, Object* excluded_object = NULL );
vec3 GetColorAtPoint( const vec3 &pos );
void AssignID(Object* obj);
void QueueLevelReset();
void GetSweptSphereCollisionCharacters( const vec3 &pos, const vec3 &pos2, float radius, SphereCollision &as_col );
int CheckRayCollisionCharacters( const vec3 &start, const vec3 &end, vec3 *point, vec3 *normal, int *bone );
void GetPlayerCharacterIDs(int* num_avatars, int avatar_ids[], int max_avatars);
void GetNPCCharacterIDs(int* num_avatars, int avatar_ids[], int max_avatars);
void GetCharacterIDs(int* num_avatars, int avatar_ids[], int max_avatars);
void SendMessageToAllObjects( OBJECT_MSG::Type type );
void SendScriptMessageToAllObjects( std::string& msg );
std::vector<MovementObject*> GetControlledMovementObjects();
std::vector<MovementObject*> GetControllableMovementObjects();
bool VerifySanity();
enum DepthType {
kDepthShadow,
kDepthAllShadowCascades,
kDepthPrePass
};
void DrawDepthMap(const mat4& proj_view_matrix, const vec4* cull_planes, int num_cull_planes, DepthType depth_type, SceneDrawType scene_draw_type);
int GetAndReserveID();
int LinkUpdateObject( Object* obj );
void UnlinkUpdateObject( Object* obj, int entry );
void Dispose();
void SetNavMeshVisible( bool v );
void SetCollisionNavMeshVisible( bool v );
bool IsNavMeshVisible();
bool IsCollisionNavMeshVisible();
void PrepareLightsAndDecals(vec2 active_screen_start, vec2 active_screen_end, vec2 screen_dims);
void BindDecals(int the_shader);
void BindLights(int the_shader);
static void ApplyScriptParams(SceneGraph* scenegraph, const ScriptParamMap& spm);
void PrintCurrentObjects();
int CountObjectsWithName(const char* name);
bool IsObjectSane(Object* obj);
const char* GetDestroyedObjectInfo(int object_id);
void DumpState();
enum PreloadType {
kUnknown = (1<<0),
kDrawDepthOnly = (1<<1),
kDrawAllShadowCascades = (1<<2),
kDrawDepthNoAA = (1<<3),
kFullDraw = (1<<4),
kWireframe = (1<<5),
kDecal = (1<<6),
kPreloadTypeAll = (1<<7) - 1,
kOptionalNone = (1<<7),
kOptionalGeometry = (1<<8),
kOptionalTessellation = (1<<9)
};
void GetParticleShaderNames(std::map<std::string, int>& preload_shaders);
void PreloadForDrawType(std::map<std::string, int>& preload_shaders, PreloadType type);
void PreloadShaders();
private:
bool visible_objects_need_sort;
bool queued_level_reset_;
typedef std::vector<Object*> IDMap;
IDMap object_from_id_map_;
NavMesh *nav_mesh_;
NavMeshRenderer nav_mesh_renderer_;
// this buffer contains decal and light data
std::vector<float> decal_tbo;
TextureRef decal_data_texture;
// this buffer contains
// 1. grid lookup (should be in a separate 3D texture)
// 2. decal cluster contents (decal indices)
// 2. light cluster contents (light indices)
std::vector<uint32_t> decal_cluster_buffer;
TextureRef decal_cluster_texture;
// int is a bitmask of Object::DrawType
std::map<std::string, int> preload_shaders;
struct ClusterData {
uint32_t next;
uint16_t item;
ClusterData()
: next(0)
, item(0)
{
}
};
// in pixels
// This value is hardcoded in shaders, so has to be modified there as well.
unsigned int cluster_size;
unsigned int num_z_clusters;
// 3D texture of cluster pointers
std::vector<uint32_t> cluster_list_heads;
// 1D texture of cluster pointers
std::vector<ClusterData> cluster_list_contents;
// number of decals in the cluster, size = number of clusters
std::vector<uint16_t> cluster_decal_counts;
std::vector<uint32_t> decal_grid_lookup;
std::vector<unsigned int> cluster_decals;
// number of lights in each cluster, size = number of clusters
std::vector<unsigned int> cluster_light_counts;
std::vector<uint32_t> light_grid_lookup;
std::vector<unsigned int> cluster_lights;
public:
void LoadReflectionCaptureCubemaps();
void UnloadReflectionCaptureCubemaps();
};
vec3 ColorFromString(const char* str);
|