Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
   Copyright (c) 2016 DIVIDE-Studio
   Copyright (c) 2009 Ionut Cava

   This file is part of DIVIDE Framework.

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software
   and associated documentation files (the "Software"), to deal in the Software
   without restriction,
   including without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
   PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
   IN CONNECTION WITH THE SOFTWARE
   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 */

#ifndef _SCENE_H_
#define _SCENE_H_

#include "SceneState.h"
#include "SceneInput.h"
#include "Core/Headers/cdigginsAny.h"
#include "Platform/Threading/Headers/Task.h"

/*All these includes are useful for a scene, so instead of forward declaring the
  classes, we include the headers
  to make them available in every scene source file. To reduce compile times,
  forward declare the "Scene" class instead
*/
// Core files
#include "Core/Headers/Kernel.h"
#include "Core/Headers/Application.h"
#include "Graphs/Headers/SceneGraph.h"
#include "Rendering/Camera/Headers/Camera.h"
#include "Rendering/Lighting/Headers/LightPool.h"
// Hardware
#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/Input/Headers/InputInterface.h"
// Scene Elements
#include "SceneEnvironmentProbePool.h"
#include "AI/Headers/AIManager.h"
#include "Environment/Sky/Headers/Sky.h"
#include "Rendering/Lighting/Headers/Light.h"
#include "Rendering/Lighting/Headers/DirectionalLight.h"
#include "Physics/Headers/PXDevice.h"
#include "Dynamics/Entities/Particles/Headers/ParticleEmitter.h"
// GUI
#include "GUI/Headers/GUI.h"
#include "GUI/Headers/SceneGUIElements.h"

#include <ArenaAllocator/arena_allocator.h>

namespace Divide {
class Sky;
class Light;
class Object3D;
class LoadSave;
class TerrainDescriptor;
class ParticleEmitter;
class PhysicsSceneInterface;

FWD_DECLARE_MANAGED_CLASS(Player);

namespace Attorney {
    class SceneManager;
    class SceneGraph;
    class SceneRenderPass;
    class SceneLoadSave;
    class SceneGUI;
};

/// The scene is a resource (to enforce load/unload and setName) and it has a 2 states:
/// one for game information and one for rendering information

class Scene : public Resource {
    friend class Attorney::SceneManager;
    friend class Attorney::SceneGraph;
    friend class Attorney::SceneRenderPass;
    friend class Attorney::SceneLoadSave;
    friend class Attorney::SceneGUI;

   protected:
    typedef std::stack<FileData, vectorImpl<FileData> > FileDataStack;
    static bool onStartup();
    static bool onShutdown();

   public:
    explicit Scene(PlatformContext& context, ResourceCache& cache, SceneManager& parent, const stringImpl& name);
    virtual ~Scene();

    /**Begin scene logic loop*/
    /// Get all input commands from the user
    virtual void processInput(const U64 deltaTime);
    /// Update the scene based on the inputs
    virtual void processTasks(const U64 deltaTime);
    virtual void processGUI(const U64 deltaTime);
    /// Scene is rendering, so add intensive tasks here to save CPU cycles
    bool idle();  
    /// The application has lost focus
    void onLostFocus();  
    /**End scene logic loop*/

    /// Update animations, network data, sounds, triggers etc.
    void updateSceneState(const U64 deltaTime);
    /// Override this for Scene specific updates
    virtual void updateSceneStateInternal(const U64 deltaTime) {}
    inline SceneState& state() { return *_sceneState; }
    inline const SceneState& state() const { return *_sceneState; }
    inline SceneRenderState& renderState() { return _sceneState->renderState(); }
    inline const SceneRenderState& renderState() const { return _sceneState->renderState(); }
    inline SceneInput& input() { return *_input; }

    inline SceneGraph& sceneGraph() { return *_sceneGraph; }
    void registerTask(const TaskHandle& taskItem);
    void clearTasks();
    void removeTask(I64 jobIdentifier);

    inline void addModel(FileData& model) { _modelDataArray.push(model); }
    inline void addTerrain(TerrainDescriptor* ter) {
        _terrainInfoArray.push_back(ter);
    }
    void addMusic(MusicType type, const stringImpl& name, const stringImpl& srcFile);
    void addPatch(vectorImpl<FileData>& data);

    // DIRECTIONAL lights have shadow mapping enabled automatically
    SceneGraphNode_ptr addLight(LightType type, SceneGraphNode& parentNode);
    SceneGraphNode_ptr addSky(const stringImpl& nodeName = "");

