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
#include "stdafx.h"

#include "Headers/MainScene.h"

#include "Core/Headers/ParamHandler.h"
#include "Core/Headers/StringHelper.h"
#include "Core/Headers/PlatformContext.h"
#include "Core/Math/Headers/Transform.h"
#include "Core/Time/Headers/ApplicationTimer.h"
#include "Managers/Headers/SceneManager.h"
#include "Managers/Headers/RenderPassManager.h"
#include "Environment/Water/Headers/Water.h"
#include "Geometry/Material/Headers/Material.h"
#include "Environment/Terrain/Headers/Terrain.h"
#include "Platform/File/Headers/FileManagement.h"
#include "Dynamics/Entities/Units/Headers/Player.h"
#include "Rendering/RenderPass/Headers/RenderQueue.h"
#include "Environment/Terrain/Headers/TerrainDescriptor.h"

namespace Divide {

namespace {
    vec3<F32> g_waterDimensions(500, 500, 500);
    I64 g_boxMoveTaskID = 0;
};

MainScene::MainScene(PlatformContext& context, ResourceCache& cache, SceneManager& parent, const stringImpl& name)
   : Scene(context, cache, parent, name),
    _beep(nullptr),
    _freeflyCamera(true/*false*/),
    _updateLights(true),
    _musicPlaying(false),
    _sun_cosy(0.0f)
{
}

void MainScene::updateLights() {
    if (!_updateLights) return;

    _sun_cosy = cosf(_sunAngle.y);
    _sunColour =
        Lerp(vec4<F32>(1.0f, 0.5f, 0.0f, 1.0f),
             vec4<F32>(1.0f, 1.0f, 0.8f, 1.0f), 0.25f + _sun_cosy * 0.75f);

    _sun.lock()->get<PhysicsComponent>()->setPosition(_sunvector);
    _sun.lock()->getNode<Light>()->setDiffuseColour(_sunColour);
    _currentSky.lock()->getNode<Sky>()->setSunProperties(_sunvector, _sunColour);

    _updateLights = false;
    return;
}

void MainScene::processInput(U8 playerIndex, const U64 deltaTime) {
    if (state().playerState(playerIndex).cameraUpdated()) {
        Camera& cam = getPlayerForIndex(playerIndex)->getCamera();
        const vec3<F32>& eyePos = cam.getEye();
        const vec3<F32>& euler = cam.getEuler();
        if (!_freeflyCamera) {
            F32 terrainHeight = 0.0f;
            vec3<F32> eyePosition = cam.getEye();

            vectorImpl<SceneGraphNode_wptr> terrains = Object3D::filterByType(_sceneGraph->getNodesByType(SceneNodeType::TYPE_OBJECT3D), Object3D::ObjectType::TERRAIN);

            for (SceneGraphNode_wptr terrainNode : terrains) {
                const std::shared_ptr<Terrain>& ter = terrainNode.lock()->getNode<Terrain>();
                assert(ter != nullptr);
                CLAMP<F32>(eyePosition.x,
                           ter->getDimensions().width * 0.5f * -1.0f,
                           ter->getDimensions().width * 0.5f);
                CLAMP<F32>(eyePosition.z,
                           ter->getDimensions().height * 0.5f * -1.0f,
                           ter->getDimensions().height * 0.5f);

                vec3<F32> position = terrainNode.lock()->get<PhysicsComponent>()->getWorldMatrix() *
                                     ter->getPositionFromGlobal(eyePosition.x, eyePosition.z);
                terrainHeight = position.y;
                if (!IS_ZERO(terrainHeight)) {
                    eyePosition.y = terrainHeight + 1.85f;
                    cam.setEye(eyePosition);
                    break;
                }
            }
            _GUI->modifyText(_ID("camPosition"),
                             Util::StringFormat("[ X: %5.2f | Y: %5.2f | Z: %5.2f ] [Pitch: %5.2f | Yaw: %5.2f] [TerHght: %5.2f ]",
                                                 eyePos.x, eyePos.y, eyePos.z, euler.pitch, euler.yaw, terrainHeight));
        } else {
            _GUI->modifyText(_ID("camPosition"),
                             Util::StringFormat("[ X: %5.2f | Y: %5.2f | Z: %5.2f ] [Pitch: %5.2f | Yaw: %5.2f]",
                                                eyePos.x, eyePos.y, eyePos.z, euler.pitch, euler.yaw));
        }
    }

    Scene::processInput(playerIndex, deltaTime);
}

void MainScene::processGUI(const U64 deltaTime) {
    D64 FpsDisplay = Time::SecondsToMilliseconds(0.5);
    D64 TimeDisplay = Time::SecondsToMilliseconds(1.0);

    if (_guiTimers[0] >= FpsDisplay) {
        _GUI->modifyText(_ID("fpsDisplay"),
                         Util::StringFormat("FPS: %3.0f. FrameTime: %3.1f",
                                             Time::ApplicationTimer::instance().getFps(),
                                             Time::ApplicationTimer::instance().getFrameTime()));
        _GUI->modifyText(_ID("underwater"),
                         Util::StringFormat("Underwater [ %s ] | WaterLevel [%f] ]",
                                             state().playerState(0).cameraUnderwater() ? "true" : "false",
                                             state().waterLevel()));
        _GUI->modifyText(_ID("RenderBinCount"),
                         Util::StringFormat("Number of items in Render Bin: %d. Number of HiZ culled items: %d",
                                            _context.gfx().parent().renderPassManager().getLastTotalBinSize(RenderStage::DISPLAY),
                                            _context.gfx().getLastCullCount()));
        _guiTimers[0] = 0.0;
    }

    if (_guiTimers[1] >= TimeDisplay) {
        _GUI->modifyText(_ID("timeDisplay"),
                         Util::StringFormat("Elapsed time: %5.0f", Time::ElapsedSeconds()));
        _guiTimers[1] = 0.0;
    }

    Scene::processGUI(deltaTime);
}

void MainScene::processTasks(const U64 deltaTime) {
    updateLights();
    D64 SunDisplay = Time::SecondsToMilliseconds(1.50);
    if (_taskTimers[0] >= SunDisplay) {
        _sunAngle.y += 0.0005f;
        _sunvector = vec4<F32>(-cosf(_sunAngle.x) * sinf(_sunAngle.y),
                               -cosf(_sunAngle.y),
                               -sinf(_sunAngle.x) * sinf(_sunAngle.y), 0.0f);
        _taskTimers[0] = 0.0;
        _updateLights = true;
    }

    Scene::processTasks(deltaTime);
}

bool MainScene::load(const stringImpl& name) {
    // Load scene resources
    bool loadState = SCENE_LOAD(name, true, true);
    _baseCamera->setMoveSpeedFactor(10.0f);

    _sun = addLight(LightType::DIRECTIONAL, _sceneGraph->getRoot());
    _sun.lock()->getNode<DirectionalLight>()->csmSplitCount(3);  // 3 splits
    _sun.lock()->getNode<DirectionalLight>()->csmSplitLogFactor(0.965f);
    _sun.lock()->getNode<DirectionalLight>()->csmNearClipOffset(25.0f);
    _currentSky = addSky();

    static const U32 normalMask = to_base(SGNComponent::ComponentType::NAVIGATION) |
                                  to_base(SGNComponent::ComponentType::PHYSICS) |
                                  to_base(SGNComponent::ComponentType::BOUNDS) |
                                  to_base(SGNComponent::ComponentType::RENDERING) |
                                  to_base(SGNComponent::ComponentType::NAVIGATION);

    g_waterDimensions.x = _baseCamera->getZPlanes().y;
    g_waterDimensions.z = _baseCamera->getZPlanes().y;
    ResourceDescriptor infiniteWater("waterEntity");
    infiniteWater.setUserPtr(g_waterDimensions);
    WaterPlane_ptr water = CreateResource<WaterPlane>(_resCache, infiniteWater);
    water->setParams(50.0f, vec2<F32>(10.0f, 10.0f), vec2<F32>(0.1f, 0.1f),  0.34f);
    SceneGraphNode_ptr waterGraphNode = _sceneGraph->getRoot().addNode(water, normalMask, PhysicsGroup::GROUP_IGNORE);
    waterGraphNode->usageContext(SceneGraphNode::UsageContext::NODE_STATIC);
    waterGraphNode->get<NavigationComponent>()->navigationContext(NavigationComponent::NavigationContext::NODE_IGNORE);
    waterGraphNode->get<PhysicsComponent>()->setPositionY(state().waterLevel());

    return loadState;
}

U16 MainScene::registerInputActions() {
    U16 actionID = Scene::registerInputActions();

    //ToDo: Move these to per-scene XML file
    PressReleaseActions actions;

    _input->actionList().registerInputAction(actionID, [this](InputParams param) {_context.sfx().playSound(_beep);});
    actions.actionID(PressReleaseActions::Action::RELEASE, actionID);
    _input->addKeyMapping(Input::KeyCode::KC_X, actions);
    actionID++;
    

    _input->actionList().registerInputAction(actionID, [this](InputParams param) {
        _musicPlaying = !_musicPlaying;
        if (_musicPlaying) {
            SceneState::MusicPlaylist::const_iterator it;
            it = state().music(MusicType::TYPE_BACKGROUND).find(_ID("themeSong"));
            if (it != std::cend(state().music(MusicType::TYPE_BACKGROUND))) {
                _context.sfx().playMusic(it->second);
            }
        } else {
            _context.sfx().stopMusic();
        }
    });
    actions.actionID(PressReleaseActions::Action::RELEASE, actionID);
    _input->addKeyMapping(Input::KeyCode::KC_M, actions);
    actionID++;

    _input->actionList().registerInputAction(actionID, [this](InputParams param) {
        _freeflyCamera = !_freeflyCamera;
        Camera& cam = _scenePlayers[getPlayerIndexForDevice(param._deviceIndex)]->getCamera();
        cam.setMoveSpeedFactor(_freeflyCamera ? 20.0f : 10.0f);
    });
    actions.actionID(PressReleaseActions::Action::RELEASE, actionID);
    _input->addKeyMapping(Input::KeyCode::KC_L, actions);
    actionID++;

    _input->actionList().registerInputAction(actionID, [this](InputParams param) {
        vectorImpl<SceneGraphNode_wptr> terrains = Object3D::filterByType(_sceneGraph->getNodesByType(SceneNodeType::TYPE_OBJECT3D), Object3D::ObjectType::TERRAIN);

        for (SceneGraphNode_wptr terrainNode : terrains) {
            terrainNode.lock()->getNode<Terrain>()->toggleBoundingBoxes();
        }
    });
    actions.actionID(PressReleaseActions::Action::RELEASE, actionID);
    _input->addKeyMapping(Input::KeyCode::KC_T, actions);

    return actionID++;
}

bool MainScene::unload() {
    _context.sfx().stopMusic();
    _context.sfx().stopAllSounds();
    g_boxMoveTaskID = 0;

    return Scene::unload();
}

void MainScene::test(const Task& parentTask, AnyParam a, CallbackParam b) {
    if(!parentTask.stopRequested()) {
        static bool switchAB = false;
        vec3<F32> pos;
        SceneGraphNode_ptr boxNode(_sceneGraph->findNode("box").lock());

        std::shared_ptr<Object3D> box;
        if (boxNode) {
            box = boxNode->getNode<Object3D>();
        }
        if (box) {
            pos = boxNode->get<PhysicsComponent>()->getPosition();
        }

        if (!switchAB) {
            if (pos.x < 300 && pos.z == 0) pos.x++;
            if (pos.x == 300) {
                if (pos.y < 800 && pos.z == 0) pos.y++;
                if (pos.y == 800) {
                    if (pos.z > -500) pos.z--;
                    if (pos.z == -500) switchAB = true;
                }
            }
        } else {
            if (pos.x > -300 && pos.z == -500) pos.x--;
            if (pos.x == -300) {
                if (pos.y > 100 && pos.z == -500) pos.y--;
                if (pos.y == 100) {
                    if (pos.z < 0) pos.z++;
                    if (pos.z == 0) switchAB = false;
                }
            }
        }
        if (box) boxNode->get<PhysicsComponent>()->setPosition(pos);

        std::this_thread::sleep_for(std::chrono::milliseconds(30));
        if (g_boxMoveTaskID != 0) {
            if (!parentTask.stopRequested()) {
                g_boxMoveTaskID = registerTask(CreateTask(platformContext(), 
                                               getGUID(),
                                               DELEGATE_BIND(&MainScene::test,
                                                             this,
                                                             std::placeholders::_1,
                                                             stringImpl("test"),
                                                             CallbackParam::TYPE_STRING)));
            }
        }
    }
}

bool MainScene::loadResources(bool continueOnErrors) {
    _taskTimers.push_back(0.0); // Sun
    _guiTimers.push_back(0.0);  // Fps
    _guiTimers.push_back(0.0);  // Time

    _sunAngle = vec2<F32>(0.0f, Angle::to_RADIANS(45.0f));
    _sunvector =
        vec4<F32>(-cosf(_sunAngle.x) * sinf(_sunAngle.y), -cosf(_sunAngle.y),
                  -sinf(_sunAngle.x) * sinf(_sunAngle.y), 0.0f);

    removeTask(g_boxMoveTaskID);
    g_boxMoveTaskID = registerTask(CreateTask(platformContext(),
                                              getGUID(),
                                              DELEGATE_BIND(&MainScene::test,
                                              this,
                                              std::placeholders::_1,
                                              stringImpl("test"),
                                              CallbackParam::TYPE_STRING)));

    ResourceDescriptor beepSound("beep sound");
    beepSound.setResourceName("beep.wav");
    beepSound.setResourceLocation(Paths::g_assetsLocation + Paths::g_soundsLocation);
    beepSound.setFlag(false);
    _beep = CreateResource<AudioDescriptor>(_resCache, beepSound);

    return true;
}

void MainScene::postLoadMainThread() {
    _GUI->addText(_ID("fpsDisplay"),  // Unique ID
        vec2<I32>(60, 60),  // Position
        Font::DIVIDE_DEFAULT,  // Font
        vec3<F32>(0.0f, 0.2f, 1.0f),  // Colour
        Util::StringFormat("FPS: %d", 0));  // Text and arguments

    _GUI->addText(_ID("timeDisplay"), vec2<I32>(60, 80), Font::DIVIDE_DEFAULT,
        vec4<U8>(164, 64, 64, 255),
        Util::StringFormat("Elapsed time: %5.0f", Time::ElapsedSeconds()));
    _GUI->addText(_ID("underwater"), vec2<I32>(60, 115), Font::DIVIDE_DEFAULT,
        vec4<U8>(64, 200, 64, 255),
        Util::StringFormat("Underwater [ %s ] | WaterLevel [%f] ]", "false", 0));
    _GUI->addText(_ID("RenderBinCount"), vec2<I32>(60, 135), Font::BATANG,
        vec4<U8>(164, 64, 64, 255),
        Util::StringFormat("Number of items in Render Bin: %d", 0));

    const vec3<F32>& eyePos = _baseCamera->getEye();
    const vec3<F32>& euler = _baseCamera->getEuler();
    _GUI->addText(_ID("camPosition"), vec2<I32>(60, 100), Font::DIVIDE_DEFAULT,
        vec4<U8>(64, 200, 64, 255),
        Util::StringFormat("Position [ X: %5.0f | Y: %5.0f | Z: %5.0f ] [Pitch: %5.2f | Yaw: %5.2f]",
            eyePos.x, eyePos.y, eyePos.z, euler.pitch, euler.yaw));

    Scene::postLoadMainThread();
}

};