    /// Object picking
    inline SceneGraphNode_wptr getCurrentSelection() const {
        return _currentSelection;
    }
    inline SceneGraphNode_wptr getCurrentHoverTarget() const {
        return  _currentHoverTarget;
    }
    void findSelection();

    inline void addSelectionCallback(const DELEGATE_CBK<>& selectionCallback) {
        _selectionChangeCallbacks.push_back(selectionCallback);
    }

    SceneGraphNode_ptr addParticleEmitter(const stringImpl& name,
                                          std::shared_ptr<ParticleData> data,
                                          SceneGraphNode& parentNode);

    inline vectorImpl<FileData>& getVegetationDataArray() {
        return _vegetationDataArray;
    }


    inline AI::AIManager& aiManager() { return *_aiManager; }
    inline const AI::AIManager& aiManager() const { return *_aiManager; }

    inline PlatformContext& platformContext() { return _context; }
    inline const PlatformContext& platformContext() const { return _context; }


    inline ResourceCache& resourceCache() { return _resCache; }
    inline const ResourceCache& resourceCache() const { return _resCache; }

   protected:
    virtual void rebuildShaders();
    virtual void onSetActive();
    virtual void onRemoveActive();
    // returns the first available action ID
    virtual U16 registerInputActions();
    virtual void loadKeyBindings();

    void resetSelection();
    void findHoverTarget();
    bool checkCameraUnderwater() const;
    void toggleFlashlight();

    virtual bool save(ByteBuffer& outputBuffer) const;
    virtual bool load(ByteBuffer& inputBuffer);

    virtual bool frameStarted();
    virtual bool frameEnded();
    /// Description in SceneManager
    virtual bool loadResources(bool continueOnErrors);
    virtual bool loadTasks(bool continueOnErrors) { return true; }
    virtual bool loadPhysics(bool continueOnErrors);
    /// if singleStep is true, only the first model from the modelArray will be
    /// loaded.
    /// Useful for loading one model per frame
    virtual void loadXMLAssets(bool singleStep = false);
    virtual bool load(const stringImpl& name);
    bool loadModel(const FileData& data);
    bool loadGeometry(const FileData& data);
    virtual bool unload();
    virtual void postLoad();
    // gets called on the main thread when the scene finishes loading
    // used by the GUI system
    virtual void postLoadMainThread();
    /// Description in SceneManager
    virtual bool initializeAI(bool continueOnErrors);
    virtual bool deinitializeAI(bool continueOnErrors);
    /// Check if Scene::load() was called
    bool checkLoadFlag() const { return _loadComplete; }
    /// Unload scenegraph
    void clearObjects();
    /**End loading and unloading logic*/
    /// returns true if the camera was moved/rotated/etc
    bool updateCameraControls();
    /// Draw debug entities
    virtual void debugDraw(const Camera& activeCamera, RenderStage stage, RenderSubPassCmds& subPassesInOut);

    /// simple function to load the scene elements.
    inline bool SCENE_LOAD(const stringImpl& name,
                           const bool contOnErrorRes,
                           const bool contOnErrorTasks) {
        if (!Scene::load(name)) {
            Console::errorfn(Locale::get(_ID("ERROR_SCENE_LOAD")), "scene load function");
            return false;
        }
        if (!loadResources(contOnErrorRes)) {
            Console::errorfn(Locale::get(_ID("ERROR_SCENE_LOAD")), "scene load resources");
            if (!contOnErrorRes) return false;
        }
        if (!loadTasks(contOnErrorTasks)) {
            Console::errorfn(Locale::get(_ID("ERROR_SCENE_LOAD")), "scene load tasks");
            if (!contOnErrorTasks) return false;
        }
        if (!loadPhysics(contOnErrorTasks)) {
            Console::errorfn(Locale::get(_ID("ERROR_SCENE_LOAD")), "scene load physics");
            if (!contOnErrorTasks) return false;
        }

        registerInputActions();
        loadKeyBindings();

        return true;
    }

   protected:
       /// Global info
       SceneManager& _parent;
       ResourceCache& _resCache;
       PlatformContext& _context;
       ParamHandler&  _paramHandler;
       SceneGraph*    _sceneGraph;
       AI::AIManager* _aiManager;
       SceneGUIElements* _GUI;

       vectorImpl<Player_ptr> _scenePlayers;
       U64 _sceneTimer;
       vectorImpl<D64> _taskTimers;
       vectorImpl<D64> _guiTimers;
       /// Datablocks for models,vegetation,terrains,tasks etc
       FileDataStack _modelDataArray;
       vectorImpl<FileData> _vegetationDataArray;

       vectorImpl<stringImpl> _terrainList;
       vectorImpl<TerrainDescriptor*> _terrainInfoArray;
       F32 _LRSpeedFactor;
       /// Current selection
       SceneGraphNode_wptr _currentSelection;
       SceneGraphNode_wptr _currentHoverTarget;
       SceneGraphNode_wptr _currentSky;
       SceneGraphNode_wptr _flashLight;

       /// Scene::load must be called by every scene. Add a load flag to make sure!
       bool _loadComplete;
       /// Schedule a scene graph parse with the physics engine to recreate/recheck
       /// the collision meshes used by each node
       bool _cookCollisionMeshesScheduled;
       ///_aiTask is the thread handling the AIManager. It is started before each scene's "initializeAI" is called
       /// It is destroyed after each scene's "deinitializeAI" is called
       std::thread _aiTask;

   private:
       vectorImpl<TaskHandle> _tasks;
       /// Contains all game related info for the scene (wind speed, visibility ranges, etc)
       SceneState* _sceneState;
       vectorImpl<DELEGATE_CBK<> > _selectionChangeCallbacks;
       vectorImpl<SceneGraphNode_cwptr> _sceneSelectionCandidates;

   protected:
       LightPool* _lightPool;
       SceneInput* _input;
       PhysicsSceneInterface* _pxScene;
       SceneEnvironmentProbePool* _envProbePool;

       IMPrimitive* _linesPrimitive;
       vectorImpl<IMPrimitive*> _octreePrimitives;
       vectorImpl<BoundingBox> _octreeBoundingBoxes;

       mutable std::mutex _perFrameArenaMutex;
       mutable MyArena<Config::REQUIRED_RAM_SIZE / 2> _perFrameArena;
};

namespace Attorney {
class SceneManager {
   private:
    static LightPool* lightPool(Scene& scene) {
        return scene._lightPool;
    }

    static bool updateCameraControls(Scene& scene) {
        return scene.updateCameraControls();
    }

    static bool checkLoadFlag(const Scene& scene) {
        return scene.checkLoadFlag();
    }

    static bool deinitializeAI(Scene& scene) {
        return scene.deinitializeAI(true);
    }

    /// Draw debug entities
    static void debugDraw(Scene& scene, const Camera& activeCamera, RenderStage stage, RenderSubPassCmds& subPassesInOut) {
        scene.debugDraw(activeCamera, stage, subPassesInOut);
    }

    static bool frameStarted(Scene& scene) { return scene.frameStarted(); }
    static bool frameEnded(Scene& scene) { return scene.frameEnded(); }
    static bool load(Scene& scene, const stringImpl& name) {
        return scene.load(name);
    }
    static bool unload(Scene& scene) { 
        return scene.unload();
    }

    static void postLoad(Scene& scene) {
        scene.postLoad();
    }

    static void postLoadMainThread(Scene& scene) {
        scene.postLoadMainThread();
    }

    static void onSetActive(Scene& scene) {
        scene.onSetActive();
    }

    static void onRemoveActive(Scene& scene) {
        scene.onRemoveActive();
    }

    static bool onStartup() {
        return Scene::onStartup();
    }

    static bool onShutdown() {
        return Scene::onShutdown();
    }

    static SceneGUIElements* gui(Scene& scene) {
        return scene._GUI;
    }

    friend class Divide::SceneManager;
};

class SceneRenderPass {
 private:
    static SceneEnvironmentProbePool* getEnvProbes(Scene& scene) {
        return scene._envProbePool;
    }

    friend class Divide::RenderPass;
};

class SceneLoadSave {
 private:
    static bool save(const Scene& scene, ByteBuffer& outputBuffer) {
        return scene.save(outputBuffer);
    }

    static bool load(Scene& scene, ByteBuffer& inputBuffer) {
        return scene.load(inputBuffer);
    }

    friend class Divide::LoadSave;
};

class SceneGraph {
private:
    static void onNodeDestroy(Scene& scene, SceneGraphNode& node) {
        if (!scene.getCurrentSelection().expired() &&
            scene.getCurrentSelection().lock()->getGUID() == node.getGUID())
        {
            scene.resetSelection();
        }
    }
    friend class Divide::SceneGraph;
};

class SceneGUI {
private:
    static SceneGUIElements* guiElements(Scene& scene) {
        return scene._GUI;
    }