Commits for Divide-Framework/trunk/Source Code/Scenes/MainScene/MainScene.cpp

Diff revisions: vs.
Revision Author Commited Message
909 Diff Diff IonutCava picture IonutCava Thu 29 Jun, 2017 23:57:18 +0000

[Ionut]
- Rework RenderTarget class
— RTAttachment now deal with textures directly
— RT size needs to be specified upfront
- Application class is no longer a Singleton but passed around in the PlatformContext

903 Diff Diff IonutCava picture IonutCava Sun 25 Jun, 2017 18:55:02 +0000

[IonutCava]
- Rework DebugInterface to be part of the PlatformContext and to allow adding of AntTweakBar elements via easy to use descriptors

902 Diff Diff IonutCava picture IonutCava Sun 25 Jun, 2017 16:56:57 +0000

[Ionut]
- Rework input system a bit for easier maintenance and readability
- Toggle AntTweakBar display using CTRL+F11

893 Diff Diff IonutCava picture IonutCava Sun 18 Jun, 2017 17:33:07 +0000

[Ionut]
- Initial implementation of a PipelineStateObject (holds shader program, rasterizer state, etc)
- Rework PCH implementation a bit because VS2017 no longer has a /ZM option

890 Diff Diff IonutCava picture IonutCava Sun 14 May, 2017 20:54:59 +0000

[Ionut]
- Add pre-compiled header support
- Initial code for per-stage tessellation computation

886 Diff Diff IonutCava picture IonutCava Wed 29 Mar, 2017 23:36:51 +0000

[Ionut]
- Initial code for multiple water block support (position + length/width/depth)

882 Diff Diff IonutCava picture IonutCava Thu 23 Mar, 2017 16:49:48 +0000

[IonutCava]
- More work on tessellated terrain rendering: got the mesh to render properly and match the collision mesh’s layout.
- Fixed a bug with GenericDrawCommand RenderOptions
- Added a “query number of generated primitives” option to GenericDrawCommand

880 Diff Diff IonutCava picture IonutCava Mon 20 Mar, 2017 17:27:13 +0000

[IonutCava]
- New tessellated terrain system based on this: http://victorbush.com/2015/01/tessellated-terrain/
— Work in progress: hardcoded values, new code is in Test namespace
- Split terrain geometry in two:
— tessellated rendering data
— physics verts + indices (used for collisions, vegetation, etc)
- Old Chunk system is used only for culling objects and speeding up collision detection

872 Diff Diff IonutCava picture IonutCava Mon 06 Mar, 2017 23:23:20 +0000

[IonutCava]
- Optimise static casts

867 IonutCava picture IonutCava Mon 20 Feb, 2017 17:01:12 +0000

[IonutCava]
- Rename to_XNN and to_const_XNN functions to reflect the target data type they are converting to.
- Cleanup Angle namespace and remove all “inDegrees” parameters from rotation functions. Use aliases to differentiate between Degree and Radian parameters.
- Profile guided optimizations.