    friend class Divide::GUI;
};

};  // namespace Attorney
};  // namespace Divide

#endif

Commits for Divide-Framework/trunk/Source Code/Scenes/Headers/Scene.h

Diff revisions: vs.
Revision Author Commited Message
813 Diff Diff IonutCava picture IonutCava Thu 12 Jan, 2017 17:18:55 +0000

[IonutCava]
- Initial split screen support:
— Add support for multiple Player objects per scene
— Compute viewport alignment for every player
— Render scene once for every viewport
- ToDo:
— Bind each player to an input method
— Add per-player camera
— Per player render pass: set player camera as default camera

804 Diff Diff IonutCava picture IonutCava Thu 01 Dec, 2016 17:20:59 +0000

[IonutCava]
- Singleton elimination update Part I: get it to compile
— The following classes are no longer Singletons: GFXDevice, GL_API, DX_API, SFXWrapper, FmodWrapper, SDLWrapper, ALWrapper, PXDevice, InputInterface, RenderPassManager, SceneManager and ResourceManager;
— Refactor system to a Context / Component based implementation (Pass relevant context to objects: e.g. GFXDevice object to Textures, GUI to GUIElements, etc)
— Make devices and managers components of the kernel
— Allow multiple Resource caches to co-exist. This may prove useful for later when a more fragmented memory model is need (per frame / per scene / global caches / etc)

  • next steps: part II – cleanup/refactor new code, part III – optimise code, part IV – remove remaining Singletons (e.g. Application, ParamHandler, FrameListenerManager, Recast, PostFX and DebugInterface)
801 Diff Diff IonutCava picture IonutCava Sun 27 Nov, 2016 21:28:01 +0000

[IonutCava]
- More performance analysis guided optimizations.
- Some refactoring to allow later removal of Singletons status for: GFXDevice, SFXDevice, PXDevice, GUI and Input.
— Pass a PlatformContext around objects to access these classes.

791 Diff Diff IonutCava picture IonutCava Thu 17 Nov, 2016 16:46:19 +0000

[IonutCava]
- Reworked PostFX filter list to allow multiple enable calls to the same effect (stack based)
- Added noise, vignette and underwater effects to the filter list available for PostFX
- Added better underwater camera detection

788 Diff Diff IonutCava picture IonutCava Fri 21 Oct, 2016 16:11:37 +0000

[IonutCava]
- Added support for the Arena Allocator by Mamasha Knows (http://www.codeproject.com/Articles/44850/Arena-Allocator-DTOR-and-Embedded-Preallocated-Buf)
— Used for GFX Related objects: Textures, shaders, etc

787 Diff Diff IonutCava picture IonutCava Wed 19 Oct, 2016 16:13:09 +0000

[IonutCava]
- Added a Frustum debug rendering system

777 Diff Diff IonutCava picture IonutCava Fri 07 Oct, 2016 16:14:48 +0000

[IonutCava]
- Continue to implement CommandBuffer / RenderPass / RenderSubPass system.
— Rendering artefacts are still present.

768 Diff Diff IonutCava picture IonutCava Fri 09 Sep, 2016 14:38:51 +0000

[IonutCava]
- Refactored framework entry points (init, step, shutdown) to allow the addition of hot code reloading in the future

762 Diff Diff IonutCava picture IonutCava Thu 11 Aug, 2016 16:18:35 +0000

[IonutCava]
- Removed a lot of high level GFX state and merged it back on a lower level:
— depth mask is now a framebuffer specific toggle controlled by the RenderPolicy
— rasterization is now a per-buffer draw switch toggled by a rendering flag in GenericDrawCommand
- Replaced old style GL texture binding code with DSA style glBindTextures and glBindSamplers even for single textures
— completely removed the concept of a active texture unit in the GL code
- Fixed some draw policy settings that were clearing the depth buffer in the PostFX passes
- More build type flag replacement of macros
- Render pass system bug fixing
- Add option to skip occlusion culling for certain nodes

760 IonutCava picture IonutCava Tue 09 Aug, 2016 16:30:12 +0000

[IonutCava]
- Complete rewrite of RenderPass system:
— Allow every rendering pass (reflection, environment, etc) to use the same rendering system: occlusion culling, light culling, etc
— Define render passes by a param based token similar to modern API systems (DX12/Vulkan)
— W.I.P.: Depth rendering slightly wrong; Environment Mapping ,reflection and refraction rendering currently disabled
- Add compile time variables for DEBUG/PROFILE/RELEASE builds to validate all code for every build